Codeforces 690D2 The Wall (medium)

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值