POJ 3358Period of an Infinite Binary Expansion

来源:互联网 发布:东北大学网络教育 编辑:程序博客网 时间:2024/05/30 22:45

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

求二进制小数的循环节和起始位置,二进制小数的话一定是每次乘上2的

所以先约分,然后把分母转成二进制,右边0的数量就是起始位置。

然后就是欧拉函数求循环节了。

#include<set>#include<map>#include<ctime>#include<cmath>#include<stack>#include<queue>#include<bitset>#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<algorithm>#include<functional>#define rep(i,j,k) for (int i = j; i <= k; i++)#define per(i,j,k) for (int i = j; i >= k; i--)#define loop(i,j,k) for (int i = j;i != -1; i = k[i])#define lson x << 1, l, mid#define rson x << 1 | 1, mid + 1, r#define ff first#define ss second#define mp(i,j) make_pair(i,j)#define pb push_back#define pii pair<int,LL>#define in(x) scanf("%d", &x);using namespace std;typedef long long LL;const int low(int x) { return x&-x; }const double eps = 1e-4;const int INF = 0x7FFFFFFF;const int mod = 1e9 + 7;const int N = 1e7 + 10;int n, m;int x, y, z, cas = 0;int phi(int x){int res = 1;for (int i = 2; i*i <= x; i++){if (x%i) continue;res *= i - 1;for (x /= i; !(x%i); res *= i, x /= i);}return res * max(x - 1, 1);}bool check(int x){int res = 1, now = 2;for (; x; x >>= 1){if (x & 1) res = 1LL * res* now % m;now = 1LL * now * now % m;}return res == 1;}int gcd(int x, int y) { return x%y ? gcd(y, x%y) : y; }int main(){while (scanf("%d/%d", &n, &m) != EOF){int g = gcd(n, m);n /= g; m /= g;for (x = 1; m % 2 == 0; x++, m >>= 1);z = y = phi(m);for (int i = 1; i * i <= z; i++){if (z % i) continue;if (check(i)) y = min(y, i);if (check(z / i)) y = min(y, z / i);}printf("Case #%d: %d,%d\n", ++cas, x, y);}return 0;}


0 0
原创粉丝点击