快速幂 -Yuanyuan Long and His Ballons -环形染色(2021-08-17)

Yuanyuan Long and His Ballons

题目
One day, Yuanyuan Long 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

关于环形染色(任意俩个格子不同色):(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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值