D-square root Big searchTime
limit:5000MS
Memory Limit:131072KB
64bit IO Format:%lld &%ll U Submit Status Practice CSU 1114
Description
in binary, 2 of the arithmetic square root, namely sqrt (2), is an infinite fractional 1.0110101000001001111 ...given an integer n and a 01 string s, your task is to find the first occurrence of s in the fractional portion of sqrt (n), which is the part after the decimal point. If sqrt (n) is an integer, the fractional part is considered to be a sequence of infinitely more than 0 components.
Input
Enter the number of first behavior Data group T (T<=20). Each of the following behaviors is a set of data that contains only one integer n (2<=n<=1,000,000) and a non-empty 01 string s with a length of not more than 20.
Output
For each set of data, the first occurrence of the output S, the position of the first character. The position of the first digit after the decimal point is 0. Enter a guaranteed answer of no more than 100.
Sample Input
Sample Output
Test instructions: A number is squared, the fractional part is compared to the given string, and the corresponding first position is output.
Analysis: Double no more than 100 bits of precision, only with simulation. The topic is too difficult, see the problem-solving report understanding for a long time, the following is the standard, plus their own understanding of the comments, may be written in a more chaotic, not very good expression, but also hope forgive
#include <cstdio> #include <cstring>const int Maxn=1111;char s[maxn];int a[maxn],b[maxn],res[maxn];int Main () {int t,n; scanf ("%d", &t); while (t--) {scanf ("%d%s", &n,s); memset (a,0,sizeof a); Break n into binary and save to a[280]->a[299]; for (int i=19;i>=0;i--) {if (n>= (1<<i)) {n-= (1<<i); A[i+280]=1; }} memcpy (Res,a,sizeof (a));//Why copy? Superfluous opened an an array for (int i=149;i>=0;i--) {int b1=0;//mark/* Why is this here 149, and the back is 139? Because the top ten is the integer part! n into binary system up to 20 bits, after the opening of the last 10 bits */for (int j=299;j>149+i+1;j--)//The root number found at present 149-i bit, 299-(149-i) =150+i; F (res[j]==1) {/* front if one is 1; current B[i] must be 1, because after adding a 1, the square is still less than the original number; */b1=1;break; if (b1==0)//If 0, compare res and B, and ensure quotient 1, the resulting square is less than n '; for (int j=149;j>i;j--) { if (Res[j+i+1]>b[j]) {b1=1;//ABit comparison, first find bigger than him, explain can quotient 1, first find than it small, then only quotient 0 break; } if (Res[j+i+1]<b[j]) {b1=-1;break; }}//We'll take a look here, 00*00=0000,01*01=0001,10*10=0100,11*11=1001;//11*11=1001,111*111= 1100 01,110*110=100100,1111*1111= 11100001,1110*1110=11000100//quotient zero, to the previous few did not have any effect, the last two bit is 0, no correction,//quotient 1, the last two bits 0 1, has certain influence to the preceding number, needs to revise if (b1==-1| | b1==0&&res[i+i]==0&&res[i+i+1]==0) b[i]=0; /* and b[i] corresponding to the continuous two-bit is 0, B[i] can only 0, 1 words res[i*i] must be 1;*/else{b[i]=1;//quotient 1,res minus the corresponding bit B, f or (int j=149;j>i;j--) res[j+i+1]-=b[j];//in front of minus B[j], will n gradually unelectability 0 res[i+i]--;//the last one minus 1, the penultimate bit 0 , no correction//correction of RES values is required, bit corrections less than 0 for (int j=i+1;j<300;j++) if (res[j]<0) { /* If a bit is less than 0, you need to borrow, add 2 (because it is binary), the next bit minus one */res[j]+=2; res[j+1]--; }//The corrected res must be greater than 0, otherwise it will not be 1. If the next quotient 0, the resulting square will not change, do not modify res,//If quotient 1, the resulting square must be less than N, will n cut off B, keep using two points, N approximation to 0 }} n=strlen (s); A square root is saved in B (the last one is the first digit after the decimal point) for (int i=139;i>=0;i--) {int b1=1; for (int j=0;j<n;j++)//contrast, exactly the same as the output starting position if (b[i-j]!=s[j]-' 0 ') b1=0; if (B1) {printf ("%d\n", 139-i); Break }}} return 0;}
UVA 12505 The program design contest of Hunan Provincial University students original question D-square root Big search