POJ_2109_Power of Cryptography(二分+高精度)
http://poj.org/problem?id=2109
注:代码是错误的,只是存的方法,网络上double类型能过,也有大神写对了,但是没看懂。
我感觉值得存储的地方就是调用了一个大数相乘的函数,直接可以返回大叔的字符串。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # include <stdio.h> 2 # include <string.h> 3 # include <math.h> 4 char str[1000]; 5 int n; 6 void sum(char *str,char *c) 7 { 8 int l1,l2,i,j; 9 int a[105],b[105],d[105]; 10 memset(a,0,sizeof(a)); 11 memset(b,0,sizeof(b)); 12 memset(d,0,sizeof(d)); 13 l1=strlen(str); 14 l2=strlen(c); 15 for(i=0;i<l1;i++) 16 a[i]=str[l1-1-i]-'0'; 17 for(i=0;i<l2;i++) 18 b[i]=c[l2-1-i]-'0'; 19 int beyond=0; 20 for(i=0;i<l1;i++){ 21 for(j=0;j<l2;j++) 22 d[i+j]+=a[i]*b[j]; 23 } 24 for(i=0;i<l1+l2;i++) 25 { 26 if(d[i] > 9) 27 { 28 d[i+1]+=d[i]/10; 29 d[i]=d[i]%10; 30 } 31 } 32 j=l1+l2; 33 while(d[j]==0 && j>0) 34 j--; 35 for(i=0;j>=0;i++,j--) 36 str[i]=d[j]+'0'; 37 str[i]='\0'; 38 } 39 int my_binary_search(int n,int up,int down, char p[]) 40 { 41 int j,k,z,t,len,mid,leag; 42 int i; 43 char c[105],d[105]; 44 if(up>=down) 45 { 46 return down; 47 } 48 else 49 { 50 mid=(up+down)/2; 51 t=0; 52 z=mid; 53 while(z) 54 { 55 d[t++]=z%10+'0'; 56 z=z/10; 57 } 58 d[t]='\0'; 59 for(k=0,j=t-1;k<t;k++,j--) 60 { 61 c[k]=d[j]; 62 } 63 c[t]='\0'; 64 strcpy(str,c); 65 for(j=1;j<n;j++) 66 { 67 sum(str,c); 68 } 69 if(strcmp(str,p) == 0) 70 { 71 return mid; 72 } 73 else if(mid==up || mid==down) 74 return down; 75 else 76 { 77 int h1,h2; 78 h1=strlen(str); 79 h2=strlen(p); 80 if(h1>h2) 81 my_binary_search(n,up,mid,p); 82 else if(h1<h2) 83 my_binary_search(n,mid,down,p); 84 else 85 { 86 for(i=0;i<h1;i++) 87 { 88 if(str[i]>p[i]) 89 { 90 leag=1; 91 break; 92 } 93 else if(str[i]<p[i]) 94 { 95 leag=0; 96 break; 97 } 98 99 } 100 if(leag) 101 my_binary_search(n,up,mid,p); 102 else 103 my_binary_search(n,mid,down,p); 104 } 105 } 106 } 107 } 108 int main() 109 { 110 int n; 111 int i,mid; 112 char p[105]; 113 while(scanf("%d%s",&n,p) != EOF) 114 { 115 mid=my_binary_search(n,0,100000000,p); 116 printf("%d\n",mid); 117 } 118 return 0; 119 }