poj 3358 Period of an Infinite Binary Expansion(欧拉函数+快速幂取模)

本文介绍了一种算法,该算法能够确定小于1的正有理数的最小周期及其位置。通过分析二进制展开中的预周期和周期部分,程序能够准确找到周期的起始位及长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Language:
Period of an Infinite Binary Expansion
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3124 Accepted: 833

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


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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值