当前位置:网站首页>求组合数四种方法
求组合数四种方法
2022-06-13 10:57:00 【重生之我会拧瓶盖】
给定 n 组询问,每组询问给定两个整数 a,b,请你输出 Cabmod(109+7) 的值。
O(n*n)做法递推式预处理
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 2010,mod = 1e9+7;
int c[N][N];
void init(){
for (int i=0;i<N;i++)
for (int j = 0; j <=i; j ++ )
if (!j) c[i][j]=1;
else
c[i][j] = (c[i-1][j]+c[i-1][j-1])%mod;
}
int main()
{
ios::sync_with_stdio(false);
init();
int n;
cin>>n;
while (n -- ){
int a,b;
cin>>a>>b;
cout<<c[a][b]<<endl;
}
return 0;
}
O(n*log n)逆元预处理
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 1e5+10,mod = 1e9+7;
int f1[N],f2[N];
int qmi(int a,int k,int p)
{
int res = 1;
while (k)
{
if (k&1) res = (LL)res*a%p;
a = (LL) a*a%p;
k>>=1;
}
return res;
}
int main()
{
ios::sync_with_stdio(false);
f1[0]=f2[0]=1;
for (int i=1;i<N;i++)
{
f1[i] = (LL)f1[i-1]*i%mod;
f2[i] = (LL)f2[i-1]*qmi(i,mod-2,mod)%mod;
}
int n;
cin>>n;
while (n -- ){
int a,b;
cin>>a>>b;
printf ("%d\n",(LL)f1[a]*f2[b]%mod*f2[a-b]%mod);
}
return 0;
}
Lucas定理
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
int qmi(int a, int k, int p)
{
int res = 1;
while (k)
{
if (k & 1) res = (LL)res * a % p;
a = (LL)a * a % p;
k >>= 1;
}
return res;
}
int C(int a, int b, int p)
{
if (b > a) return 0;
int res = 1;
for (int i = 1, j = a; i <= b; i ++, j -- )
{
res = (LL)res * j % p;
res = (LL)res * qmi(i, p - 2, p) % p;
}
return res;
}
int lucas(LL a, LL b, int p)
{
if (a < p && b < p) return C(a, b, p);
return (LL)C(a % p, b % p, p) * lucas(a / p, b / p, p) % p;
}
int main()
{
int n;
cin >> n;
while (n -- )
{
LL a, b;
int p;
cin >> a >> b >> p;
cout << lucas(a, b, p) << endl;
}
return 0;
}
高精度加质数分解
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 5010;
int primes[N], cnt;
int sum[N];
bool st[N];
void get_primes(int n)
{
for (int i = 2; i <= n; i ++ )
{
if (!st[i]) primes[cnt ++ ] = i;
for (int j = 0; primes[j] <= n / i; j ++ )
{
st[primes[j] * i] = true;
if (i % primes[j] == 0) break;
}
}
}
int get(int n, int p)
{
int res = 0;
while (n)
{
res += n / p;
n /= p;
}
return res;
}
vector<int> mul(vector<int> a, int b)
{
vector<int> c;
int t = 0;
for (int i = 0; i < a.size(); i ++ )
{
t += a[i] * b;
c.push_back(t % 10);
t /= 10;
}
while (t)
{
c.push_back(t % 10);
t /= 10;
}
return c;
}
int main()
{
int a, b;
cin >> a >> b;
get_primes(a);
for (int i = 0; i < cnt; i ++ )
{
int p = primes[i];
sum[i] = get(a, p) - get(a - b, p) - get(b, p);
}
vector<int> res;
res.push_back(1);
for (int i = 0; i < cnt; i ++ )
for (int j = 0; j < sum[i]; j ++ )
res = mul(res, primes[i]);
for (int i = res.size() - 1; i >= 0; i -- ) printf("%d", res[i]);
puts("");
return 0;
}
边栏推荐
- On the exploitation of a horizontal ultra vires vulnerability
- Questions and answers of the labor worker general basic (labor worker) work license in 2022
- Folder data synchronization tool sync folders Pro
- Vivo large scale kubernetes cluster automation operation and maintenance practice
- Review of last week's hot spots (6.6-6.12)
- Matplotlib learning notes
- Web3 系统构建:去中心化的原则、模型和方法(上)
- 2022煤矿探放水特种作业证考试题库模拟考试平台操作
- Brief introduction to memory structure of virtual machine
- Multithreading starts from the lockless queue of UE4 (thread safe)
猜你喜欢

QTcpServer. QTcpSocket. Differences between qudpsockets

Brief request process

EasyClick 运行代码片段出Null

首个Laravel工作流引擎发布 V1.0正式版

vivo大规模 Kubernetes 集群自动化运维实践

等个有“源”人|OpenHarmony 成长计划学生挑战赛报名启动

vivo大规模 Kubernetes 集群自动化运维实践

音视频技术开发周刊 | 249

Pagoda access changed from IP to domain name

Vivo large scale kubernetes cluster automation operation and maintenance practice
随机推荐
测试人员必须掌握的测试用例
Brief introduction to memory structure of virtual machine
Electrolytic capacitor, tantalum capacitor, ordinary capacitor
Chapter VII document management
AcWing第 55 场周赛
Spark source code (I) how spark submit submits jars and configuration parameters to spark server
Advanced technology management - what management tools can managers use
Vivo large scale kubernetes cluster automation operation and maintenance practice
As a tester, these basic knowledge are essential
阿里一季度员工减少4000人;程序员写脚本抢挂疫苗号,牟利40万被刑拘;搜狐遭遇史诗级邮件诈骗,张朝阳回应 | Q资讯
Understand an article: Spark operation mode
deepin系统中Qt5.12无法输入中文(无法切换中文输入法)解决办法
Similarities and differences between decoration mode and agency mode
2022煤矿探放水特种作业证考试题库模拟考试平台操作
Chapter VI i/o management
Do you agree that the salary of hardware engineers is falsely high?
Go zero microservice Practice Series (III. API definition and table structure design)
Flutter simple and excellent open source dialog uses free_ dialog
Brief request process
软件测试常问,你真的会搭建测试环境吗?