循环 wxl
1 #include <cstdio> 2 #include <cstring> 3 #include <string> 4 #include <algorithm> 5 #include <cmath> 6 #include <iostream> 7 using namespace std; 8 int k,num[101],key[101],test[101]; 9 int book[101][10]; 10 int keyflag=1; 11 int full[101][101]; 12 int lans[1001],lkey[1001]; 13 int se=0; 14 int add(int a[],int b[]) 15 { 16 int x=0; 17 for (int i=0;i<=1000;i++){ 18 a[i]+=b[i]+x; 19 x=a[i]/10; 20 if (a[i]>=10)a[i]-=10; 21 } 22 } 23 int printans(int a[]) 24 { 25 int flag=0; 26 for (int i=1000;i>=0;i--){ 27 if (a[i]!=0) flag=1; 28 if (flag==1) cout<<a[i]; 29 } 30 cout<<endl; 31 } 32 int print(int a[]) 33 { 34 for (int i=k-1;i>=0;i--) 35 cout<<a[i]; 36 cout<<endl; 37 } 38 int mul(int a[],int b[]) 39 { 40 int c[101]; 41 memset(c,0,sizeof(c)); 42 for (int i=0;i<k;i++){ 43 int x=0; 44 for (int j=0;j<k-i;j++){ 45 c[i+j]+=a[j]*b[i]+x; 46 x=c[i+j]/10; 47 c[i+j]%=10; 48 } 49 } 50 memcpy(a,c,sizeof(c)); 51 } 52 int find(int z) 53 { 54 int t=2; 55 while(true){ 56 if (book[z][test[z]]!=0&&book[z][test[z]]==1&&se!=0){ 57 return 0; 58 } 59 mul(test,key); 60 se=1; 61 // for (int i=0;i<=9;i++) 62 // cout<<book[z][i]<<" "; 63 // cout<<endl; 64 // printans(lans); 65 // printans(lkey); 66 // getchar(); 67 if (book[z][test[z]]==0){ 68 book[z][test[z]]=t; 69 t++; 70 } 71 else if (book[z][test[z]]!=0&&book[z][test[z]]==1){ 72 if (keyflag!=1) 73 { 74 mul(num,key); 75 add(lans,lkey);} 76 keyflag=0; 77 memcpy(key,num,sizeof(num)); 78 memcpy(lkey,lans,sizeof(lans)); 79 return 0; 80 } 81 else { 82 cout<<"-1"<<endl; 83 exit(0); 84 } 85 mul(num,key); 86 add(lans,lkey); 87 } 88 } 89 int main() 90 { 91 memset(book,0,sizeof(book)); 92 string s; 93 cin>>s>>k; 94 int l=s.size(); 95 int p=0; 96 for (int i=l-1;i>=l-k;i--){ 97 if (i<0) num[p]=0; 98 else num[p]=int(s[i]-'0'); 99 book[p][num[p]]=1; 100 p++; 101 } 102 memset(lans,0,sizeof(lans)); 103 memset(lkey,0,sizeof(lkey)); 104 lans[0]=1; 105 lkey[0]=1; 106 memcpy(key,num,sizeof(num)); 107 memcpy(test,num,sizeof(num)); 108 for (int i=0;i<k;i++){ 109 find(i); 110 } 111 printans(lans); 112 }
代码中间有许多的不合理的地方,当时是以AC为目标的,所以并没有注意代码的可读性