您的位置:首页 > 其它

【组合数】hrbustoj 1744 Pascal's Triangle

2017-07-24 12:01 232 查看
Pascal's Triangle
Time Limit: 1000 MSMemory Limit: 65535 K
Total Submit: 115(23 users)Total Accepted: 27(16 users)Rating: 







Special Judge: No
Description
The figure below shows Pascal's Triangle:



Baby H divides Pascal's Triangle into some Diagonals, like the following figure:



4000
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春季校赛 - 现场赛
SubmitStatisticDiscussSharedcodes

题意:看第二幅图片,求和:第m条对角线的前k个数的和;
思路:组合数取模(数据小),预处理阶乘与阶乘的逆元;

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
#define mod 20000003

const int maxn=1000005;
ll fac[maxn+10],inv[maxn+10];

ll mod_pow(ll x,ll n)
{
ll res=1;
while(n>0)
{
if(n&1) res=res*x%mod;
x=x*x%mod;
n>>=1;
}
return res%mod;
}

void init()
{
fac[0]=1;fac[1]=1;
for(ll i=2;i<=maxn;i++)
{
fac[i]=fac[i-1]*i*1LL;
fac[i]%=mod;
}
inv[maxn]=mod_pow(fac[maxn],mod-2);
for(int i=maxn-1;i>=0;i--)
inv[i]=(inv[i+1]*(i+1))%mod;
}

ll C(ll n,ll m)
{
return fac
*inv[m]%mod*inv[n-m]%mod;
}

int main()
{
int T;
init();
scanf("%d",&T);
while(T--)
{
ll m,k;
ll res=0;
scanf("%lld%lld",&m,&k);
for(ll i=m-1;i<m-1+k;i++)
{
// printf("%d ",C(i,m-1));
res=res+C(i,m-1);
res%=mod;
}
printf("%lld\n",res);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hrbust