Jailbreak (fast power)

prison Break

Title description The
prison has N rooms consecutively numbered 1...N. Each room holds a prisoner. There are M religions, and each prisoner
may believe in one of them. If prisoners in adjacent rooms are of the same religion, escape may occur.
How many states may escape from prison?

Input
Input two integers
M (1<=M<=10^8)
N (1<=N<=10^12)

Output number of
possible jailbreak states, modulo 100003 take the remainder

Sample input
2 3

Sample output
6

Prompt that the
6 states are (000) (001) (011) (100) (110) (111)

#include<stdio.h>
#include<math.h>
int h=100003;
int f(long long  x,long long y)
{
    
    
    x=x%h;
    long long s=1;
    while(y>0)
    {
    
    
        if(y%2==1)//指数为奇数时 
        s=(s*x)%h;//单独×一个底数 
        y=y/2;//指数变为原来的1/2 
        x=(x*x)%h;//底数扩大原来的平方 
    }
    return s;
}
int main()
{
    
    
    long long n, m;
    scanf("%lld%lld",&m,&n);
    long long y=m,x=m;
    y=f(m,n);x=((m%h)*f(m-1,n-1))%h;
    if(x>y)
        y+=100003;
    printf("%lld\n",y-x);
}

Guess you like

Origin blog.csdn.net/m0_46381590/article/details/111416511