同余&欧拉函数

同余:

简单地说,若有两个整数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
    评论
### 回答1: 欧拉函数也称为欧拉-费马函数或费马函数,用符号φ(n)表示,表示小于等于n且与n互质的正整数的个数。 以下是一个用MATLAB编写的求欧拉函数的代码示例: ```matlab function result = euler_function(n) result = n; % 初始化结果为n p = 2; % 设置初始的素数为2 % 计算欧拉函数 while p^2 <= n if mod(n, p) == 0 result = result * (1 - 1 / p); while mod(n,p) == 0 n = n / p; end end p = p + 1; end % 处理剩余的质因数 if n > 1 result = result * (1 - 1 / n); end end ``` 使用方法: ```matlab n = 12; % 输入要求解欧拉函数的数n result = euler_function(n); % 调用欧拉函数的方法 disp(result); % 输出结果 ``` 这个代码通过迭代计算n的所有质因数,并根据欧拉函数的公式进行计算,最终得到欧拉函数的值。 ### 回答2: 欧拉函数(Euler's totient function)是一个计算小于等于给定正整数n的所有与n互质的正整数的个数。以下是用Matlab实现欧拉函数的代码: ```matlab function euler = euler_function(n) euler = n; % 将结果初始化为n p = 2; % 从最小的素数2开始 while p * p <= n % 当p的平方大于n时结束循环 if mod(n, p) == 0 % 若n能整除p euler = euler - euler / p; % 将结果减去n除以p的值(贡献) while mod(n, p) == 0 % 如果n继续能整除p,则继续除以p n = n / p; end end p = p + 1; % 继续查找下一个素数 end if n > 1 % 若n不为1,则n是一个大于sqrt(n)的质数 euler = euler - euler / n; % 将结果减去n除以n的值(贡献) end end ``` 这个函数接受一个正整数n作为输入,并返回n的欧拉函数值。函数首先将结果初始化为n,然后从最小的素数2开始,一直遍历到sqrt(n)为止。如果n能被p整除,它将以p的贡献减少结果。然后继续查找下一个素数。最后,如果n不等于1,则n是一个大于sqrt(n)的质数,将其贡献减少结果。 希望以上的代码可以满足你的需求。 ### 回答3: 欧拉函数是数论中一个重要的函数,用于计算小于n且与n互质的正整数的个数。欧拉函数的公式为:φ(n)=n×(1-1/p1)×(1-1/p2)×⋯×(1-1/pk)其中p1,p2,...,pk是n的所有质因数。 下面是求欧拉函数的MATLAB代码: ```matlab function result = euler_phi(n) result = n; for i = 2 : round(sqrt(n)) if (n % i == 0) result = result * (1 - 1/i); while (n % i == 0) n = n / i; end end end if (n > 1) result = result * (1 - 1/n); end end ``` 在这段代码中,我们首先将结果初始化为n。然后从2到sqrt(n)遍历,如果n能被i整除,则说明i是n的一个质因数。这时,我们将结果乘以(1-1/i),然后不断将n除以i直到n不能再被i整除为止。最后,如果n大于1,说明n本身也是一个质因数,我们将结果乘以(1-1/n)。 这段代码可以在MATLAB中调用,例如: ```matlab n = 12; result = euler_phi(n); disp(result); ``` 这样就可以得到12的欧拉函数值。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值