您的位置:首页 > 其它

Codeforces Round #324 (Div. 2) 584ABCD题解

2015-10-10 10:46 134 查看
题目链接:点击打开链接

A:给你一个n、t,让你输出长度为n且可以整除t的数。

注意到t的范围是1~10,把10特判一下,其他的情况直接输出n个t就好。

AC代码:

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
int n, t;
int main(int argc, char const *argv[])
{
scanf("%d%d", &n, &t);
if(t == 10) {
if(n == 1) printf("-1\n");
else {
printf("1");
for(int i = 1; i < n; ++i)
printf("0");
printf("\n");
}
}
else {
for(int i = 0; i < n; ++i)
printf("%d", t);
printf("\n");
}
return 0;
}


B:给你一个n,3n个gnome构成一个圈,每个gnome的值为1~3,问你有多少种不同的方案,要满足ai + ai + n + ai + 2n ≠ 6。

由第一个样例n输入为1下手,共有27种情况,但是符合条件的有20种,也就是说对于任意输入的n,n^27 - n^7模1e9+7即可得到答案,

可以快速幂直接算,也可分开一步步算。

AC代码:

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int MOD = 1e9 + 7;
typedef long long ll;
int n;
int main(int argc, char const *argv[])
{
scanf("%d", &n);
ll a = 1, b = 1;
for(int i = 1; i <= n; ++i) {
a = (a * 27) % MOD;
b = (b * 7) % MOD;
}
if(a < b) a += MOD;
a = a - b;
printf("%I64d\n", a);
return 0;
}


C:给你长度为n的两个字符串,让你输出一个字符串要求与给出的两个字符串有t个字母不相同。
首先找出两个字符串相同的字母并标记,接下来遇到两个字符串相同的字母则直接添加,只与1字符串相同且与1字符串相同数目不够

则添加的字符串1的字符,只与2字符串相同且与2字符串相同数目不够则添加的字符串2的字符,如果上面情况都不符合,那么就添加

和字符串12都不同的字符,最终得到答案。

AC代码:

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "string"
using namespace std;
const int MAXN = 1e5 + 5;
int n, t;
string a, b, ans;
bool vis[MAXN];
int main(int argc, char const *argv[])
{
cin >> n >> t;
cin >> a >> b;
int same = n - t, cur = 0;
for(int i = 0; i < n; ++i)
if(a[i] == b[i] && cur < same) {
cur++;
vis[i] = true;
}
int x[2] = {cur, cur};
for(int i = 0; i < n; ++i) {
if(vis[i]) ans += a[i];
else {
if(x[0] < same) {
x[0]++;
ans += a[i];
}
else {
if(x[1] < same) {
x[1]++;
ans += b[i];
}
else {
for(int j = 0; j < 26; ++j)
if(a[i] - 'a' != j && b[i] - 'a' != j) {
ans += 'a' + j;
break;
}
}
}
}
}
if(x[0] == same && x[1] == same) cout << ans << endl;
else cout << -1 << endl;
return 0;
}


D:给你一个奇数,让你分成1~3个质数。

哥德巴赫猜想的应用,任何一个大于4的奇数都可以分成两个质数的和,也就是说对于3、5特判一下,其他情况就自减3,也就是肯定有

一个质数为3,然后暴力就能得到答案。

AC代码:

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int MAXN = 1e5;
int n, ans;
bool judge(int x)
{
if(x == 2) return true;
for(int i = 2; i * i <= x; ++i)
if(x % i == 0) return false;
return true;
}
int main(int argc, char const *argv[])
{
scanf("%d", &n);
if(n == 3) {
printf("1\n");
printf("3\n");
return 0;
}
if(n == 5) {
printf("2\n");
printf("2 3\n");
return 0;
}
n -= 3;
for(int i = 2; i <= n / 2; ++i)
if(judge(i) && judge(n - i)) {
ans = i;
break;
}
printf("3\n");
printf("3 %d %d\n", ans, n - ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: