codeforces 490C. Hacking Cypher 解题报告

题目链接:http://codeforces.com/problemset/problem/490/C

题目意思:给出一个可能有10^6 位长的字符串且没有前导0的整数,问能否一分为二,使得前面的一部分被 a 整除 且 后面那部分被 b 整除,可以的话输出 “YES” 和 划分后的两部分,否则输出“NO”.

  看到字符串这么长,一下子就吓怕了~~~用 mod 即可!这些要靠数学积累吧......

  好常规的方法做——暴力枚举,当然不能遗漏啦,所以从左至右直到倒数第二个数字,都要算出mod a 的结果且保存到 vis[] 数组里面(vis初始化为-1)。然后从右往前直到顺数第二个数字,算出 mod b 的结果,当这个结果为0(即可以被b整除),且vis[前一位数字] = 0(表示前面的数可以被a整除),还有一个容易遗漏的一点,就是没有前导0(对应代码中的 s[i] != ‘0‘)那么就代表找到一个解。遍历完之后都没有的话就表示没有答案,输出“NO”

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 using namespace std;
 6
 7 const int maxn = 1e6 + 5;
 8 int vis[maxn];     // 保存string中 0~len-1 个数字mod a 的结果
 9 string s;
10
11 int main()
12 {
13     #ifndef ONLINE_JUDGE
14         freopen("in.txt", "r", stdin);
15     #endif // ONLINE_JUDGE
16     int a, b;
17     while (cin >> s >> a >> b)
18     {
19         memset(vis, -1, sizeof(vis));
20         int tmp = 0;
21         int len = s.size();
22
23         for (int i = 0; i < len-1; i++)    // 至少要留一位给 b 嘛
24         {
25             tmp = (tmp * 10 + (s[i]-‘0‘)) % a;     // 用到数学一些小知识
26             vis[i] = tmp;
27         }
28
29         int base = 1;
30         tmp = 0;
31         for (int i = len-1; i > 0; i--)      // 同理,a都要至少留一位数
32         {
33             tmp = ((s[i]-‘0‘) * base + tmp) % b;
34             base = base * 10 % b;
35             if (tmp == 0 && !vis[i-1] && s[i] != ‘0‘)
36             {
37                 cout << "YES" << endl;
38                 cout << s.substr(0, i) << endl;
39                 cout << s.substr(i, len-i) << endl;
40                 return 0;
41             }
42         }
43         cout << "NO" << endl;
44     }
45     return 0;
46 }

时间: 2024-08-11 09:55:44

codeforces 490C. Hacking Cypher 解题报告的相关文章

CodeForces 490C Hacking Cypher

Hacking Cypher Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 490C Description Polycarpus participates in a competition for hacking into a new secure messenger. He's almost won. Having

Codeforces 490C Hacking Cypher【前缀模+后缀模+暴力】

C. Hacking Cypher time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Polycarpus participates in a competition for hacking into a new secure messenger. He's almost won. Having carefully studied

codeforces 591A. Wizards&#39; Duel 解题报告

题目链接:http://codeforces.com/problemset/problem/591/A 题目意思:其实看下面这幅图就知道题意了,就是Harry 和 He-Who-Must-Not-Be-Named 分别在走廊末端,各发射自己的impulse,其中Harry 的 impulse 速度为 p 米/s,He-...-Named (这个名字太长,姑且写成这样)为 q米/s.然后相遇之后各自回到它们的主人身边,再发射,问第二次相遇的时候,Harry的impulse 离他的位置距离是多少.

CodeForces 250B Restoring IPv6 解题报告

Description An IPv6-address is a 128-bit number. For convenience, this number is recorded in blocks of 16 bits in hexadecimal record, the blocks are separated by colons — 8 blocks in total, each block has four hexadecimal digits. Here is an example o

Codeforces Round #277.5 解题报告

又熬夜刷了cf,今天比正常多一题,比赛还没完但我知道F过不了了,一个半小时贡献给F还是没过--应该也没人Hack,写写解题报告吧= =! 解题报告如下: A题:选择排序直接搞,因为不要求最优交换次数,代码: #include <iostream> #include <algorithm> #include <cstdio> #include <memory.h> #include <vector> #include <stack> #

codeforces 495B. Modular Equations 解题报告

题目链接:http://codeforces.com/problemset/problem/495/B 题目意思:给出两个非负整数a,b,求出符合这个等式      的所有x,并输出 x 的数量,如果 x 有无限多个,那么输出 infinity. 想了半个多小时......有个地方想遗漏了. a mod x == b,等价于  a = k*x + b.设 mul = a - b,那么 k*x = mul,然后就不断枚举 mul 的因子,即 kx = mul.由于 mod 出来的结果为 b,那么

codeforces 495A. Digital Counter 解题报告

题目链接:http://codeforces.com/problemset/problem/495/A 这个题目意思好绕好绕~~好绕~~~~~,昨天早上做得 virtual 看不懂,晚上继续看还是,差点就想求救 XX 兽了,最终还是打住,尽量不要依赖人嘛.今天终于想到了,大感动 ~_~ 理解能力有待提高... One of the sticks of the counter was broken    这句话有一点误导人的成分,我刚开始就以为只有一条 stick 坏了= =,再看这句 becau

codeforces 577B. Modulo Sum 解题报告

题目链接:http://codeforces.com/problemset/problem/577/B 题目意思:就是给出 n 个数(a1, a2, ..., an) 和 m,问能不能从这 n 个数中选出一些数(不能为空),使得这些数的总和能整除 m . 实不相瞒,完全没想法...看题解,有个地方看都看不懂: n > m的情况.求助乌冬子,连带被批英语水皮 >___<.还是谢谢他啦,一步一步引导我. 貌似挺多人也有这个疑惑的.他说那个是特例优化,原谅我懒,直接摘抄吧~ 首先知道一些参数.

codeforces 496B. Secret Combination 解题报告

题目链接:http://codeforces.com/problemset/problem/496/B 题目意思:给出 n 位数你,有两种操作:1.将每一位数字加一(当某一位 > 9 时只保存个位数)   2.循环右移(最右边那个数字去到第一位上).问经过若个两种操作的组合后,得到的最小数值为多少. 我一开始用了vector来做= =,没有考虑到循环右移的情况.以为每一位从1加到9之后,找出最小的那个就可以..... 留个纪念(错误代码,别学,如果没有循环右移的限制,这个是对的) 1 #incl