编辑
2025-08-30
XCPC
00

https://www.luogu.com.cn/problem/P1330

#include <bits/stdc++.h> using namespace std; const int M = 2e5 + 5; int a[M], vis[M], color[M], ans, anss; vector<int> f[M]; void dfss(int now) { vis[now] = color[now] = 0; for (auto x : f[now]) if (vis[x]) dfss(x); } bool dfs(int now) { vis[now] = 1; if (color[now]) ans++; for (auto x : f[now]) if (!vis[x]) { color[x] = 1 - color[now]; if (!dfs(x)) return 0; } else if (color[now] == color[x]) return 0; return 1; } void solve() { int n, m, x, y; cin >> n >> m; while (m--) { cin >> x >> y; f[x].push_back(y); f[y].push_back(x); } for (int i = 1; i <= n; ++i) if (!vis[i]) { int cnt = 1e9; ans = 0; if (dfs(i)) cnt = min(cnt, ans); ans = 0; dfss(i); color[i] = 1; if (dfs(i)) cnt = min(cnt, ans); if (cnt == 1e9) { cout << "Impossible"; return; } anss += cnt; } cout << anss; } signed main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int t = 1; // cin>>t; while (t--) solve(); return 0; }
编辑
2025-08-30
XCPC
00

https://www.luogu.com.cn/problem/P3386

#include <bits/stdc++.h> using namespace std; const int M = 2e5 + 5; int match[M], vis[M]; vector<int> f[M]; bool dfs(int now) { for (auto x : f[now]) if (!vis[x]) { vis[x] = 1; if (!match[x] || dfs(match[x])) { match[x] = now; return 1; } } return 0; } void solve() { int n, m, t; cin >> n >> m >> t; while (t--) { int x, y; cin >> x >> y; f[x].push_back(y); } int ans = 0; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) vis[j] = 0; if (dfs(i)) ans++; } cout << ans; } signed main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int t = 1; // cin>>t; while (t--) solve(); return 0; }
编辑
2025-08-30
XCPC
00

这里的 factinv 即为阶乘逆元。

#include <cstdio> const int maxn = 3000005; int n, p; int inv[maxn], factinv[maxn]; int main() { scanf("%d%d", &n, &p); factinv[1] = inv[1] = 1; printf("%d\n", 1); for (int i = 2; i <= n; ++i) { inv[i] = 1ll * (p - p / i) * inv[p % i] % p; printf("%d\n", inv[i]); factinv[i] = 1ll * factinv[i - 1] * inv[i] % p; } return 0; }
编辑
2025-08-30
XCPC
00

https://www.luogu.com.cn/problem/P3384

#include<bits/stdc++.h> using namespace std; #define lc now<<1 #define rc now<<1|1 const int M=1e5+5; struct dian { int l,r,sum,add; } f[M<<2|1]; vector<int>p[M]; int a[M],w[M],mod,top[M],size[M],fa[M],son[M],dep[M],id[M]; void pushup(int now) { f[now].sum=(f[lc].sum+f[rc].sum)%mod; } void build(int now,int l,int r) { f[now]= {l,r,a[l],0}; if(l==r)return ; int mid=l+r>>1; build(lc,l,mid); build(rc,mid+1,r); pushup(now); } void pushdown(int now) { if(f[now].add) { f[lc].sum+=(f[lc].r-f[lc].l+1)*f[now].add; f[rc].sum+=(f[rc].r-f[rc].l+1)*f[now].add; f[rc].sum%=mod; f[lc].sum%=mod; f[lc].add+=f[now].add; f[rc].add+=f[now].add; f[now].add=0; } } void modi(int now,int x,int y,int k) { if(x<=f[now].l&&y>=f[now].r) { f[now].sum+=(f[now].r-f[now].l+1)*k; f[now].sum%=mod; f[now].add+=k; return; } pushdown(now); int mid=f[now].l+f[now].r>>1; if(x<=mid)modi(lc,x,y,k); if(y>mid)modi(rc,x,y,k); pushup(now); } int que(int now,int x,int y) { if(x<=f[now].l&&y>=f[now].r) return f[now].sum; int mid=f[now].l+f[now].r>>1; pushdown(now); int sum=0; if(x<=mid)sum+=que(lc,x,y); sum%=mod; if(y>mid)sum+=que(rc,x,y); return sum%mod; } void dfs1(int now,int f) { dep[now]=dep[f]+1,fa[now]=f,size[now]=1; for(auto to:p[now]) { if(to==f)continue; dfs1(to,now); size[now]+=size[to]; if(size[to]>size[son[now]])son[now]=to; } } int cnt=0; void dfs2(int now,int t) { id[now]=++cnt; a[cnt]=w[now]; top[now]=t; if(!son[now])return; dfs2(son[now],t); for(int to:p[now]) { if(to==fa[now]||to==son[now])continue; dfs2(to,to); } } void fun1(int u,int v,int k) { while(top[u]!=top[v]) { if(dep[top[u]]<dep[top[v]])swap(u,v); modi(1,id[top[u]],id[u],k); u=fa[top[u]]; } if(dep[u]<dep[v])swap(u,v); modi(1,id[v],id[u],k); } int fun2(int u,int v) { int ans=0; while(top[u]!=top[v]) { if(dep[top[u]]<dep[top[v]])swap(u,v); ans+=que(1,id[top[u]],id[u]); ans%=mod; u=fa[top[u]]; } if(dep[u]<dep[v])swap(u,v); ans+=que(1,id[v],id[u]); return ans%mod; } signed main() { ios::sync_with_stdio(false),cin.tie(0); int n,m,s; cin>>n>>m>>s>>mod; for(int i=1; i<=n; ++i) cin>>w[i]; for(int i=1; i<n; ++i) { int x,y; cin>>x>>y; p[x].push_back(y),p[y].push_back(x); } dfs1(s,s); dfs2(s,s); build(1,1,n); while(m--) { int op,x,y,k; cin>>op; if(op==1) { cin>>x>>y>>k; fun1(x,y,k); } else if(op==2) { cin>>x>>y; cout<<fun2(x,y)%mod<<'\n'; } else if(op==3) { cin>>x>>k; modi(1,id[x],id[x]+size[x]-1,k); } else { cin>>x; cout<<que(1,id[x],id[x]+size[x]-1)%mod<<'\n'; } } return 0; }
编辑
2025-08-30
XCPC
00
#include <bits/stdc++.h> using namespace std; #define int long long #define endl '\n' const int M = 1e5 + 5, mod = 1e9 + 7; int tr[M*33][2], tot; void insert(int x) { int now = 0; for (int i = 31; i >= 0; i--) { int j = x >> i & 1; if (!tr[now][j]) tr[now][j] = ++tot; now = tr[now][j]; } } int query(int x) { int now = 0, ans = 0; for (int i = 31; i >= 0; --i) { int j = x >> i & 1; if (j == 1) { if (tr[now][0]) { now = tr[now][0]; ans += (1 << i); } else { now = tr[now][1]; } } else { if (tr[now][1]) { now = tr[now][1]; ans += (1 << i); } else { now = tr[now][0]; } } } return ans; } void solve() { int n; cin >> n; int ans = 0; while (n--) { int x; cin >> x; insert(x); ans = max(ans, query(x)); } cout << ans << endl; } signed main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int t = 1; // cin >> t; while (t--) solve(); return 0; }