码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-04-11 13:18:57      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

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


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

标签:

原文地址:http://blog.csdn.net/u013486414/article/details/44994349

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!