Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3711 | Accepted: 1051 |
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
Source
题目大意:给出一个小于1的分数,把他转换成二进制形式,并找出小数点到循环部分的最少距离以及循环节的最小长度。
思路:
当得到我们所需的精度时跳出循环即可。可以知道,对于某一位的n,设此时为ni=n * 2^i,当到了某一位nj=n * 2^j满足ni mod m=nj mod m时,那么就说明小数部分出现了循环,循环节的长度为j-i。
对于模型我们可列出式子:
对于最简分数p/q,我们有p*2^i≡p*2^j(mod q),(j>i)
上面的式子化简为: p* 2^i *(2^(j-i)-1)能被q整除,
那么,对于p,q而言,他们是互质的,gcd(p,q)=1,又有 2^(j-i)-1为奇数,所以若q中有2,那么他一定在2^i中
那么我们把q中的2都拿掉并计数,那么数目就为i的值,当然,还要加1;
设去除所有2的q为q1;
然后就有( 2^(j-i)-1 )%q1=0,就有:2^(j-i)≡ 1(mod q);
由欧拉定理可以得到:
由于q'和2互素,所以(由欧拉定理:2^Φ(q')≡1(mod q') )此方程必有解,且Φ(q')为方程的一个解。但不一定是最小解。
问题来到了怎样求最小解:
得出结果.x=φ(q)一定是一个解,但不一定是最小的解,若x=φ(q)不是最小的解,那么最小的解x一定是φ(q)的因子!
证明:假设x是满足2^x≡1(mod n)的最小的x,假设x不是φ(n)的因子,令r=φ(n)mod x,则r>0&&r<x.由2^φ(n)≡ 1(mod n)和2^x≡1(mod n),可以得2^r mod n=1,则存在一个比x还小的数,使得2^r≡1(mod n),这与假设矛盾,所以x为φ(n)因子。
最后还要检查模是否为1:
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
int a[1000];
int euler(int n)
{
int ans = n;
for(int i = 2; i * i <= n; ++i)
if(n % i== 0)
{
ans -= ans / i;
while(n % i == 0)
n /= i;
}
if(n > 1)
ans -= ans / n;
return ans;
}
ll qpow(ll x, ll n, ll mod)
{
ll ans = 1;
while(n)
{
if (n&1)
ans = ans * x % mod;
x = x * x % mod;
n >>= 1;
}
return ans;
}
int gcd(int a,int b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
int main()
{
int p,q,kase=0;
while(scanf("%d/%d",&p,&q)!=EOF)
{
int d=gcd(p,q);
p/=d;
q/=d;
int x=1;
while(!(q&1))
{
q>>=1;
x++;
}
int k=euler(q);
int cnt=0;
for(int i=2; i*i<=k; i++)
if(k%i==0)
{
a[++cnt]=i;
a[++cnt]=k/i;
}
a[++cnt] = k;
sort(a+1,a+1+cnt);
int y;
for(int i = 1; i <= cnt; ++i)
if(qpow(2, a[i], q) == 1)
{
y = a[i];
break;
}
printf("Case #%d: %d,%d\n", ++kase, x, y);
}
return 0;
}