AtCoder Beginner Contest 171 题解

传送门:https://atcoder.jp/contests/abc171/submissions/me

A

#include<bits/stdc++.h>
using namespace std;

int main(){
	char t; cin>>t; 
	putchar(islower(t)? 'a': 'A');	
	return 0;
}

B

#include<bits/stdc++.h>
using namespace std;

const int N=1010;

int w[N];

int main(){
	int n, k; cin>>n>>k;
	for(int i=1; i<=n; i++) cin>>w[i];
	sort(w+1, w+1+n);
	
	int res=0;
	for(int i=1; i<=k; i++) res+=w[i];
	cout<<res;
		
	return 0;
}

C

尽管只是 C 题。
但还是觉得这种题目很折磨(悲
主要是细节问题一直是我的痛点。

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

int main(){
	ll n; cin>>n;
	int stk[20]={0}, top=0;
	
	while(n>0){
		stk[++top]=n%26? n%26: 26;
		
		bool ok=false;
		if(n%26==0) ok=true;
		n/=26; n-=ok;
	} 
	
	for(int i=top; i; i--) cout<<(char)('a'+stk[i]-1);
	
	return 0;
}

D

看看每个数转换后的盈亏,维护一下每个值的数量即可,比 C 这种折磨题简单多了。

#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define endl '
'
#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 all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define ll_INF 0x7f7f7f7f7f7f7f7f
typedef long long ll;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;

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=1e5+5;

int n, q;
int cnt[N];
ll res;

int main(){
	read(n);
	rep(i,1,n){
		int t; read(t);
		cnt[t]++; res+=t;
	}
	
	read(q);
	rep(i,1,q){
		int a, b; read(a), read(b);
		res+=cnt[a]*(b-a);
		cnt[b]+=cnt[a];
		cnt[a]=0;
		cout<<res<<endl;
	}
	
    return 0;
}

E

意外的很水的一道脑筋急转弯。
注意到保证 (n) 为偶数,所以有给出的数列的异或和等于所求数列的异或和,记为 (s) ,位置 (i) 对应的答案为 (soplus w_i)

#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define endl '
'
#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 all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define ll_INF 0x7f7f7f7f7f7f7f7f
typedef long long ll;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;

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;

int w[N], res[N], s;

int main(){
	int n; read(n);
	rep(i,1,n) read(w[i]), s^=w[i];
	
	rep(i,1,n) res[i]=s^w[i];
	rep(i,1,n) cout<<res[i]<<' ';
	
    return 0;
}

F

组合计数。
组合计数一般都难在统计的时候需要保证不重不漏。

分析

我们一边枚举一边进行统计。
这里我枚举的是第一个被统计入子序列为 (s)的位置。

(s) 的长度为 (n)

比如说:
给出的 (s=abc)
那么我枚举的就是 (c) 的位置 (i)

那第一个被统计入子序列为 (s) 是什么意思呢?

比如我得到了一个串;(abeeeczzzzzabc) ,那么第一个统计入子序列为 (s) 在串中则为:(dot{a}dot{b}eeedot{c}zzzzzabc)

显然,(i in [n, n+k])

(i) 右边的位置可以任意选择,方案数为 (26^{n+k-i})

(i) 位置对应的字母是 (s) 的结尾(也就是 (s)(n) 个字符)

(i) 左边,可以选出 (n-1) 个位置为 (s) 的前 (n-1) 个字符,方案数为 (C_{i-1}^{n-1}) ,剩下的位置方案数为 (25^{i-1-(n-1)}=25^{i-n}) 。(因为要保证第一个子序列的形态不重复)

因此 (ans = 25^{i-n} C_{i-1}^{n-1}26^{n+k-i})

#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define endl '
'
#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 all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define ll_INF 0x7f7f7f7f7f7f7f7f
typedef long long ll;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;

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=2e6+5, mod=1e9+7;

ll fpow(ll x,ll p)
{
    ll res=1;
    for(;p;p>>=1,x=x*x%mod)
        if(p&1)res=res*x%mod;
    return res%mod;
}

ll inv(ll x){
	return fpow(x,mod-2)%mod;
}

ll fac[N];

void init(){
	fac[0]=1;
	for(int i=1; i<N; i++) fac[i]=fac[i-1]*i%mod;
}

ll C(ll a, ll b){
	return fac[a]*inv(fac[b])%mod*inv(fac[a-b])%mod;
}

int main(){
	init();
	int k; string s; cin>>k>>s;
	int n=s.size();
	
	ll res=0;
	rep(i,n,n+k) res=(res+C(i-1, n-1)*fpow(25, i-n)%mod*fpow(26, n+k-i))%mod;
	cout<<res;
	
    return 0;
}
原文地址:https://www.cnblogs.com/Tenshi/p/15032402.html