poj3358 Period of an Infinite Binary Expansion

首先要明确分式的二进制表达方式:

1 //supposing the fraction is a / b, (a < b && (a, b))
2 //its binary expression is denoted as : 0.bit[1]bit[2]...
3 seed[0] = a;4 for(int i = 1; ; i++){
5     bit[i] = (seed[i - 1] << 1) / b;
6     seed[i] = (seed[i - 1] << 1) % b;7 }

不妨设其中一个循环节为bit[r]..bit[s],显然有seed[r - 1] = seed[s] 且bit[r] = bit[s + 1],其中循环节长度l = (s - r + 1)。

由seed[i] = 2i+1 % b, 则有 2r  % b = 2s+1 % b,  即2r(2s-r+1-1) ≡ 0(modb),

记b = 2t*b1,其中 (b1,2),且令r = t,则 2l≡ 1(modb1)。

则l|φ(b1),由此枚举b1的约数即可得到周期l。

并且有初始位置p = t + 1 。

http://poj.org/problem?id=3358

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <cmath>
 5 using namespace std;
 6 typedef __int64 LL;
 7 const int maxn = 1e2 + 10;
 8 int prime[maxn], k;
 9 int fac[maxn];
10 int a, b, B;
11
12 int power(int a, int p){
13     a %= B;
14     int ans = 1;
15     while(p){
16         if(p & 1) ans = (LL)ans * a % B;
17         p >>= 1;
18         a = (LL)a * a % B;
19     }
20     return ans;
21 }
22
23 int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
24
25 void solve(){
26     a %= b;
27     int d = gcd(a, b);
28     a /= d, b /= d;
29     int t = 0;
30     while(b % 2 == 0) b /= 2, ++t;
31     printf("%d,", ++t);
32     if(b == 1) { printf("%d\n",1); return; }
33     k = 0;
34     B = b;
35     int phi = b;
36     int mid = (int)sqrt(b);
37     for(int i = 3; i <= mid; i += 2){
38         if(b % i == 0){
39             prime[k++] = i;
40             while(b % i == 0) b /= i;
41          }
42     }
43     if(b != 1) prime[k++] = b;
44     for(int i = 0; i < k; i++) phi /= prime[i];
45     for(int i = 0; i < k; i++) phi *= (prime[i] - 1);
46     k = 0;
47     for(int i = 1; i * i <= phi; i++){
48         if(phi % i == 0) fac[k++] = i, fac[k++] = phi / i;
49     }
50     sort(fac, fac + k);
51     for(int i = 0; i < k; i++) if(power(2, fac[i]) == 1){
52         printf("%d\n", fac[i]);
53         break;
54     }
55 }
56
57 int main(){
58     //freopen("in.txt", "r", stdin);
59     int kase = 0;
60     while(~scanf("%d/%d", &a, &b)){
61         printf("Case #%d: ", ++kase);
62         solve();
63     }
64     return 0;
65 }

时间: 2024-08-01 16:44:19

poj3358 Period of an Infinite Binary Expansion的相关文章

POJ 3358 Period of an Infinite Binary Expansion( 数论好题 + 欧拉定理 + 欧拉函数 )

POJ 3358 Period of an Infinite Binary Expansion( 数论好题 + 欧拉定理 + 欧拉函数 ) #include <cstdio> #include <cstring> #include <algorithm> #include <algorithm> using namespace std; typedef long long LL; LL fac[ 100000 ], pf; LL gcd( LL a, LL

Period of an Infinite Binary Expansion POJ - 3358(欧拉函数)

Period of an Infinite Binary Expansion POJ - 3358 题意:给一个分数,让求其二进制形式的最小循环节ans2和循环节开始的位置ans1. 以下内容转自http://blog.csdn.net/u013508213/article/details/42496543 小数二进制的转换方法就是不断*2,mod 分母. 比如:1/10  2/10  4/10  8/10  16/10  32/10... 模后:1/10  2/10  4/10  8/10  

POJ 3358- Period of an Infinite Binary Expansion(欧拉函数+欧拉定理)

Period of an Infinite Binary Expansion Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3358 Appoint description:  System Crawler  (2015-04-08) Description Let {x} = 0.a1a2a3... be the binary rep

poj3358 Period of an Infimite Bimary Expansion

题目大意 链接 把分数转化为二进制小数,找出二进制小数的最小循环节长度以及开始位置. 思路 有理数n的第k位小数应为(n*2^k)mod\ 2 这里用分数p/q表示,则第 k 位小数位$(\frac{p}{q}*2^k)mod\ 2=(p*2^k)/q mod 2$,同时后k位小数为$(p*2^k )mod\ q$:若周期开始位置位x,长度为y,则$ (p*2^x\ mod\ q\equiv(p*2^{(x+y)} \ mod\ q$.y) 那么有 $q|p*2^x*(2^y-1)$ 由于p.q

数论poj题目

http://blog.sina.com.cn/s/blog_76f6777d0101ir50.html 1.素数,整数分解,欧拉函数 素数是可能数论里最永恒,最经典的问题了.素数的判断,筛法求素数,大素数的判断···还有很多其他问题都会用到素数. *最水最水的:(心情不爽时用来解闷吧) pku1365 Prime Land pku2034 Anti-prime Sequences pku2739 Sum of Consecutive Prime Numbers pku3518 Prime Ga

[转] POJ数学问题

转自:http://blog.sina.com.cn/s/blog_6635898a0100magq.html 1.burnside定理,polya计数法 这个大家可以看brudildi的<组合数学>,那本书的这一章写的很详细也很容易理解.最好能完全看懂了,理解了再去做题,不要只记个公式. *简单题:(直接用套公式就可以了) pku2409 Let it Bead      http://acm.pku.edu.cn/JudgeOnline/problem?id=2409 pku2154 Co

『转』数学专辑

1.burnside定理,polya计数法 这个大家可以看brudildi的<组合数学>,那本书的这一章写的很详细也很容易理解.最好能完全看懂了,理解了再去做题,不要只记个公式. *简单题:(直接用套公式就可以了) pku2409 Let it Bead   http://acm.pku.edu.cn/JudgeOnline/problem?id=2409 pku2154 Color http://acm.pku.edu.cn/JudgeOnline/problem?id=2154 pku12

ACM数学(转)

从放暑假前周sir给我讲了一个用polya计数法和burnside定理做的题目(pku2409)后,突然觉得组合数学挺有意思,然后从那时起到现在几乎都在做这类的题目. 做到现在感觉这类题目的一些基本知识点都差不多有所了解了,水题也刷了不少,但还有很多难题自己实在是做不动,所以准备把这类题目先放一放,然后把前段时间做的水题整理一下(供以后的初学者参考,大牛就不要看了哈,都是水题).剩下的比较难的题目就慢慢来吧,以后做出来再不上,这个小结会不断地更新.也希望大家有好的题目可以推荐一下,分享一下哈.

POJ【数论/组合/博弈论】

 POJ[数论/组合/博弈论]题目列表 POJ[数论/组合/博弈论]题目列表 原来的列表比较水,今天换了一个难一些的列表,重新开始做~ 红色的代表已经AC过,蓝色的代表做了但是还没过.这句话貌似在我空间里的每份列表里都有额. 博弈论 POJ 2234 Matches Game POJ 2975 Nim POJ 2505 A multiplication game POJ 1067 取石子游戏 POJ 2484 A Funny Game POJ 2425 A Chess Game POJ 29