Codeforces Round #279 (Div. 2)
C. Hacking Cyphertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output
Polycarpus participates in a competition for hacking into a new secure messenger. He's almost won.
Having carefully studied the interaction protocol, Polycarpus came to the conclusion that the secret key can be obtained if he properly cuts the public key of the application into two parts. the public key is a long integer which may consist of even a million digits!
Polycarpus needs to find such a way to cut the public key into two nonempty parts, that the first (left) part is divisibleAAs a separate number, and the second (right) part is divisibleBAs a separate number. Both parts shocould be positive integers that have no leading zeros. Polycarpus knows valuesAAndB.
Help Polycarpus and find any suitable method to cut the public key.
Input
The first line of the input contains the public key of the messenger-an integer without leading zeroes, its length is in range from 1 to106 digits. the second line contains a pair of space-separated positive integersA,B(1? ≤?A,?B? ≤? 108 ).
Output
In the first line print "YES" (without the quotes), if the method satisfying conditions above exists. in this case, next print two lines-the left and right parts after the cut. these two parts, being concatenated, must be exactly identical to the public key. the left part must be divisibleA, And the right part must be divisibleB. The two parts must be positive integers having no leading zeros. If there are several answers, print any of them.
If there is no answer, print in a single line "NO" (without the quotes ).
Sample test (s) input
11640102497 1024
Output
YES116401024
Input
2842545891539281719112818110001009 1000
Output
YES284254589153928171911281811000
Input
12012 1
Output
NO
Question: Give You a string and ask if you can divide it into two sections. The first section can divide a, the next section can divide B, and the next section cannot contain leading 0.
Train of Thought: This is the use of the modulo of large numbers. After understanding the principle of Modulo of large numbers, we can scan them from the front to the back to mark the positions that can be divisible by, and then from the Back Forward scan, to see whether there is a division B, judge the front I-1 whether the division a on the line, as to why from the Back Forward scan, because this can reduce the time complexity, if both are from the past and the next, the worst will be o (n ^ 2)
If you do not understand the modulo of large numbers, see the following:
A + B) % n = (a % n + B % n) % n (a-B) % n = (a % n-B % n + n) % n: Why n is added? Because a % n may be less than B % n, adding n is a positive integer a * B % n = (a % n * B % n) % n the basic int mod (char str [], int num) {int number [100]; for (int I = 0; I
#include
#include
#include
#include
#include using namespace std;const int maxn = 1e6+10;char str[maxn];bool flag[maxn];int a,b;int main(){#ifdef xxz freopen("in.txt","r",stdin);#endif while(scanf("%s%d%d",str,&a,&b) != EOF) { int len = strlen(str); int sum = 0; memset(flag,0,sizeof(flag)); for(int i = 0; i < len; i++) { sum = (sum*10 + (str[i] - '0'))%a; if(i < len-1 && sum == 0 && str[i+1] != '0') flag[i] = true; } bool ok = false; int pos = 0, k = 1; sum = 0; for(int i = len-1; i > 0; i--) { sum = (sum + (str[i] - '0')*k)%b; k *= 10; k %= b; if(sum == 0 && flag[i-1]) { ok = 1; pos = i; break; } } if(ok) { printf("YES\n"); for(int i = 0; i < pos-1; i++) printf("%c",str[i]); printf("%c\n",str[pos-1]); for(int i = pos; i < len-1; i++) printf("%c",str[i]); printf("%c\n",str[len-1]); } else printf("NO\n"); } return 0;}