Codeforces Round 957 (Div. 3) A-G 题解
Codeforces Round 957 (Div. 3) A-G 题解
A. Only Pluses 枚举
思路:
枚举
代码:
#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 贪心
思路:
贪心,最小操作数一定是将除了最大值其余全部分成
代码:
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 构造
思路:
显然,先放所有大于等于
接着如果
最后放所有小于等于
代码:
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 贪心 + 模拟
思路:
贪心,显然为了能够更少的去水里游,我们的策略自然就是能跳上圆木就上圆木,
否则只能下水,游到下一个圆木。因此我们只需要依次处理两个圆木。如果可以直接到达,
则直接跳过去,否则直接跳到能跳到的最远距离。(如果最远距离是鳄鱼的话,说明无法到达。为什么直接可以跳到最远距离。因为你在水中想要游到下一个圆木,就必须走过两者之间的所有河段。那么你跳到之前的水中,后面有鳄鱼,自然也是不满足情况的。因此,直接跳最远距离判断能简化问题。)
如果最远距离是水,那么我们还要判断从当前河段的水到下一圆木之间是否都是水,这样才能走过 。反之,无法到达。记录游过的距离。
最后如果可以到达终点,并且游过的距离不超过
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 思维 + 枚举
思路:
看到数据范围不大,考虑枚举。但是不能直接枚举
我们发现
因此我们可以枚举
对比一下是否相等即可。
代码:
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 思维
思路:
我们可以设
那么我们可以枚举 x%a[i]==0&&dp[x/a[i]]
那么就必须要新开一组。
接着我们只需要去维护好
实际上对我们有用的只有
如果
这里更新的时候要注意,我们需要新开一个临时
否则,就会造成新更新的值又去更新了别的值的情况。
代码:
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 思维 + 组合数学
思路:
直接算答案,不好下手。那么我们可以考虑算贡献。考虑枚举集合的大小
需要满足一下条件:
- 集合中不能含有
。 - 对于
中的数字,要取 个。这样才能保证再取 个就是 。 - 如果此时集合大小还没有满
个,那么就从 中选择剩下的数字即可。
预处理出组合数,然后枚举计算即可。
那么对于第
第
代码:
#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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效