同余&欧拉函数

同余:

简单地说,若有两个整数x,y且x%k==y%k,那么我们记x\equivy(mod k)

同余运算满足以下性质:

1,传递性:若x\equivy(mod k),且y\equivz(mod k),则x\equivz(mod k)

2.相加相乘性:即同余式两边可同时加或乘一个整数


剩余系:

即%k后均不相同的整数组成的数集,如(10,1,22,38,44),它们在%5后分别为(0,1,2,3,4)各不相我们说(10,1,22,38,44)是5的一个剩余系,而(0,1,2,3,4)就是5的标准剩余系

欧拉函数


欧拉函数(\varphi(x)):

 \varphi(x)==比x小且与x互质的正整数的个数;

若x==a_{1}p_{1}^{n_1}*a_{2}p_{2}^{n_2}*...........*a_{i}p_{i}^{n_{i}}(p为x的质因数),则 \varphi(x)==x*(1-p_{1})*(1-p_{2})*.........*(1-p_{i})


欧拉定理: 

若gcd(a,p)==1,则a^{\phi p}==1(mod p)

 


拓展欧拉定理:
 

若b>,则


例题:

Period of an Infinite Binary Expansion:

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, ≥ 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

Inputcopy Outputcopy
1/10 
1/5 
101/120 
121/1472
Case #1: 2,4 
Case #2: 1,4 
Case #3: 4,4 
Case #4: 7,11

题解:

#include
#include
#include
using namespace std;
typedef long long ll;
ll p,q;
ll o;
char c;
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
ll oula(ll q){
	ll s=q;
	for(ll i=2;i*i<=q;i++){
		if(q%i==0){
			s/=i,s*=(i-1);
			while(q%i==0)q/=i;
		}
	}
	if(q>1)s/=q,s*=(q-1);
	return s;
}
ll fpow(ll base,ll power){
	ll res=1;
	while(power>0){
		if(power&1)res=res*base%q;
		power>>=1;
		base=(base*base)%q;
	}
	return res;
}
int main () {
	ll u=0;
	while(~scanf("%lld/%lld",&p,&q)){
		ll g=gcd(q,p);
		ll ans=q;
		p/=g,q/=g; 
		ll cnt=1;
		while(q%2==0){
			q/=2;
			cnt++;
		}
		ll w=oula(q);
		for(int i=1;i*i<=w;i++){
			if(w%i==0){
				ll z=i;
				ll v=w/i;
				if(fpow(2,z)==1)ans=min(ans,z);
				if(fpow(2,v)==1)ans=min(ans,v);
			}
		}
		u++;
		printf("Case #%lld: %lld,%lld\n",u,cnt,ans);
	}
}

你可能感兴趣的:(线性代数)