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互素,则$q|2^x*(2^y-1)$

显然,x的最小值即为q中素因子2的幂;而$q=q<<x$后,$2^y\equiv1\ mod\ q$,y的一个值即为$\varphi(q)$,y的最小值为$\varphi(q)$的一个因数。

注意

  • 先化简分数!
  • $\varphi(q)$不一定是最小的y!
  • 数据很大,可用快速积和快速幂边算边模。

代码

 1 #include<cstring>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<algorithm>
 5
 6 using namespace std;
 7
 8 const int Ma=1000006;
 9 long long q,p;
10
11 long long phi(long long n)
12 {
13     long long ret=n;
14     for(int i=2;i*i<=n;i++)
15     {
16         if(n%i==0)
17         {
18             while(n%i==0)
19             {
20                 ret-=ret/i;
21                 while(n%i==0)n/=i;
22             }
23         }
24     }
25     if(n>1)ret=ret-ret/n;
26     return ret;
27 }
28
29 long long gcd(long long x,long long y)
30 {
31     if(y==0)return x;
32     else return gcd(y,x%y);
33 }
34
35 long long qmulti(long long a,long long b,long long mo)
36 {
37     long long ret=0;
38     while(b)
39     {
40         if(b&1)ret=(ret+a)%mo;
41         b>>=1;
42         a=(a+a)%mo;
43     }
44     return ret;
45 }
46
47 long long qpow(long long a,long long b,long long mo)
48 {
49     long long ret=1;
50     while(b)
51     {
52         if(b&1)ret=qmulti(ret,a,mo)%mo;
53         a=qmulti(a,a,mo)%mo;
54         b>>=1;
55     }
56     return ret%mo;
57 }
58
59 int main()
60 {
61     int cnt=0;
62     while(scanf("%lld/%lld",&p,&q)!=EOF)
63     {
64         cnt++;
65         if(p==0)
66         {
67             printf("Case #%d: 1,1 \n",cnt);
68             continue;
69         }
70         long long g=gcd(p,q);
71         p/=g;q/=g;
72         long long x=1,y;
73         while(!(q&1))q>>=1,x++;
74         y=phi(q);
75         long long t=y;
76         for(long long i=2;i<=t/i;i++)
77         {
78             while(y%i==0&&(qpow(2,y/i,q)==1))
79             {
80                 y/=i;
81             }
82         }
83         printf("Case #%d: %lld,%lld \n",cnt,x,y);
84     }
85     return 0;
86 }

原文地址:https://www.cnblogs.com/LiqgNonqfu/p/10807141.html

时间: 2024-08-10 01:13:40

poj3358 Period of an Infimite Bimary Expansion的相关文章

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] <<

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

See the world from the eyes of shell

In this chapter we are going to look at some of the "magic" that occurs on the command line when you press the enter key.While we will examine several interesting and complex features of the shell,we will do it with just one new command: echo -

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