AtCoder Beginner Contest 156 题解
题目是今天中午我在车站写的,纪念一下~
前三题很水就不说了。
A
#include<bits/stdc++.h>
using namespace std;
int main(){
int c, r; cin>>c>>r;
r= c>=10? r: (10-c)*100+r;
cout<<r;
return 0;
}
B
#include<bits/stdc++.h>
using namespace std;
int main(){
int n, b; cin>>n>>b;
int cnt=0;
while(n) n/=b, cnt++;
cout<<cnt;
return 0;
}
C
#include<bits/stdc++.h>
using namespace std;
const int N=105;
int w[N], n;
int main(){
cin>>n;
for(int i=1; i<=n; i++) cin>>w[i];
int res=1e9;
for(int p=1; p<=100; p++){
int d=0;
for(int i=1; i<=n; i++) d+=(w[i]-p)*(w[i]-p);
res=min(res, d);
}
cout<<res;
return 0;
}
D
容斥。
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define debug(x) cerr << #x << ": " << x << endl
#define pb(a) push_back(a)
#define set0(a) memset(a,0,sizeof(a))
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define dwn(i,a,b) for(int i=(a);i>=(b);i--)
#define ceil(a,b) (a+(b-1))/b
#define INF 0x3f3f3f3f
#define ll_INF 0x7f7f7f7f7f7f7f7f
typedef long long ll;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
#define int long long
inline void read(int &x) {
int s=0;x=1;
char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-')x=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+ch-'0',ch=getchar();
x*=s;
}
const int N=2e5+5, mod=1e9+7;
int n, a, b;
int fpow(int x, int p){
int res=1;
for(; p; p>>=1, x=x*x%mod)
if(p&1) res=res*x%mod;
return res;
}
int fac[N];
void init(){
fac[0]=1;
rep(i,1,N-1) fac[i]=fac[i-1]*i%mod;
}
int inv(int x){
return fpow(x, mod-2);
}
int C(int a, int b){
int res=1;
dwn(i,a,a-b+1) res=res*i%mod;
res=res*inv(fac[b])%mod;
return res;
}
signed main(){
init();
read(n), read(a), read(b);
int res=((fpow(2, n)-1-C(n, a)-C(n, b))%mod+mod)%mod;
cout<<res<<endl;
return 0;
}
E
组合数知识+容斥
当 时,我们可以构造出所有情况,转变为 个不同的盒子放 个相同的球, 个盒子都可为空的问题,答案自然是
否则,必然至少有 个盒子非空,我们考虑统计反面:枚举一下 个盒子为空的贡献。
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define debug(x) cerr << #x << ": " << x << endl
#define pb(a) push_back(a)
#define set0(a) memset(a,0,sizeof(a))
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define dwn(i,a,b) for(int i=(a);i>=(b);i--)
#define ceil(a,b) (a+(b-1))/b
#define INF 0x3f3f3f3f
#define ll_INF 0x7f7f7f7f7f7f7f7f
typedef long long ll;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
#define int long long
inline void read(int &x) {
int s=0;x=1;
char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-')x=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+ch-'0',ch=getchar();
x*=s;
}
const int N=4e5+5, mod=1e9+7;
int n, k;
int fpow(int x, int p){
int res=1;
for(; p; p>>=1, x=x*x%mod)
if(p&1) res=res*x%mod;
return res;
}
int fac[N];
void init(){
fac[0]=1;
rep(i,1,N-1) fac[i]=fac[i-1]*i%mod;
}
int inv(int x){
return fpow(x, mod-2);
}
int C(int a, int b){
return fac[a]*inv(fac[a-b])%mod*inv(fac[b])%mod;
}
signed main(){
init();
read(n), read(k);
int res;
res=C(2*n-1, n-1);
if(k<n){ // must be a least n-k bits more than 0
rep(i,1,n-k-1) res=((res-C(n, i)*C(n-1, i-1))%mod+mod)%mod;
}
cout<<res;
return 0;
}
F很怪qwq,会补的
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】