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 divisible by a as a separate number, and the second (right) part is divisible by b as a separate number. Both parts should be positive integers that have no leading zeros. Polycarpus knows values a and b.
Help Polycarpus and find any suitable method to cut the public key.
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 to 106 digits. The second line contains a pair of space-separated positive integers a, b (1 ≤ a, b ≤ 108).
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 divisible by a, and the right part must be divisible by b. 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).
116401024 97 1024
YES 11640 1024
284254589153928171911281811000 1009 1000
YES 2842545891539 28171911281811000
120 12 1
NO
分别处理前后缀对a和b取模的值,这里要用到取模运算的一些性质
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1000010;
char str[N];
__int64 pre[N], suf[N];
int main()
{
int a, b;
while (~scanf("%s", str))
{
scanf("%d%d", &a, &b);
int n = strlen(str);
//处理后缀
suf[n - 1] = (str[n - 1] - '0') % b;
// printf("%d\n", suf[n - 1]);
__int64 last = 1 % b;
for (int i = n - 2; i >= 0; --i)
{
suf[i] = ( ((str[i] - '0') * 10 % b * (last % b)) % b + (suf[i + 1] % b) ) % b;
// printf("%d\n", suf[i]);
last = ((10 % b) * (last % b)) % b;
}
//处理前缀
last = 1;
pre[0] = (str[0] - '0') % a;
for (int i = 1; i < n; ++i)
{
pre[i] = (((pre[i - 1]) % a * 10 % a) % a + (str[i] - '0') % a) % a;
}
//枚举分割点
bool flag = false;
int end;
for (int i = 1; i < n; ++i)
{
if (str[i] != '0' && !pre[i - 1] && !suf[i])
{
flag = true;
end = i;
break;
}
}
if (!flag)
{
printf("NO\n");
continue;
}
printf("YES\n");
for (int i = 0; i < end; ++i)
{
printf("%c", str[i]);
}
printf("\n");
for (int i = end; i < n; ++i)
{
printf("%c", str[i]);
}
printf("\n");
}
return 0;
}