Uva 12505 - Searching in sqrt(n)
这题不会高精度求平方根,只能对照标程了!
大概看懂了什么意思,可是我根本就没看到平方根也没有看到平方啊,他难道直接比较的?求路过大牛解释!
Searching in sqrt(n)
Time limit: 5.000 seconds
In binary, the square root of 2, denoted by sqrt(2), is an infinite number 1.0110101000001001111...
Given an integer n and a binary string (i.e. a string consisting of 0 and 1) S, your task is to find the first occurrence of S in the fraction part (i.e. the part after the decimal point) of sqrt(n). In case sqrt(n) is an integer, the fraction part is an infinite sequence of zeros.
Input
The first line contains T (T 100), the number of test cases. Each of the following lines contains an integer n ( 2
100), the number of test cases. Each of the following lines contains an integer n ( 2 n
n 1, 000, 000) and a binary string S with at most 20 characters.
1, 000, 000) and a binary string S with at most 20 characters.
Output
For each case, print the position of the first character in the first occurrence of S. The first digit after the dot is at position 0. The answer is guaranteed to be no greater than 100.
Sample Input
2 2 101 1202 110011
Sample Output
2 58
Problemsetter: Rujia Liu, Special Thanks: Yiming Li, Feng Chen, Jane Alam Jan
 
1 #include <map> 2 #include <iostream> 3 #include <string> 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 using namespace std; 8 #define maxn 1111 9 #define ll long long 10 int n,m; 11 char s[maxn]; 12 int a[maxn]; 13 int b[maxn]; 14 int res[maxn]; 15 int main(){ 16 int t; 17 scanf("%d",&t); 18 while(t--){ 19 scanf("%d%s",&n,s); 20 memset(a,0,sizeof a); 21 for(int i=19;i>=0;i--) 22 if(n>=(1<<i)){ 23 n-=(1<<i); 24 a[i+280]=1; 25 } 26 memcpy(res,a,sizeof a); 27 for(int i=149;i>=0;i--){ 28 int b1=0; 29 for(int j=299;j>149+i+1;j--) 30 if(res[j]==1){b1=1;break;} 31 if(b1==0) 32 for(int j=149;j>i;j--){ 33 if(res[j+i+1]>b[j]){b1=1;break;} 34 if(res[j+i+1]<b[j]){b1=-1;break;} 35 } 36 if((b1==-1)||(b1==0)&&(res[i+i]==0)&&(res[i+i+1]==0))b[i]=0; 37 else { 38 b[i]=1; 39 for(int j=149;j>i;j--)res[j+i+1]-=b[j]; 40 res[i+i]--; 41 for(int j=i+i;j<300;j++) 42 if(res[j]<0){ 43 res[j]+=2; 44 res[j+1]--; 45 } 46 } 47 } 48 n=strlen(s); 49 for(int i=139;i>=0;i--){ 50 int b1=1; 51 for(int j=0;j<n;j++)if(b[i-j]!=s[j]-'0')b1=0; 52 if(b1){printf("%d\n",139-i);break;} 53 } 54 55 } 56 return 0; 57 }
 
                    
                 
 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号