Codeforces 490C. Hacking Cypher Problem Solving report

Source: Internet
Author: User



Title Link: Http://codeforces.com/problemset/problem/490/C



Title meaning: Give a string that may have a 10^6 bit length and no leading 0 integer, ask whether it can be divided into two, so that the previous part is divisible by a and the latter part is divisible by B, you can output "YES" and the division after the second part, otherwise output "NO".



See the string so long, suddenly scared the ~ ~ ~ with mod! It depends on the accumulation of mathematics ...



The usual way to do it--the brute force enumeration, of course, cannot be omitted, so from left to right until the penultimate number, the result of MoD A is calculated and saved to the vis[] array (Vis is initialized to-1). Then from right forward until the second number of the number of CIS, the results of the MoD B, when the result is 0 (that is, can be divisible by B), and vis[before a digit] = 0 (indicating that the preceding number can be divisible by a), there is a simple omission, there is no leading 0 (s[i in the corresponding code]! = Then the representative finds a solution. If you don't have any answers after the traversal, the output "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]; // Save the result of mod a of 0~len-1 numbers in string
 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();
twenty two        
23 for (int i = 0; i <len-1; i++) // At least one must be left for b
twenty four         {
25 tmp = (tmp * 10 + (s[i]-‘0’))% a; // Some small knowledge of mathematics
26 vis[i] = tmp;
27}
28
29 int base = 1;
30 tmp = 0;
31 for (int i = len-1; i> 0; i--) // Similarly, a must have at least one digit
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} 
View Code





Codeforces 490C. Hacking Cypher Problem Solving report


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.