AtCoder Beginner Contest 172 总结

颓了三天端午回来打比赛,还是秒切前几道水题但做不动后面的难题,任重道远啊。

前四题思路都比较简单,C枚举一个数组的前缀和并用差值在另外一个数组里二分查找,D可以线性筛约数个数函数然后暴力算,也可以分析每个数的贡献来搞,复杂度都为O(n)。

E没有做出来是真的不爽,只想到了先考虑A,枚举一个排列AtCoder Beginner Contest 172 总结,A确定以后对B的计算方式确实没有想到。看了题解说的容斥原理恍然大悟。先不考虑A算B有多少种排法(也是AtCoder Beginner Contest 172 总结),然后减去至少有一位与A相同的排法(AtCoder Beginner Contest 172 总结),再加回来至少两位与A相同的排法(AtCoder Beginner Contest 172 总结)......

当年搞OI时数学的薄弱板块容斥果然没有放过本人......

F有关博弈论,涉及的Nim游戏还不太会,补了坑再回来写。

 

 

A题

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

inline int read() {
	int x=0,f=1;char c=getchar();
	while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
	while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
	return x*f;
}

int main() {
	int n=read();
	printf("%d\n",n+n*n+n*n*n);
	return 0;
}

B题

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
string S,T;
inline int read() {
	int x=0,f=1;char c=getchar();
	while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
	while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
	return x*f;
}

int main() {
	cin>>S>>T;
	int n=S.length();
	int ans=0;
	for (register int i=0;i<n;++i)
		ans+=(S[i]!=T[i]);
	cout<<ans<<endl;
	return 0;
}

C题

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=2e5+4;
int n,m;
ll K;
ll a[N],b[N];
inline int read() {
	int x=0,f=1;char c=getchar();
	while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
	while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
	return x*f;
}
inline void smax(int &x,int y) {
	x=x<y?y:x;
}
int main() {
	n=read(),m=read(),K=read();
	for (register int i=1;i<=n;++i) a[i]=a[i-1]+read();
	for (register int i=1;i<=m;++i) b[i]=b[i-1]+read();
	int ans=0;
	for (register int i=0;i<=n;++i) {
		if (a[i]>K) break;
		int res=K-a[i];
		int pos=upper_bound(b+1,b+m+1,res)-b-1;
		smax(ans,i+pos);
	}
	printf("%d\n",ans);
	return 0;
}

D题

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e7+4;
typedef long long ll;
int n;
int prime[N],tot;
bool vis[N];
int d[N],t[N];
inline int read() {
	int x=0,f=1;char c=getchar();
	while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
	while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
	return x*f;
}

inline void linear_shaker(int lim) {
	d[1]=1;
	for (register int i=2;i<=lim;i++) {
		if (!vis[i]) prime[++tot]=i,d[i]=2,t[i]=1;
		for (register int j=1;j<=tot&&i*prime[j]<=lim;j++) {
			vis[i*prime[j]]=true;
			if (i%prime[j]==0) {
				t[i*prime[j]]=t[i]+1;
				d[i*prime[j]]=d[i]/(t[i]+1)*(t[prime[j]*i]+1);
				break;
			}
			t[i*prime[j]]=1;
			d[i*prime[j]]=d[i]*2;
		}
	}
}
ll ans;
int main() {
	n=read();
	linear_shaker(n);
	for (register int i=1;i<=n;++i)
		ans+=1ll*i*d[i];
	cout<<ans<<endl;
	return 0;
}

E题

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=5e5+5;
const ll MOD=1e9+7;
inline int read() {
	int x=0,f=1;char c=getchar();
	while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
	while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
	return x*f;
}
ll fac[N]={1,1};
ll inv[N]={1,1};
int n,m;
inline ll fpow(ll a,int b) {
	ll ret=1;
	while (b) {
		if (b&1) ret=ret*a%MOD;
		b>>=1,a=a*a%MOD;
	}
	return ret;
}
inline ll C(int x,int y) {
	ll ans=fac[x];
	ans=ans*inv[y]%MOD;
	ans=ans*inv[x-y]%MOD;
	return ans;
}
inline ll P(int x,int y) {
	ll ans=fac[x];
	ans=ans*inv[x-y]%MOD;
	return ans;
}
inline void init() {
	for (register int i=1;i<N;++i) fac[i]=fac[i-1]*i%MOD;
	inv[N-1]=fpow(fac[N-1],MOD-2);
	for (int i=N-2;~i;--i) inv[i]=inv[i+1]*(i+1)%MOD;
}
int main() {
	n=read(),m=read();
	init();
	ll f=P(m,n);
	ll g=P(m,n),mk=1;
	for (register int i=1;i<=n;++i) {
		g-=mk*C(n,i)*P(m-i,n-i);
		g=(g%MOD+MOD)%MOD;
		mk=-mk;
	}
	ll ans=f*g%MOD;
	cout<<ans<<endl;
	return 0;
}

把D题的标准算法也贴一下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e7+4;
typedef long long ll;
int n;
inline int read() {
	int x=0,f=1;char c=getchar();
	while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
	while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
	return x*f;
}
ll ans;
int main() {
	n=read();
	for (register int i=1;i<=n;++i) {
		int cnt=n/i;
		int s=i,t=cnt*i;
		ans+=1ll*(s+t)*cnt/2;
	}
	cout<<ans<<endl;
	return 0;
}

 

上一篇:AtCoder Beginner Contest 178 E - Dist Max


下一篇:检测用户是否已接受签名的Java小程序.