LightOJ - 1102 (组合数取模逆元,当n和m小于1e6,p为任意素数时,打表法)
2018-02-28 20:23
357 查看
题意:n分为k部分有多少种方法.
题解:显然隔板法C(n+k-1,k-1)%mod,用到组合数逆元求法。#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
typedef long long LL;
const LL maxn(2e6+100), mod(1e9 + 7);
LL Jc[2000100];
void calJc() //求maxn以内的数的阶乘
{
Jc[0] = Jc[1] = 1;
for(LL i = 2; i < maxn; i++)
Jc[i] = Jc[i - 1] * i % mod;
}
/*
//拓展欧几里得算法求逆元
void exgcd(LL a, LL b, LL &x, LL &y) //拓展欧几里得算法
{
if(!b) x = 1, y = 0;
else
{
exgcd(b, a % b, y, x);
y -= x * (a / b);
}
}
LL niYuan(LL a, LL b) //求a对b取模的逆元
{
LL x, y;
exgcd(a, b, x, y);
return (x + b) % b;
}
*/
//费马小定理求逆元
LL pow(LL a, LL n, LL p) //快速幂 a^n % p
{
LL ans = 1;
while(n)
{
if(n & 1) ans = ans * a % p;
a = a * a % p;
n >>= 1;
}
return ans;
}
LL niYuan(LL a, LL b) //费马小定理求逆元
{
return pow(a, b - 2, b);
}
LL C(LL a, LL b) //计算C(a, b)
{
return Jc[a] * niYuan(Jc[b], mod) % mod
* niYuan(Jc[a - b], mod) % mod;
}
int main()
{
int T;
LL n,m,p,cas=1; calJc();
scanf("%d",&T);
while(T--)
{
scanf("%lld %lld",&n,&m);
printf("Case %lld: %lld\n",cas++,C(n+m-1,m-1));
}
return 0;
}
题解:显然隔板法C(n+k-1,k-1)%mod,用到组合数逆元求法。#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
typedef long long LL;
const LL maxn(2e6+100), mod(1e9 + 7);
LL Jc[2000100];
void calJc() //求maxn以内的数的阶乘
{
Jc[0] = Jc[1] = 1;
for(LL i = 2; i < maxn; i++)
Jc[i] = Jc[i - 1] * i % mod;
}
/*
//拓展欧几里得算法求逆元
void exgcd(LL a, LL b, LL &x, LL &y) //拓展欧几里得算法
{
if(!b) x = 1, y = 0;
else
{
exgcd(b, a % b, y, x);
y -= x * (a / b);
}
}
LL niYuan(LL a, LL b) //求a对b取模的逆元
{
LL x, y;
exgcd(a, b, x, y);
return (x + b) % b;
}
*/
//费马小定理求逆元
LL pow(LL a, LL n, LL p) //快速幂 a^n % p
{
LL ans = 1;
while(n)
{
if(n & 1) ans = ans * a % p;
a = a * a % p;
n >>= 1;
}
return ans;
}
LL niYuan(LL a, LL b) //费马小定理求逆元
{
return pow(a, b - 2, b);
}
LL C(LL a, LL b) //计算C(a, b)
{
return Jc[a] * niYuan(Jc[b], mod) % mod
* niYuan(Jc[a - b], mod) % mod;
}
int main()
{
int T;
LL n,m,p,cas=1; calJc();
scanf("%d",&T);
while(T--)
{
scanf("%lld %lld",&n,&m);
printf("Case %lld: %lld\n",cas++,C(n+m-1,m-1));
}
return 0;
}
相关文章推荐
- lightoj 1102 - Problem Makes Problem(组合数+逆元)
- LightOJ - 1067 Combinations 大组合数取模(费马小定理与逆元)
- 【板子】gcd、exgcd、乘法逆元、快速幂、快速乘、筛素数、快速求逆元、组合数
- 各种逆元求法 组合数取模 comb (组合数 Lucas)
- 求组合数取模(杨辉三角打表 & 求逆元(扩展欧几里得、费马小定理、欧拉定理、线性求法) & Lucas)
- hdu 3037 Saving Beans 【大组合数取模-Lucas定理+逆元取模】
- LightOJ - 1124 同codeforce-451E (隔板法,卢卡斯定理,当n和m很大,p为任意素数时)
- 2017-07-25 51nod 1119 组合数取模 某数关于质数的逆元
- 为什么组合数取模要用逆元
- fzu 2020 组合数对素数取模lucas定理
- 验证任意一个大于4并且小于500的偶数都可以拆分成两个素数之和
- HDU 5698 大组合数取模(逆元)
- 【板子】gcd、exgcd、乘法逆元、快速幂、快速乘、筛素数、快速求逆元、组合数
- fzu 2020 组合数对素数取模
- LightOJ-1102-组合数学,逆元
- 输入任意一个大于2的正整数n,输出所有小于等于n的素数
- light oj 1102 Problem Makes Problem [组合数+逆元]
- [置顶] 【板子】gcd、exgcd、乘法逆元、快速幂、快速乘、筛素数、快速求逆元、组合数
- [置顶] 【板子】gcd、exgcd、乘法逆元、快速幂、快速乘、筛素数、快速求逆元、组合数
- lightoj 1102 - Problem Makes Problem (组合+逆元)