同余&欧拉函数

同余:

简单地说,若有两个整数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>\phi{p},则a^{b}==a^(b%\phi{p}+\phi{p}) (mod p)


例题:

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

InputcopyOutputcopy
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<iostream>
#include<algorithm>
#include<cmath>
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);
	}
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值