计算组合数

devtools/2024/9/22 14:53:18/

1.递推

#include<bits/stdc++.h>
#include<unordered_map> 
#include<unordered_set>
using namespace std;
#define int long long //可能会超时 
#define PII pair<int,int>
const int INF = 0x3f3f3f3f, mod = 1e9 + 7;
const int N = 2005;
int a, b,n;
int c[N][N];
signed main()
{ios::sync_with_stdio, cin.tie(0), cout.tie(0);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;}}cin >> n;while (n--) {cin >> a >> b;cout << c[a][b] << endl;}return 0;
}

2.定义出发,快速幂逆元

#include<bits/stdc++.h>
#include<unordered_map> 
#include<unordered_set>
using namespace std;
#define int long long //可能会超时 
#define PII pair<int,int>
const int INF = 0x3f3f3f3f, mod = 1e9 + 7;
const int N = 1e5 + 5;
int fact[N], infact[N];
int n;
int pow(int a, int b, int p) {int ans = 1;while (b) {if (b & 1) ans = ans * a % p;b >>= 1;a = a * a % p;}return ans;
}
signed main()
{ios::sync_with_stdio, cin.tie(0), cout.tie(0);fact[0] = infact[0] = 1;for (int i = 1;i < N;i++) {fact[i] = fact[i - 1] * i % mod;infact[i] = infact[i - 1] * pow(i, mod - 2, mod) % mod;}cin >> n;while (n--) {int a, b;cin >> a >> b;cout << fact[a] * infact[b] % mod * infact[a - b] % mod;puts("");}return 0;
}

3.卢卡斯定理

#include<bits/stdc++.h>
#include<unordered_map> 
#include<unordered_set>
using namespace std;
#define int long long //可能会超时 
#define PII pair<int,int>
const int INF = 0x3f3f3f3f, mod = 998244353;
int a, b, p;
int n;
int pow(int a, int b, int p) {int ans = 1;while (b) {if (b & 1) ans = ans * a % p;a = a * a % p;b >>= 1;}return ans;
}
int C(int a, int b, int p) {if (b > a) return 0;int ans = 1;for (int i = 1,j = a;i <= b;i++, j--) {ans = ans * j % p;ans = ans * pow(i, p - 2, p) % p;}return ans;
}
int lucas(int a, int b, int p) {if (a < p && b < p) return C(a, b, p);return C(a % p, b % p, p) * lucas(a / p, b / p, p) % p;
}
signed main()
{ios::sync_with_stdio, cin.tie(0), cout.tie(0);cin >> n;while (n--) {cin >> a >> b >> p;cout << lucas(a, b, p) << endl;}return 0;
}

4.无模数,高精度

#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;
}

http://www.ppmy.cn/devtools/115514.html

相关文章

《MmAP : Multi-Modal Alignment Prompt for Cross-Domain Multi-Task Learning》中文校对版

系列论文研读目录 文章目录 系列论文研读目录摘要1 引言2 相关工作3 方法3.1对比图像预训练3.2 多模式对齐提示3.3 多任务提示学习框架 4 实验4.1基准设置4.2实验结果4.3消融研究 5、结论 摘要 多任务学习&#xff08;Multi-Task Learning&#xff0c;MTL&#xff09;是为了同…

MySQL高阶1919-兴趣相同的朋友

题目 请写一段SQL查询获取到兴趣相同的朋友。用户 x 和 用户 y 是兴趣相同的朋友&#xff0c;需满足下述条件&#xff1a; 用户 x 和 y 是朋友&#xff0c;并且用户 x and y 在同一天内听过相同的歌曲&#xff0c;且数量大于等于三首. 结果表 无需排序 。注意&#xff1a;返…

获取STM32 MCU的唯一ID

STM32每个系列都会有唯一的一个芯片序列号&#xff08;96位bit&#xff09; STM32F10X 的起始地址是 0x1FFFF7E8 STM32F20X 的起始地址是 0x1FFF7A10 STM32F30X 的起始地址是 0x1FFFF7AC STM32F40X 的起始地址是 0x1FFF7A10 STM32L0XX 的起始地址是 0x1FF80050 STM32L1XX 的起…

浏览器插件利器--allWebPluginV2.0.0.20-stable版发布

allWebPlugin简介 allWebPlugin中间件是一款为用户提供安全、可靠、便捷的浏览器插件服务的中间件产品&#xff0c;致力于将浏览器插件重新应用到所有浏览器。它将现有ActiveX控件直接嵌入浏览器&#xff0c;实现插件加载、界面显示、接口调用、事件回调等。支持Chrome、Firefo…

扣子智能体实战-汽车客服对话机器人(核心知识:知识库和卡片)

这一节的主要内容是通过创建一个汽车客户对话机器人学习扣子平台知识库和卡片的使用。 机器人参考&#xff1a; 企业汽车客服 资深汽车销售 一&#xff0c;汽车销售机器人需求简介 汽车销售是一个需要 7*24h在线的客服咨询岗位&#xff0c;专业性强&#xff0c;但流动性非…

2、论文阅读:用于超高清交通监控的双域引导实时低光图像增强

用于超高清交通监控的双域引导实时低光图像增强 前言动机传统弱光增强方法的缺陷基于深度神经网络方法的缺陷解决贡献双域导引微光图像增强网络高斯拉普拉斯算子(边缘特征的抽取)二阶导数离散卷积核 KL 生成 梯度特征图高斯平滑滤波来降低噪声对图像的干扰前言 弱光条件下拍…

C# 找到给定点集的简单闭合路径(Find Simple Closed Path for a given set of points)

给定一组点&#xff0c;将这些点连接起来而不相交 例子&#xff1a; 输入&#xff1a;points[] {(0, 3), (1, 1), (2, 2), (4, 4), (0, 0), (1, 2), (3, 1}, {3, 3}}; 输出&#xff1a;按以下顺序连接点将 不造成任何交叉 {(0, 0), (3, …

QT<24> Qt和windows中获取CPU序列号号以及主板序列号

前言&#xff1a;在qt中获取CPU和主板唯一序列号&#xff0c;可以在程序构造函数中判断是否与windows中一致&#xff0c;不一致可以直接退出程序&#xff0c;防止程序daoyong。 一、获取电脑CPU唯一序列号 QString MainPage::get_cpu() {QString cmd"wmic cpu get proc…