首页 > 代码库 > 1008: [HNOI2008]越狱

1008: [HNOI2008]越狱

1008: [HNOI2008]越狱

Time Limit: 1 Sec  Memory Limit: 162 MB
Submit: 9035  Solved: 3925
[Submit][Status][Discuss]

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)

 

Source

/* * Author: lyucheng * Created Time:  2017年05月17日 星期三 09时24分52秒 * File Name: BZOJ-1008.cpp *//* 题意:有n间监狱,每个监狱住着一个犯人,有m种信仰,每个犯人都对有一种信仰,如果两个相邻的犯人的信仰相同,那么就会发生越狱 *      问你总共有多少种越狱的情况 * * 思路:排列组合问题,总的情况有m^n种,考虑无法越狱的情况:第一个犯人有m中信仰,接下来每个犯人都有m-1中信仰(简单画画就会 *      出结果),那么得到结果:m^n-m*(m-1)^(n-1) * */#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <algorithm>#include <string>#include <vector>#include <stack>#include <queue>#include <set>#include <time.h>#define LL long longusing namespace std;#define mod 100003LL power(LL a,LL b){    if(b==0) return 1;    LL cnt=power(a,b/2);    cnt=cnt*cnt%mod;    if(b%2==1) cnt=cnt*a%mod;    return cnt;}LL m,n;int main(int argc, char* argv[]){     //freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout;    scanf("%lld%lld",&m,&n);    LL res=power(m,n);    res=(res-m*power(m-1,n-1)%mod)%mod;    while(res<mod) res+=mod;    printf("%lld\n",res%mod);    return 0;}

 

1008: [HNOI2008]越狱