CodeForces - 690D2 The Wall (medium)(还是组合数)

D2. 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:

  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 nbricks.

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

Copy

5 1

output

Copy

5

input

Copy

2 2

output

Copy

5

input

Copy

3 2

output

Copy

9

input

Copy

11 5

output

Copy

4367

input

Copy

37 63

output

Copy

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

规律是:C(n+c,min(n,c))-1;

(找规律的过程省略,因为根本没有找到规律,只知道a[n][m]=a[m][n]=a[m][n-1]+a[m-1][n]+1)

这个是用卢卡斯加逆元求组合数:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;

const int p = 1e6+3;

/*ll quick_mod(ll a,ll b){//这个也是快速幂求逆元
    ll ans = 1;
    a %= p;
    while(b){
        if(b&1){
            ans = ans*a%p;
            b--;
        }
        b >>= 1;
        a = a*a%p;
    }
    return ans;
}*/

ll quick_mod(ll a ,ll b){//快速幂求逆元
    if(b==0) return 1;
    ll res = quick_mod(a*a%p,b/2);
    if(b%2)
        res = res*a%p;
    return res;
}

ll C(ll n,ll m){//求组合数C(m,n)
    if(m>n) return 0;
    ll ans = 1;
    for(ll i=1;i<=m;i++){
        ll a=(n-m+i)%p;
        ll b = i%p;
        ans = ans*(a*quick_mod(b,p-2)%p)%p;
    }
    return ans;
}

ll Lucas(ll n,ll m){//卢卡斯定理
    return m==0?1:(C(n%p,m%p)*Lucas(n/p,m/p)%p);
}

int main()
{
    int n,c;
    while(cin >> n >> c){
        cout << Lucas(n+c,min(n,c))-1<<endl;
    }
    return 0;
}

这个题的数据比较小,所以也可以直接用阶乘和逆元求:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;

const int p = 1e6+3;

/*ll quick_mod(ll a,ll b){//这个也是快速幂求逆元
    ll ans = 1;
    a %= p;
    while(b){
        if(b&1){
            ans = ans*a%p;
            b--;
        }
        b >>= 1;
        a = a*a%p;
    }
    return ans;
}*/

ll quick_mod(ll a ,ll b){//快速幂求逆元
    if(b==0) return 1;
    ll res = quick_mod(a*a%p,b/2);
    if(b%2)
        res = res*a%p;
    return res;
}

int main()
{
    int n,c;
    while(cin >> n >> c){
        int i;
        ll ans=1;
        for(int i = 1;i <= min(n,c);i++){
            ans = (ans%p*(n+c+i-min(n,c))%p*quick_mod(i,p-2)%p)%p;
        }
        cout <<ans-1 <<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42754600/article/details/81175223
今日推荐