题目描述

Yuanyuan Long is a dragon like this picture?

                                   
I don’t know, maybe you can ask him. But I’m sure Yuanyuan Long likes ballons, he has a lot of ballons.          

One day, he gets n white ballons and k kinds of pigment, and he thought a problem:

1.      Number these ballons b1, b2,  … , bi, …,  to bn.

2.      Link these ballons to a circle in order, and color these ballons.

3.      He need to make sure that any neighbor ballons have different colors.

He wants to know how many solutions to solve this problem. Can you get the answer to him? The answer maybe very large, get the answer MOD 100000007.

For Example: with 3 ballons and 3 kinds of pigment

Your answer is 3*2*1 MOD 100000007 = 6. 
The answer maybe large, you can use integer type long long.

输入描述:

The first line is the cases T. ( T <=
100)
For next T lines, each line contains n and
k. (2<= n <= 10000, 2<= k
<=100)

输出描述:

For each test case, output the answer on
each line.
示例1

输入

3
3 3
4 2
5 3

输出

6
2
30

题解

$dp$。

$dp[i][j]$表示在第$1$个人涂第一种颜色,涂完$i$个人,且第$i$个人涂第$j$种颜色的方案数。

$sum = dp[n][2]+...+dp[n][k]$,答案就是$sum*k$。

有很多优化可以搞,什么优化都没做就过了......

#include<cstdio>
using namespace std; long long mod = 100000007LL;
long long dp[10010][110]; int main() {
int T, n, k;
scanf("%d", &T);
while(T --) {
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= k; j ++) {
dp[i][j] = 0;
}
}
dp[1][1] = 1;
for(int i = 2; i <= n; i ++) {
long long sum = 0;
for(int j = 1; j <= k; j ++) {
sum = (sum + dp[i - 1][j]) % mod;
}
for(int j = 1; j <= k; j ++) {
dp[i][j] = (sum - dp[i - 1][j] + mod) % mod;
}
}
long long sum = 0;
for(int j = 2; j <= k; j ++) {
sum = (sum + dp[n][j]) % mod;
}
sum = sum * k % mod;
printf("%lld\n", sum);
}
return 0;
}

  

湖南大学ACM程序设计新生杯大赛(同步赛)H - Yuanyuan Long and His Ballons的更多相关文章

  1. 湖南大学ACM程序设计新生杯大赛(同步赛)J - Piglet treasure hunt Series 2

    题目描述 Once there was a pig, which was very fond of treasure hunting. One day, when it woke up, it fou ...

  2. 湖南大学ACM程序设计新生杯大赛(同步赛)A - Array

    题目描述 Given an array A with length n  a[1],a[2],...,a[n] where a[i] (1<=i<=n) is positive integ ...

  3. 湖南大学ACM程序设计新生杯大赛(同步赛)L - Liao Han

    题目描述 Small koala special love LiaoHan (of course is very handsome boys), one day she saw N (N<1e1 ...

  4. 湖南大学ACM程序设计新生杯大赛(同步赛)B - Build

    题目描述 In country  A, some roads are to be built to connect the cities.However, due to limited funds, ...

  5. 湖南大学ACM程序设计新生杯大赛(同步赛)I - Piglet treasure hunt Series 1

    题目描述 Once there was a pig, which was very fond of treasure hunting. The treasure hunt is risky, and ...

  6. 湖南大学ACM程序设计新生杯大赛(同步赛)E - Permutation

    题目描述 A mod-dot product between two arrays with length n produce a new array with length n. If array ...

  7. 湖南大学ACM程序设计新生杯大赛(同步赛)D - Number

    题目描述 We define Shuaishuai-Number as a number which is the sum of a prime square(平方), prime cube(立方), ...

  8. 湖南大学ACM程序设计新生杯大赛(同步赛)G - The heap of socks

    题目描述 BSD is a lazy boy. He doesn't want to wash his socks, but he will have a data structure called ...

  9. 湖南大学ACM程序设计新生杯大赛(同步赛)C - Do you like Banana ?

    题目描述 Two endpoints of two line segments on a plane are given to determine whether the two segments a ...

随机推荐

  1. AngularJs附件上传下载

    首先:angular-file-upload 是一款轻量级的 AngularJS 文件上传工具,为不支持浏览器的 FileAPI polyfill 设计,使用 HTML5 直接进行文件上传. 第一步: ...

  2. 优先队列 逆向思维 Gym 101128C

    题目链接:http://codeforces.com/gym/101128/my 具体题目大意可以看这个人的:http://blog.csdn.net/v5zsq/article/details/61 ...

  3. 常用的20个强大的 Sublime Text 插件

    作为一个开发者你不可能没听说过 Sublime Text.不过你没听说过也没关系,下面让你明白. Sublime Text是一款非常精巧的文本编辑器,适合编写代码.做笔记.写文章.它用户界面十分整洁, ...

  4. 用Matlab实现字符串分割(split)

    用Matlab实现字符串分割(split)Posted on 2011/08/08 Matlab的字符串处理没有C#强大,本身又没有提供OO特性,需要依赖别的手段完成这项任务. 我们在这里借助正则表达 ...

  5. 【BZOJ】1778: [Usaco2010 Hol]Dotp 驱逐猪猡

    [题意]给定无向图,炸弹开始在1,在每个点爆炸概率Q=p/q,不爆炸则等概率往邻点走,求在每个点爆炸的概率.n<=300. [算法]概率+高斯消元 [题解]很直接的会考虑假设每个点爆炸的概率,无 ...

  6. 面试整理(2)跨域:jsonp与CORS

    问题:跨域有哪些方法?jsonp的原理是什么? jsonp: 先说jsonp,jsonp的主要原理是利用script标签的src可以跨域请求,据说有src属性的都可以跨域请求,但script标签返回的 ...

  7. flex图片布局

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>f ...

  8. Django之kindeditor

    1.什么是kindeditor? KindEditor是一套开源的HTML可视化编辑器,主要用于让用户在网站上获得所见即所得编辑效果,兼容IE.Firefox.Chrome.Safari.Opera等 ...

  9. 【译】第二篇 SQL Server代理作业步骤和子系统

    本篇文章是SQL Server代理系列的第二篇,详细内容请参考原文. SQL Server代理作业由一系列的一个或多个作业步骤组成.一个作业步骤分配给一个特定的作业子系统(确定作业步骤去完成的工作). ...

  10. docker-It's possible that too few managers are online. Make sure more than half of the managers are online.

    问题:docker ---- It's possible that too few managers are online. Make sure more than half of the manag ...