AtCoder Beginner Contest 171

A - αlphabet

题目链接:点击这里

题意:给出一个字母,如果是大写字母,输出 ‘A’;如果是小写字母,输出 ‘a’ 。

#include
#include

using namespace std;

int main()
{
	char a;
	scanf("%c", &a);
	
	if(a >= 'a' && a <= 'z')	puts("a");
	else if(a >= 'A' && a <= 'Z')	puts("A");
	
	return 0;
}

B - Mix Juice

题目链接:点击这里

题意:从 n n n 个数中选出 k k k 个数,使得选出的 k k k 个数之和最小。

#include
#include
#include

using namespace std;

const int N = 1010;
int a[N];

int main()
{
	int n, k;
	scanf("%d%d", &n, &k);
	for(int i = 0; i < n; i++)	scanf("%d", &a[i]);
	
	sort(a, a + n);
	
	int ans = 0;
	for(int i = 0; i < k; i++)	ans += a[i];
	printf("%d\n", ans);
	return 0;
}

C - One Quadrillion and One Dalmatians

题目链接:点击这里

题意:把一个数字转化为对应的字符串,其规则为 [ 1 , 26 ] [1,26] [1,26] 对应 [ a , z ] [a,z] [a,z] [ 27 , 702 ] [27,702] [27,702] 对应 [ a a , z z ] [aa,zz] [aa,zz]

思路:转化为 26 26 26 进制即可,需要注意每次取余前令 n n n 减一来把 [ 1 , 26 ] [1,26] [1,26] 映射到 [ 0 , 25 ] [0,25] [0,25]

非递归:

#include

using namespace std;

typedef long long ll;
const int N = 110;

int a[N];

int main()
{
    ll n;
    cin >> n;
    
	int cnt = 0;
    while(n)
	{
		n--;
		a[cnt++] = n % 26;
        n /= 26;
    }
    
    for(int i = cnt - 1; i >= 0; i--)	cout << char(a[i] + 'a');
    return 0;
}

递归:

#include
#include

using namespace std;
typedef long long ll;

void solve(ll n)
{
	if(n == 0)	return;
	
	if(n % 26 == 0)							// 被26整除不需要进位,单独处理
	{
		solve((n - 1) / 26);
		printf("z");
	}
	else									// 进制转换的正常处理方法 
	{
		solve(n / 26);
		printf("%c", n % 26 + 'a' - 1);
	}
}

int main()
{
	ll n;
	scanf("%lld", &n);
	
	solve(n);
	
	return 0;
}

D - Replacing

题目链接:点击这里

题意:给出 n n n 个数和 q q q 次操作,每次操作将 n n n 个数中所有值为 b b b 的数全部替换为 c c c,计算每次操作后 n n n 个数的和。

#include
#include

using namespace std;
typedef long long ll;
const int N = 100010;
int p[N];

int main()
{
	int n;
	scanf("%d", &n);
	
	ll sum = 0;
	for(int i = 1; i <= n; i++)
	{
		int x;
		scanf("%d", &x);
		sum += x;
		p[x]++;
	}
	
	int T;
	scanf("%d", &T);
	while(T--)
	{
		int b, c;
		scanf("%d%d", &b, &c);
		sum += (ll)(c - b) * p[b];
		p[c] += p[b];
		p[b] = 0;
		printf("%lld\n", sum);
	}
	
	return 0;
}

E - Red Scarf

题目链接:点击这里

题意:给定整数 n n n n n n 是偶数)和 包含 n n n 个数的数组 a a a,其中,每一个 a i a_i ai 都是数组 s s s 中除 s i s_i si 以外的其他所有数的异或值,请构造出一个满足条件的原数组 s s s

思路:利用 x ⊕ x = 0 x⊕x=0 xx=0 的特性,先对所有数求一次异或,然后再对每个数求一次异或,即可从总的异或值中抵消掉当前数。

#include

using namespace std;
const int N = 200010;

int a[N];

int main()
{
    int n;
	cin >> n;
	int x = 0;
    for(int i = 0; i < n; i++)	cin >> a[i], x ^= a[i];
    for(int i = 0; i < n; i++)	cout << (x ^ a[i]) << ' ';
    return 0;
}

F - Strivore

题目链接:点击这里

AtCoder Beginner Contest 171_第1张图片

思路:组合计数。

AtCoder Beginner Contest 171_第2张图片
#include
#include
#include

using namespace std;

typedef long long ll;
const int mod = 1e9 + 7;
const int N = 2e6 + 10;

int fact[N], infact[N];

int qmi(int a, int b)
{
	int res = 1;
	while(b)
	{
		if(b & 1)	res = (ll)res * a % mod;
		a = (ll)a * a % mod;
		b >>= 1;
	}
	return res;
}

void init()
{
	fact[0] = 1, infact[0] = 1;
    for(int i = 1; i < N; ++i)
    {
        fact[i] = (ll)fact[i - 1] * i % mod;
        infact[i] = (ll)infact[i - 1] * qmi(i, mod - 2) % mod;
    }
}

int C(int a, int b)
{
	return (ll)fact[a] * infact[b] % mod * infact[a - b] % mod;
}

int main()
{
	int k;
	string s;
	cin >> k >> s;
	
	int len = s.length();
	int n = k + len;
	init();						// 预处理阶乘
	
	int ans = 0;
	for(int i = len; i <= n; i++)
	{
		int t = (ll)C(i - 1, len - 1) * qmi(25, i - len) % mod * qmi(26, n - i) % mod;
		ans = (ans % mod + t % mod) % mod;
	}
	cout << ans << endl;
	return 0;
}

你可能感兴趣的:(AtCoder)