POJ3358

Description
POJ3358
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/10:化成二进制小数为0.0(0011)w以0011循环从第二位开始循环。
解:基本原理这里写图片描述

比如1/10,用乘二法,2/10,4/10,8/10,6/10,2/10
其中1*2^1=1*2^5(mod10)
用p代表分子,q代表分母,i代表一个位置,j代表一个位置,比如在1/10中,i为1,j为5,一下的p和q均为最简式
那么我们要求的就是
p*2^i=p*2^j(modq)
化简得2^(j-i)=1(modq)
观察得q能被2整除多少次,i就是多少,而且i就是循环开始位置的前一位,然后将q不断除以2直到不能整除
求出i后,另k为j-i
那么所求为
2^k=1(modq)
下面就是求K的过程
AC代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<bitset>
#include<iomanip>
using namespace std;
int t,n,m,GCD,phi,ans1,ans2;
int temp,num;
int fac[1000000];
int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}

int euler(int n)///欧拉函数
{
    int ret=1,i;
    for(i=2; i*i<=n; i++)
    {
        if(n%i==0)
        {
            n/=i,ret*=i-1;
            while(n%i==0)
            {
                n/=i,ret*=i;
            }
        }
    }
    if(n>1)
    {
        ret*=n-1;
    }
    return ret;
}
int Pow(int a,int b,int c)///快速幂
{
    int ans=1;
    while(b>0)
    {
        if(b&1)
        {
            ans=(long long)ans*a%c;
        }
        b>>=1;
        a=(long long)a*a%c;
    }
    return ans;
}

int main()
{
    int Case=1;
    while(scanf("%d/%d",&n,&m)!=EOF)
    {
        GCD=gcd(n,m);
        n/=GCD;///使m,n最大公约数为1
        m/=GCD;
        t=0;
        while(m%2==0)
        {
            t++;
            m/=2;
        }
        ans1=t+1;///求出循环的起始位置
        phi=euler(m);
        if(phi==1)
        {
            ans2=1;
        }
        else
        {
            int num=0;
            for(int i=1; i*i<=phi; ++i)///求欧拉函数的因子
            {
                if(phi%i==0)
                {
                    fac[num++]=i;
                    fac[num++]=phi/i;
                }
            }
            sort(fac,fac+num);
            for(int i=0; i<num; ++i)///找出最小的的因子使等式成立
            {
                temp=Pow(2,fac[i],m);
                if(temp==1)
                {
                    ans2=fac[i];
                    break;
                }
            }
        }
        printf("Case #%d: %d,%d\n",Case++,ans1,ans2);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值