HIT15:Birthday Party(dp)

来源:互联网 发布:静音键盘推荐知乎 编辑:程序博客网 时间:2024/04/29 00:31

J - J - Birthday Party

Time limit : 1 sMemory limit : 512 mbSubmitted : 53Accepted : 1964bit Integer Format : %lld

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

题意:有张N个座位的圆形桌,有M种菜,要求相邻座位的菜式不一样,问有多少种分配方法。

思路: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;}



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 搜淘宝找不到关键词和店铺怎么办 小超市开在一起竞争太大怎么办 淘宝店铺被投诉盗图怎么办 充电宝ic坏了怎么办 淘宝店宝贝权重下降怎么办 淘宝卖家评分低怎么办 淘宝买东西客服不理人怎么办 支付宝本次交易嫌疑违规怎么办 支付宝一年的交易总额怎么办 交易关闭钱扣了怎么办 交易猫账号绑定支付宝打不开怎么办 拼多多涉假处罚怎么办 淘宝店铺重复铺货扣6分怎么办 帆布鞋子买大了怎么办 开淘宝店没销量怎么办 开淘宝店没有销量怎么办 淘宝买东西支付密码错了怎么办 淘宝登入密码忘记了怎么办 逛街时手机没电了怎么办 逛街手机没有电了怎么办 卖家拒收退回来怎么办 群英会奖池钱不够买超了怎么办 三星s8卡顿严重怎么办 三星s8变卡了怎么办 新开店铺没有人怎么办 电视显示没有usb设备怎么办 华为平板电脑触摸屏没反应怎么办 平板电脑液晶显示屏坏了怎么办 6p的home键失灵怎么办 读书郎屏幕坏了怎么办 苹果平板激活锁忘了怎么办 平板电脑充电插口坏了怎么办? 电视频幕花了怎么办 苹果笔记本电脑硬盘坏了怎么办 mac屏幕压坏了怎么办 新电脑连不上网怎么办 新买电脑后悔了怎么办 装显卡后玩游戏黑屏怎么办 顺丰寄主机坏了怎么办 征信账户忘记了怎么办 疑似qq和疑似应用宝怎么办