Prison Break (inclusion-exclusion principle + Fast power)

Prison continuous numbered 1 ... N of N rooms, each room held a prisoner, there are M religions, each religion may be one of the prisoners. If the same religious prisoners in the adjacent room, it could happen to escape, how many states seeking to escape possible

Input format: input two integers M, N.1 <= M <= 108,1 <= N <= 1012

Output format: possible escape state number, analog 100003 modulo
data ranges and prompted  
six states (000) at the end (001) (011) (100) (110) (111) when the output of each row of the extra space, does not affect The answer correctness

Input Sample
23
Sample Output
6

1. Direct calculation of all the programs is not convenient to escape, are calculated considering the total number of programs using the program and do not escape inclusion and exclusion, can subtract;
2. Program total number m ^ n, since there are n rooms, each there m selection;
3. program number does not escape m * (m-1) ^ (n-1), because the first room has m choices, each of the front and rear must be different, later each selection has m-1
4. due to the modulus, it is not necessary to consider the use of high precision and attention to detail modulo.

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define ll long long
int main()
{
 ll n,m,s,s1=1,s2=1,t,k;
 scanf("%lld%lld",&m,&n);
 k=n,t=m;
 while(k)
 {
  if(k%2==1) s1=s1*t%100003;
  k/=2;
  t=t*t%100003;
 }
 k=n-1,t=m-1;
 while(k)
 {
  if(k%2==1)
  s2=s2*t%100003;
  k/=2;
  t=t*t%100003;
 }
 s=(s1-m*s2%100003+100003)%100003;
 printf("%lld\n",s);
}
Published 12 original articles · won praise 1 · views 189

Guess you like

Origin blog.csdn.net/csx_zzh/article/details/105167224