当前位置:网站首页>Four methods of finding combinatorial numbers
Four methods of finding combinatorial numbers
2022-06-13 11:02:00 【I can screw the bottle cap when I am born again】
Given n Group inquiry , Each group is given two integers a,b, Please output Cabmod(109+7) Value .
O(n*n) Practice recursive preprocessing
#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) Inverse element preprocessing
#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 Theorem
#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;
}
High precision additive prime decomposition
#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;
}
边栏推荐
- 2022 coal mine water exploration and drainage special operation certificate examination question bank simulated examination platform operation
- Necessary for Architects: system capacity status checklist
- WinForm resolves frequent refresh of black screen
- [dynamic planning] beginner level
- 2022 tailings recurrent training question bank and simulated examination
- 宝塔访问从IP改为域名
- Understand an article: Spark operation mode
- 高斯消元求n元方程组
- What is 400g Ethernet?
- 数据库学习笔记(第十六章)
猜你喜欢

Wait for someone with "source" | openharmony growth plan student challenge registration to start

EasyClick 运行代码片段出Null

很妙的贪心(F2. Nearest Beautiful Number (hard version))

终于,月入 20000 !!

Do you agree that the salary of hardware engineers is falsely high?

Idea remote debugging jar submitted by spark submit

Flutter simple and excellent open source dialog uses free_ dialog

5.5 clock screensaver

Experience of electric competition - program-controlled wind pendulum
Some experience in database table structure design
随机推荐
服务器的使用
Talk about MySQL indexing mechanism
MFC自定义button实现颜色控制
Matplotlib learning notes
微众银行OSPO建设之路:如何通过OSPO的建设推动企业开源?
Easyclick run code snippet out null
2022 tailings recurrent training question bank and simulated examination
Environ. Sci. Technol.(IF=9.028) | 城市绿化对大气环境的影响
Necessary for Architects: system capacity status checklist
Vivo large scale kubernetes cluster automation operation and maintenance practice
Simple query cost estimation [Gauss is not a mathematician this time]
Chapter VI i/o management
Nature communications - modeling armed conflict risk under climate change using machine learning and time series data
数位DP例题
Test cases that testers must master
Software testing often asks, do you really build a testing environment?
Nim游戏阶梯 Nim游戏和SG函数应用(集合游戏)
宝塔中navicat连接mysql
Private computing fat core concepts and stand-alone deployment
报告录屏+PPT 傅云飞-喜马拉雅山脉南坡云降水特征研究