【SSL】20210808D
题目描述
有n个人依次排队打饭,有m种饭菜可以选择,每个人可能选择其中一种,如果相邻排队的人打的菜一样,那么就会影响彼此吃饭的心情,求这个队伍中有人被影响心情的状态数,对100003取余。
输入格式
一行整数依次给出m,n
输出格式
相应的状态数
输入样例
2 3
输出样例
6
样例解释
样例解释
000 001
011 100
110 111
6种
解题思路
在草稿纸上列出多种方案,找规律,最终得到
ans=m^n-m*(m-1) ^ (n-1)
要使用快速幂和龟速乘!
Code
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
long long inf=100003;
typedef long long ll;
ll slow_chen(ll x,ll n)
{
ll res = 0;
while(n > 0){
if(n & 1) res = res + x %inf;
x = (x%inf + x %inf)%inf;
n >>= 1;
}
return res;
}//龟速乘,以避免负数情况
ll quick_pow(ll x,ll n){
ll res = 1;
while(n > 0){
if(n & 1) res = slow_chen(res,x)%inf;
x = slow_chen(x,x)%inf;
n >>= 1;
}
return res%inf;
}
ll fast_pow(ll x,ll n){
ll res = 1;
while(n > 0){
if(n & 1) res = slow_chen(res,x) ;
x = slow_chen(x,x) ;
n >>= 1;
}
return res;
} //第二种快速幂,不用取余,以避免负数
int main()
{
ll m,n;
long long ans;
cin>>m>>n;
// cout<<quick_pow(m,n)<<endl;
ans=((fast_pow(m,n)-(m*quick_pow(m-1,n-1)%inf)%inf))%inf;
cout<<ans;
}