Codeforces Round 957 (Div. 3) A-G 题解

Codeforces Round 957 (Div. 3) A-G 题解

A. Only Pluses 枚举

思路

枚举 \(a\) , \(b\) , \(c\) 增加的次数,维护最值即可。

代码

#include<bits/stdc++.h>

using namespace std;

#define ff first
#define ss second
#define pb push_back
#define all(u) u.begin(), u.end()
#define endl '\n'
#define debug(x) cout<<#x<<":"<<x<<endl;

typedef pair<int, int> PII;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 1e5 + 10, M = 105;
const int mod = 1e9 + 7;
const int cases = 1;

void Showball(){
   int a,b,c;
   cin>>a>>b>>c;
   int ans=a*b*c;
   for(int i=0;i<=5;i++){
      for(int j=0;j+i<=5;j++){
         for(int k=0;i+j+k<=5;k++){
            ans=max(ans,(a+i)*(b+j)*(c+k));
         }
      }
   }
   cout<<ans<<endl; 
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int T=1;
    if(cases) cin>>T;
    while(T--)
    Showball();
    return 0;
}

B. Angry Monk 贪心

思路

贪心,最小操作数一定是将除了最大值其余全部分成 \(1\),然后再依次与最大值合并即可。

代码

void Showball(){
   int n,k;
   cin>>n>>k;
   vector<int> a(k);
   for(int i=0;i<k;i++) cin>>a[i];
   sort(all(a));
   int ans=0;
   for(int i=0;i<k-1;i++) ans+=(a[i]-1);
   cout<<ans+n-a[k-1]<<endl;
}

C. Gorilla and Permutation 构造

思路

显然,先放所有大于等于 \(k\) 的数,并且从大到小放。

接着如果 \(m < k\),那么将 \(m+1\)\(k-1\) 之间的数放置。

最后放所有小于等于 \(m\) 的数,从小到大放即可。

代码

void Showball(){
   int n,m,k;
   cin>>n>>m>>k;
   for(int i=n;i>=k;i--) cout<<i<<" ";
   for(int i=m+1;i<k;i++) cout<<i<<" ";
   for(int i=1;i<min(m+1,k);i++) cout<<i<<" ";
   cout<<endl;
}

D. Test of Love 贪心 + 模拟

思路

贪心,显然为了能够更少的去水里游,我们的策略自然就是能跳上圆木就上圆木,

否则只能下水,游到下一个圆木。因此我们只需要依次处理两个圆木。如果可以直接到达,

则直接跳过去,否则直接跳到能跳到的最远距离。(如果最远距离是鳄鱼的话,说明无法到达。为什么直接可以跳到最远距离。因为你在水中想要游到下一个圆木,就必须走过两者之间的所有河段。那么你跳到之前的水中,后面有鳄鱼,自然也是不满足情况的。因此,直接跳最远距离判断能简化问题。)

如果最远距离是水,那么我们还要判断从当前河段的水到下一圆木之间是否都是水,这样才能走过 。反之,无法到达。记录游过的距离。

最后如果可以到达终点,并且游过的距离不超过 \(k\) 米,则满足题意。

TIPS:我们可以给字符串首尾增加一个 “L”,方便我们处理。

代码

void Showball(){
   int n,m,k;
   cin>>n>>m>>k;
   string s;
   cin>>s;
   s="L"+s+"L";
   n+=2;
   int cur=0,ans=0;
   while(cur<n-1){
      int p=s.find("L",cur+1);
      if(p==-1) break;
      if(p-cur<=m) {
         cur=p;
         continue;
      }
      cur+=m;
      if(s[cur]=='C') break;
      string tmp=s.substr(cur,p-cur);
      if(tmp!=string(p-cur,'W')) break;
      ans+=p-cur;
      cur=p;
   }
   if(cur<n-1||ans>k) cout<<"NO\n";
   else cout<<"YES\n";
}

E. Novice's Mistake 思维 + 枚举

思路

看到数据范围不大,考虑枚举。但是不能直接枚举 \(a\)\(b\)

我们发现 \(n*a\) 的最大值是 \(1000000\) 。那么 \(n*a-b\) 最多也就是一个六位数。

因此我们可以枚举 \(n*a-b\) 的位数,这样 \(b\) 也就是已知的了。模拟求出对应字符串

对比一下是否相等即可。

代码

void Showball(){
   int n;
   cin>>n;
   vector<PII> res;
   string sn=to_string(n);
   for(int i=1;i<=10000;i++){
   	  string na="";
   	  while(na.size()<10) na+=sn;
   	  int len=sn.size();
   	  for(int j=1;j<=6;j++){
   	  	int b=i*len-j;
   	  	int ans=n*i-b;
   	  	if(b<1||b>min(10000,n*i)) continue;
   	  	string sans=to_string(ans);
   	  	string sres=na.substr(0,j);
   	  	if(sans==sres){
   	  		res.pb({i,b});
   	  	}
   	  }
   } 
   cout<<res.size()<<endl;
   for(auto [a,b]:res) cout<<a<<" "<<b<<endl;
}

F. Valuable Cards 思维

思路

我们可以设 \(dp\) 数组,\(dp_i=1\) 表示 \(i\) 可以被之前的子序列的乘积表示。

那么我们可以枚举 \(a_i\) ,如果满足 x%a[i]==0&&dp[x/a[i]] 那么就必须要新开一组。

接着我们只需要去维护好 \(dp\) 数组即可。每次分组后我们需要清空 \(dp\) 数组,但是全部清空显然超时。

实际上对我们有用的只有 \(x\) 的因子。因此我们可以预处理出 \(x\) 的因子即可。

如果 \(a_i\)\(x\) 的因子,我们可以将所有 \(a_i \times d\)\(dp\) 状态更新。其中 \(d\)\(x\) 的因子。

这里更新的时候要注意,我们需要新开一个临时 \(ndp\) 数组。最后遍历完,再用 \(ndp\) 数组去更新 \(dp\) 数组。

否则,就会造成新更新的值又去更新了别的值的情况。

代码

void Showball(){
    int n,x;
    cin>>n>>x;
    vector<int> a(n);
    for(int i=0;i<n;i++) cin>>a[i];
    vector<int> div;
	for(int i=2;i<=x/i;i++){
		if(x%i==0){
			div.pb(i);
			if(x/i!=i) div.pb(x/i);
		}
	}
    vector<int> dp(x+1);
	dp[1]=1;
	int ans=1;
	for(int i=0;i<n;i++){
		if(a[i]>x) continue;
		if(x%a[i]==0&&dp[x/a[i]]){
	   		for(auto d:div) dp[d]=0;
	   		ans++;
	   		dp[1]=1;
	   		dp[a[i]]=1;
	   		continue;
	   	}
	   	if(x%a[i]==0){
	    	vector<int> ndp(x+1);
	   		for(auto d:div) if(dp[d]&&1LL*a[i]*d<=x) dp[d*a[i]]=1;
	   	 	dp[a[i]]=1;
	   	 	for(auto d:div) dp[d]|=ndp[d];		
	   	}
	}
	cout<<ans<<endl;
}

G. Ultra-Meow 思维 + 组合数学

思路

直接算答案,不好下手。那么我们可以考虑算贡献。考虑枚举集合的大小 \(i\) 和最终的 \(mex\)\(j\)

需要满足一下条件:

  1. 集合中不能含有 \(j\)
  2. 对于 \([1,j-1]\) 中的数字,要取 \(j-(i+1)\) 个。这样才能保证再取 \(i+1\) 个就是 \(j\)
  3. 如果此时集合大小还没有满 \(i\) 个,那么就从 \([j+1,n]\) 中选择剩下的数字即可。

预处理出组合数,然后枚举计算即可。

那么对于第 \(2\) 部分的贡献就是 \(j*C[min(j-1,n)][j-i-1]\)

\(3\) 部分得到贡献就是 \(C[n-j][i-cnt]\)

代码

#include<bits/stdc++.h>

using namespace std;

#define ff first
#define ss second
#define pb push_back
#define all(u) u.begin(), u.end()
#define endl '\n'
#define debug(x) cout<<#x<<":"<<x<<endl;

typedef pair<int, int> PII;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 5000 + 10, M = 105;
const int mod = 1e9 + 7;
const int cases = 1;

template<const int T>
struct ModInt {
    const static int mod = T;
    int x;
    ModInt(int x = 0) : x(x % mod) {}
    ModInt(long long x) : x(int(x % mod)) {} 
    int val() { return x; }
    ModInt operator + (const ModInt &a) const { int x0 = x + a.x; return ModInt(x0 < mod ? x0 : x0 - mod); }
    ModInt operator - (const ModInt &a) const { int x0 = x - a.x; return ModInt(x0 < 0 ? x0 + mod : x0); }
    ModInt operator * (const ModInt &a) const { return ModInt(1LL * x * a.x % mod); }
    ModInt operator / (const ModInt &a) const { return *this * a.inv(); }
    bool operator == (const ModInt &a) const { return x == a.x; };
    bool operator != (const ModInt &a) const { return x != a.x; };
    void operator += (const ModInt &a) { x += a.x; if (x >= mod) x -= mod; }
    void operator -= (const ModInt &a) { x -= a.x; if (x < 0) x += mod; }
    void operator *= (const ModInt &a) { x = 1LL * x * a.x % mod; }
    void operator /= (const ModInt &a) { *this = *this / a; }
    friend ModInt operator + (int y, const ModInt &a){ int x0 = y + a.x; return ModInt(x0 < mod ? x0 : x0 - mod); }
    friend ModInt operator - (int y, const ModInt &a){ int x0 = y - a.x; return ModInt(x0 < 0 ? x0 + mod : x0); }
    friend ModInt operator * (int y, const ModInt &a){ return ModInt(1LL * y * a.x % mod);}
    friend ModInt operator / (int y, const ModInt &a){ return ModInt(y) / a;}
    friend ostream &operator<<(ostream &os, const ModInt &a) { return os << a.x;}
    friend istream &operator>>(istream &is, ModInt &t){return is >> t.x;}

    ModInt pow(int64_t n) const {
        ModInt res(1), mul(x);
        while(n){
            if (n & 1) res *= mul;
            mul *= mul;
            n >>= 1;
        }
        return res;
    }
    
    ModInt inv() const {
        int a = x, b = mod, u = 1, v = 0;
        while (b) {
            int t = a / b;
            a -= t * b; swap(a, b);
            u -= t * v; swap(u, v);
        }
        if (u < 0) u += mod;
        return u;
    }
    
};
typedef ModInt<mod> mint;

mint C[N][N];
void init(){
    for(int i=0;i<N;i++){
        for(int j=0;j<=i;j++){
            if(!j) C[i][j]=1;
            else C[i][j]=C[i-1][j]+C[i-1][j-1];
        }
    }
}
void Showball(){
    int n;
    cin>>n;
    mint ans=1;
    for(int i=1;i<=n;i++){
    	for(int j=i+1;j<=2*i+1;j++){
    		int cnt=j-i-1;
    		mint t=j*C[min(j-1,n)][cnt];
    		if(j>n){
    			if(cnt!=i) t=0;
    		}else{
    			t*=C[n-j][i-cnt];
    		}
    		ans+=t;
    	}
    }
    cout<<ans<<endl;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int T=1;
    init();
    if(cases) cin>>T;
    while(T--)
    Showball();
    return 0;
}
posted @ 2024-07-12 22:02  Showball  阅读(94)  评论(0编辑  收藏  举报