[BZOJ2463] [Zhongshan election 2009] Who can win?
Time Limit: 1 Sec Memory Limit: 162 MB
Description
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
Input two integers M, N.1 <= M <= 10 ^ 8,1 <= N <= 10 ^ 12
Output
It may escape state number, modulo mold 100003
Sample Input
2 3
Sample Output
6
HINT
Six states (000) (001) (011) (100) (110) (111)
Source
[Submit]
Thinking
The question is not difficult, mainly permutations and combinations. If there are M number of the first species of options, the latter has M - 1 kinds of programs. Thus, the scheme does not meet the full set minus the meaning of the questions is the answer.
Code
#include <cctype>
#include <cstdio>
#include <iostream>
long long nextInt()
{
long long num = 0ll;
char c;
bool flag = false;
while ((c = std::getchar()) == ' ' || c == '\r' || c == '\t' || c == '\n');
if (c == '-')
flag = true;
else
num = c - 48;
while (std::isdigit(c = std::getchar()))
num = num * 10 + c - 48;
return (flag ? -1 : 1) * num;
}
const long long M = 100003ll;
long long qpow(long long x, long long t)
{
long long ans = 1;
while (t)
{
if (t & 1)
ans = (ans * x) % M;
x = (x * x) % M;
t >>= 1;
}
return ans;
}
int main(int argc, char **argv)
{
long long m = nextInt(), n = nextInt();
std::cout << (M + qpow(m, n) % M - m * qpow(m - 1, n - 1) % M) % M << std::endl;
#ifdef __EDWARD_EDIT
std::cin.get();
std::cin.get();
#endif
return 0;
}