Mutual Training for Wannafly Union #8 D - Mr.BG Hates Palindrome 取余

本文介绍了一个有趣的编程挑战:计算Mr.BG在特定条件下能够形成的非回文字符串的数量。通过数学公式和高效的取余操作解决大数问题,并提供了一个C++实现示例。

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\n",k++,(po(n,m)-po((n+1)/2,m)+mod)%mod);
}
return 0;
}

转载于:https://www.cnblogs.com/BobHuang/p/6775613.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值