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, 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,4Case #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;}
- Period of an Infinite Binary Expansion POJ
- 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-3358Period of an Infinite Binary Expansion
- POJ 3358Period of an Infinite Binary Expansion
- poj 2462 Period of an Infinite Binary Expansion
- POJ_3358 Period of an Infinite Binary Expansion
- pku3358 Period of an Infinite Binary Expansion
- poj3358 Period of an Infinite Binary Expansion
- POJ3358 Period of an Infinite Binary Expansion
- poj3358 Period of an Infinite Binary Expansion
- 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(欧拉函数+欧拉定理)
- 【从C到C++学习笔记】引用/const引用/引用传递/引用作为函数返回值/引用和指针的区别
- go Hijack
- 计算机各层网络协议
- linux主机防御ids 常用开源工具
- CDMA PDU 编解码补充解析
- POJ 3358Period of an Infinite Binary Expansion
- SQL语句的MINUS,INTERSECT和UNION ALL
- Hbase禁用自带ZooKeeper,使用已经安装的ZooKeeper
- Axure8.0基础教程(1-10)AxureRP8实战手册
- 设计模式之七:适配器模式—不兼容结构的协调
- 设置状态栏字体颜色;设置导航栏返回按钮的title和返回按钮的颜色
- Android导入第三方jar包
- BootStrap简介
- 7.4.1 程序查询方式