Atcoder Beginner Contest 425 A-E题解

A

CODE
#include<bits/stdc++.h>
#define usetime() (double)clock () / CLOCKS_PER_SEC * 1000.0
using namespace std;
typedef long long LL;
void read(int& x){
	char c;
	bool f=0;
	while((c=getchar())<48) f|=(c==45);
	x=c-48;
	while((c=getchar())>47) x=(x<<3)+(x<<1)+c-48;
	x=(f ? -x : x);
}
int n;
int main(){
	read(n);
	LL ans=0;
	for(int i=1;i<=n;i++){
		ans+=1ll*((i&1) ? -1 : 1)*i*i*i;
	}
	printf("%lld",ans);
	return 0;
}
//^o^

B

把没用过的标记出来,然后扫一遍即可

CODE
#include<bits/stdc++.h>
#define usetime() (double)clock () / CLOCKS_PER_SEC * 1000.0
using namespace std;
typedef long long LL;
const int maxn=1e6+5;
void read(int& x){
	char c;
	bool f=0;
	while((c=getchar())<48) f|=(c==45);
	x=c-48;
	while((c=getchar())>47) x=(x<<3)+(x<<1)+c-48;
	x=(f ? -x : x);
}
int a[maxn],b[maxn];
bool f[maxn],ans=1;
int n;
int main(){
	read(n);
	for(int i=1;i<=n;i++){
		read(a[i]);
		if(a[i]>0){
			ans&=(!f[a[i]]),f[a[i]]=1;
		}
	}
	if(!ans){
		printf("No");
		return 0;
	}
	printf("Yes\n");
	int p=1;
	for(int i=1;i<=n;i++){
		if(a[i]==-1){
			while(f[p]) ++p;
			b[i]=p++;
		}
		else b[i]=a[i];
	}
	for(int i=1;i<=n;i++) printf("%d ",b[i]);
	return 0;
}
//^o^

C

赛时题意理解错了,还以为是什么大难题呢

记录一下开头的位置就好了

ABC410-C 差不多

CODE
#include<bits/stdc++.h>
#define usetime() (double)clock () / CLOCKS_PER_SEC * 1000.0
using namespace std;
typedef long long LL;
const int maxn=2e5+5;
void read(int& x){
	char c;
	bool f=0;
	while((c=getchar())<48) f|=(c==45);
	x=c-48;
	while((c=getchar())>47) x=(x<<3)+(x<<1)+c-48;
	x=(f ? -x : x);
}
int n,q;
int a[maxn];
LL sum[maxn];
int main(){
	read(n),read(q);
	for(int i=1;i<=n;i++) read(a[i]),sum[i]=sum[i-1]+a[i];
	int op,x,l,r;
	int b=1;
	while(q--){
		read(op);
		if(op==1){
			read(x),b=(b-1+x)%n+1;
		}
		else if(op==2){
			read(l),read(r);
			l=(b+l-2)%n+1,r=(b+r-2)%n+1;
			if(l>r) printf("%lld\n",sum[r]+sum[n]-sum[l-1]);
			else printf("%lld\n",sum[r]-sum[l-1]);
		}
	}
	return 0;
}
//^o^

D

每次暴力去做肯定会超时的

考虑一个长为 \(3 \times 10^5\) ,宽为 \(1\) 的地图,中间是 \(1\),这时很明显就超了

可以考虑每次只更新上一次被更新出来的黑色块的周围,因为老黑色块在以前已经更新过了,而且新生成的要求是要在一个黑色块边上的

CODE
#include<bits/stdc++.h>
#define usetime() (double)clock () / CLOCKS_PER_SEC * 1000.0
#define mkp(a,b) make_pair(a,b)
#define fst first
#define sec second
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int maxn=3e5+5;
void read(int& x){
	char c;
	bool f=0;
	while((c=getchar())<48) f|=(c==45);
	x=c-48;
	while((c=getchar())>47) x=(x<<3)+(x<<1)+c-48;
	x=(f ? -x : x);
}
int n,m;
vector<vector<int> > mp;
int wx[4]={0,0,-1,1},wy[4]={-1,1,0,0};
bool check(int x,int y){
	if(x<1||x>n||y<1||y>m) return false;
	if(mp[x][y]==1) return false;
	int cnt=0;
	for(int i=0;i<4;i++){
		int xi=x+wx[i],yi=y+wy[i];
		if(xi<1||xi>n||yi<1||yi>m) continue;
		cnt+=mp[xi][yi];
	}
	return cnt==1;
}
char c;
int main(){
	read(n),read(m);
	mp.resize(n+2,vector<int>(m+2,0));
	queue<pii> st;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>c;
			if(c=='#') mp[i][j]=1;
			else mp[i][j]=0;
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(mp[i][j]) st.push(mkp(i,j));
		}
	}
	while(!st.empty()){
		stack<pii> si;
		int tp=st.size();
		for(int i=1;i<=tp;i++){
			pii p=st.front();
			st.pop();
			for(int i=0;i<4;i++){
				if(check(p.fst+wx[i],p.sec+wy[i])){
					st.push(mkp(p.fst+wx[i],p.sec+wy[i]));
					si.push(mkp(p.fst+wx[i],p.sec+wy[i]));
				}
			}
		}
		while(!si.empty()){
			pii p=si.top();
			si.pop();
			mp[p.fst][p.sec]=1;
		}
	}
	int ans=0;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			ans+=mp[i][j];
		}
	}
	printf("%d",ans);
	return 0;
}
//^o^

E

意外的简单

这不就是基础排列组合吗

公式不用我说了吧

由于模数不一定为质数,这里考虑到用杨辉三角预处理 \(C_i^j\) 的值

赛时没有想到这个递推式,花了一个小时推出一个桶思想+差分数组+质因数分解的做法,代码放第二个了

感兴趣可以看看,这个做法是可以支持每次询问改变模数的,我觉得可以再出一道题

CODE
#include<bits/stdc++.h>
#define usetime() (double)clock () / CLOCKS_PER_SEC * 1000.0
using namespace std;
typedef long long LL;
const int maxn=2e5+5;
const int maxm=5e3+5;
void read(int& x){
	char c;
	bool f=0;
	while((c=getchar())<48) f|=(c==45);
	x=c-48;
	while((c=getchar())>47) x=(x<<3)+(x<<1)+c-48;
	x=(f ? -x : x);
}
int t,mod;
int n;
int a[maxn];
LL C[maxm][maxm];
int main(){
	read(t),read(mod);
	C[0][0]=1;
	for(int i=1;i<=5000;i++){
		C[i][0]=1;
		for(int j=1;j<=i;j++){
			C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;
		}
	}
	while(t--){
		read(n);
		int sum=0;
		for(int i=1;i<=n;i++) read(a[i]),sum+=a[i];
		LL ans=1;
		for(int i=1;i<=n;i++){
			ans=ans*C[sum][a[i]]%mod;
			sum-=a[i];
		}
		ans%=mod;
		printf("%lld\n",ans);
	}
	return 0;
}
//^o^
CODE
#include<bits/stdc++.h>
#define usetime() (double)clock () / CLOCKS_PER_SEC * 1000.0
#define mkp(a,b) make_pair(a,b)
#define fst first
#define sec second
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int maxn=1e6+5;
void read(int& x){
	char c;
	bool f=0;
	while((c=getchar())<48) f|=(c==45);
	x=c-48;
	while((c=getchar())>47) x=(x<<3)+(x<<1)+c-48;
	x=(f ? -x : x);
}
void read(LL& x){
	char c;
	bool f=0;
	while((c=getchar())<48) f|=(c==45);
	x=c-48;
	while((c=getchar())>47) x=(x<<3)+(x<<1)+c-48;
	x=(f ? -x : x);
}
int n,ti;
int a[maxn];
int t[maxn];
LL mod;
LL fpow(LL x,LL y){
	LL ans=1;
	while(y){
		if(y&1) ans=ans*x%mod;
		x=x*x%mod,y>>=1;
	}
	return ans;
}
void C(int x,int y){
	y=min(y,x-y);
	++t[x-y+1],--t[x+1],--t[1],++t[y+1];
}
vector<pii> v[maxn];
vector<int> z;
LL p[maxn];
bool f[maxn];
int main(){
	read(ti),read(mod);
	for(int i=2;i<=5005;i++){
		if(!f[i]) z.push_back(i);
		for(int j=i*2;j<=5005;j+=i){
			f[j]=1;
		}
	}
	for(int i=1;i<=5005;i++){
		int k=i;
		for(int j=0;j<(int)z.size();j++){
			int cnt=0;
			while(k%z[j]==0){
				k/=z[j];
				++cnt;
			}
			if(cnt) v[i].push_back(mkp(cnt,z[j]));
		}
		if(k>1) v[i].push_back(mkp(1,k));
	}
	while(ti--){
		read(n);
		int sum=0,summ;
		for(int i=1;i<=n;i++) read(a[i]),sum+=a[i];
		summ=sum;
		for(int i=1;i<=n;i++){
			C(sum,a[i]);
			sum-=a[i];
		}
		LL ans=1;
		for(int i=1;i<=summ;i++){
			t[i]+=t[i-1];
			if(!t[i]) continue;
			for(int j=0;j<(int)v[i].size();j++){
				p[v[i][j].sec]+=t[i]*v[i][j].fst;
			}
		}
		fill(t+1,t+summ+2,0);
		for(int i=0;i<(int)z.size();i++){
			ans=ans*fpow(z[i],p[z[i]])%mod;
			p[z[i]]=0;
		}
		t[summ+1]=0;
		ans%=mod;
		printf("%lld\n",ans);
	}
	return 0;
}
//^o^
posted @ 2025-09-29 22:58  huangems  阅读(8)  评论(0)    收藏  举报