Luo Gu P3197 [HNOI2008] Jailbreak solution to a problem

P3197 [HNOI2008] escape

Title Description

Prison continuous numbered \ (1 ... N \) of \ (N \) rooms, each room held a prisoner, there \ (M \) religions, each religion may be one of the prisoners. If the same religious prisoners in the adjacent room, it is possible to escape, find how many status escape may occur.

Input Format

Input two integers \ (M, N \)

Output Format

Jailbreak possible number of states, module \ (100003 \) modulo

Sample input and output

Input # 1

2 3

Output # 1

6

Description / Tips

Six states (000) (001) (011) (100) (110) (111)

\ (1 \ M \ 10 ^ 8 \)
\ (1 \ N \ 10 ^ {12} \)

[Thinking]

Combinatorics + fast power

[Title] effect

n rooms there are prisoners of their different religious beliefs m kind of
situation people seeking the same religious belief of at least one pair of side by side

[Core] ideas

Positive with demand is difficult to find or there is no way of seeking
so hard being the anti
unable to find out the situation directly jailbreak
it obtained the overall situation and the situation does not escape the
situation without jailbreak by subtracting the total of cases
is subject It requires us to seek escape case

The overall situation
in each room of m may be, a total of n rooms
so the possibility is m ^ n th
overall situation will know
then look at the situation does not escape
the first room there are m can select the
second rooms and religion can not be the same as the first room '
so only in m-1 may be
the third and the second is the same
so there is a m and the n-1 m-1
case then no jailbreak is m * (m-1) ^ (n -1)
knows both
the case a difference can be made in order to be out of the escape

【nitty gritty】

Exponentiation great need quick power

[Complete code]

#include<iostream>
#include<cstdio>
#define int long long
using namespace std;
const int mo = 100003;

int p(int a,int b)
{
  int ans = 1;
  while(b != 0)
  {
    if(b & 1 == 1)
    {
      ans *= a;
      ans %= mo;
    }
    b /= 2;
    a = ((a % mo) * (a % mo)) % mo;
  }
  return ans;
} 

signed main()
{
  int n,m;
  cin >> m >> n;
  cout << ((p(m,n) % mo - (m * p(m - 1,n - 1)))%mo + mo ) % mo;//先做减法,因为减法之后可能出现负数,但是这个负数的绝对值一定会小于m的 ,因为这是两个已经%过m的数,保证小于m所以做的差的绝对值也一定小于m,只需要将这个可能是服饰的数加上mo保证是正数之后再%一遍mo 
  return 0;
}

Guess you like

Origin www.cnblogs.com/acioi/p/11729219.html