Mutual Training for Wannafly Union #8 D

Mr.BG is very busy person. So you have been given enough time (1000 milliseconds) to help him.

Mr. BG has a bag of marbles with different alphabets written on them. And he has become busy on playing with these marbles by putting them in N boxes placed in a row. There are exactly M distinct type of marbles, N of each type.

Now he puts only N marbles (out of M*N) in N boxes, one by one and upon completion he writes down the letters on the marbles on a paper to form a string. As Mr.BG hates palindrome strings (strings which read same from both sides e.g. MADAM), he erases palindrome string from the paper as soon as he finds one.

Now he is wondering how many different strings he might get on his paper if he could try all possible combination of putting the marbles in the boxes. So you have to help him by answering. As there could be many strings so print it modulo 1,000,000,007.

Input

Input starts with an integer TC(<=10), denoting the number of test cases. Each case starts with two non negative integers N(<=100000) and M(<=26) as described above.

Output

For each case, print the case number and total number of strings written on the paper modulo 1000000007.

Example

Input:
2
2 2
2 3
Output:
Case 1: 2
Case 2: 6

大佬给我们讲的题解,一句带过,我现在还不是很清楚那个公式是怎么来的,先标记下这个题目,m的n方-m的(n+1)/2次方,然后就是大数取余了,wa了一次,因为取余会出现负数,所以要先加上mod

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod=1e9+7;
LL po(LL n,LL m){
LL s=m%mod;
for(LL i=1;i<n;i++)
s=(s*m)%mod;
return s;}
int main(){
int t,k=1;
scanf("%d",&t);
while(t--){
LL n,m;
scanf("%lld%lld",&n,&m);
printf("Case %d: %lld
",k++,(po(n,m)-po((n+1)/2,m)+mod)%mod);
}
return 0;
}
原文地址:https://www.cnblogs.com/BobHuang/p/6775613.html