越狱[数论+快速幂]

传送门

一共有m^n种情况 , 不发生越狱的有 m * (m-1) * (m-1) ... (n-1 个 m-1)

因为第一个人有m个可以选 , 后面的只有m-1 个可以选 , 快速幂乘一下就可以了


#include<bits/stdc++.h>
#define LL long long
#define P 100003
using namespace std;
LL n,m; // m^n - m * [(m-1) ^ (n-1)]
LL Mod(LL x){while(x<0) x+=P; return x%P;} 
LL power(LL a,LL b){
	LL ans = 1;
	for(;b;b>>=1){
		if(b&1) ans = (ans * a) % P;
		a = (a * a) % P;
	}return ans;
}
int main(){
	scanf("%lld%lld",&m,&n);
	printf("%lld",Mod( power(m,n)%P - (m * power(m-1,n-1)) % P ));
	return 0;
}

猜你喜欢

转载自blog.csdn.net/sslz_fsy/article/details/84484591