Codeforces Round #818 (Div. 2) 题解

比赛链接:Dashboard - Codeforces Round #818 (Div. 2) - Codeforces

A. Madoka and Strange Thoughts

题目大意

给定n,求满足条件 1<=a,b<=n,且 lcm(a,b)/gcd(a,b) <= 3 的二元组(a,b) 有多少个。  

题解

lcm(a,b)=a*b/gcd(a,b)

令d=gcd(a,b)

所以 lcm(a,b)/gcd(a,b)

=a*b/d^2

=(a/d)*(b/d) <= 3

我们知道a/d 与 b/d一定互素,所以只有以下几种情况:

a:b=1:1,1:2, 2:1, 1:3, 3:1

所以ans=n+2*(n/2)+2*(n/3)

Code

#include <bits/stdc++.h>
using namespace std;
int R(){int a=0,b=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')b=-1;c=getchar();}while(c>='0'&&c<='9'){a=a*10+c-'0';c=getchar();}return a*b;}

int main(){
	int t=R();
	while(t--){
		int n=R();
		int ans=n+2*(n/2)+2*(n/3);
		printf("%d\n",ans);
	}
	return 0;
}

B. Madoka and Underground Competitions

题目大意

给定n,k,r,c

构造一个n*n的图,要求(r,c)必须是 'X'

且任意1*k 或者 k*1 的区域中至少含有一个 'X'

其余位置填 '.'

Example

input

1
6 3 4 2

output

.X..X.
X..X..
..X..X
.X..X.
X..X..
..X..X

题解

观察规律并模拟即可

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int R(){int a=0,b=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')b=-1;c=getchar();}while(c>='0'&&c<='9'){a=a*10+c-'0';c=getchar();}return a*b;}
int n,k,r,c;
bool m[510][510];
int main(){
//	freopen("a.txt","r",stdin);
	int t=R();
	while(t--){
		n=R();k=R();r=R();c=R();
		memset(m,0,sizeof m);
		for(int x=r,y=c;;){
			m[x][y]=1;
			x--;y++;
			if(x==0)x=n;
			if(y==n+1)y=1;
			if(m[x][y]){
				y=(y+k-1)%n+1;
				if(m[x][y])break;
			}
		}
		for(int i=1;i<=n;++i){
			for(int j=1;j<=n;++j){
				if(m[i][j])putchar('X');
				else putchar('.');
			}
			putchar('\n');
		}
	}
	return 0;
}

C. Madoka and Formal Statement

 

题目大意

给你a,b数组,当a[i]<=a[i+1] (a[n]<=a[1])时,a[i]可以自加一

问a数组能否经过一系列操作变成b数组

题解

遍历,若a[i]>b[i]则不可能实现,因为a[i]只能变大,不能变小

若a[i]==b[i]则跳过,判断下一个

若b[i]-b[i+1]>=2则不可能实现,因为a[i+1]最大只能加到b[i+1],而由于a[i]最终只能比a[i+1]大一,而b[i]却比b[i+1]大2以上,所以不可能实现

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int R(){int a=0,b=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')b=-1;c=getchar();}while(c>='0'&&c<='9'){a=a*10+c-'0';c=getchar();}return a*b;}
const int maxn=200010;
int n,a[maxn],b[maxn];
int main(){
//	freopen("a.txt","r",stdin);
	int T=R();
	while(T--){
		n=R();
		bool defeat=0;
		for(int i=1;i<=n;++i)a[i]=R();
		for(int i=1;i<=n;++i){
			b[i]=R();
			if(b[i]<a[i])defeat=1;
			if(i!=1){
				if(b[i-1]-b[i]>=2&&b[i-1]!=a[i-1])defeat=1;
			}
		}
		if(b[n]-b[1]>=2&&b[n]!=a[n])defeat=1;
		if(defeat)printf("NO\n");
		else printf("YES\n");
	} 
	return 0;
}

D. Madoka and The Corruption Scheme

 题目大意

一共有2^n个参赛者,Madoka可设定初始比赛赛程,并规定谁胜谁负

但是赞助商有k次改变某场比赛胜负的权利(并非一定要改k次)

题解

 如上图,蓝色数字是指这个位置的选手想要获得总冠军需要至少需要改变多少次胜负

所以我们只需要知道改变 i 次胜负可获胜的位置数,可以发现就是杨辉三角

然后我们把编号小的选手放在蓝色数字小的位置上,即可使得最终的获胜者编号尽可能小

例如,n=3,k=2,那么改变2次可获胜的位置数一共有1+3+3=7,所以最后答案即为7

一般情况下,ans=\sum_{i=0}^{k}C_{n}^{i}

我们可以预处理出阶乘,和阶乘的逆元,具体实现见代码

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll R(){ll a=0,b=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')b=-1;c=getchar();}while(c>='0'&&c<='9'){a=a*10+c-'0';c=getchar();}return a*b;}
const ll mod=1e9+7;
ll fac[100010],inv[100010];
ll ksm(ll a,ll b){
	ll ans=1;
	while(b){
		if(b&1)ans=(ans*a)%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ans;
}
void init(){
	fac[0]=1;
	for(ll i=1;i<=100000;++i){
		fac[i]=fac[i-1]*i%mod;
	}
	inv[100000]=ksm(fac[100000],mod-2);
	for(ll i=100000-1;i>=0;--i){
		inv[i]=(inv[i+1]*(i+1))%mod;
	}
}
int main(){
//	freopen("a.txt","r",stdin);
	init();
	ll n=R(),k=R();
	ll ans=0;
	k=min(k,n);
	for(ll i=0;i<=k;++i){
		ans=(ans+fac[n]*inv[n-i]%mod*inv[i]%mod)%mod;
	}
	cout<<ans;
	return 0;
}

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值