Hrbust 1744 Pascal's Triangle【数论求C(n,m)】
2017-03-19 17:29
288 查看
Pascal's Triangle | ||||||
| ||||||
Description | ||||||
The figure below shows Pascal's Triangle: Baby H divides Pascal's Triangle into some Diagonals, like the following figure: Baby H wants to know the sum of K number in front on the Mth diagonal. Try to calculate it. | ||||||
Input | ||||||
There are multiple test cases. The first line is a positive integer T (1<=T<=100) indicating the number of test cases. For each test case: Line 1. Two positive integers M and K (1<= M , K <= 100 000). | ||||||
Output | ||||||
For each test case, output the sum of K number in front on the Mth diagonal in one line. The answer should modulo to 20 000 003. | ||||||
Sample Input | ||||||
2 2 3 3 4 | ||||||
Sample Output | ||||||
6 20 | ||||||
Source | ||||||
哈理工2013春季校赛 - 现场赛 |
让你在在第Mdiagonal分块中,求前K个数的和。
思路:
预处理C(n,m);
过程求和注意取模;
Ac代码:
#include<stdio.h>
#include<string.h>
using namespace std;
#define LL long long int
const LL mod = 20000003;
const LL N = 300000+5;
const LL M = 3e5+3;
LL fac[1000005]; //阶乘
LL inv_of_fac[1000005]; //阶乘的逆元
LL qpow(LL x,LL n)
{
LL ret=1;
for(; n; n>>=1)
{
if(n&1) ret=ret*x%mod;
x=x*x%mod;
}
return ret;
}
void init()
{
fac[1]=1;
for(int i=2; i<=M; i++)
fac[i]=fac[i-1]*i%mod;
inv_of_fac[M]=qpow(fac[M],mod-2);
for(int i=M-1; i>=0; i--)
inv_of_fac[i]=inv_of_fac[i+1]*(i+1)%mod;
}
LL C(LL a,LL b)
{
if(b>a) return 0;
if(b==0) return 1;
return fac[a]*inv_of_fac[b]%mod*inv_of_fac[a-b]%mod;
}
int main()
{
init();
int t;
scanf("%d",&t);
while(t--)
{
LL n,m;
LL sum=0;
scanf("%lld%lld",&m,&n);
for(int i=0;i<n;i++)
{
sum+=C(m-1+i,m-1);
sum%=mod;
}
printf("%lld\n",sum);
}
}
相关文章推荐
- 【组合数】hrbustoj 1744 Pascal's Triangle
- 118. Pascal's Triangle
- 118. Pascal's Triangle
- [Leetcode] Pascal's Triangle II
- Pascal's Triangle
- [leetcode, python] Pascal's Triangle II 杨辉三角
- 119. Pascal's Triangle II
- Pascal's Triangle (Java)
- [Leetcode]Pascal's Triangle II
- Pascal's Triangle II
- Pascal's Triangle
- 119. Pascal's Triangle II
- [LeetCode]118.Pascal's Triangle
- [LeetCode] Pascal's Triangle
- Pascal's Triangle
- UVa485 - Pascal's Triangle of Death
- 118. Pascal's Triangle
- leetcode 118 —— Pascal's Triangle
- [leetcode]Pascal's Triangle
- 118. Pascal's Triangle