Yuanyuan Long and His Ballons
题目
One day, Yuanyuan Long gets n white ballons and k kinds of pigment, and he thought a problem:
-
Number these ballons b1, b2, … , bi, …, to bn.
-
Link these ballons to a circle in order, and color these ballons.
-
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
关于环形染色(任意俩个格子不同色):(m-1)^n + (-1) ^n*(m-1) 注:m为颜色,n为格子
证明:
#include<iostream>
#include<math.h>
using namespace std;
const int mod=100000007;
#define int long long
long long power(int a,int n){//必须全部 long long
//数学公式 (m-1)的n次+(m-1)*(-1)的n次(n-2)
long long ans=1;
while(n){
if(n%2){
ans=(ans*a)%mod;//模
}
a=(a*a)%mod;//模
n/=2;
}
return ans;
}
signed main(){
int t;
cin>>t;
int n,m;
long long sum;
while(t--){
sum=0;
cin>>n>>m;
sum=power(m-1,n)+pow(-1,n)*((m-1)%mod);
cout<<sum<<endl;
}
return 0;
}