欧拉函数、费马小定理、Lucas定理

这里只是说这三个定理的一些皮毛而已,后面附上了其他大佬比较优秀的博客。

欧拉函数:

欧拉定理:a^(φ(m))1  (mod m)  (a与m互质,且a>m)   后面的mod m是两边都要取模的

欧拉函数的定义:  在数论中,对于正整数<=N ,且与N互质的正整数(包括1)的个数,记作φ(n)。      (素数p的欧拉函数是p-1(只是去除了本身))

φ函数的值:

φ(x)=x(1-1/p(1))(1-1/p(2))(1-1/p(3))(1-1/p(4))…..(1-1/p(n))

其中p(1),p(2)…p(n)为x的所有质因数;x是正整数; φ(1)=1(唯一和1互质的数,且小于等于1)。注意:每种质因数只有一个。

 P(n)为分解的值;例如12=2*2*3,p(n)有两个值2和3;

例如:

         φ(10)=10×(1-1/2)×(1-1/5)=4;

         1 3 7 9

         φ(30)=30×(1-1/2)×(1-1/3)×(1-1/5)=8;

         φ(49)=49×(1-1/7)=42;

欧拉定理:a^(φ(m))1  (mod m)  (a与m互质,且a>m)   后面的mod m是两边都要取模的

eg:   (1^4)%10=1    (3^4)%10=1   (7^4)%10=1  (9^4)%10=1

https://www.cnblogs.com/handsomecui/p/4755455.html

 

费马小定理:

假如p是素数,且(a,p)=1(a,p互质),那么a^(p-1)≡1(mod p)          或者写成a^p≡a (mod p)

 

推导:

a^φ(p)1 (mod p)(恒等于)此公式即 欧拉定理

当p为素数时,得      费马小定理a^(p-1)≡1 (mod p)

https://www.cnblogs.com/handsomecui/p/4755455.html

 

Lucas定理:(SummerII S 题)

Cm(a , b ,mod) = Cm(a % mod, b % mod) * Lucas(a / mod ,  b / mod , mod)%p;

a/b  =  a*(b ^ (M-2)) (mod M)除变乘(费马小定理推导)

只要M是一个素数,  而且b不是M的倍数

https://blog.csdn.net/wyg1997/article/details/52152282

 

附上一道题:

Gdufe_2018_Summer II (S)

                                                                            Saving Beans

Although winter is far away, squirrels have to work day and night to save beans. They need plenty of food to get through those long cold days. After some time the squirrel family thinks that they have to solve a problem. They suppose that they will save beans in n different trees. However, since the food is not sufficient nowadays, they will get no more than m beans. They want to know that how many ways there are to save no more than m beans (they are the same) in n trees.

Now they turn to you for help, you should give them the answer. The result may be extremely huge; you should output the result modulo p, because squirrels can’t recognize large numbers.

Input

The first line contains one integer T, means the number of cases.

Then followed T lines, each line contains three integers n, m, p, means that squirrels will save no more than m same beans in n different trees, 1 <= n, m <= 1000000000, 1 < p < 100000 and p is guaranteed to be a prime.

Output

You should output the answer modulo p.

Sample Input

2
1 2 5
2 1 5

Sample Output

3
3

        
  

Hint

Hint

For sample 1, squirrels will put no more than 2 beans in one tree. Since trees are different, we can label them as 1, 2 … and so on. 
The 3 ways are: put no beans, put 1 bean in tree 1 and put 2 beans in tree 1. For sample 2, the 3 ways are:
put no beans, put 1 bean in tree 1 and put 1 bean in tree 2.

题解:首先输入测试的样例的个数T,然后输入n,m,p。求C(n+m,m)mod p

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
	static Scanner scan = new Scanner(System.in);
	static long[] arr = new long[100002];
	static long p;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		while(scan.hasNext()){
			int T = scan.nextInt();
			while(T-->0){
				long n = scan.nextLong();
				long m = scan.nextLong();
				p = scan.nextInt();			
				arr[0] = 1;
				for(int i=1;i<p;i++){
					arr[i] = arr[i-1]*i%p;
				}
				
				long ans = Lucas(n+m,m,p);
				System.out.println(ans);
			}
		}
	}
	
	private static long Lucas(long a, long b, long mod) {
		// TODO Auto-generated method stub
		if(b==0)
			return 1;
	        return Cm(a % mod, b % mod) * Lucas(a / mod, b / mod,mod)%p;
	}
	private static long Cm(long a, long b) {
		// TODO Auto-generated method stub	
	        if(a < b)
	    	    return 0;
		return arr[(int)a]*power(arr[(int)(a-b)]*arr[(int)b]%p,p-2,p) % p; //费马小定理的应用
	}
	private static long power(long a, long b,long mod) {  //java版的快速幂取模
		BigInteger a1 = new BigInteger(a+"");
		BigInteger b1 = new BigInteger(b+"");
		BigInteger mod1 = new BigInteger(mod+"");
		BigInteger a11 = a1.modPow(b1, mod1);
		long ans = a11.longValue();
		return ans;	
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值