http://codeforces.com/contest/490/problem/C
题意:把一个很大的数分成两部分,前一部分可以被a整除,后一部分可以被b整除,如果存在输出这两部分,两部分都不能含有前导0;
思路:从左到右求出在每一位对a的余数记录在aa数组里面,再从右向左在每一位对b的余数记录在bb数组里面。在aa[i]==0&&bb[i+1]==0&&bb[i+1]!='0'的情况下,就存在答案。
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define LL __int64 5 using namespace std; 6 7 char str[2000000]; 8 int a,b; 9 int aa[2000000],bb[2000000]; 10 11 int main() 12 { 13 while(scanf("%s",str)!=EOF) 14 { 15 scanf("%d%d",&a,&b); 16 int k=strlen(str); 17 int t=0; 18 for(int i=0; i<k; i++) 19 { 20 t+=(str[i]-'0'); 21 aa[i]=t%a; 22 t%=a; 23 t*=10; 24 } 25 int s=1,t1=0; 26 for(int i=k-1; i>=0; i--) 27 { 28 bb[i]=((str[i]-'0')*s+t1)%b; 29 t1=((str[i]-'0')*s+t1)%b; 30 s=(s*10)%b; 31 } 32 bool flag=false; 33 int pos=-1; 34 for(int i=0; i<k-1; i++) 35 { 36 if(aa[i]==0&&bb[i+1]==0&&str[i+1]!='0') 37 { 38 pos=i; 39 flag=true; 40 break; 41 } 42 } 43 if(flag) 44 { 45 printf("YES "); 46 for(int i=0; i<=pos; i++) 47 { 48 printf("%c",str[i]); 49 } 50 printf(" "); 51 for(int i=pos+1; i<k; i++) 52 { 53 printf("%c",str[i]); 54 } 55 printf(" "); 56 } 57 else 58 { 59 printf("NO "); 60 } 61 } 62 return 0; 63 }