Codefoece Educational Codeforces Round 83 (Rated for Div. 2)题解,(ABCDE)

比赛链接
A题:
思路:签到题。
代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+50;
int t,n,m;
int arr[maxn];
int main()
{
	ios::sync_with_stdio(false);
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		if(n<=4)
		{
			cout<<"NO"<<endl;
		}
		else
		{
			if(n%m) cout<<"NO"<<endl;
			else cout<<"YES"<<endl;
		}
	}
	return 0;
}

B题:
思路:签到题,把题目给的要求的不等式移项,然后发现直接将数组从大到小排序就一定符合条件。
代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+50;
int t,n,m;
int arr[maxn];
bool comp(int a,int b)
{
	return a>b;
}
int main()
{
	ios::sync_with_stdio(false);
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(int i=1;i<=n;i++)
		{
			cin>>arr[i];
		}
		sort(arr+1,arr+1+n,comp);
		for(int i=1;i<=n;i++)
		{
			if(i==1) cout<<arr[i];
			else cout<<" "<<arr[i];
		}
		cout<<endl;
	}
	return 0;
}

C题:
思路:类似于二进制1100110这样,看每一位上需不需要拿上一个,模拟即可,最终k的 i 次方被用过一次以上的就不合法。
代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+50;
ll t,n,m;
ll arr[maxn];
int rec[105];
int main()
{
	ios::sync_with_stdio(false);
	cin>>t;
	while(t--)
	{
		memset(rec,0,sizeof(rec));
		cin>>n>>m;
		for(int i=1;i<=n;i++)
		{
			cin>>arr[i];
		}
		int flag=1;
		for(int i=1;i<=n;i++)
		{
			if(arr[i]==0) continue;
			if(arr[i]==1)
			{
				rec[0]++;continue;
			}
			ll z=arr[i];
			ll tmp=1;int num=0;
			while(tmp<=z)
			{
				tmp=tmp*m;num++;
			}
			tmp=tmp/m;num--;
			while(z)
			{
				while(z>=tmp)
				{
					z=z-tmp;
					rec[num]++;
				}
				tmp=tmp/m;num--;
			}
		}
		for(int i=0;i<=103;i++)
		{
			if(rec[i]>1)
			{
				flag=0;break;
			}
		}
		if(flag) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
	return 0;
}

D题:
思路:显然需要推式子,一共m个人,数列一共n个数,不同的数字n-1个,因此需要从m里面先选出n-1个不同的数字,C(m,n-1),然后n-1个数中最大的数字一定是中转点,中转点不可以成为出现两次的那个数字,所以要从剩下的 n-2 个数字中找出一个出现两次的数字,因此需要乘上一个(n-2),剩下的n-3个数字要么在中转点左边要么就是右边,而出现两次的数字是不需要考虑的,由于剩下n-3个数字可以左可以右,所以还需要乘以一个2的(n-3)次方。套个lucas和快速幂可以算出来答案。
需要注意的是,n为2的时候,快速幂中的次数会是-1,会卡死。因此2的话特判,n是2的时候直接输出0.
板子是网上找的套的。

代码:

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

typedef long long LL;

LL fast_power(LL a,LL b,LL p){
    LL ans=1;
    a%=p;
    while(b){
        if(b&1) ans=(ans*a)%p;
        a=(a*a)%p;
        b>>=1;
    }
    return ans;
}

LL C(LL a,LL b,LL p){
    if(b>a) return 0;
    if(a==b) return 1;
    LL ans1=1,ans2=1;
    for(LL i=1;i<=b;i++){
        ans1=ans1*(a-i+1)%p;
        ans2=ans2*i%p;
    }
    return ans1*fast_power(ans2,p-2,p)%p;
}

LL Lucas(LL a,LL b,LL p){
    if(b==0) return 1;
    return C(a%p,b%p,p)*Lucas(a/p,b/p,p)%p;
}

int main(){
    int t;
        LL n,m,p;
        cin>>n>>m;
        p=998244353;
        LL fin=0;
        if(n==2)
        {
        	cout<<"0"<<endl;
        	return 0;
		}
		LL zz=(Lucas(m,n-1,p)*fast_power(2,n-3,p))%p;
		fin=zz;
        cout<<(fin*(n-2))%p<<endl;
    return 0;
}

E题:

思路:n的大小是500,观察到转移是一个一个区间的转移,因此基本上确定区间dp。需要注意的是转移过程中需要记录每一个区间左边右边的值来辅助转移。
代码写的是记忆化搜索型的区间DP。

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=550;
int dp[maxn][maxn];
int t,n,m;
int arr[maxn];
int ll[maxn][maxn],rr[maxn][maxn];
int solve(int l,int r)
{
	if(l==r)
	{
		dp[l][r]=1;ll[l][r]=rr[l][r]=arr[l];
		return 1;
	}
	if(dp[l][r]<=n) return dp[l][r];
	for(int i=l;i<r;i++)
	{
		if(dp[l][r]>(solve(l,i)+solve(i+1,r)-(rr[l][i]==ll[i+1][r])))
		{
			dp[l][r]=dp[l][i]+dp[i+1][r]-(rr[l][i]==ll[i+1][r]);
			ll[l][r]=ll[l][i];rr[l][r]=rr[i+1][r];
			if(rr[l][i]==ll[i+1][r])
			{
				if(dp[l][i]==1) ll[l][r]++;
				if(dp[i+1][r]==1) rr[l][r]++;
			}
		}
	}
	return dp[l][r];
}
int main()
{
	ios::sync_with_stdio(false);
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>arr[i];
	}
	int fin=n;
	memset(dp,0x3f,sizeof(dp));
	fin=min(fin,solve(1,n));
	cout<<fin<<endl;
	return 0;
}

呼,下班了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值