POJ 3358 Period of an Infinite Binary Expansion(欧拉定理)

Period of an Infinite Binary Expansion
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3646 Accepted: 1021

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

Source

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top


题意:给你一个分数,问你该分数的小数部分的二进制表示的循环节开始的位置和循环节的长度。

题解:我们首先将该分数化为最简分数,然后考虑这样一个问题,对于这个分数的小数部分我们直接求肯定是不好求的。

我们知道一个数的小数部分的二进制其实就是舍弃整数位一直让小数部分乘2,看每一次乘2后的整数部分的数是1还是0.

如果是1的话,记下继续舍弃乘2。。。这样我们就能表示出来了。我们设p和q化简后为p1和q1.

假如我们从第i次开始乘到第j次出现了循环节,呢么存在这样一个等式:p1*2^i=p1*2^j(mod q1)

我们考虑移项,化简为:p1*2^i*(2^(j-i)-1)=0 (mod q1)..此时说明q1中一定存在2的若干次方。

呢么我们可以把这一部分给去掉。也就是让q1一直除以2直到不能除尽为止,呢此时的位置其实也就是循环节开始的地方了。

又因为p1和q1是互质的,其实问题便已经转化为求2^x-1=1 (mod q1)中的x并且让x最小。

这式子是不是很熟悉? 没错,这不就是欧拉定理的等式吗。其中x一定是q1的欧拉函数的因数。。。

呢么我们剩下直接暴力找phi(q1)的因数就好啦。。。

#include<set>      
#include<map>         
#include<stack>                
#include<queue>                
#include<vector>        
#include<string>     
#include<time.h>    
#include<math.h>                
#include<stdio.h>                
#include<iostream>                
#include<string.h>                
#include<stdlib.h>        
#include<algorithm>       
#include<functional>        
using namespace std;                
#define ll long long          
#define inf 1000000000           
#define mod 1000000007                
#define maxn  50500    
#define lowbit(x) (x&-x)                
#define eps 1e-9   
ll gcd(ll x,ll y)
{
	if(y==0)
		return x;
	return gcd(y,x%y);
}
ll eular(ll x)
{
	ll ans=x;
	for(ll i=2;i*i<=x;i++)
	{
		if(x%i!=0)
			continue;
		ans=ans-ans/i;
		while(x%i==0)
			x/=i;
	}
	if(x>1)
		ans=ans-ans/x;
	return ans;
}
ll q1(ll x,ll y,ll Mod)
{
	ll res=1;
	while(y)
	{
		if(y%2)
			res=res*x%Mod;
		x=x*x%Mod;
		y/=2;
	}
	return res;
}
int main(void)
{
	ll p,q,i,cases=0;
	while(scanf("%lld/%lld",&p,&q)!=EOF)
	{
		ll x=gcd(p,q),cnt=0;
		p/=x;q/=x;
		while(q%2==0)
			q/=2,cnt++;
		x=eular(q);
		ll ans=0;
		for(i=1;i*i<=x;i++)
		{
			if(x%i==0 && q1(2,i,q)==1)
			{
				ans=i;
				break;
			}
			if(x%i==0 && q1(2,x/i,q)==1)
				ans=x/i;
		}
		printf("Case #%lld: %lld,%lld\n",++cases,cnt+1,ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值