【组合数】hrbustoj 1744 Pascal's Triangle
2017-07-24 12:01
232 查看
Pascal's Triangle | ||||||
| ||||||
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春季校赛 - 现场赛 |
Submit | Statistic | Discuss | Sharedcodes |
思路:组合数取模(数据小),预处理阶乘与阶乘的逆元;
代码:
#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 1744 Pascal's Triangle【数论求C(n,m)】
- Pascal's triangle
- LeetCode-Pascal's Triangle II
- 118. Pascal's Triangle
- [LeetCode]118. Pascal's Triangle
- [leetcode, python] Pascal's Triangle II 杨辉三角
- leetcode之 Pascal's Triangle && Pascal's Triangle II
- LeetCode 118. Pascal's Triangle
- Pascal's Triangle II
- Leetcode Pascal's Triangle
- Pascal's Triangle I && II--JavaScript
- leetcode:Pascal's Triangle II 【Java】
- 119. Pascal's Triangle II -- Python
- 118.Pascal's Triangle
- Pascal's Triangle (leetcode)
- [LeetCode]118 Pascal's Triangle
- [Leetcode从零开刷]119. Pascal's Triangle II
- LeetCode OJ:Pascal's Triangle II
- Pascal's Triangle II
- 119. Pascal's Triangle II