题目描述

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. BestCoder Round #40 解题报告

    这场是第一场没有米的BC... 大概也是想震一震那些一听说没米了就不打BC的人吧 这次的题目质量比以往高了许多 (然而我并没有打这一场BC 但是今天下午到现在做的过程中真的学到了不少知识呢 A题略水. ...

  2. oozie与mapreduce简单案例

    准备工作  拷贝原来的模板 mkdir oozie-apps cd oozie-apps/ cp -r ../examples/apps/mar-reduce . mv map-reduce mr-w ...

  3. 在Unity中实现屏幕空间反射Screen Space Reflection(3)

    本篇讲一下相交检测的优化.有两个措施. 线段相交检测 之前的检测都是检测光线的终点是否在物体内.我们可以尝试检测光线的线段是否与物体相交. 比如说有一个非常薄的物体,光线差不多垂直于它的表面.如果用普 ...

  4. F - Debate CodeForces - 1070F 思维

    题目链接:https://vjudge.net/problem/CodeForces-1070F 具体思路:首先把所有的00放进去,然后对于10 和01 的取两个数目最小值t,排完序后将前t个加起来, ...

  5. 用js拼接url为pathinfo模式

    用js拼接url为pathinfo模式

  6. Coursera在线学习---第三节.归一化处理(Normalize)

    一.归一化(也说标准化)作用 1)将有量纲特征转化为无量纲特征 2)能够加快收敛(主要指梯度下降法时) 二.Octave中计算          mean(A)   求解矩阵中每一列的均值 std(A ...

  7. 贪心算法_01背包问题_Java实现

    原文地址:http://blog.csdn.net/ljmingcom304/article/details/50310789 本文出自:[梁敬明的博客] 1.贪心算法 什么是贪心算法?是指在对问题进 ...

  8. GO-指针与函数

    一.指针类型 1.普通类型,变量存的就是值,也叫值类型.指针类型存的是地址 2.获取变量的地址,用&,比如:var a int, 获取a的地址 &a 3.指针类型,变量存的是一个地址, ...

  9. IP负载均衡技术

    参考链接:http://www.360doc.com/content/12/1117/19/820209_248442094.shtml

  10. C语言使用数学库编译不通过问题

    #include <stdio.h>#include <math.h> int main(){        double a = 10.0,b = 3.0;        f ...