Period of an Infinite Binary Expansion POJ - 3358 欧拉函数

题目链接


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

题意:

给出一个小于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),变换得到p * 2^i * ( 2^(j-i) -1 )≡0(mod q),也就是说q | p * 2^i * ( 2^(j-i) -1 ),由于gcd(p,q)=1,故有q | 2^i * ( 2^(j-i) -1 ).

因为2^(j-i)-1为奇数,所以q有多少个2的幂,i的值就是多少了,而且i就是循环开始位置的前一位。令q'为q除去2^i之后的数,此时有q' | ( 2^(j-i) -1 ),也就是求出x,使得2^x≡1(mod q').

由于q'和2互素,所以(由欧拉定理:2^Φ(q')≡1(mod q') )此方程必有解,且Φ(q')为方程的一个解。但不一定是最小解。

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;

typedef long long LL;
int a[10000];

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;
}

int gcd(int a, int b){
    return b ? gcd(b, a % b) : a ;
}

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 main(){
    int p, q;
    int kase = 0;
    while(scanf("%d/%d", &p, &q) != EOF){
        int g = gcd(p, q);
        p /= g;
        q /= g;
        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;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值