码迷,mamicode.com
首页 > 其他好文 > 详细

The Wall (medium)

时间:2016-07-14 03:01:52      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

The Wall (medium)

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:

  1. Take a set of bricks.
  2. Select one of the possible wall designs. Computing the number of possible designs is left as an exercise to the reader.
  3. 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.

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 modulo106 + 3.

Input

The first line contains two space-separated integers n and C1 ≤ 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
Note

The number 106 + 3 is prime.

In the second sample case, the five walls are:


B B
B., .B, BB, B., and .B

In the third sample case, the nine walls are the five as in the second sample case and in addition the following four:


B B
B B B B
B., .B, BB, and BB
分析:球盒模型(隔板法)+乘法逆元;注意爆int;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <ext/rope>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define vi vector<int>
#define pii pair<int,int>
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=7e5+10;
const int mod=1e6+3;
const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
using namespace std;
using namespace __gnu_cxx;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p%mod;p=p*p%mod;q>>=1;}return f;}
int n,m,ans,fac[maxn]={1};
void init()
{
    for(int i=1;i<=maxn-10;i++)fac[i]=1ll*i*fac[i-1]%mod;
}
int inv(int p)
{
    return qpow(p,mod-2);
}
int C(int x,int y)
{
    return 1ll*fac[x]*inv(fac[y])%mod*inv(fac[x-y])%mod;
}
int main()
{
    int i,j,k,t;
    scanf("%d%d",&n,&m);
    init();
    rep(i,m,n+m-1)ans=(ans+C(i,m-1))%mod;
    printf("%d\n",ans);
    //system ("pause");
    return 0;
}

 

The Wall (medium)

标签:

原文地址:http://www.cnblogs.com/dyzll/p/5668741.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!