Solution to a problem P3197 [HNOI2008] Jailbreak

The so-called "counter-being is difficult," we consider this issue from the opposite side.

Obviously there: it happens jailbreak = total number of cases - the number of cases jailbreak does not occur.

Consider the case of calculating the total number of cases and the jailbreak does not occur.

The total number of cases there is (n-\) \ grid, each grid has \ (\ m) several selection schemes. I.e. \ (n-m ^ \) .

Similarly, where the number of escape is that there does not occur \ (n-\) grid, the first \ (1 \) lattices have \ (m \) choices, others have lattice \ (m-1 \) choices of the number of programs. I.e. \ (m \ Times (. 1-m). 1-n-^ {} \) .

Then the final answer is more the result of subtracting the two equations. Note that with the rapid power maintenance, note-taking, careful not to burst longlong.

code:

#include<bits/stdc++.h>
using namespace std;
#define re register
#define ll long long
#define il inline
#define dou double
#define un unsigned
il ll read()
{
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return (ll)x*f;
}
#define INF 114514114
#define clr(x) memset(x,0,sizeof(x))
#define P 100003
ll m,n,s1,s2;
il ll fast_pow(ll a,ll n,ll p)
{
    if(n==0)return 1%p;
    if(n==1)return a%p;
    ll ret=fast_pow(a,n/2,p);
    ret=(ll)ret*ret%p;
    if(n&1)ret=(ll)ret*a%p;
    return ret;
}
int main()
{
    m=read();n=read();
    s1=fast_pow(m,n,P);
    s2=(ll)fast_pow(m-1,n-1,P)*m%P;
    cout<<(s1+P-s2)%P<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/Hakurei-Reimu/p/11482214.html