</pre><pre name="code" class="cpp">/*
《数论及应用》P168
*/
#pragma warning(disable:4786)
#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cmath>
#include<string>
#include<sstream>
#define LL long long
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define lson l,m,x<<1
#define rson m+1,r,x<<1|1
using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const double eps=1e-6;
LL gcd(LL a , LL b)
{
return b == 0? a : gcd( b , a % b);
}
int phi(int n)
{
int ret = n;
for(int i = 2 ; i * i <= n ; i++){
if( n % i == 0){
ret = ret / i * (i - 1);
while(n % i == 0){
n /= i;
}
}
}
if(n > 1) ret = ret / n * (n - 1);
return ret;
}
LL quick_mod(LL x , LL n , LL mod)
{
if(n == 0) return 1;
LL res = quick_mod(x * x % mod , n / 2 , mod);
if(n&1) res = res * x % mod ;
return res;
}
int main()
{
LL p , q;
int kase = 0;
while(scanf("%lld/%lld",&p,&q) != EOF ){
LL d = gcd( p , q );
p /= d ; q /= d ;
LL ans1= 0 , ans2 = INF;
while(q % 2 == 0){
++ans1 ;
q /= 2;
}
LL cur = phi( q ); //phi(q)一定为2^x = 1的一个根,但不一定是最小的根
ans2 = cur;
for(LL i = 2 ; i * i <= cur ; i++){ //最小根一定是phi(q)的一个因子,现寻找这个因子
if(cur % i == 0){
LL res = quick_mod( (LL) 2 , i , q);
if(res == 1)
ans2 = min(ans2 , i);
res = quick_mod( (LL) 2 , cur / i , q );
if(res == 1)
ans2 = min(ans2 , cur / i);
}
}
printf("Case #%d: %lld,%lld\n",++kase , ans1+1 , ans2);
}
return 0;
}
POJ3358 Period of an Infinite Binary Expansion
最新推荐文章于 2019-09-12 21:15:25 发布