The first step is to define the binary representation of the fraction:
1 // supposing the fraction is a/b, (a < b && (A, b)) 2 // Its binary expression is denoted as:0.bit[1]bit[2] ... 3 seed[0] = A;
4 for (int1;; i++) {5 1)/b; 6 Seed[i] = (Seed[i-1] << 1)% B;
7 }
You may wish to set one of the cycle sections to Bit[r]. Bit[s], apparently seed[r-1] = Seed[s] and bit[r] = bit[s + 1], where the cyclic section length L = (s-r + 1).
by seed[i] = 2i+1% B, then there is 2r% B = 2s+1% B, i.e. 2r (2s-r+1-1) ≡0 (MODB),
Note B = 2t*b1, where (b1,2), and r = T, then 2l≡1 (MODB1).
L|φ (B1), which enumerates the approximate b1 of a period L.
and has the initial position P = t + 1.
http://poj.org/problem?id=3358
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <cmath>5 using namespacestd;6 typedef __int64 LL;7 Const intMAXN = 1e2 +Ten;8 intPRIME[MAXN], K;9 intFAC[MAXN];Ten intA, B, b; One A intPowerintAintp) { -A%=B; - intAns =1; the while(p) { - if(P &1) ans = (LL) ans * a%B; -P >>=1; -A = (LL) A * a%B; + } - returnans; + } A at intgcdintAintb) {return!b? A:GCD (b, a%b); } - - voidsolve () { -A%=b; - intD =gcd (A, b); -A/= D, b/=D; in intt =0; - while(b%2==0) b/=2, ++T; toprintf"%d,", ++t); + if(b = =1) {printf ("%d\n",1);return; } -K =0; theB =b; * intPhi =b; $ intMID = (int) sqrt (b);Panax Notoginseng for(inti =3; I <= mid; i + =2){ - if(b% i = =0){ theprime[k++] =i; + while(b% i = =0) b/=i; A } the } + if(b! =1) prime[k++] =b; - for(inti =0; I < K; i++) Phi/=Prime[i]; $ for(inti =0; I < K; i++) Phi *= (Prime[i]-1); $K =0; - for(inti =1; I * I <= phi; i++){ - if(phi% i = =0) fac[k++] = i, fac[k++] = phi/i; the } -Sort (FAC, FAC +k);Wuyi for(inti =0; I < K; i++)if(Power (2, fac[i]) = =1){ theprintf"%d\n", Fac[i]); - Break; Wu } - } About $ intMain () { - //freopen ("In.txt", "R", stdin); - intKase =0; - while(~SCANF ("%d/%d", &a, &b)) { Aprintf"Case #%d:", ++Kase); + solve (); the } - return 0; $}
View Code
poj3358 Period of an Infinite Binary Expansion