The Wall (medium)
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Heidi the Cow is aghast: cracks in the northern Wall? Zombies gathering outside, forming groups, preparing their assault? This must not happen! Quickly, she fetches her HC2 (Handbook of Crazy Constructions) and looks for the right chapter:
How to build a wall:
Take a set of bricks.
Select one of the possible wall designs. Computing the number of possible designs is left as an exercise to the reader.
Place bricks on top of each other, according to the chosen design.
This seems easy enough. But Heidi is a Coding Cow, not a Constructing Cow. Her mind keeps coming back to point 2b. Despite the imminent danger of a zombie onslaught, she wonders just how many possible walls she could build with up to n bricks.
A wall is a set of wall segments as defined in the easy version. How many different walls can be constructed such that the wall consists of at least 1 and at most n bricks? Two walls are different if there exist a column c and a row r such that one wall has a brick in this spot, and the other does not.
Along with n, you will be given C, the width of the wall (as defined in the easy version). Return the number of different walls modulo 106 + 3.
Input
The first line contains two space-separated integers n and C, 1 ≤ n ≤ 500000, 1 ≤ C ≤ 200000.
Output
Print the number of different walls that Heidi could build, modulo 106 + 3.
Examples
Input
5 1
Output
5
Input
2 2
Output
5
Input
3 2
Output
9
Input
11 5
Output
4367
Input
37 63
Output
230574
题目大意:给你n块砖和m的宽度,问有多少种放置方式。
思路先手动打个表查找其中规律,发现对任意一个结果
K[m][n] = k[m-1][n] + k[m][n-1]+1,且对称分布。
1 2 3 4 5
2 5 9 14 20
3 9 19 34 55
4 14 34 69 125
5 20 55 125 251
由此想到杨辉三角
去m,n中较大的为m,较小的为n
则以2为(1,1)的菱形中的每一个点都是所求值加1;
则只需求出坐标表示用组合数求法求出减1即可。
对比后得出k[m][n] = 组合数(m + n)(n) - 1;
下面ac代码(30ms)
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<string>
#include<map>
#include<stack>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<cstdio>
#include<climits>
const int inf = 0x3f3f3f3f;
using namespace std;
typedef long long LL;
LL f[700010];
void init(int p){
f[0] = 1;
for(int i = 1; i <= 700000; ++i)
f[i] = f[i-1] * i % p;
}
LL pow_mod(LL a, LL x, int p){ //求组合数取余
LL ret = 1;
a %= p;
while(x){
if(x & 1){
ret = ret * a % p;
--x;
}
else{
a = a * a % p;
x >>= 1;
}
}
return ret;
}
LL Lucas(LL n, LL k, int p){
LL ret = 1;
while(n && k){
LL nn = n % p, kk = k % p;
if(nn < kk) return 0;
ret = ret * f[nn] * pow_mod(f[kk] * f[nn - kk] % p, p - 2, p) % p;
n /= p;
k /= p;
}
return ret;
}
int main()
{
int n,m;
init(1e6+3);
while(cin>>n>>m)
{
LL ma = max(n,m);
LL mi = min(n,m);
LL ans = Lucas(ma+mi,mi,1e6+3) - 1 ;
cout<<ans<<endl;
}
return 0;
}