It is also a question which is determined by Euler .. Well, the key is to build an equation and pay attention to some simplification techniques.
Question:
Given a cyclic decimal number generated by P/Q, find the minimum cyclic section in the binary representation of this cyclic decimal number and not the prefix of the cyclic section.
Ideas:
Decimal to binary. We should take the remainder by 2 and set the cycle section with the length of Y starting from the decimal point X,
First, convert P/Q to the simplest score. At this time, p and q are mutually qualitative.
Then it must satisfy the same remainder equation p * 2 ^ x = p * 2 ^ (x + y) mod q
Sort out q | p * 2 ^ x * (2 ^ y-1) q | 2 ^ x * (2 ^ y-1) due to the mutual quality of p and q)
At this time, because 2 ^ Y-1 is an odd number, then there is a division formula we can know that the number of the factor of 2 in Q is X, so we can deal with Q to get X, change Q to Q/(2 ^ X );
Finally, we can obtain the homogeneous equation 2 ^ y = 1 (mod q)
Use Euler's theory to understand this same-remainder equation.
The Code is as follows:
#include <iostream>#include <stdio.h>#include<string.h>#include<algorithm>#include<string>#include<ctype.h>using namespace std;#define MAXN 10000long long gcd(long long a,long long b){ return b?gcd(b,a%b):a;}long long phi(long long n){ long long res=n; for(int i=2;i*i<=n;i++) { if(n%i==0) { res=res-res/i; while(n%i==0) { n/=i; } } } if(n>1) res=res-res/n; return res;}long long multi(long long a,long long b,long long m)//a*b%m{ long long res=0; while(b>0) { if(b&1) res=(res+a)%m; b>>=1; a=(a<<1)%m; } return res;}long long quickmod(long long a,long long b,long long m) //a^b%m{ long long res=1; while(b>0) { if(b&1) res=multi(res,a,m); b>>=1; a=multi(a,a,m); } return res;}int main(){ long long p,q,x,y; int cas=0; while(scanf("%I64d/%I64d",&p,&q)!=EOF) { if(p==0) { puts("1,1"); continue; } cas++; long long t=gcd(p,q); x=1; p/=t;q/=t; while(q%2==0) { q/=2;x++; } long long m=phi(q); y=m; for(long long i=2;i*i<=m;i++) { if(m%i==0) { while(m%i==0) m/=i; while(y%i==0) { y/=i; if(quickmod(2,y,q)!=1) { y*=i; break; } } } } printf("Case #%d: %I64d,%I64d\n",cas,x,y); } return 0;}
Poj3358: Euler's Theorem