UVa1638 Pole Arrangement 递推
来源:互联网 发布:交朋友软件哪个好 编辑:程序博客网 时间:2024/05/19 03:41
There are poles of height 1, 2, . . . , n in a row. If you look at these poles from the left side or the right
side, smaller poles are hidden by taller poles. For example, consider the two arrangements of 4 poles
in the next figure:
For each arrangement, only one pole can be seen from the left, and two poles can be seen from the
right.
You are to write a program to calculate the number of arrangements of n poles such that seen from
the left you see l poles and seen from the right you see r poles.
Input
Your program is to read from standard input. The input consists of T test cases. The number of test
cases T is given in the first line of the input. Each test case consists of a line containing three integers,
n, l, and r (1 ≤ l, r ≤ n ≤ 20), where n is the number of poles and l (resp. r) is the number of poles
that can be seen from the left (resp. right).
Output
Your program is to write to standard output. Print exactly one line for each test case. The line should
contain the number of arrangements of poles for the test case.
The following shows sample input and output for four test cases.
Sample Input
4
4 1 2
4 1 1
5 2 4
20 2 1
Sample Output
2
0
4
side, smaller poles are hidden by taller poles. For example, consider the two arrangements of 4 poles
in the next figure:
For each arrangement, only one pole can be seen from the left, and two poles can be seen from the
right.
You are to write a program to calculate the number of arrangements of n poles such that seen from
the left you see l poles and seen from the right you see r poles.
Input
Your program is to read from standard input. The input consists of T test cases. The number of test
cases T is given in the first line of the input. Each test case consists of a line containing three integers,
n, l, and r (1 ≤ l, r ≤ n ≤ 20), where n is the number of poles and l (resp. r) is the number of poles
that can be seen from the left (resp. right).
Output
Your program is to write to standard output. Print exactly one line for each test case. The line should
contain the number of arrangements of poles for the test case.
The following shows sample input and output for four test cases.
Sample Input
4
4 1 2
4 1 1
5 2 4
20 2 1
Sample Output
2
0
4
6402373705728000
这道题可以考虑从大到小安排杆子,这样的好处是,插入的杆子不会遮挡到已插入的杆子,
同时插如的杆子在最左端,则从左边可看到的杆子数加一;
在最右端,则从右边可看到的杆子数加一;
插入中间则无任何影响。
这样,我们可以得到递推式: a[i][j][k]=a[i-1][j-1][k]+a[i-1][j][k-1]+a[i-1][j][k]*(i-2);
其中j、k表示从左、右端所看见的杆子数。
需要注意的是n=20时,结果已超过int范围,故用long long 来存储
#include <iostream>#include <cstdio>#include <map>#include <cmath>#include <algorithm>#include <cstring>#include <string>using namespace std;#define LL long longLL a[25][25][25];void p(){ memset(a,0,sizeof(a)); a[1][1][1]=1; for(int i=2;i<=20;i++){ for(int j=1;j<=20;j++){ for(int k=1;k<=20;k++){ a[i][j][k]=a[i-1][j-1][k]+a[i-1][j][k-1]+a[i-1][j][k]*(i-2); } } }}int main(){ int t,n,l,r; p(); scanf("%d",&t); while(t--){ scanf("%d%d%d",&n,&l,&r); printf("%lld\n",a[n][l][r]); } return 0;}
0 0
- UVa1638 Pole Arrangement 递推
- 1638 - Pole Arrangement (递推)
- uva 1638 Pole Arrangement 递推
- UVA 1608 Pole Arrangement(递推)
- 【组合数学】Pole Arrangement, ACM/ICPC Daejeon 2012, UVa1638
- Uva 1638 Pole Arrangement 数学,递推,dp
- uva1638(递推关系,dp)
- UVa 1638-Pole Arrangement
- UVa 1638 Pole Arrangement
- uva 1638 Pole Arrangement
- UVa 1638 Pole Arrangement
- UVA 1638 Pole Arrangement
- uva 1638 Pole Arrangement
- Uva 1638 Pole Arrangement DP
- UVA 1638 - Pole Arrangement (dp计数)
- UVA 1638- Pole Arrangement(动态规划)
- ZOJ Problem Arrangement 递推+状压
- UVALive 6117 (LA 6117) Pole Arrangement 动态规划
- 【HDU】5493 Queue(2015 ACM/ICPC Asia Regional Hefei Online)
- 有什么让你相见恨晚的 iPhone 使用技巧?
- 读写进程同步问题
- 357. Count Numbers with Unique Digits
- Test Macro群中关键字(回忆Linux中find,grep)
- UVa1638 Pole Arrangement 递推
- Linux中su命令
- mysql-5.7.13-winx64的安装
- ajax返回object Object解决方法
- 运算符的优先级和结合性问题
- socket连接库,兼容ipv6,ipv4,为IOS上架做准备
- cf 689 C(二分)
- vim 全选
- spring @Autowired 注解