ZROJ-NOIP二十连

CSP-S模拟 1

T1 喜剧的迷人之处在于

咕咕咕

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+9;
int T,cnt,mx,prime[maxn],v[maxn];
long long sqr[maxn];
inline long long read(){
	long long res=0;char c=getchar();
	for(;c<'0'||c>'9';c=getchar());
	for(;c>='0'&&c<='9';c=getchar()) res=(res<<3)+(res<<1)+(c^48);
	return res;
}
int main(){
	for(int i=2;i<=1000000;i++){
		if(!v[i]) prime[++cnt]=i;
		for(int j=1;prime[j]*i<=1000000;j++){
			v[prime[j]*i]=1;
			if(!(i%prime[j])) break;
		}
		sqr[i]=1ll*i*i;
	}
	scanf("%d",&T);
	memset(v,0,sizeof v);
	while(T--){
		long long a=read(),b=1,L=read(),R=read();
		for(int i=1;a>1;i++,mx=i) while(!(a%prime[i])) a/=prime[i],v[prime[i]]++;
		for(int i=1;i<mx;i++){
			if(v[prime[i]]&1) b*=prime[i];
			v[prime[i]]=0;
		}
		long long sq=sqrt((L-1)/b)+1;
		b*=sq*sq;
		printf("%lld\n",b<=R?b:-1);
	}
	return 0;
}

T2 镜中的野兽

咕咕咕

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+9;
int n,m,p,cnt,ans;
int phi[maxn],v[maxn];
int f[29][1<<18];
void getprime(int lcm){
	cnt=0;
	for(int i=2;i<=lcm;i++){
		if(lcm%i) continue;
		phi[++cnt]=i,v[cnt]=0;
		while(!(lcm%i)) lcm/=i,v[cnt]++;
	}
}
void dfs(int pos,int now){
	if(pos>cnt){
		for(int i=n;i;i--){
			for(int j=(1<<(cnt<<1))-1;~j;j--){
				(f[i][j|now]+=f[i-1][j])%=p;
			}
		}
		return;
	}
	dfs(pos+1,now|1<<pos>>1);
	for(int i=1;i<v[pos];i++) dfs(pos+1,now);
	dfs(pos+1,now|1<<(pos+cnt)>>1);
}
void solve(int gcd){
	if(m<=gcd<<1) return;
	memset(f,0,sizeof f),f[0][0]=1;
	getprime((m-gcd)/gcd),dfs(1,0);
	(ans+=f[n][(1<<(cnt<<1))-1])%=p;
}
int main(){
	scanf("%d%d%d",&n,&m,&p);
	int s=sqrt(m);
	for(int i=1;i<=s;i++){
		if(!(m%i)){
			solve(i);
			if(i*i^m) solve(m/i);
		}
	}
	return printf("%d\n",ans),0;
}

T3 我愿相信由你所描述的童话

咕咕咕

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=3e5+9;
const int inf=1<<20;
const int mod=1e9+7;
int n,m,ans,f[1<<10][1<<10];
int s[maxn],up[maxn],low[maxn],las[inf];
void update(int x,int val){
	int l=x>>m,r=x^(l<<m),u=(~r)^(~r>>m<<m);
	for(int sb=u;;sb=(sb-1)&u){
		(f[l][r|sb]+=val)%=mod;
		if(!sb) return;
	}
}
int query(int x){
	int l=x>>m,r=x^(l<<m),res=1;
	for(int sb=l;;sb=(sb-1)&l){
		(res+=f[sb][r])%=mod;
		if(!sb) return res;
	}
}
int main(){
	scanf("%d%d",&n,&m),m>>=1;
	for(int i=1;i<=n;i++) scanf("%d",&s[i]);
	for(int i=1;i<=n;i++) update(s[i],up[i]=query(s[i]));
	memset(f,0,sizeof f);
	for(int i=n;i;i--) update(s[i],low[i]=query(s[i]));
	for(int i=1;i<=n;i++){
		(ans+=1ll*up[i]*low[i]%mod)%=mod;
		(ans-=1ll*las[s[i]]*low[i]%mod-mod)%=mod;
		(las[s[i]]+=up[i])%=mod;
	}
	return printf("%d\n",ans),0;
}

T4 Baby Doll

咕咕咕

CSP-S模拟 2

T1 不相邻集合

咕咕咕

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int inf=5e5+9;
int n,die[inf],sz[inf],v[inf];
int findie(int x){return x==die[x]?x:die[x]=findie(die[x]);}
void merge(int x,int y){die[findie(x)]=findie(y);}
int main(){
	scanf("%d",&n);
	for(int i=1;i<inf;i++) die[i]=i,sz[i]=1;
	for(int i=1,a,ans=0;i<=n;i++){
		scanf("%d",&a);
		if(!(v[a]++)){
			int nsz=1;
			if(v[a-1]){
				int lsz=sz[findie(a-1)];
				ans-=ceil(1.0*lsz/2);
				nsz+=lsz;merge(a,a-1);
			}
			if(v[a+1]){
				int rsz=sz[findie(a+1)];
				ans-=ceil(1.0*rsz/2);
				nsz+=rsz;merge(a,a+1);
			}
			ans+=ceil(1.0*(sz[findie(a)]=nsz)/2);
		}
		printf("%d ",ans);
	}
	return 0;
}

T2 线段树

subtask 1

直接建树查询,递归到每个区间内的叶子结点,对路径上所有合法根的编号求和。

时间复杂度 \(O(n)\)\(n\) 的范围是 \(1e18\) ,直接T飞。

暴力
#include<bits/stdc++.h>
#define lcs (rt<<1)
#define rcs (rt<<1|1)
using namespace std;
using ll=long long;
const int mod=1e9+7;
ll T,n,x,y;
int query(int rt,ll l,ll r,ll L,ll R){
	int res=0;
	if(L>r||R<l) return res;
	if(l==r) return rt;
	if(L<=l&&r<=R) res=rt;
	ll mid=(l+r)>>1;
	(res+=(query(lcs%mod,l,mid,L,R)+query(rcs%mod,mid+1,r,L,R))%mod)%=mod;
	return res;
}
int main(){
	scanf("%lld",&T);
	while(T--){
		scanf("%lld%lld%lld",&n,&x,&y);
		printf("%d\n",query(1,1,n,x,y));
	}
	return 0;
}

subtask 别的

当这个节点代表的 \(l\)\(r\) 刚好在要查询的区间之内时,整个子树内的节点均合法,此事无需继续递归,想办法求出子树内所有节点编号之和,作为这个根节点的贡献。

\(f(rt,n)\) 代表以 \(rt\) 为根且子树叶子结点数为 \(n\) 的节点的贡献

可以得出递推式 \(f(rt,n)=f(rt×2,\lceil{\frac{n}{2}}\rceil)+f(rt×2+1,\lfloor{\frac{n}{2}}\rfloor)+rt\)

发现 \(f(rt,n)\) 可以写成 \(k_{n}×rt+b_{n}\) 的一次函数形式,所以再把两边换一下:

\[k_{n}×rt+b_{n}=k_{\lceil{\frac{n}{2}}\rceil}×(rt×2)+b_{\lceil{\frac{n}{2}}\rceil}+k_{\lfloor{\frac{n}{2}}\rfloor}×(rt×2+1)+b_{\lfloor{\frac{n}{2}}\rfloor}+rt \]

\[k_{n}×rt+b_{n}=(2×(k_{\lceil{\frac{n}{2}}\rceil}+k_{\lfloor{\frac{n}{2}}\rfloor})+1)×rt+b_{\lceil{\frac{n}{2}}\rceil}+b_{\lfloor{\frac{n}{2}}\rfloor}+k_{\lfloor{\frac{n}{2}}\rfloor} \]

所以就有了 \(k\)\(b\) 的递推式:

\[k_{n}=2×(k_{\lceil{\frac{n}{2}}\rceil}+k_{\lfloor{\frac{n}{2}}\rfloor})+1 \]

\[b_{l,r}=b_{\lceil{\frac{n}{2}}\rceil}+b_{\lfloor{\frac{n}{2}}\rfloor}+k_{\lfloor{\frac{n}{2}}\rfloor} \]

最后再用map加个记搜把用到的 \(k\)\(b\) 记一下,把贡献 \(k_{n}×rt+b_{n}\) 累加到答案上,就过了。

点击查看代码
#include<bits/stdc++.h>
#define lcs (rt<<1)
#define rcs (rt<<1|1)
#define axe r-l+1
using namespace std;
using ll=long long;
const int mod=1e9+7;
ll T,n,x,y;map<ll,ll> k,b;
void dfs(ll n){
	if(k[n]) return;
	ll n1=ceil(1.0*n/2);dfs(n1);
	ll n2=floor(1.0*n/2);dfs(n2);
	k[n]=((k[n1]+k[n2])<<1|1)%mod;
	b[n]=(b[n1]+b[n2]+k[n2])%mod;
}
int query(int rt,ll l,ll r,ll L,ll R){
	if(L>r||R<l) return 0;
	if(L<=l&&r<=R) return dfs(axe),(k[axe]*rt+b[axe])%mod;
	ll mid=(l+r)>>1;
	return (query(lcs%mod,l,mid,L,R)+query(rcs%mod,mid+1,r,L,R))%mod;
}
int main(){
	scanf("%lld",&T);k[1]=1;
	while(T--){
		scanf("%lld%lld%lld",&n,&x,&y);
		printf("%d\n",query(1,1,n,x,y));
	}
	return 0;
}

T3 魔法师

咕咕咕

点击查看代码
#include<bits/stdc++.h>
#define lcs (rt<<1)
#define rcs (rt<<1|1)
using namespace std;
const int inf=250000;
int Q,T,axe,op,t,a,b;
multiset<int> A[2][inf<<1];
multiset<int> B[2][inf<<1];
multiset<int>::iterator it;
struct segtree{
	int mn,l,r,a[2],b[2];
	segtree(){mn=a[0]=b[0]=a[1]=b[1]=0x3f3f3f3f;}
}tree[inf<<3];
void pushup(int rt){
	tree[rt].a[0]=min(tree[lcs].a[0],tree[rcs].a[0]);
	tree[rt].b[0]=min(tree[lcs].b[0],tree[rcs].b[0]);
	tree[rt].a[1]=min(tree[lcs].a[1],tree[rcs].a[1]);
	tree[rt].b[1]=min(tree[lcs].b[1],tree[rcs].b[1]);
	tree[rt].mn=min(tree[lcs].a[1]+tree[rcs].a[0],tree[lcs].b[0]+tree[rcs].b[1]);
	tree[rt].mn=min(tree[rt].mn,min(tree[lcs].mn,tree[rcs].mn));
}
void build(int rt,int l,int r){
	tree[rt].l=l,tree[rt].r=r;
	if(l==r) return;
	int mid=(l+r)>>1;
	build(lcs,l,mid),build(rcs,mid+1,r);
}
void update(int rt,int pos){
	if(pos<tree[rt].l||pos>tree[rt].r) return;
	if(tree[rt].l==tree[rt].r){
		int now=tree[rt].l;
		if(!A[0][now].size()){
			A[0][now].insert(0x3f3f3f3f);
			B[0][now].insert(0x3f3f3f3f);
			A[1][now].insert(0x3f3f3f3f);
			B[1][now].insert(0x3f3f3f3f);
		}
		if(op==1){
			A[t][now].insert(a);
			B[t][now].insert(b);
		}
		if(op==2){
			it=A[t][now].lower_bound(a);
			if(*it==a) A[t][now].erase(it);
			it=B[t][now].lower_bound(b);
			if(*it==b) B[t][now].erase(it);
		}
		tree[rt].a[0]=*A[0][now].begin();
		tree[rt].b[0]=*B[0][now].begin();
		tree[rt].a[1]=*A[1][now].begin();
		tree[rt].b[1]=*B[1][now].begin();
		tree[rt].mn=min(tree[rt].a[0]+tree[rt].a[1],tree[rt].b[0]+tree[rt].b[1]);
		return;
	}
	update(lcs,pos),update(rcs,pos),pushup(rt);
}
int main(){
	scanf("%d%d",&Q,&T);
	build(1,1,inf<<1);
    while(Q--){
        scanf("%d%d%d%d",&op,&t,&a,&b);
        if(T) a^=axe,b^=axe;
        update(1,(t?b-a:a-b)+inf);
        printf("%d\n",axe=tree[1].mn^0x3f3f3f3f?tree[1].mn:0);
    }
    return 0;
}

T4 园艺

咕咕咕

CSP-S模拟 3

T1 奇观

咕咕咕

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+9;
const int mod=998244353;
int n,m,u,v,sum,C,F;
int v1[maxn],v2[maxn];
vector<int> del[maxn];
int main(){
	freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);
	scanf("%d%d",&n,&m);sum=1ll*n*(n-1)%mod;
	for(int i=1;i<=n;i++) del[i].push_back(i),v1[i]=n-1;
	while(m--){
		scanf("%d%d",&u,&v);
		del[u].push_back(v);
		del[v].push_back(u);
		v1[u]--,v1[v]--,sum-=2;
	}
	for(int i=1;i<=n;i++){
		v2[i]=sum;
		for(auto j:del[i]) (v2[i]-=v1[j]-mod)%=mod;
		(C+=1ll*v1[i]*v2[i]%mod)%=mod;
		(F+=1ll*v1[i]*v1[i]%mod*v2[i]%mod)%=mod;
	}
	return printf("%lld\n",1ll*C*C%mod*F%mod),0;
}

T2 铁路

咕咕咕

点击查看代码
#include<bits/stdc++.h>
#define lcs (rt<<1)
#define rcs (rt<<1|1)
using namespace std;
const int maxn=5e5+9;
int n,m,tot,cnt,u,v;
int h[maxn],nxt[maxn<<1],to[maxn<<1];
int ys[maxn],die[maxn],dep[maxn],siz[maxn];
int son[maxn],top[maxn],dfn[maxn],rnk[maxn];
struct segtree{
	int sum,l,r,lz,bel;
}tree[maxn<<2];
void add(int x,int y){
	tot++;
	nxt[tot]=h[x];
	to[tot]=y;
	h[x]=tot;
}
inline void pushup(int rt){tree[rt].sum=tree[lcs].sum+tree[rcs].sum;}
void pushdown(int rt){
	if(!tree[rt].lz) return;
	tree[lcs].lz=tree[rcs].lz=1;
	tree[lcs].bel=tree[rt].bel;
	tree[rcs].bel=tree[rt].bel;
	tree[lcs].sum=tree[rt].bel>=tree[lcs].l&&tree[rt].bel<=tree[lcs].r;
	tree[rcs].sum=tree[rt].bel>=tree[rcs].l&&tree[rt].bel<=tree[rcs].r;
	tree[rt].lz=0;
}
void build(int rt,int l,int r){
	tree[rt].l=l,tree[rt].r=r;
	if(l==r) return tree[rt].bel=l,tree[rt].sum=1,void(0);
	int mid=(l+r)>>1;
	build(lcs,l,mid),build(rcs,mid+1,r),pushup(rt);
}
void update(int rt,int l,int r,int y){
	if(l>tree[rt].r||r<tree[rt].l) return;
	if(l<=tree[rt].l&&tree[rt].r<=r){
		tree[rt].sum=y>=tree[rt].l&&tree[rt].r>=y;
		tree[rt].bel=y,tree[rt].lz=1;
		return;
	}
	pushdown(rt),update(lcs,l,r,y),update(rcs,l,r,y),pushup(rt);
}
int query(int rt,int pos){
	if(tree[rt].l==tree[rt].r) return tree[rt].bel;
	int mid=(tree[rt].l+tree[rt].r)>>1;
	return pushdown(rt),query(pos<=mid?lcs:rcs,pos);
}
void dfs1(int x,int fa){
	siz[x]=1,die[x]=fa,dep[x]=dep[fa]+1;
	for(int i=h[x];i;i=nxt[i]){
		int y=to[i];
		if(y==fa) continue;
		dfs1(y,x),siz[x]+=siz[y];
		if(siz[y]>siz[son[x]]) son[x]=y;
	}
}
void dfs2(int x,int tp){
	top[x]=tp,dfn[x]=++cnt,rnk[dfn[x]]=x;
	if(!son[x]) return;
	dfs2(son[x],tp);
	for(int i=h[x];i;i=nxt[i]){
		int y=to[i];
		if(y^die[x]&&y^son[x]) dfs2(y,y);
	}
}
int lca(int x,int y){
	while(top[x]^top[y]){
		if(dep[top[x]]<dep[top[y]]) swap(x,y);
		x=die[top[x]];
	}
	if(dep[y]<dep[x]) swap(x,y);
	return x;
}
void merge(int x,int y,int z){
	while(top[x]^top[y]){
		if(dep[top[x]]<dep[top[y]]) swap(x,y);
		update(1,dfn[top[x]],dfn[x],z);
		x=die[top[x]];
	}
	if(dep[x]>dep[y]) swap(x,y);
	update(1,dfn[x],dfn[y],z);
}
int find(int x){
	int tmp=query(1,dfn[x]);
	return tmp==dfn[x]?x:find(rnk[tmp]);
}
int main(){
	freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);
	scanf("%d%d",&n,&m);
	for(int i=1;i<n;i++){
		scanf("%d%d",&u,&v);
		add(u,v),add(v,u);
	}
	dfs1(1,0),dfs2(1,1),build(1,1,n);
	for(int i=1;i<=m;i++){
		scanf("%d%d",&u,&v);
		if(u>n) u=ys[u-n];
		if(v>n) v=ys[v-n];
		ys[i]=find(lca(u,v));
		merge(u,v,dfn[ys[i]]);
		printf("%d\n",tree[1].sum);
	}
	return 0;
}

T3 光纤

咕咕咕

T4 权值

咕咕咕

CSP-S模拟 4

T1 商品

咕咕咕

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+9;
int n,d,cnt,l,r;long long ans;
int num[maxn],big[maxn],small[maxn];
long long sa[maxn],sb[maxn];
map<int,int> t;
inline long long sua(int l,int r){return sa[r]-sa[l-1];}
inline long long sub(int l,int r){return sb[r]-sb[l-1];}
int main(){
	freopen("goods.in","r",stdin);
	freopen("goods.out","w",stdout);
	scanf("%d%d",&n,&d);
	for(int i=1,a[2];i<=n;i++){
		scanf("%d",&a[i&1]);
		if(!t[a[i&1]]) num[++cnt]=a[i&1];
		t[a[i&1]]++;if(i==1) continue;
		int mx=max(a[i&1],a[!(i&1)]),mn=min(a[i&1],a[!(i&1)]);
		big[++big[0]]=mx,small[++small[0]]=mn;
	}
	sort(num+1,num+cnt+1);
	sort(big+1,big+big[0]+1);
	sort(small+1,small+small[0]+1);
	for(int i=1;i<=n;i++) sa[i]=sa[i-1]+big[i],sb[i]=sb[i-1]+small[i];
	for(int i=1,L,R;i<cnt;i++){
		l=num[i],r=num[i]+d;
		L=upper_bound(big+1,big+big[0]+1,l)-big-1;
		R=lower_bound(big+1,big+big[0]+1,r)-big-1;
		long long mx=1ll*L*l+1ll*(big[0]-R)*r+sua(L+1,R);
		L=upper_bound(small+1,small+small[0]+1,l)-small-1;
		R=lower_bound(small+1,small+small[0]+1,r)-small-1;
		long long mn=1ll*L*l+1ll*(small[0]-R)*r+sub(L+1,R);
		ans=max(ans,mx-mn);
	}
	for(int i=cnt,L,R;i>1;i--){
		l=num[i]-d,r=num[i];
		L=upper_bound(big+1,big+big[0]+1,l)-big-1;
		R=lower_bound(big+1,big+big[0]+1,r)-big-1;
		long long mx=1ll*L*l+1ll*(big[0]-R)*r+sua(L+1,R);
		L=upper_bound(small+1,small+small[0]+1,l)-small-1;
		R=lower_bound(small+1,small+small[0]+1,r)-small-1;
		long long mn=1ll*L*l+1ll*(small[0]-R)*r+sub(L+1,R);
		ans=max(ans,mx-mn);
	}
	return printf("%lld\n",ans),0;
}

T2 价值

咕咕咕

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+9;
const int mod=998244353;
int n,tot,mn,mx,ans;
long long f[maxn][2][3][3],g[2][3][3];
int h[maxn],nxt[maxn],to[maxn];
void add(int x,int y){
	tot++;
	nxt[tot]=h[x];
	to[tot]=y;
	h[x]=tot;
}
void dfs(int x){
    if(!h[x]) mn=min(mn,x),mx=max(mx,x);
    for(int i=h[x];i;i=nxt[i]){
        int y=to[i];
        dfs(y);
    }
}
void dp(int x,int y,bool flag){
	if(flag){
		for(int j=0;j<=2;j++){
	        for(int k=0;k<=2;k++){
	            g[1][j][k]=(
					f[x][1][j][0]*f[y][0][0][k]%mod+
					f[x][1][j][1]*f[y][0][1][k]%mod+
					f[x][1][j][2]*f[y][0][2][k]%mod+
					f[x][1][j][2]*f[y][0][0][k]%mod+
					f[x][1][j][0]*f[y][0][2][k]%mod+
					
					f[x][1][j][0]*f[y][1][0][k]%mod+
					f[x][1][j][1]*f[y][1][1][k]%mod+
					f[x][1][j][2]*f[y][1][2][k]%mod+
					f[x][1][j][2]*f[y][1][0][k]%mod+
					f[x][1][j][0]*f[y][1][2][k]%mod+
					
					f[x][0][j][0]*f[y][0][0][k]%mod+
					f[x][0][j][1]*f[y][0][1][k]%mod+
					f[x][0][j][2]*f[y][0][2][k]%mod+
					f[x][0][j][2]*f[y][0][0][k]%mod+
					f[x][0][j][0]*f[y][0][2][k]%mod
				)%mod;
	            g[0][j][k]=(
					f[x][0][j][0]*f[y][0][0][k]%mod+
					f[x][0][j][1]*f[y][0][1][k]%mod+
					f[x][0][j][2]*f[y][0][2][k]%mod+
					f[x][0][j][2]*f[y][0][0][k]%mod+
					f[x][0][j][0]*f[y][0][2][k]%mod+
					
					f[x][0][j][0]*f[y][1][0][k]%mod+
					f[x][0][j][1]*f[y][1][1][k]%mod+
					f[x][0][j][2]*f[y][1][2][k]%mod+
					f[x][0][j][2]*f[y][1][0][k]%mod+
					f[x][0][j][0]*f[y][1][2][k]%mod
				)%mod;
	        }
    	}
	    for(int op=0;op<=1;op++){
	        for(int j=0;j<=2;j++){
	            for(int k=0;k<=2;k++){
	                f[x][op][j][k]=g[op][j][k];
	            }
	        }
		}
    }
	else{
		for(int j=0;j<=2;j++){
	        for(int k=0;k<=2;k++){
	            f[x][0][j][k]=(f[y][0][j][k]+f[y][1][j][k])%mod,
	            f[x][1][j][k]=f[y][0][j][k];
	        }
	    }
	}
}
void solve(int x){
    if(!h[x]) f[x][0][0][0]=f[x][1][1][2]=f[x][1][2][1]=1;
    for(int i=h[x];i;i=nxt[i]){
    	int y=to[i];
        solve(y);
    }
    for(int i=h[x];i;i=nxt[i]){
    	int y=to[i];
    	dp(x,y,i^h[x]);
    }
}
int main(){
	freopen("value.in","r",stdin);
	freopen("value.out","w",stdout);
	scanf("%d",&n);mn=0x3f3f3f3f;
	for(int i=2,fa;i<=n;i++) scanf("%d",&fa),add(fa,i);
	dfs(1),solve(1);
	ans=(
		f[1][0][0][0]+
		f[1][1][1][1]+
		f[1][0][2][2]+
		f[1][0][2][0]+
		f[1][0][0][2]+
		
		f[1][0][1][1]+
		f[1][1][0][0]+
		f[1][1][2][2]+
		f[1][1][2][0]+
		f[1][1][0][2]
	)%mod;
	return printf("%d\n",ans),0;
}

T3 货币

咕咕咕

点击查看代码
#include <bits/stdc++.h>
using namespace std;
const int maxn=509,maxm=3009;
int n,m,s,t,x,y,c,tot,dis[maxn],cur[maxn];long long ans;
int h[maxn],nxt[maxm<<1],to[maxm<<1],w[maxm<<1];
void add(int x,int y,int z){
	tot++;
	nxt[tot]=h[x];
	to[tot]=y;
	w[tot]=z;
	h[x]=tot;
}
void join(int x,int y,int z){add(x,y,z),add(y,x,0);}
bool bfs(){
	memset(dis,0x3f,sizeof dis),dis[s]=0;
	queue<int> q;q.push(s);cur[s]=h[s];
	while(q.size()){
		int x=q.front();q.pop();
		for(int i=h[x];i;i=nxt[i]){
			int y=to[i];
			if(w[i]>0&&dis[y]==0x3f3f3f3f){
				q.push(y);cur[y]=h[y];
				dis[y]=dis[x]+1;
				if(y==t) return true;
			}
		}
	}
	return false;
}
int dfs(int x,int sum){
	if(x==t) return sum;
	int res=0;
	for(int i=cur[x];sum&&i;i=nxt[i]){
		cur[x]=i;
		int y=to[i];
		if(w[i]>0&&(dis[y]==dis[x]+1)){
			int delta=dfs(y,min(sum,w[i]));
			if(!delta) dis[y]=0x3f3f3f3f;
			w[i]-=delta,w[i^1]+=delta;
			res+=delta,sum-=delta;
		}
	}
	return res;
}
int main(){
	freopen("currency.in","r",stdin);
	freopen("currency.out","w",stdout);
	scanf("%d%d",&n,&m);tot=1,s=n+1,t=n+2;
	join(s,0,0),join(s,n,0x3f3f3f3f);
	for(int i=1;i<n;i++) scanf("%d",&c),join(s,i,c);
	for(int i=1;i<=n;i++){
		scanf("%d",&c);
		join(i,i-1,c);
		if(i==1||i==n) continue;
		join(i-1,i,c);
	}
	for(int i=1;i<n;i++) scanf("%d",&c),join(i,t,c);
	join(0,t,0x3f3f3f3f);join(n,t,0);
	while(m--) scanf("%d%d%d",&x,&y,&c),join(y,x,c);
	while(bfs()) ans+=dfs(s,0x3f3f3f3f);
	return printf("%lld",ans),0;
}

T4 资本

咕咕咕

CSP-S模拟 5

T1 光

咕咕咕

点击查看代码
#include<bits/stdc++.h>
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
int A,B,C,D,ans;
int main(){
	freopen("light.in","r",stdin);
	freopen("light.out","w",stdout);
	scanf("%d%d%d%d",&A,&B,&C,&D);
	ans=A+B+C+D;register int a,b,c,d;
	for(a=max(0,A-950);a<=min(A,ans);a++){
		for(b=max(0,B-950);b<=min(B,ans-a);b++){
			for(c=max(0,C-950);c<=min(C,ans-a-b);c++){
				d=max(0,D-(a>>2)-(b>>1)-(c>>1));
				if(A>a+(b>>1)+(c>>1)+(d>>2)) d=max(d,(A-a-(b>>1)-(c>>1))<<2);
				if(B>(a>>1)+b+(c>>2)+(d>>1)) d=max(d,(B-(a>>1)-b-(c>>2))<<1);
				if(C>(a>>1)+(b>>2)+c+(d>>1)) d=max(d,(C-(a>>1)-(b>>2)-c)<<1);
				ans=min(ans,a+b+c+d);
			}
		}
	}
	return printf("%d\n",ans),0;
}
#include<bits/stdc++.h>
using namespace std;
int A,B,C,D,ans;
int main(){
	freopen("light.in","r",stdin);
	freopen("light.out","w",stdout);
	scanf("%d%d%d%d",&A,&B,&C,&D);
	while(A>0||B>0||C>0||D>0){
		int mx=max({A,B,C,D}),delta=4;
		if(mx<4){
			int cp[4]={A,B,C,D};sort(cp,cp+4);
			if(cp[3]==3&&cp[2]==2&&cp[1]==1&&cp[0]<=0) ans--;
			if(cp[3]==2&&cp[2]==2&&cp[1]==1&&cp[0]<=0) ans--;
		}
		if(A<=0&&B<=0&&C<=0&&D<=0) break;
		else if(mx==A){
			if(mx>=4) A-=delta,B-=delta>>1,C-=delta>>1,D-=delta>>2,ans+=delta;
			else A-=mx,B-=mx>>1,C-=mx>>1,ans+=mx;
		}
		else if(mx==B){
			if(mx>=4) A-=delta>>1,B-=delta,C-=delta>>2,D-=delta>>1,ans+=delta;
			else A-=mx>>1,B-=mx,D-=mx>>1,ans+=mx;
		}
		else if(mx==C){
			if(mx>=4) A-=delta>>1,B-=delta>>2,C-=delta,D-=delta>>1,ans+=delta;
			else A-=mx>>1,C-=mx,D-=mx>>1,ans+=mx;
		}
		else if(mx==D){
			if(mx>=4) A-=delta>>2,B-=delta>>1,C-=delta>>1,D-=delta,ans+=delta;
			else B-=mx>>1,C-=mx>>1,D-=mx,ans+=mx;
		}
	}
	return printf("%d\n",ans),0;
}

T2 爬

咕咕咕

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+9;
const int mod=1e9+7;
int n,tot,cnt,delta,ans;
int a[maxn],son[maxn],_0[33],_1[33];
int h[maxn],nxt[maxn],to[maxn];
void add(int x,int y){
	tot++;
	nxt[tot]=h[x];
	to[tot]=y;
	h[x]=tot;
}
inline int ksm(int a,int b){
	int res=1;
	for(;b;b>>=1,a=1ll*a*a%mod) if(b&1) res=1ll*res*a%mod;
	return res;
}
int sum(int x){
	memset(_0,0,sizeof _0);
	memset(_1,0,sizeof _1);
	int res=mod-a[x];
	for(int j=1,tmp=a[x];j<=32;j++){
		tmp&1?_1[j]++:_0[j]++;
		tmp>>=1;
	}
	for(int i=h[x];i;i=nxt[i]){
		int y=to[i];(res-=a[y]-mod)%=mod;
		for(int j=1,tmp=a[y];j<=32;j++){
			tmp&1?_1[j]++:_0[j]++;
			tmp>>=1;
		}
	}
	for(int i=1;i<=32;i++){
		if(!_1[i]) continue;
		(res+=1ll*ksm(2,_1[i]+_0[i]-1)*ksm(2,i-1)%mod)%=mod;
	}
	return res;
}
void dfs(int x){
	if(!son[x]) return;
	for(int i=h[x];i;i=nxt[i]){
		int y=to[i];
		dfs(y);
	}
	if(x==1) (ans+=1ll*((sum(x)-delta)%mod+mod)*ksm(2,n-son[x]-1)%mod)%=mod;
	else (ans+=1ll*sum(x)*ksm(2,n-son[x]-2)%mod)%=mod;
}
int main(){
	freopen("climb.in","r",stdin);
	freopen("climb.out","w",stdout);
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=2,fa;i<=n;i++) scanf("%d",&fa),add(fa,i),son[fa]++;
	for(int i=h[1];i;i=nxt[i]){
		int y=to[i];(delta-=a[y]-mod)%=mod;
		for(int j=1,tmp=a[y];j<=32;j++){
			tmp&1?_1[j]++:_0[j]++;
			tmp>>=1;
		}
	}
	for(int i=1;i<=32;i++){
		if(!_1[i]) continue;
		(delta+=1ll*ksm(2,_1[i]+_0[i]-1)*ksm(2,i-1)%mod-mod)%=mod;
	}
	return dfs(1),printf("%d\n",ans),0;
}

T3 字符串

咕咕咕

T4 奇怪的函数

咕咕咕

CSP-S模拟 6

T1 一般图最小匹配

咕咕咕

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=5009;
int n,m,a[maxn],dp[maxn][maxn>>1][2];
int main(){
	freopen("match.in","r",stdin);
	freopen("match.out","w",stdout);
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	sort(a+1,a+n+1);
	memset(dp,0x3f,sizeof dp);
	for(int i=1;i<=n;i++){
		dp[i][0][0]=0;
		for(int j=1;j<=min(m,i>>1);j++){
			dp[i][j][0]=min(dp[i-1][j][0],dp[i-1][j][1]);
			dp[i][j][1]=dp[i-1][j-1][0]+abs(a[i]-a[i-1]);
		}
	}
	return printf("%d\n",min(dp[n][m][0],dp[n][m][1])),0;
}
#include<bits/stdc++.h>
#define min(a,b) (a<b?a:b)
using namespace std;
const int maxn=5009;
int n,m,a[maxn],dp[maxn][maxn>>1];
int main(){
	freopen("match.in","r",stdin);
	freopen("match.out","w",stdout);
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	sort(a+1,a+n+1);memset(dp,0x3f,sizeof dp),dp[0][0]=0;
	for(int i=1;i<=n;i++){
		int max_pair=min(m,i>>1);
		for(int j=0;j<=max_pair;j++){
			dp[i][j]=min(dp[i-1][j],dp[i-2][j-1]+a[i]-a[i-1]);
		}
	}
	return printf("%d\n",dp[n][m]),0;
}

T2 重定向

咕咕咕

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+9;
int T,n,a[maxn];bool del,vis[maxn];
set<pair<int,int>> st;
int main(){
	freopen("repeat.in","r",stdin);
	freopen("repeat.out","w",stdout);
	scanf("%d",&T);
	while(T--){
		memset(vis,0,sizeof vis);
		del=false;st.clear();
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
			vis[a[i]]=true;
			if(a[i]) st.insert(make_pair(a[i],i));
		}
		for(int i=1,now=1;!del&&i<n;i++){
			while(vis[now]) now++;
			if(a[i]) st.erase(make_pair(a[i],i));
			if(!a[i]&&st.size()){
				pair<int,int> mn=*st.begin();
				if(mn.first<now) vis[mn.first]=false,a[mn.second]=-1,del=true;
				else now++;
			}
			else if(a[i+1]&&a[i+1]<a[i]) vis[a[i]]=false,a[i]=-1,del=true;
			else if(!a[i+1]&&now<a[i]) vis[a[i]]=false,a[i]=-1,del=true;
		}
		if(!del) vis[a[n]]=false,a[n]=-1,del=true;
		for(int i=1,now=1;i<=n;i++){
			if(!a[i]){
				while(vis[now]) now++;
				vis[a[i]=now]=true;
			}
			if(~a[i]) printf("%d ",a[i]);
		}
		puts("");
	}
	return 0;
}

T3 斯坦纳树

咕咕咕

T4 直径

咕咕咕

CSP-S模拟 7

T1 median

咕咕咕

点击查看代码
#include<bits/stdc++.h>
#define _ num[i]
using namespace std;
const int maxn=1e5+9;
const int mod=998244353;
int n,cnt,ans,s[7][maxn],num[maxn<<3];
int low(int x,int id){return lower_bound(s[id]+1,s[id]+n+1,x)-s[id]-1;}
int up(int x,int id){return n-(upper_bound(s[id]+1,s[id]+n+1,x)-s[id]-1);}
int same(int x,int id){return upper_bound(s[id]+1,s[id]+n+1,x)-lower_bound(s[id]+1,s[id]+n+1,x);}
int main(){
	freopen("median.in","r",stdin);
	freopen("median.out","w",stdout);
	scanf("%d",&n);
	for(int i=1;i<=5;i++){
		for(int j=1;j<=n;j++){
			scanf("%d",&s[i][j]);
			num[++cnt]=s[i][j];
		}
		sort(s[i]+1,s[i]+n+1);
	}
	sort(num+1,num+cnt+1);
	cnt=unique(num+1,num+cnt+1)-num-1;
	for(int i=1;i<=cnt;i++){
		int tmp=0;
		long long sm=0;
		sm=same(_,1);
		if(sm){
			(tmp+=sm*low(_,2)%mod*low(_,3)%mod*up(_,4)%mod*up(_,5)%mod)%=mod;
			(tmp+=sm*low(_,2)%mod*low(_,4)%mod*up(_,3)%mod*up(_,5)%mod)%=mod;
			(tmp+=sm*low(_,2)%mod*low(_,5)%mod*up(_,3)%mod*up(_,4)%mod)%=mod;
			(tmp+=sm*low(_,3)%mod*low(_,4)%mod*up(_,2)%mod*up(_,5)%mod)%=mod;
			(tmp+=sm*low(_,3)%mod*low(_,5)%mod*up(_,2)%mod*up(_,4)%mod)%=mod;
			(tmp+=sm*low(_,4)%mod*low(_,5)%mod*up(_,2)%mod*up(_,3)%mod)%=mod;
		}
		sm=same(_,2);
		if(sm){
			(tmp+=sm*low(_,1)%mod*low(_,3)%mod*up(_,4)%mod*up(_,5)%mod)%=mod;
			(tmp+=sm*low(_,1)%mod*low(_,4)%mod*up(_,3)%mod*up(_,5)%mod)%=mod;
			(tmp+=sm*low(_,1)%mod*low(_,5)%mod*up(_,3)%mod*up(_,4)%mod)%=mod;
			(tmp+=sm*low(_,3)%mod*low(_,4)%mod*up(_,1)%mod*up(_,5)%mod)%=mod;
			(tmp+=sm*low(_,3)%mod*low(_,5)%mod*up(_,1)%mod*up(_,4)%mod)%=mod;
			(tmp+=sm*low(_,4)%mod*low(_,5)%mod*up(_,1)%mod*up(_,3)%mod)%=mod;
		}
		sm=same(_,3);
		if(sm){
			(tmp+=sm*low(_,2)%mod*low(_,1)%mod*up(_,4)%mod*up(_,5)%mod)%=mod;
			(tmp+=sm*low(_,2)%mod*low(_,4)%mod*up(_,1)%mod*up(_,5)%mod)%=mod;
			(tmp+=sm*low(_,2)%mod*low(_,5)%mod*up(_,1)%mod*up(_,4)%mod)%=mod;
			(tmp+=sm*low(_,1)%mod*low(_,4)%mod*up(_,2)%mod*up(_,5)%mod)%=mod;
			(tmp+=sm*low(_,1)%mod*low(_,5)%mod*up(_,2)%mod*up(_,4)%mod)%=mod;
			(tmp+=sm*low(_,4)%mod*low(_,5)%mod*up(_,2)%mod*up(_,1)%mod)%=mod;
		}
		sm=same(_,4);
		if(sm){
			(tmp+=sm*low(_,2)%mod*low(_,1)%mod*up(_,3)%mod*up(_,5)%mod)%=mod;
			(tmp+=sm*low(_,2)%mod*low(_,3)%mod*up(_,1)%mod*up(_,5)%mod)%=mod;
			(tmp+=sm*low(_,2)%mod*low(_,5)%mod*up(_,3)%mod*up(_,1)%mod)%=mod;
			(tmp+=sm*low(_,3)%mod*low(_,1)%mod*up(_,2)%mod*up(_,5)%mod)%=mod;
			(tmp+=sm*low(_,1)%mod*low(_,5)%mod*up(_,2)%mod*up(_,3)%mod)%=mod;
			(tmp+=sm*low(_,3)%mod*low(_,5)%mod*up(_,2)%mod*up(_,1)%mod)%=mod;
		}
		sm=same(_,5);
		if(sm){
			(tmp+=sm*low(_,2)%mod*low(_,1)%mod*up(_,3)%mod*up(_,4)%mod)%=mod;
			(tmp+=sm*low(_,2)%mod*low(_,3)%mod*up(_,4)%mod*up(_,1)%mod)%=mod;
			(tmp+=sm*low(_,2)%mod*low(_,4)%mod*up(_,3)%mod*up(_,1)%mod)%=mod;
			(tmp+=sm*low(_,3)%mod*low(_,1)%mod*up(_,2)%mod*up(_,4)%mod)%=mod;
			(tmp+=sm*low(_,4)%mod*low(_,1)%mod*up(_,2)%mod*up(_,3)%mod)%=mod;
			(tmp+=sm*low(_,3)%mod*low(_,4)%mod*up(_,2)%mod*up(_,1)%mod)%=mod;
		}
		sm=1ll*same(_,1)*same(_,2)%mod;
		if(sm){
			(tmp+=sm*low(_,4)%mod*low(_,5)%mod*up(_,3)%mod)%=mod;
			(tmp+=sm*low(_,3)%mod*low(_,5)%mod*up(_,4)%mod)%=mod;
			(tmp+=sm*low(_,3)%mod*low(_,4)%mod*up(_,5)%mod)%=mod;
			(tmp+=sm*low(_,5)%mod*up(_,4)%mod*up(_,3)%mod)%=mod;
			(tmp+=sm*low(_,4)%mod*up(_,5)%mod*up(_,3)%mod)%=mod;
			(tmp+=sm*low(_,3)%mod*up(_,4)%mod*up(_,5)%mod)%=mod;
		}
		sm=1ll*same(_,1)*same(_,3)%mod;
		if(sm){
			(tmp+=sm*low(_,4)%mod*low(_,5)%mod*up(_,2)%mod)%=mod;
			(tmp+=sm*low(_,2)%mod*low(_,5)%mod*up(_,4)%mod)%=mod;
			(tmp+=sm*low(_,2)%mod*low(_,4)%mod*up(_,5)%mod)%=mod;
			(tmp+=sm*low(_,5)%mod*up(_,2)%mod*up(_,4)%mod)%=mod;
			(tmp+=sm*low(_,4)%mod*up(_,5)%mod*up(_,2)%mod)%=mod;
			(tmp+=sm*low(_,2)%mod*up(_,4)%mod*up(_,5)%mod)%=mod;
		}
		sm=1ll*same(_,1)*same(_,4)%mod;
		if(sm){
			(tmp+=sm*low(_,3)%mod*low(_,5)%mod*up(_,2)%mod)%=mod;
			(tmp+=sm*low(_,2)%mod*low(_,5)%mod*up(_,3)%mod)%=mod;
			(tmp+=sm*low(_,3)%mod*low(_,2)%mod*up(_,5)%mod)%=mod;
			(tmp+=sm*low(_,5)%mod*up(_,3)%mod*up(_,2)%mod)%=mod;
			(tmp+=sm*low(_,3)%mod*up(_,2)%mod*up(_,5)%mod)%=mod;
			(tmp+=sm*low(_,2)%mod*up(_,5)%mod*up(_,3)%mod)%=mod;
		}
		sm=1ll*same(_,1)*same(_,5)%mod;
		if(sm){
			(tmp+=sm*low(_,3)%mod*low(_,4)%mod*up(_,2)%mod)%=mod;
			(tmp+=sm*low(_,4)%mod*low(_,2)%mod*up(_,3)%mod)%=mod;
			(tmp+=sm*low(_,3)%mod*low(_,2)%mod*up(_,4)%mod)%=mod;
			(tmp+=sm*low(_,4)%mod*up(_,2)%mod*up(_,3)%mod)%=mod;
			(tmp+=sm*low(_,3)%mod*up(_,2)%mod*up(_,4)%mod)%=mod;
			(tmp+=sm*low(_,2)%mod*up(_,4)%mod*up(_,3)%mod)%=mod;
		}
		sm=1ll*same(_,2)*same(_,3)%mod;
		if(sm){
			(tmp+=sm*low(_,4)%mod*low(_,5)%mod*up(_,1)%mod)%=mod;
			(tmp+=sm*low(_,1)%mod*low(_,5)%mod*up(_,4)%mod)%=mod;
			(tmp+=sm*low(_,1)%mod*low(_,4)%mod*up(_,5)%mod)%=mod;
			(tmp+=sm*low(_,1)%mod*up(_,4)%mod*up(_,5)%mod)%=mod;
			(tmp+=sm*low(_,4)%mod*up(_,5)%mod*up(_,1)%mod)%=mod;
			(tmp+=sm*low(_,5)%mod*up(_,1)%mod*up(_,4)%mod)%=mod;
		}
		sm=1ll*same(_,2)*same(_,4)%mod;
		if(sm){
			(tmp+=sm*low(_,3)%mod*low(_,5)%mod*up(_,1)%mod)%=mod;
			(tmp+=sm*low(_,1)%mod*low(_,5)%mod*up(_,3)%mod)%=mod;
			(tmp+=sm*low(_,3)%mod*low(_,1)%mod*up(_,5)%mod)%=mod;
			(tmp+=sm*low(_,1)%mod*up(_,5)%mod*up(_,3)%mod)%=mod;
			(tmp+=sm*low(_,3)%mod*up(_,1)%mod*up(_,5)%mod)%=mod;
			(tmp+=sm*low(_,5)%mod*up(_,3)%mod*up(_,1)%mod)%=mod;
		}
		sm=1ll*same(_,2)*same(_,5)%mod;
		if(sm){
			(tmp+=sm*low(_,3)%mod*low(_,4)%mod*up(_,1)%mod)%=mod;
			(tmp+=sm*low(_,4)%mod*low(_,1)%mod*up(_,3)%mod)%=mod;
			(tmp+=sm*low(_,3)%mod*low(_,1)%mod*up(_,4)%mod)%=mod;
			(tmp+=sm*low(_,4)%mod*up(_,1)%mod*up(_,3)%mod)%=mod;
			(tmp+=sm*low(_,3)%mod*up(_,1)%mod*up(_,4)%mod)%=mod;
			(tmp+=sm*low(_,1)%mod*up(_,4)%mod*up(_,3)%mod)%=mod;
		}
		sm=1ll*same(_,3)*same(_,4)%mod;
		if(sm){
			(tmp+=sm*low(_,2)%mod*low(_,5)%mod*up(_,1)%mod)%=mod;
			(tmp+=sm*low(_,1)%mod*low(_,5)%mod*up(_,2)%mod)%=mod;
			(tmp+=sm*low(_,1)%mod*low(_,2)%mod*up(_,5)%mod)%=mod;
			(tmp+=sm*low(_,5)%mod*up(_,2)%mod*up(_,1)%mod)%=mod;
			(tmp+=sm*low(_,2)%mod*up(_,5)%mod*up(_,1)%mod)%=mod;
			(tmp+=sm*low(_,1)%mod*up(_,5)%mod*up(_,2)%mod)%=mod;
		}
		sm=1ll*same(_,3)*same(_,5)%mod;
		if(sm){
			(tmp+=sm*low(_,2)%mod*low(_,4)%mod*up(_,1)%mod)%=mod;
			(tmp+=sm*low(_,1)%mod*low(_,4)%mod*up(_,2)%mod)%=mod;
			(tmp+=sm*low(_,1)%mod*low(_,2)%mod*up(_,4)%mod)%=mod;
			(tmp+=sm*low(_,4)%mod*up(_,2)%mod*up(_,1)%mod)%=mod;
			(tmp+=sm*low(_,2)%mod*up(_,4)%mod*up(_,1)%mod)%=mod;
			(tmp+=sm*low(_,1)%mod*up(_,2)%mod*up(_,4)%mod)%=mod;
		}
		sm=1ll*same(_,4)*same(_,5)%mod;
		if(sm){
			(tmp+=sm*low(_,2)%mod*low(_,3)%mod*up(_,1)%mod)%=mod;
			(tmp+=sm*low(_,1)%mod*low(_,3)%mod*up(_,2)%mod)%=mod;
			(tmp+=sm*low(_,1)%mod*low(_,2)%mod*up(_,3)%mod)%=mod;
			(tmp+=sm*low(_,3)%mod*up(_,1)%mod*up(_,2)%mod)%=mod;
			(tmp+=sm*low(_,2)%mod*up(_,3)%mod*up(_,1)%mod)%=mod;
			(tmp+=sm*low(_,1)%mod*up(_,2)%mod*up(_,3)%mod)%=mod;
		}
		sm=1ll*same(_,1)*same(_,2)%mod*same(_,3)%mod;
		if(sm){
			(tmp+=sm*low(_,4)%mod*up(_,5)%mod)%=mod;
			(tmp+=sm*low(_,5)%mod*up(_,4)%mod)%=mod;
			(tmp+=sm*low(_,4)%mod*low(_,5)%mod)%=mod;
			(tmp+=sm*up(_,5)%mod*up(_,4)%mod)%=mod;
		}
		sm=1ll*same(_,1)*same(_,2)%mod*same(_,4)%mod;
		if(sm){
			(tmp+=sm*low(_,3)%mod*up(_,5)%mod)%=mod;
			(tmp+=sm*low(_,5)%mod*up(_,3)%mod)%=mod;
			(tmp+=sm*low(_,3)%mod*low(_,5)%mod)%=mod;
			(tmp+=sm*up(_,5)%mod*up(_,3)%mod)%=mod;
		}
		sm=1ll*same(_,1)*same(_,2)%mod*same(_,5)%mod;
		if(sm){
			(tmp+=sm*low(_,3)%mod*up(_,4)%mod)%=mod;
			(tmp+=sm*low(_,4)%mod*up(_,3)%mod)%=mod;
			(tmp+=sm*low(_,3)%mod*low(_,4)%mod)%=mod;
			(tmp+=sm*up(_,4)%mod*up(_,3)%mod)%=mod;
		}
		sm=1ll*same(_,1)*same(_,3)%mod*same(_,4)%mod;
		if(sm){
			(tmp+=sm*low(_,2)%mod*up(_,5)%mod)%=mod;
			(tmp+=sm*low(_,5)%mod*up(_,2)%mod)%=mod;
			(tmp+=sm*low(_,2)%mod*low(_,5)%mod)%=mod;
			(tmp+=sm*up(_,5)%mod*up(_,2)%mod)%=mod;
		}
		sm=1ll*same(_,1)*same(_,3)%mod*same(_,5)%mod;
		if(sm){
			(tmp+=sm*low(_,2)%mod*up(_,4)%mod)%=mod;
			(tmp+=sm*low(_,4)%mod*up(_,2)%mod)%=mod;
			(tmp+=sm*low(_,2)%mod*low(_,4)%mod)%=mod;
			(tmp+=sm*up(_,4)%mod*up(_,2)%mod)%=mod;
		}
		sm=1ll*same(_,1)*same(_,4)%mod*same(_,5)%mod;
		if(sm){
			(tmp+=sm*low(_,2)%mod*up(_,3)%mod)%=mod;
			(tmp+=sm*low(_,3)%mod*up(_,2)%mod)%=mod;
			(tmp+=sm*low(_,2)%mod*low(_,3)%mod)%=mod;
			(tmp+=sm*up(_,3)%mod*up(_,2)%mod)%=mod;
		}
		sm=1ll*same(_,2)*same(_,3)%mod*same(_,4)%mod;
		if(sm){
			(tmp+=sm*low(_,1)%mod*up(_,5)%mod)%=mod;
			(tmp+=sm*low(_,5)%mod*up(_,1)%mod)%=mod;
			(tmp+=sm*low(_,1)%mod*low(_,5)%mod)%=mod;
			(tmp+=sm*up(_,5)%mod*up(_,1)%mod)%=mod;
		}
		sm=1ll*same(_,2)*same(_,3)%mod*same(_,5)%mod;
		if(sm){
			(tmp+=sm*low(_,1)%mod*up(_,4)%mod)%=mod;
			(tmp+=sm*low(_,4)%mod*up(_,1)%mod)%=mod;
			(tmp+=sm*low(_,1)%mod*low(_,4)%mod)%=mod;
			(tmp+=sm*up(_,4)%mod*up(_,1)%mod)%=mod;
		}
		sm=1ll*same(_,2)*same(_,4)%mod*same(_,5)%mod;
		if(sm){
			(tmp+=sm*low(_,1)%mod*up(_,3)%mod)%=mod;
			(tmp+=sm*low(_,3)%mod*up(_,1)%mod)%=mod;
			(tmp+=sm*low(_,1)%mod*low(_,3)%mod)%=mod;
			(tmp+=sm*up(_,3)%mod*up(_,1)%mod)%=mod;
		}
		sm=1ll*same(_,3)*same(_,4)%mod*same(_,5)%mod;
		if(sm){
			(tmp+=sm*low(_,1)%mod*up(_,2)%mod)%=mod;
			(tmp+=sm*low(_,2)%mod*up(_,1)%mod)%=mod;
			(tmp+=sm*low(_,1)%mod*low(_,2)%mod)%=mod;
			(tmp+=sm*up(_,2)%mod*up(_,1)%mod)%=mod;
		}
		sm=1ll*same(_,1)*same(_,2)%mod*same(_,3)%mod*same(_,4)%mod;
		if(sm){
			(tmp+=sm*low(_,5)%mod)%=mod;
			(tmp+=sm*up(_,5)%mod)%=mod;
		}
		sm=1ll*same(_,1)*same(_,2)%mod*same(_,3)%mod*same(_,5)%mod;
		if(sm){
			(tmp+=sm*low(_,4)%mod)%=mod;
			(tmp+=sm*up(_,4)%mod)%=mod;
		}
		sm=1ll*same(_,1)*same(_,2)%mod*same(_,4)%mod*same(_,5)%mod;
		if(sm){
			(tmp+=sm*low(_,3)%mod)%=mod;
			(tmp+=sm*up(_,3)%mod)%=mod;
		}
		sm=1ll*same(_,1)*same(_,3)%mod*same(_,4)%mod*same(_,5)%mod;
		if(sm){
			(tmp+=sm*low(_,2)%mod)%=mod;
			(tmp+=sm*up(_,2)%mod)%=mod;
		}
		sm=1ll*same(_,2)*same(_,3)%mod*same(_,4)%mod*same(_,5)%mod;
		if(sm){
			(tmp+=sm*low(_,1)%mod)%=mod;
			(tmp+=sm*up(_,1)%mod)%=mod;
		}
		sm=1ll*same(_,1)*same(_,2)%mod*same(_,3)%mod*same(_,4)%mod*same(_,5)%mod;
		if(sm) (tmp+=sm)%=mod;
		(ans+=1ll*_*tmp%mod)%=mod;
	}
	return printf("%d\n",ans),0;
}

T2 travel

咕咕咕

T3 game

咕咕咕

T4 counter

咕咕咕

CSP-S模拟8

T1 score and rank

咕咕咕

T2 HZOI大作战

咕咕咕

T3 Delov的旅行

咕咕咕

T4 gtm和joke的星球

咕咕咕

CSP-S模拟9

T1 邻面合并

咕咕咕

点击查看代码
#include<bits/stdc++.h>
using namespace std;
int n,m,ans,dp[101][1<<9];
int mp[101],cp[101],sp[101];
bool need(int id,int s1,int s2,int jd){
	for(int i=jd;i<m;i++){
		bool a=1<<i&mp[id],b=1<<i&mp[id-1],c=1<<i&s1,d=1<<i&s2;
		if((c&&!a)||(d&&!b)) return false;
		if(a^b||c^d) return true;
		if(!a&&!b) break;
	}
	return false;
}
int merge(int i,int s1,int s2){
	int res=0;
	for(int j=0;j<m;j++){
		if(s1&(1<<j)&&s2&(1<<j)) res+=need(i,s1,s2,j);
		else if(s2&(1<<j)) res++;
	}
	return res;
}
int main(){
	freopen("merging.in","r",stdin);
	freopen("merging.out","w",stdout);
	scanf("%d%d",&n,&m);
	memset(dp,0x3f,sizeof dp);
	dp[0][0]=0;ans=0x3f3f3f3f;
	for(int i=1;i<=n;i++){
		for(int j=1,a;j<=m;j++) scanf("%d",&a),mp[i]|=a<<(j-1);
		for(int j=0,las=0;j<m;j++){
			if(!las&&1<<j&mp[i]) cp[i]|=1<<j,las=1;
			if(!(1<<j&mp[i])) las=0;
		}
		sp[i]=mp[i]^cp[i];
	}
	for(int i=1;i<=n;i++){
		for(int j=sp[i];;j=(j-1)&sp[i]){
			j|=cp[i];
			for(int k=sp[i-1];;k=(k-1)&sp[i-1]){
				k|=cp[i-1];
				dp[i][j]=min(dp[i][j],dp[i-1][k]+merge(i,k,j));
				if(!(k^=cp[i-1])) break;
			}
			if(i==n) ans=min(ans,dp[i][j]);
			if(!(j^=cp[i])) break;
		}
	}
	return printf("%d\n",ans),0;
}

T2 光线追踪

咕咕咕

T3 百鸽笼

咕咕咕

T4 滑稽树下你和我

咕咕咕

CSP-S模拟10

T1 欧几里得的噩梦

咕咕咕

T2 清扫

咕咕咕

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+9;
int n,tot,a[maxn],deg[maxn];
int h[maxn],nxt[maxn<<1],to[maxn<<1];
void add(int x,int y){
	tot++;
	nxt[tot]=h[x];
	to[tot]=y;
	h[x]=tot;
}
int dfs(int x,int die){
	int s=0,cnt=0,mx=0;
	for(int i=h[x];i;i=nxt[i]){
		int y=to[i];
		if(y==die) continue;
		int cp=dfs(y,x);
		s+=cp,mx=max(mx,cp),cnt++;
	}
	if(!cnt) return a[x];
	if(cnt==1){
		if(s^a[x]) puts("NO"),exit(0);
		return s;
	}
	if(s<a[x]||s>a[x]<<1||mx>a[x]) puts("NO"),exit(0);
	return (a[x]<<1)-s;
}
int main(){
	freopen("tree.in","r",stdin);
	freopen("tree.out","w",stdout);
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	if(n==2) return puts(a[1]^a[2]?"NO":"YES"),0;
	for(int i=1,x,y;i<n;i++){
		scanf("%d %d",&x,&y);
		add(x,y),add(y,x);
		deg[x]++,deg[y]++;
	}
	for(int i=1;i<=n;i++){
		if(deg[i]==1) continue;
		return puts(dfs(i,0)?"NO":"YES"),0;
	}
}

T3 购物

咕咕咕

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+9;
int n,a[maxn];long long sum,ans;
int main(){
	freopen("buy.in","r",stdin);
	freopen("buy.out","w",stdout);
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++){
		int cp=(a[i]+1)>>1;
		ans+=cp>sum?sum+a[i]-cp+1:a[i];
		sum+=a[i];
	}
	return printf("%lld\n",ans),0;
}

T4 ants

咕咕咕

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+9;
int n,m,L,R,blk,res;bool vis[maxn];
int ant[maxn],die[maxn],sz[maxn],st[maxn],ans[maxn];
struct query{
	int l,r,id,pos;
	bool operator <(const query &cp)const{
		return pos^cp.pos?pos<cp.pos:r<cp.r;
	}
}q[maxn];
int findie(int x){return x==die[x]?x:findie(die[x]);}
void merge(int x,int y){
	x=findie(x),y=findie(y);
	if(x==y) return;
	if(sz[x]>sz[y]) swap(x,y);
	die[x]=y,sz[y]+=sz[x];
	st[++st[0]]=x;
}
void add(int x){
	vis[x]=true;
	if(vis[x-1]) merge(x,x-1);
	if(vis[x+1]) merge(x,x+1);
	res=max(res,sz[findie(x)]);
}
void del(int x){sz[findie(x)]-=sz[x],die[x]=x;}
int main(){
	freopen("ants.in","r",stdin);
	freopen("ants.out","w",stdout);
	scanf("%d%d",&n,&m);blk=sqrt(n);
	for(int i=1;i<=n;i++) scanf("%d",&ant[i]);
	for(int i=1;i<=m;i++){
		scanf("%d%d",&q[i].l,&q[i].r);
		q[i].id=i,q[i].pos=(q[i].l-1)/blk+1;
	}
	sort(q+1,q+m+1);
	for(int i=1,lsp;i<=m;i++){
		if(q[i].pos^q[i-1].pos){
			for(int j=1;j<=n;j++) vis[j]=false,die[j]=j,sz[j]=1;
			L=lsp=q[i].pos*blk+1,R=L-1;
			res=st[0]=0;
		}
		if(q[i].pos==(q[i].r-1)/blk+1){
			for(int j=q[i].l;j<=q[i].r;j++) st[++st[0]]=ant[j];
			sort(st+1,st+st[0]+1);
			for(int j=1,now=1;j<=st[0];j++){
				now=(j>1&&st[j]==st[j-1]+1)?now+1:1;
				ans[q[i].id]=max(ans[q[i].id],now);
			}
			st[0]=0;
		}
		else{
			while(R<q[i].r) add(ant[++R]);
			int las=res,top=st[0];
			while(L>q[i].l) add(ant[--L]);
			ans[q[i].id]=res,res=las;
			while(L<lsp) vis[ant[L++]]=false;
			while(st[0]>top) del(st[st[0]--]);
		}
	}
	for(int i=1;i<=m;i++) printf("%d\n",ans[i]);
	return 0;
}

CSP-S模拟11

T1 玩水

咕咕咕

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=1009;
int T,n,m,cnt;
char a[maxn][maxn];
pair<int,int> ok[1000000];
int main(){
	freopen("water.in","r",stdin);
	freopen("water.out","w",stdout);
	scanf("%d",&T);
	while(T--){
		scanf("%d%d",&n,&m);cnt=0;
		for(int i=1;i<=n;i++) scanf("%s",a[i]+1);
		for(int i=1;i<n;i++){
			for(int j=1;j<m;j++){
				if(a[i][j+1]==a[i+1][j]){
					ok[++cnt]=make_pair(i,j);
				}
			}
		}
		bool v=false;
		sort(ok+1,ok+cnt+1);
		for(int i=1;i<cnt;i++){
			if(ok[i].first<ok[i+1].first&&ok[i].second<ok[i+1].second) v=true;
			if(ok[i].first+1==ok[i+1].first&&ok[i].second==ok[i+1].second) v=true;
			if(ok[i].first==ok[i+1].first&&ok[i].second+1==ok[i+1].second) v=true;
		}
		puts(cnt>1&&v?"1":"0");
	}
	return 0;
}

T2 AVL树

咕咕咕

T3 暴雨

咕咕咕

T4 置换

咕咕咕

T5 传统题

咕咕咕

CSP-S模拟12

小h的几何

咕咕咕

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const long double pi=acos(-1);
int n;long double ag,x,y;
int main(){
	freopen("geometry.in","r",stdin);
	freopen("geometry.out","w",stdout);
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%Lf",&ag);
		x+=cos(ag*pi/1e9);
		y+=sin(ag*pi/1e9);
	}
	return printf("%.9Lf %.9Lf\n",x*1.5/n,y*1.5/n),0;
}

小w的代数

咕咕咕

小y的数论

咕咕咕

小j的组合

咕咕咕