您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: