HIT15:Birthday Party(dp)
来源:互联网 发布:静音键盘推荐知乎 编辑:程序博客网 时间:2024/04/29 00:31
J - J - Birthday Party
Submit
Problem Description
Mr. Frog's birthday party will be held tomorrow. His friends will come to celebrate his birthday. He invited N friends(including himself) to come to his party. For some reason, he had not enough time to prepare for the party. He only cooked M(M<N) dishes. As a tradition, each dish should be placed in front of a person so that the number of dishes should be equal to the number of person who come to the party. It will be embarrassing if the number of dishes is not enough. So Mr. Frog planned to place the dishes in some ways so that the embarrassment will not be easy to find out.
The table is round and his friend will sit around the table together with him. If two dishes placed next to each other are same, it will be easy for his friends to find the embarrassment. Mr. Frog is wonder to know the number of ways to place the dishes. Certainly, the number of dishes placed on the table can be less than M. Please notice that the dishes will be placed on the table in front of each of his friends, which means the following two pictures shows different ways to place the dishes.
Input
First line is an integer T, which means the number of test cases.
The following T lines has T test cases. Each line has two integers, which indicates N and M.(4<=N<M<=100000)
Output
For each test case, output the case number and the number of ways Mr. frog can place the dishes. Since the number is too large, you should output the answer mod 100000007.
Sample Input
2
5 3
5 4
Sample Output
Case #1: 30
Case #2: 240
Hint
For the first case, the following ways are allowed(the first dish is next to the last dish to form a circle):
1-2-1-2-3 2-1-2-3-1 1-2-3-1-2 2-3-1-2-1 3-1-2-1-2
2-1-2-1-3 1-2-1-3-2 2-1-3-2-1 1-3-2-1-2 3-2-1-2-1
3-1-3-1-2 1-3-1-2-3 3-1-2-3-1 1-2-3-1-3 2-3-1-3-1
1-3-1-3-2 3-1-3-2-1 1-3-2-1-3 3-2-1-3-1 2-1-3-1-3
2-3-2-3-1 3-2-3-1-2 2-3-1-2-3 3-1-2-3-2 1-2-3-2-3
3-2-3-2-1 2-3-2-1-3 3-2-1-3-2 2-1-3-2-3 1-3-2-3-2
思路:dp[i][0]表示第i个座位与第1个座位菜式一样的方案数,dp[i][1]表示第i个座位与第1个座位菜式不一样的方案数,答案为dp[n][1]。
# include <bits/stdc++.h># define ll long longusing namespace std;const int maxn = 1e5;const int mod = 100000007;ll dp[maxn+3][2];int main(){ int t, n, m, cas=1; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); dp[1][0] = m; dp[1][1] = 0; for(int i=2; i<=n; ++i) { dp[i][0] = dp[i-1][1]; dp[i][1] = (dp[i-1][0]*(m-1) + dp[i-1][1]*(m-2)) % mod; } printf("Case #%d: %lld\n",cas++, dp[n][1]); } return 0;}
- HIT15:Birthday Party(dp)
- Birthday (背包dp)
- July birthday party
- Anniversary party(树形dp)
- Anniversary party(树形DP)
- Anniversary party(树形DP)
- Anniversary party (树形dp)
- UVA 12002 Happy Birthday(dp+LIS)
- 【LightOJ 1104】Birthday Paradox(概率DP)
- hdu(1520) Anniversary party(树形dp)
- poj 2342 Anniversary Party(树形dp)
- HDU 1520 Anniversary party (树状dp)
- hdu 1520 Anniversary party(树dp)
- POJ 2342 Anniversary party(树形DP)
- hdu 1520 Anniversary party(树形dp)
- hdu 3506 monkey party(环形dp)
- 树形dp Anniversary party(HDU1520)
- poj 2342Anniversary party(树形dp)
- 如何解决安装多个桌面时出现 account-plugin-google error 的情况
- log4j配置文件的使用详解
- 小脚本之windows批量修改文件后缀名
- Postman使用说明补充
- 单例模式
- HIT15:Birthday Party(dp)
- ZZZZ
- ORACLE 中exist和in的区别
- Java 随笔
- ajax传对象的方式
- 一段有趣的话
- 解决No resource found that matches the given name :Theme.AppCompat.Light error: Error retrieving paren
- java基础学习——局部变量、实例变量、静态变量
- 解析ISO8583报文实例