POJ 3358- Period of an Infinite Binary Expansion(欧拉函数+欧拉定理)

Period of an Infinite Binary Expansion
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Appoint description: 

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, ≥ 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

题意:求一个分数的2进制小数的循环节长度和起始位置。

思路:题解地址------->点击打开链接。这道题RE了一上午,差点郁闷死,然后后来把求欧拉的函数从筛选法换成直接法的时候就AC了,到现在也没搞懂为啥筛选法不行。

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const double pi= acos(-1.0);
LL y[10010];
LL cnt;
int gcd(LL a,LL b)//求最大公约数
{
    while(b!=0){
        int r=b;
        b=a%b;
        a=r;
    }
    return a;
}
LL modexp(LL a,LL b,LL mod)//快速幂取模
{
    LL res=1;
    while(b>0){
        a=a%mod;
        if(b&1){
            res=res*a%mod;
        }
        b=b>>1;
        a=a*a%mod;
    }
    return res;
}

LL Euler(LL n)//直接法求欧拉函数
{
    LL m=floor(sqrt(n+0.5));
    LL ans=n;
    for(LL i=2;i<=m;i++){
        if(n%i==0){
            ans=ans/i*(i-1);
            while(n%i==0){
                n/=i;
            }
        }
    }
    if(n>1)
        ans=ans/n*(n-1);
    return ans;
}

void fac(LL x)//求约数
{
    cnt=0;
    LL m=sqrt(x+0.5);
    for(LL i=1;i<=m;i++){
        if(x%i==0){
            y[cnt++]=i;
            y[cnt++]=x/i;
        }
    }
}

int main()
{
    LL a,b,i,j,k;
    LL icase=1;
    LL d,t,q;
    LL res;
    while(~scanf("%lld/%lld",&a,&b)){
        printf("Case #%lld: ",icase++);
        if(a==0){
            printf("1,1\n");
            continue;
        }
        d=gcd(a,b);
        a=a/d;
        b=b/d;
        t=1;
        while(b%2==0){
            b=b>>1;
            t++;
        }
        LL xx=Euler(b);
        fac(xx);
        sort(y,y+cnt);
        for(i=0;i<cnt;i++){
            if(modexp(2,y[i],b)==1){
                res=y[i];
                break;
            }
        }
        printf("%lld,%lld\n",t,res);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rocky0429

一块也是爱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值