NC14584. Yuanyuan Long and His Ballons
描述
Yuanyuan Long is a dragon like this picture?
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
输入描述
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
C++14(g++5.4) 解法, 执行用时: 14ms, 内存消耗: 344K, 提交时间: 2019-02-14 22:48:44
#include<stdio.h> #define fo(i,a,b) for(int i=a;i<=b;i++) int p=100000007; int T,n,k; long long f[11000][2]; int main(){ scanf("%d",&T); while (T--){ scanf("%d%d",&n,&k); f[1][0]=1; fo(i,2,n){ f[i][0]=f[i-1][1]*(k-1)%p; f[i][1]=(f[i-1][0]+f[i-1][1]*(k-2))%p; } printf("%lld\n",f[n][1]*k*(k-1)%p); } }
C++11(clang++ 3.9) 解法, 执行用时: 5ms, 内存消耗: 384K, 提交时间: 2017-12-24 15:38:24
#include<cstdio> const int mod=100000007; long long a[10001]; int main(){ int T,n,k,i; scanf("%d",&T); while(T--){ scanf("%d%d",&n,&k); a[0]=k; a[1]=0; for(i=2;i<=n;i++)a[i]=(a[i-2]*(k-1)+a[i-1]*(k-2))%mod; printf("%lld\n",a[n]); } return 0; }