• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
royecode
博客园    首页    新随笔    联系   管理    订阅  订阅
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> 2 3 using namespace std; 4 5 const int MAXN = 1e5+5; 6 7 int main() 8 { 9 int n, t; 10 cin >> n >> t; 11 if(t == 10) 12 { 13 if(n == 1)//无解 14 puts("-1"); 15 else if(n == 2) 16 puts("10"); 17 else 18 { 19 cout << 1; 20 for(int i = 0; i < n - 3; ++i) 21 cout << 0; 22 puts("10"); 23 }

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)

参考代码:

 1 By Royecode, contest: Codeforces Round #324 (Div. 2), problem: (B) Kolya and Tanya , Accepted, #
 2 #include <bits/stdc++.h>
 3 
 4 #define ll long long
 5 #define pb push_back
 6 #define mp make_pair
 7 #define debug(x) cout << #x << " " << x << endl;
 8 
 9 using namespace std;
10 
11 const int MAXN = 1e5+7;
12 const int INF = 2e9+7;
13 const double EPS = 1e-9;
14 const int MOD = 1e9+7;
15 
16 ll quick(ll n, int i)
17 {
18     if(i != 1)
19     {
20         ll t = quick(n, i / 2) % MOD;
21         if(i % 2 == 1) return t * t * n % MOD;
22         return t * t % MOD;
23     }
24     return n;
25 }
26 int main()
27 {
28     ll n;
29     cin >> n;
30     cout << (quick(3, 3 * n) - quick(7, n) + MOD ) % MOD<< endl; //注意加上MOD,再取模
31     return 0;
32 }

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,或则无解

参考代码:

 1 #include <bits/stdc++.h>
 2 
 3 #define ll long long
 4 #define pb push_back
 5 #define mp make_pair
 6 #define debug(x) cout << #x << " " << x << endl;
 7 
 8 using namespace std;
 9 
10 const int MAXN = 1e5+7;
11 const int INF = 2e9+7;
12 const double EPS = 1e-9;
13 char get(char ch1, char ch2)
14 {
15 
16     for(char j = 'a'; j <= 'z'; ++j)
17     {
18         if(ch1 != j && ch2 != j)
19         {
20             return j;
21         }
22     }
23 }
24 int main()
25 {
26     int n, t;
27     cin >> n >> t;
28     string st1, st2, st3(n, ' ');
29     cin >> st1 >> st2;
30     int cnt = 0;
31     for(int i = 0; i < n; ++i)
32     {
33         if(st1[i] == st2[i]) st3[i] = st1[i], cnt++;
34         else st3[i] = get(st1[i], st2[i]);
35     }
36     if(n - cnt > t)
37     {
38         bool f = false;
39         for(int i = 0; i < n && n - cnt != t; ++i)
40         {
41             if(st1[i] != st2[i])
42             {
43                 if(f) st3[i] = st1[i];
44                 else st3[i] = st2[i];
45                 f = !f;
46                 if(!f)cnt++;
47                 
48                 //debug(cnt);debug(st3);
49             }
50         }
51     }
52     else 
53     {
54         for(int i = 0; i < n && n - cnt != t; ++i)
55         {
56             if(st1[i] == st2[i])
57             {
58                 st3[i] = get(st1[i], st2[i]);
59                 cnt--;
60             }
61         }
62     }
63     //debug(cnt);
64     if(n - cnt == t) cout <<  st3 << endl;
65     else cout << -1 << endl;
66     return 0;
67 }

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都是素数,则找到答案。

参考代码:

 1 #include <iostream>
 2 #define ll long long
 3 using namespace std;
 4 
 5 bool is_prime(int n)
 6 {
 7     for(ll i = 2; i * i <= n; ++i)
 8         if(n % i == 0) return false;
 9     return n > 1? true: false;
10 }
11 int main(int argc, char **argv)
12 {
13     int n;
14     cin >> n;
15     if(is_prime(n))
16         cout << 1 << endl << n << endl;
17     else if(is_prime(n - 2))
18         cout << 2 << endl << 2 << " " << n - 2 << endl;
19     else
20     {
21         int p;
22         for(p = n - 3; ; --p)
23             if(is_prime(p)) break;
24         n -= p;
25         for(int i = 2; ;++i)
26         {
27             if(is_prime(i) && is_prime(n - i))
28             {
29                 cout << 3 << endl << p << " " << i << " " << n - i << endl;
30                 break;      
31             }
32         }
33     }
34     return 0;
35 }

 

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

posted on 2015-10-07 16:38  royecode  阅读(236)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3