Codeforces Round #324 (Div. 2)解题报告

---恢复内容开始---

Codeforces Round #324 (Div. 2)

Problem A

题目大意:给二个数n、t,求一个n位数能够被t整除,存在多组解时输出任意一组,不存在时输出“-1”。

数据范围: 1 ≤ n ≤ 100, 2 ≤ t ≤ 10

so easy!首先考虑无解的时候,只有当n==1,t==10是无解,其他情况都存在解。很容易想到最简单的n位数能被t整除的数是,t个n组成的数。

参考代码:

 By Royecode, contest: Codeforces Round #324 (Div. 2), problem: (A) Olesya and Rodion, Accepted, #
1 #include <bits/stdc++.h> using namespace std; const int MAXN = 1e5+; int main()
{
int n, t;
cin >> n >> t;
if(t == )
{
if(n == )//无解
puts("-1");
else if(n == )
puts("");
else
{
cout << ;
for(int i = ; i < n - ; ++i)
cout << ;
puts("");
}

Problem B

题目大意:给一个n,表示有3n个数,每个数的取值范围是[1,3],统计有多少个i,使a[i] + a[i+1] + a[i+2] != 6,所得答案模1e9+7。

数据范围:1 ≤ n ≤ 10^5,0<=i<3*n

数学问题,通过推公式可得答案:pow(3, 3*n) - pow(7, n)

参考代码:

 By Royecode, contest: Codeforces Round # (Div. ), problem: (B) Kolya and Tanya , Accepted, #
2 #include <bits/stdc++.h> #define ll long long
#define pb push_back
#define mp make_pair
#define debug(x) cout << #x << " " << x << endl; using namespace std; const int MAXN = 1e5+;
const int INF = 2e9+;
const double EPS = 1e-;
const int MOD = 1e9+; ll quick(ll n, int i)
{
if(i != )
{
ll t = quick(n, i / ) % MOD;
if(i % == ) return t * t * n % MOD;
return t * t % MOD;
}
return n;
}
int main()
{
ll n;
cin >> n;
cout << (quick(, * n) - quick(, n) + MOD ) % MOD<< endl; //注意加上MOD,再取模
return ;
}

Problems C

题目大意:有2个长度为n的字符串s1,s2,求一个长度为n的字符串s3,让s1跟s3有t个字符不一样,s2跟s3也有t个字符不一样。答案不唯一。

数据范围:1 ≤ n ≤ 10^5, 0 ≤ t ≤ n

先统计s1跟s2相同的字符数cnt,可以通过反面来做,则需要构造表达式:n - cnt == t,n、t都是不变的,所以只能改变cnt的值。

构造s3[i] = s1[i] == s[2]? s1[i]: ' '; (0<=i<n)

分两种情况:

1、n - cnt > t,说明相同的字符数太少,导致构造出的字符串s3分别与s1、s2不同的字符太多,要使cnt++,则需在s1[i] != s2[i]处,先让s3[i] = s1[i],再找到一个s1[j] != s2[j],让s3[i] = s2[i],直到n - cnt == t,或则无解

2、n - cnt < t,说明相同的字符数太多,导致构造出的字符串s3分别与s1、s2不同的字符太少,只需将s1[i]==s2[i]==s3[i]的s3[i]变化,直到n - cnt == t,或则无解

参考代码:

 #include <bits/stdc++.h>

 #define ll long long
#define pb push_back
#define mp make_pair
#define debug(x) cout << #x << " " << x << endl; using namespace std; const int MAXN = 1e5+;
const int INF = 2e9+;
const double EPS = 1e-;
char get(char ch1, char ch2)
{ for(char j = 'a'; j <= 'z'; ++j)
{
if(ch1 != j && ch2 != j)
{
return j;
}
}
}
int main()
{
int n, t;
cin >> n >> t;
string st1, st2, st3(n, ' ');
cin >> st1 >> st2;
int cnt = ;
for(int i = ; i < n; ++i)
{
if(st1[i] == st2[i]) st3[i] = st1[i], cnt++;
else st3[i] = get(st1[i], st2[i]);
}
if(n - cnt > t)
{
bool f = false;
for(int i = ; i < n && n - cnt != t; ++i)
{
if(st1[i] != st2[i])
{
if(f) st3[i] = st1[i];
else st3[i] = st2[i];
f = !f;
if(!f)cnt++; //debug(cnt);debug(st3);
}
}
}
else
{
for(int i = ; i < n && n - cnt != t; ++i)
{
if(st1[i] == st2[i])
{
st3[i] = get(st1[i], st2[i]);
cnt--;
}
}
}
//debug(cnt);
if(n - cnt == t) cout << st3 << endl;
else cout << - << endl;
return ;
}

Problem D

题目大意:给以个奇数n,需找到k个素数,让这k个素数的和等于n,1个素数,2个素数,3个素数之和都行,题目保证有解。

数据范围:3 ≤ n < 10^9,1<=k<=3

通过简单的算,可以看出答案是不唯一的。

如:

n=27

答案一:

3

2 2 23

答案二:

3

5 11 11

将k分3种情况:

1、k等于1,若n是素数则答案是

1

n

2、k等于2,由于给的n是奇数,素数之中除了2之外都是奇数,所以要使k等于的时候有解,必然有一个数是2,另一个数n-2

3、k等于3,由于题目保证有解,并且n是奇数,所以答案之中肯定没有2。任意指定一个素数p(2<p<=n-3)为其中的答案,然后再去找另一个素数i,和另一个素数n-p-i。

只要p、i、n-p-i都是素数,则找到答案。

参考代码:

 #include <iostream>
#define ll long long
using namespace std; bool is_prime(int n)
{
for(ll i = ; i * i <= n; ++i)
if(n % i == ) return false;
return n > ? true: false;
}
int main(int argc, char **argv)
{
int n;
cin >> n;
if(is_prime(n))
cout << << endl << n << endl;
else if(is_prime(n - ))
cout << << endl << << " " << n - << endl;
else
{
int p;
for(p = n - ; ; --p)
if(is_prime(p)) break;
n -= p;
for(int i = ; ;++i)
{
if(is_prime(i) && is_prime(n - i))
{
cout << << endl << p << " " << i << " " << n - i << endl;
break;
}
}
}
return ;
}

---恢复内容结束---

上一篇:nginx 虚拟主机


下一篇:(转)ecshop产品详情页显示不清晰