poj_3358_Period of an Infinite Binary Expansion
2013-08-20 15:54
323 查看
Period of an Infinite Binary Expansion
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
Sample Output
Source
Manila 2006
题型:数论(欧拉函数+费马小定理)
题意:求分数的二进制的循环节的起始位置和循环长度。
分析:
可以观察一下1/10这组数据,按照二进制转换法(乘二法就是*2取整),可以得到:
1/10 2/10 4/10 8/10 16/10 32/10 ...
然后都分子都尽可能减去10,得到:
1/10 2/10 4/10 8/10 6/10 2/10 ...
这时候,发现出现了重复,那么这个重复就是要求的最小循环。
首先处理一下分数,即将p/q化简,p=gcd(p,q) , q=gcd(p,q)。
然后将q不断除2知道不能除尽为止,除的次数+1就是循环节的起始位置。
下一步就是求2^x = 1 (mod q)的最小的x,如何求呢?
枚举phi(q)的因子,判断2^x = 1 (mod q),取最小的x。
代码:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2564 | Accepted: 673 |
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
Source
Manila 2006
题型:数论(欧拉函数+费马小定理)
题意:求分数的二进制的循环节的起始位置和循环长度。
分析:
可以观察一下1/10这组数据,按照二进制转换法(乘二法就是*2取整),可以得到:
1/10 2/10 4/10 8/10 16/10 32/10 ...
然后都分子都尽可能减去10,得到:
1/10 2/10 4/10 8/10 6/10 2/10 ...
这时候,发现出现了重复,那么这个重复就是要求的最小循环。
首先处理一下分数,即将p/q化简,p=gcd(p,q) , q=gcd(p,q)。
然后将q不断除2知道不能除尽为止,除的次数+1就是循环节的起始位置。
下一步就是求2^x = 1 (mod q)的最小的x,如何求呢?
枚举phi(q)的因子,判断2^x = 1 (mod q),取最小的x。
代码:
#include<iostream> #include<cstring> #include<cstdio> #include<cmath> #define LL long long using namespace std; LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; } //计算 (a*b)%c. a,b都是long long的数,直接相乘可能溢出的 // a,b,c <2^63 long long mult_mod(long long a,long long b,long long c) { a%=c; b%=c; long long ret=0; while(b) { if(b&1){ret+=a;ret%=c;} a<<=1; if(a>=c)a%=c; b>>=1; } return ret; } //计算 x^n %c long long pow_mod(long long x,long long n,long long mod)//x^n%c { if(n==1)return x%mod; x%=mod; long long tmp=x; long long ret=1; while(n) { if(n&1) ret=mult_mod(ret,tmp,mod); tmp=mult_mod(tmp,tmp,mod); n>>=1; } return ret; } LL phi(LL n){ LL ret = n; for(LL i=2;i*i<=n;i+=(i==2)?1:2){ if(!(n%i)){ ret=ret/i*(i-1); while(n%i==0) n/=i; } } if(n>1) ret=ret/n*(n-1); return ret; } int main(){ //getphi(); LL p,q,time=1; while(~scanf("%I64d/%I64d",&p,&q)){ LL temp=gcd(p,q); p/=temp; q/=temp; LL i=1; while(q%2==0){ q/=2; i++; } LL a=phi(q); LL j=-1; LL k; for(k=2;k*k<=a;k++){ if(a%k==0){ if(pow_mod(2,k,q)==1){ if(j==-1){ j=k; } else if(j>k){ j=k; } break; } else{ LL temp=a/k; if(pow_mod(2,temp,q)==1){ if(j==-1){ j=temp; } else if(j>temp){ j=temp; } } } } } if(j==-1) j=a; printf("Case #%I64d: %I64d,%I64d\n",time,i,j); time++; } return 0; }
相关文章推荐
- POJ3358 Period of an Infinite Binary Expansion【欧拉函数】
- POJ 3358 Period of an Infinite Binary Expansion 解题报告(欧拉函数+因式分解)
- POJ 3358 Period of an Infinite Binary Expansion ★ (数论好题:欧拉函数)
- POJ 3358 Period of an Infinite Binary Expansion ★ (数论好题:欧拉函数)
- poj 3358 Period of an Infinite Binary Expansion(欧拉函数+快速幂取模)
- POJ-3358-Period of an Infinite Binary Expansion 解题报告
- POJ 3358 Period of an Infinite Binary Expansion
- POJ 3358- Period of an Infinite Binary Expansion(欧拉函数+欧拉定理)
- POJ 3358 Period of an Infinite Binary Expansion
- poj3358 Period of an Infinite Binary Expansion 数论有难度
- Period of an Infinite Binary Expansion POJ - 3358 欧拉函数
- poj 3358 Period of an Infinite Binary Expansion (费马小定理+分数化二进制小数)
- poj 3358 Period of an Infinite Binary Expansion 欧拉定理
- POJ 3358 Period of an Infinite Binary Expansion 欧拉函数+欧拉定理
- poj 3358 Period of an Infinite Binary Expansion
- poj 3358——Period of an Infinite Binary Expansion
- poj 3358 Period of an Infinite Binary Expansion——欧拉函数+费马小定理
- POJ 3358 Period of an Infinite Binary Expansion(欧拉定理)
- poj 3358 Period of an Infinite Binary Expansion(欧拉函数+欧拉定理)
- POJ 3358 Period of an Infinite Binary Expansion