Language:
Period of an Infinite Binary Expansion
Description Let {x} = 0.a1a2a3... be the binary representation of the fractional part of a rational number z. Suppose that {x} is periodic then, we can write {x} = 0.a1a2...ar(ar+1ar+2...ar+s)w for some integers r and s with r ≥ 0 and s > 0. Also, (ar+1ar+2...ar+s)wdenotes a nonterminating and repeating binary subsequence of {x}. The subsequence x1 = a1a2 ... aris called the preperiod of {x} and x2 = ar+1ar+2 ... ar+s is the period of {x}. Suppose that |x1| and |x2| are chosen as small as possible then x1 is called the least preperiod and x2 is called the least period of {x}. For example, x = 1/10 = 0.0001100110011(00110011)w and 0001100110011 is a preperiod and 00110011 is a period of 1/10. However, we can write 1/10 also as 1/10 = 0.0(0011)w and 0 is the least preperiod and 0011 is the least period of 1/10. The least period of 1/10 starts at the 2nd bit to the right of the binary point and the the length of the least period is 4. Write a program that finds the position of the first bit of the least period and the length of the least period where the preperiod is also the minimum of a positive rational number less than 1. Input Each line is test case. It represents a rational number p/q where p and q are integers, p ≥ 0 and q > 0. Output Each line corresponds to a single test case. It represents a pair where the first number is the position of the first bit of the least period and the the second number is the length of the least period of the rational number. Sample Input 1/10 1/5 101/120 121/1472 Sample Output Case #1: 2,4 Case #2: 1,4 Case #3: 4,4 Case #4: 7,11 |
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define MAXM 100010
#define INF 99999999
#define ll long long
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
ll Read()
{
char ch;
ll a = 0;
while((ch = getchar()) == ' ' | ch == '\n');
a += ch - '0';
while((ch = getchar()) != ' ' && ch != '\n')
{
a *= 10;
a += ch - '0';
}
return a;
}
void Prll(ll a) //输出外挂
{
if(a>9)
Prll(a/10);
putchar(a%10+'0');
}
ll gcd(ll a,ll b)
{
if(b==0) return a;
return gcd(b,a%b);
}
ll quick_mod(ll a,ll b,ll m)
{
ll ans=1;
while(b)
{
if(b&1)
{
ans=(ll)(ans*a)%m;
b--;
}
b>>=1;
a=(ll)a*a%m;
}
return ans;
}
int main()
{
// fread;
ll p,q;
ll cs=1;
while(scanf("%lld/%lld",&p,&q)!=EOF)
{
printf("Case #%lld: ",cs++);
if(p==0)
{
printf("1,1\n");continue;
}
ll gc=gcd(p,q);
p/=gc; q/=gc;
ll num=0;
while(!(q&1))
{
q>>=1;
num++;
}
num++;//后一位
ll rea=q,n=q;
ll pri[100][2];
ll k=0;
for(ll i=2;i*i<=n;i++)
if(n%i==0)
{
rea=rea-rea/i;
while(n%i==0)
n/=i;
}
if(n>1) rea=rea-rea/n;
n=rea;
for(ll i=2;i*i<=n;i++)
if(n%i==0)
{
pri[k][0]=i;
pri[k][1]=0;
while(n%i==0)
{
n/=i;
pri[k][1]++;
}
k++;
}
if(n>1)
{
pri[k][0]=n;
pri[k][1]=1;
k++;
}
for(ll i=0;i<k;i++)
{
for(ll j=1;j<=pri[i][1];j++)
{
if(quick_mod(2,rea/pri[i][0],q)!=1) break;
rea/=pri[i][0];
}
}
printf("%lld,%lld\n",num,rea);
}
return 0;
}