POJ 3358

此题的主要还是如何把小数化作分数来解答。

设p/q。对于二进制(三进制,四进制一样),若p>q便商1,取mod,p*2-->p,然后再作p/q,若p<q,商0。

于是有,在去公约数GCD后,p/q的小数是否重复,和上述步骤p是否重复是一致的。若重复,得方程

p*2^i=p*2^j (mod q)。则有p*2^i(2^(j-i)-1)=0 (mod q)。即q|2^i(2^(j-i)-1)。而要求最小,只需使两者相等。即q中2的幂即为i。利用欧拉定理也可求出j-i的最小值。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#define LL __int64
using namespace std;

LL fac[10000]; int cnt;

LL gcd(LL a,LL b){
	if(b==0) return a;
	return gcd(b,a%b);
}

LL Euler(LL m){
	LL res=m;
	LL k=(LL)sqrt((double)m);
	for(LL i=2;i<=k;i++){
		if(m%i==0){
			res=res-res/i;
			while(m%i==0)
			m/=i;
		}
	}
	if(m>1)
	res=res-res/m;
	return res;
}

LL quick(LL a,LL b,LL m){
	LL ans=1;
	while(b){
		if(b&1){
			ans=(ans*a)%m;
		}
		b>>=1;
		a=(a*a)%m;
	}
	return ans;
}

int main(){
	LL p,q;
	int kase=0;
	while(scanf("%I64d/%I64d",&p,&q)!=EOF){
		p=p%q;
		LL g=gcd(p,q);
		p/=g; q/=g;
		LL ai=0,aj=0;
		while(q%2==0){
			ai++;
			q/=2;
		}
		LL phi=Euler(q);
		cnt=0;
		LL k=(LL)sqrt((double)phi);
		for(LL i=1;i<=k;i++){
			if(phi%i==0){
				fac[cnt++]=i;
				fac[cnt++]=phi/i;
			}
		}
		sort(fac,fac+cnt);
		for(int i=0;i<cnt;i++){
			LL ans=quick((LL)2,fac[i],q);
			if(ans==1){
				aj=fac[i];
				break;
			}
		}
		printf("Case #%d: %I64d,%I64d\n",++kase,ai+1,aj);
	}
	return 0;
}

  

时间: 2024-08-01 06:31:06

POJ 3358的相关文章

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 (欧拉定理)

题目:求 q/p 二进制小数的循环节,起点和长度. 若满足 2^phi[ n ] = 1 (mod n )   则 数 t = phi [ n ] 一定有一个使 2^k=1 (mod n )成立的 因子 k #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<vector> #define

数学之欧拉函数 &amp;几道poj欧拉题

欧拉函数总结+证明 欧拉函数总结2 POJ 1284 原根 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; int Euler(int n) { int res=n; for(int i=2;i*i<=n;i++) { while(n%i==0) { n/=i; res

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

POJ - 3186 Treats for the Cows (区间DP)

题目链接:http://poj.org/problem?id=3186 题意:给定一组序列,取n次,每次可以取序列最前面的数或最后面的数,第n次出来就乘n,然后求和的最大值. 题解:用dp[i][j]表示i~j区间和的最大值,然后根据这个状态可以从删前和删后转移过来,推出状态转移方程: dp[i][j]=max(dp[i+1][j]+value[i]*k,dp[i][j-1]+value[j]*k) 1 #include <iostream> 2 #include <algorithm&

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)

POJ——T2271 Guardian of Decency

http://poj.org/problem?id=2771 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5932   Accepted: 2463 Description Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he is

POJ——T2446 Chessboard

http://poj.org/problem?id=2446 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18560   Accepted: 5857 Description Alice and Bob often play games on chessboard. One day, Alice draws a board with size M * N. She wants Bob to use a lot of c