题目大意
有一个n个元素的序列,有m种颜色。求有相邻元素颜色相同的序列总数。
对100003取模。
解题思路
答案的情况太多了!!!
所以需要正难则反,考虑没有相邻元素相同颜色的序列总数。
第一个元素可以有
m
m
种颜色,后面的元素只有种元素可选。所以不合法的方案数为
m∗(m−1)n−1
m
∗
(
m
−
1
)
n
−
1
又总方案数为
mn
m
n
。所以作个差就是了。
快速幂一下。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define LL long long
#define mo 100003
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
LL n,m,x,y,ans;
LL ksm(LL x,LL y){
LL rs=1;
for(;y;y>>=1,x=(x*x)%mo)if(y&1)rs=(rs*x)%mo;
return rs;
}
int main(){
scanf("%lld%lld",&m,&n);
x=ksm(m,n);
y=(m*ksm(m-1,n-1))%mo;
ans=(x-y+mo)%mo;
printf("%lld",ans);
return 0;
}