监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种。如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱
监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种。如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱
输入两个整数M,N.1<=M<=10^8,1<=N<=10^12
可能越狱的状态数,模100003取余
6种状态为(000)(001)(011)(100)(110)(111)
题目链接:
http://www.lydsy.com/JudgeOnline/problem.php?id=1008
求可能越狱的方案数,可以用方案总数减去不可能越狱的方案数,快速幂即可。。
#include<iostream> #include<cstdio> #define LL long long using namespace std; LL n,m; LL mod; LL power(LL x,LL y){ LL s=1; if(x==0) return 1; if(x&1){ s=power(x/2,y); s=(s*s*y)%mod; } else{ s=power(x/2,y); s=(s*s)%mod; } return s; } int main(){ LL ans; mod=100003; scanf("%lld%lld",&m,&n); m=m%mod; ans=((power(n,m)-(m*power(n-1,m-1)%mod))+mod)%mod; printf("%lld\n",ans); return 0; }
This passage is made by Iscream-2001.
BZOJ 1008--[HNOI2008]越狱(容斥&快速幂)
原文:https://www.cnblogs.com/Yuigahama/p/9651974.html