POJ 3358 Period of an Infinite Binary Expansion

传送门:http://poj.org/problem?id=3358

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

题意

  给出一个分数,二进制形式的最小循环节b,和循环节开始的位置a。

思路

  小数p/q,二进制的转换方式就是不断 * 2,mod分母。
以1/10为例:

位置0123456789
* 21/102/104/108/1016/1012/104/108/1016/1012/10
mod 分母1/102/104/108/106/102/104/108/106/102/10
二进制0.000110011

  我们可以看出,循环节开始的位置是2(a=2),最小循环节是4(b=4)。
  从 i 位置到 j 位置循环节的分子表示为:p * 2i = p * 2j (mod q)(i<j)
  也就是:2j - 2i = 0 (mod q),
  2i * (2j-i - 1) = 0 (mod q)。可以看出 a = i,b = j - i;

步骤

  1. 保证gcd(p,q) == 1;
  2. 如果q%2 == 0,便依次 /= 2,知道 q %2 == 1,求出 a ,q′;
  3. 这时也就是 2j-i ≡ 1 ( mod q′) ,b = j - i ∈ φ(q′),也就是φ(q′)的因数,爆找一下……

PS: POJ的数据有点水,在POJ过了的建议去,https://vjudge.net/problem/UVALive-3172,交一下(这个也有漏洞,就是数据比较小):

  1. POJ 缺少当p==0时,即分数值为0时,应该输出 1,1的情况。
  2. POJ 缺少当找不到循环节的时候,应该输出1,列如:1/4 = (0.010000000)2,应该输出3,1……
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<ctime>
#define ll long long
#define ld long double
#define ull unsigned long long
using namespace std;
const int maxn = 1000010;
const int inf = 0x3f3f3f3f3f;
const ll lnf = 0x3f3f3f3f3f3f3f;
const ll mod =  1000000007;
ll pp[maxn];
int tot;
ll gcd(ll a,ll b)
{
    return b==0 ? a:gcd(b,a%b);
}
ll mul(ll a,ll b,ll m)
{
    ll sm = (ld)a/m * b;
    return ((ull)a*b-(ull)sm*m+m)%m;
}
ll ksm(ll a,ll b,ll m)
{
    ll ans = 1;
    for(ll i=b;i;i>>=1){
        if(i&1) ans = mul(ans,a,m);
        a = mul(a,a,m);
    }
    return ans;
}
ll phi(ll n)
{
    ll res = 1;
    for(ll i=2;i*i<=n;i++){
        if(n%i==0){
            res *= (i-1);
            n /= i;
            while(n%i==0){
                n /= i;
                res *= i;
            }
        }
    }
    if(n>1) res *= (n-1);
    return res;
}
void fac(ll n)
{
    tot = 0;
    for(ll i=1;i*i<=n;i++){
        if(n%i==0){
            pp[++tot] = i;
            pp[++tot] = n/i;
        }
    }
    sort(pp+1,pp+tot+1);
}
int main(void)
{
    int t = 0;
    ll q,p,a,b;
    while(~scanf("%lld/%lld",&p,&q)){
        if(p==0){	//POJ缺少1
           printf("Case #%d: 1,1\n",++t);
           continue;
        }
        
        //保证gcd(p,q) == 1
        ll m = gcd(p,q);
        p /= m;q /= m;
		
		//求解 a
        a = 1;
        while(q%2==0){
            q >>= 1;
            a++;
        }
		
		//求解 b 
        b = lnf;
        ll ans = phi(q);	//q的欧拉函数
        fac(ans);	//q的欧拉函数的因子
        for(int i=1;i<=tot;i++){
            if(ksm(2,pp[i],q)==1){	//找到符合题意的,标记,跳出
                b = pp[i];
                break;
            }
        }
        //b找到就输出b,找不到就输出1(循环节内的元素是0,也就是0.001(0))
        printf("Case #%d: %lld,%lld\n",++t,a,b==lnf?1:b);
    }	
    return 0;
}

1.如果给出的数据比较小的话:直接爆找就行了,相同分子的第二次出现的位置,就行了
序号0123456789
* 21/102/104/108/1016/1012/104/108/1016/1012/10
mod 分母1/102/104/108/106/102/104/108/106/102/10
位置0123456789

我们知道分子为2时,第一次出现是在1,第二次出现是在5,那么说明他的循环节起点是2,长度就是4。

memset(ha,-1,sizeof(ha));
int a,b;
for(int i=1;1;i++){
	if(ha[p]+1){	//分子p是否出现过一次
		a = ha[p];	//循环节开始的位置
		b = i - ha[p];	//最小循环节
		break;
	}
	ha[p] = i;	//出现一次
	p *= 2;p %= q;	
}

UVALive AC,POJ RE:

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<ctime>
#define ll long long
#define ld long double
#define ull unsigned long long
using namespace std;
const int maxn = 1000010;	//给出的数据<=maxn
const int inf = 0x3f3f3f3f3f;
const ll lnf = 0x3f3f3f3f3f3f3f;
const ll mod =  1000000007;
int ha[maxn];
int gcd(int a,int b)
{
    return b==0 ? a:gcd(b,a%b);
}
int main(void)
{
    int t=0;
    int p,q;
    while(~scanf("%d/%d",&p,&q)){
        int g = gcd(p,q);
        p /= g;q /= g;
        memset(ha,-1,sizeof(ha));
        int a,b;
        for(int i=1;1;i++){
            if(ha[p]+1){
                a = ha[p];
                b = i - ha[p];
                break;
            }
            ha[p] = i;
            p *= 2;p %= q;
        }
        printf("Case #%d: %d,%d\n",++t,a,b);
    }
    return 0;
}

2.如果时间给的是3s的话,可以直接用map,找:

UVALive AC ,POJ TL:

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<ctime>
#define ll long long
#define ld long double
#define ull unsigned long long
using namespace std;
const int maxn = 1000010;
const int inf = 0x3f3f3f3f3f;
const ll lnf = 0x3f3f3f3f3f3f3f;
const ll mod =  1000000007;
map<ll,int>ha;
ll gcd(ll a,ll b)
{
    return b==0 ? a:gcd(b,a%b);
}
int main(void)
{
    int t=0;
    ll p,q;
    while(~scanf("%lld/%lld",&p,&q)){
        ll g = gcd(p,q);
        p /= g;q /= g;
        int tot = 0;
        ha.clear();
        while(ha.find(p)==ha.end()){
            ha[p] = ++tot;
            p *= 2;p %= q;
        }
        printf("Case #%d: %d,%d\n",++t,ha[p],tot-ha[p]+1);
    }
    return 0;
}

C++11版的编译器,可以用unordered_map优化一下 :
UVALive AC 913ms,POJ CE:

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<tr1/unordered_map>
#include<ctime>
#define ll long long
#define ld long double
#define ull unsigned long long
using namespace std;
using namespace std::tr1;
const int maxn = 1000010;
const int inf = 0x3f3f3f3f3f;
const ll lnf = 0x3f3f3f3f3f3f3f;
const ll mod =  1000000007;
ll gcd(ll a,ll b)
{
    return b==0 ? a:gcd(b,a%b);
}
unordered_map<ll, int> ha;
int main(void)
{
    int t=0;
    ll p,q;
    while(~scanf("%lld/%lld",&p,&q)){
        ha.clear();
        ll g = gcd(p,q);
        p /= g;q /= g;
        int tot = 0;
        while(ha.find(p)==ha.end()){
            ha[p] = ++tot;
            p *= 2;p %= q;
        }
        printf("Case #%d: %d,%d\n",++t,ha[p],tot-ha[p]+1);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

逃夭丶

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值