逆元, 组合数取模,费马小定理, HDU 6114

本文深入解析费马小定理,并展示如何利用该定理求解复杂组合数问题,通过实例代码详细说明逆元的概念及求解过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

json##### 费马小定理(Fermat’s little theorem)

是数论中的一个重要定理,若p是一个质数,而整数a不是p的倍数有a^(p-1)≡1(mod p), 理解为若a和p互素且p为质数, 满足a^(p-1)≡1(mod p)。
定理先放这, 现在目标是求在这里插入图片描述

一般的可以做如下转换

在这由里插入图片描述
但是取模对除法不适用故
在这里插入图片描述

逆元:对于a和p(a和p互素),若a*b%p≡1,则称b为a%p的逆元。
应用:t / a 对 p取模, 由于b是a%p的逆元, t / a 对p取模可以转换为t * b %p 转换为乘法了, 乘法对求模是适用的,接下来用这个来求解组合数。
求逆元:a^(p-1)≡1(mod p)即a^(p-2)* a≡1(mod p), 有a%p的逆元为a^(p-2)

求组合数

对p取模, 即n! 与 m!% p的逆元,(n-m)!%p的逆元的乘积。用f(x) 表示x!,有表达式为f(n)* f(m)^(p-2) * f(n - m) ^ (p -2)对p取模。其中次方可以用快速幂求,而f(x)可以一开始用一个数组保存小于max_num的阶乘, 注意是对p取模的阶乘,加上取模完整的表达式为

((f(n)* f(m)^(p-2)) % p * f(n - m) ^ (p -2))%p, 对于乘的过程中可能超过int范围, 可以先用long long 转换一下再%p, 例如HDU 6114

代码实现如下, solve(a,b)实现的是求得从a个中取b个的组合数对mod取模。


#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<vector>
#include<stack>
using namespace std;
typedef long long ll;
const int maxn = 100000, mod = 1e9 + 7;
int fac[maxn * 2];

int pow_mod(int a, int b)
{
	int base = a, res = 1;
	while(b)
	{
		if(b & 1) res = (ll) res * base % mod;
		
			base = (ll) base * base % mod;
		b >>= 1;
	}

	return res;
}

int solve(int a, int b)    				//solve()函数求得从a个中取b个的组合数, 对mod取模
{
	int temp = (ll) fac[b] * fac[a - b] % mod;
	int re = (ll)fac[a] * pow_mod(temp, mod - 2) % mod;
	return re;
}

int main()
{
	ios::sync_with_stdio(false);
	int t;
	cin >> t;
	fac[0] = 1;
	for(int i = 1; i <= 200000; i++) fac[i] = (ll)i * fac[i - 1] % mod;
	while(t--)
	{
		int m, n;
		cin >> m >> n;
		cout << (solve(m + n - 1, m) - solve(m + n -1, m - 1) + mod) % mod << endl;
	}
}

json### HDU 6114 Chess

Problem Description

車是中国象棋中的一种棋子,它能攻击同一行或同一列中没有其他棋子阻隔的棋子。一天,小度在棋盘上摆起了许多車……他想知道,在一共N×M个点的矩形棋盘中摆最多个数的車使其互不攻击的方案数。他经过思考,得出了答案。但他仍不满足,想增加一个条件:对于任何一个車A,如果有其他一个車B在它的上方(車B行号小于車A),那么車A必须在車B的右边(車A列号大于車B)。

现在要问问你,满足要求的方案数是多少。

Input

第一行一个正整数T,表示数据组数。

对于每组数据:一行,两个正整数N和M(N<=1000,M<=1000)。

Output

对于每组数据输出一行,代表方案数模1000000007(1e9+7)。

Sample Input

1
1 1

Sample Output

1


#include<iostream>
#include<string>
#include<string.h>
#include<vector>
#include<cstdio>
#include<set>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
int f[1005];
int n, m;

int pp(int a, int b)
{
	int base = a, ans = 1;
	while (b)
	{
		if (b & 1) ans = (ll)ans * base % mod;
			base = (ll)base * base % mod;
		b >>= 1;
	}
	return ans;
}

int solve(int a, int b)
{
	return (ll)f[a] * pp(f[b], mod - 2) % mod * pp(f[a - b], mod - 2) % mod;
}

int main()
{
	ios::sync_with_stdio(false);
	int t;
	f[0] = 1;
	for (int i = 1; i < 1005; i++)
		f[i] = (ll)f[i - 1] * i % mod;

	cin >> t;
	while (t--)
	{
		cin >> n >> m;
		if (n < m)
			swap(n, m);
		cout << solve(n, m) << endl;
	}
}

json

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值