bzoj1008 [HNOI2008]越狱

来源:互联网 发布:一键生成淘宝客二维码 编辑:程序博客网 时间:2024/06/15 07:40

Description

  监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种。如果
相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱

Input

  输入两个整数M,N.1<=M<=10^8,1<=N<=10^12

Output

  可能越狱的状态数,模100003取余

Sample Input

2 3

Sample Output

6

HINT

  6种状态为(000)(001)(011)(100)(110)(111)


正解:组合数学+快速幂。

这题如果转化一下思路,把所有情况减去不可能越狱的情况,那就很容易了。公式为m^n-m*(m-1)^(n-1)。


//It is made by wfj_2048~#include <algorithm>#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <vector>#include <cmath>#include <queue>#include <stack>#include <map>#include <set>#define rhl 100003#define inf 1<<30#define il inline#define RG register#define ll long long using namespace std; ll m,n; il ll gi(){    RG ll x=0,q=0; RG char ch=getchar();    while ((ch<'0' || ch>'9') && ch!='-') ch=getchar(); if (ch=='-') q=1,ch=getchar();    while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar(); return q ? -x : x;} il ll qpow(RG ll a,RG ll b){ RG ll ans=1,x=a; while (b){ if (b & 1) ans=ans*x%rhl; x=x*x%rhl,b>>=1; } return ans; } il void work(){ m=gi(),n=gi(); printf("%lld",(qpow(m,n)-qpow(m-1,n-1)*m%rhl+rhl)%rhl); return; } int main(){    work();    return 0;}

0 0