codeforces 690D2 D2. The Wall (medium)(组合数学)
2016-07-11 22:28
751 查看
题目链接:
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:
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 modulo106 + 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 10^6 + 3.
Examples
input
output
input
output
input
output
input
output
input
output
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:
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 modulo106 + 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 10^6 + 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个砖,给定了宽c,问你有多少种墙; 这也是[1,n]的按顺序拆分成c个数的种数;也是把[1,n]放在c个盒子里(允许有空盒)的方案数; 思路: dp [m]=dp [m-1]+dp[n-1][m]+...+dp[0][m]+dp[n-1][m]+dp[n-2][m]+...+dp[1][m]; 把n个相同的球放在m个盒子里(允许有空盒)的方案数为C(n+m-1,m-1); dp [m]=C(n+m-1,m-1)=(n+m-1)/n*C(n+m-2,m-1)=(n+m-1)/n*dp[n-1][m]; 取模的时候要快速幂求一下逆; AC代码:
#include <bits/stdc++.h> /* #include <vector> #include <iostream> #include <queue> #include <cmath> #include <map> #include <cstring> #include <algorithm> #include <cstdio> */ using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++) #define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) { char CH; bool F=false; for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar()); for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar()); F && (num=-num); } int stk[70], tp; template<class T> inline void print(T p) { if(!p) { puts("0"); return; } while(p) stk[++ tp] = p%10, p/=10; while(tp) putchar(stk[tp--] + '0'); putchar('\n'); } const LL mod=1e6+3; const double PI=acos(-1.0); const LL inf=1e18; const int N=5e5+10; const int maxn=1005; const double eps=1e-10; LL n,m,dp ; LL pow_mod(LL x,LL y) { LL s=1,base=x; while(y) { if(y&1)s=s*base%mod; base=base*base%mod; y>>=1; } return s; } void Init() { dp[0]=1; for(LL i=1;i<=n;i++) { dp[i]=(dp[i-1]*(i+m-1)%mod)*pow_mod(i,mod-2)%mod; } } int main() { read(n);read(m); Init(); LL sum=0; for(LL i=1;i<=n;i++) { sum=(sum+dp[i])%mod; } cout<<sum<<"\n"; return 0; }
相关文章推荐
- HDU 1003 最大连续和
- linux的GPIO驱动的使用(s5pv210)
- JAVA异常处理机制
- xlslib安装, aclocal-1.13: command not found, 安装升级autoconf-2.65.tar.gz, automake-1.13.tar.gz两个文件
- Spark资源
- hashmap庖丁解牛
- xlslib安装, aclocal-1.13: command not found, 安装升级autoconf-2.65.tar.gz, automake-1.13.tar.gz两个文件
- cannot change version web module 2.5
- gcc编译时对'xxxx'未定义的引用问题
- codeforces 641D. Little Artem and Random Variable
- 在stackoverflow回答被认可
- Unsupported major.minor version 52.0问题的解决
- java里面的输入流、输出流、节点流和处理流区别
- JavaScript的执行上下文
- hbase表结构模型
- matlab--特征值分解
- LeetCode 217. Contains Duplicate
- 卡尔曼滤波、粒子滤波的滤波是什么意思?
- 思科路由与交换系列--------NA系列
- iOS学习(二十九)创建第一个app应用,不用故事板,创建主页