Tata picking apples
A tree outside the gate
Drug harvesting
The above three are not the point.
Cycle
Title Description
Lele is a clever and studious child. He always likes to search for the law of things. One day, he suddenly had an interest in the positive integer power of the logarithm.
As we all know, the last digit of the power of 2 is always repeated 2,4,8,6,2,4,8,6 ... We say 2 is the positive integer power the last one's loop length is 4 (actually a multiple of 4 can be said to be the loop length, but we only consider the smallest loop length). Similarly, the number of positive integers of the remaining numbers has a similar cyclic behavior to the last digit:
Cycle length
2 2, 4, 8, 6
4
3 3, 9, 7, 1
4
4 4, 6 2
5 5 1
6 6 1
7 7, 9, 3, 1
4
8 8, 4, 2, 6
4
9 9, 1 2
Then Lele's problem came out: Is it only the last one to have such a cycle? Does the post-K bit of an integer n have a loop if it has a positive integer power? If the loop, what is the length of the loop?
Attention:
1. If the number of a positive integer power of n is less than k, then the insufficient high position is considered to be 0.
2. If the loop length is L, then the last K-bit is the same for any positive integer a,n of a power and A + L power.
Input/output format
Input format:
The input file circle.in has only one row, contains two integers n (1 <= n < 10^100) and K (1 <= k <=), separated by a space between N and K, representing the loop length of the last K-bit that requires the power of the positive integer of N.
Output format:
The output file Circle.out includes one row, which contains only an integer that represents the loop length. If the loop does not exist, output-1.
Input and Output Sample input example # #:
32 2
Sample # # of output:
4
Description
For 30% of data, K <= 4;
For all the data, K <= 100.
The fourth question of NOIP2005 popularization group
According to test instructions, set the original number is n, assuming that after X times, the last one (the number of L-bit) is the same as the beginning of the first, then start to calculate the second-lowest (L-1-bit) cycle section.
Since the L-bit must be at least x times to cycle, it is not meaningful to count the L-1 bits at least by n^x times to allow the L-bit to repeat (L-1-bit loops and L-bit differences).
And so on, each find a layer of cycle, in the next layer can be found each time directly by n^x^y^ ..., the efficiency will be much higher.
The specific implementation to use high precision.
This code is estimated to be high-precision multiplication constant write-off, time-consuming near 1000ms
1#include <iostream>2#include <cstring>3#include <cmath>4#include <cstdio>5 using namespacestd; 6 intt=1; 7Unsignedintn=0, K;8Unsignedintxh[ $];9 intNum//How many layers are recordedTen Chars[ $]; One BOOLflag=0, flag2=0; A structnode{ -Unsignedintv[ $]; - ints; the }a,b,c,bas,ans,nn; -Node multiple (ConstNode A1,ConstNode B1) {//High-precision multiplication Section - inti,j,x=0; - if(a1.s==1&&a1.v[0]==0)returnA1; + if(b1.s==1&&b1.v[0]==0)returnB1; -Node c1={0}; + for(i=0; i<= -&& i<a1.s;i++){ A for(j=0; j<= -&& j<b1.s;j++){ atc1.v[i+j]+=a1.v[i]*B1.v[j]; -c1.v[i+j+1]+=c1.v[i+j]/Ten; -c1.v[i+j]%=Ten; - } -c1.s=i+J; - if(c1.v[i+j]!=0) c1.s++; in } - if(c1.s>k) c1.s=k+1; to returnC1; + } - the intMain () { * inti,j; $scanf"%s%d",s,&k);Panax Notoginsengc.s=strlen (s); -ans.v[0]=1; theans.s=1; + for(i=0; i<c.s;i++) c.v[i]=s[c.s-i-1]-'0'; ABas=c;//original number backup, used as a comparison theA=C; +b=C; - intK1; $ for(k1=0; k1<k;k1++){ $num=0; -b=Bas; - //C=bas; the Do{ -b=multiple (A, b);Wuyinum++; the} while(num<Ten&& b.v[k1]!=Bas.v[k1]); - if(bas.v[k1]!=B.V[K1]) { Wuprintf"-1"); - return 0; About } $b=A; - for(j=0; j<num-1; j + +) -A=multiple (A, b); - Ann.s=1; +nn.v[0]=num; theans=multiple (ANS,NN); - } $I= -; the while(ans.v[i]==0) i--; the for(; i>=0; i--) printf ("%d", Ans.v[i]); the return 0; the}
[NOIP2005] Universal group cycle