[HNOI2008] [BZOJ1008] Jailbreak

Source: Internet
Author: User

Description

The prison has consecutively numbered 1 ... n rooms of N, one prisoner in each room, a religion of M, and every prisoner may believe in one. If the inmates of the adjoining room are of the same religion, a jailbreak may occur, begging for the number of possible escapes

Input

Enter a two integer m,n.1<=m<=10^8,1<=n<=10^12

Output

Number of possible jailbreak states, modulo 100003 take-up

Sample Input2 3Sample Output6HINT

6 states of (000) (001) (011) (100) (110) (111)

This is titled A combinatorial Mathematics topic, I am the mathematics konjac Konjac, looked the problem after only then understood. In fact, the key is reverse thinking.

N Rooms m religions, the total number of states that is m^n, if not jailbreak, that is, adjacent rooms of two people religion is different, so the first person has m choice, then each person has m-1 choice, so not jailbreak the total number of States I m* (m-1) ^ (n-1); so the number of jailbreak states is m^n-m* ( m-1) ^ (n-1).

Solve this problem, the quick power to solve the good.

#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#defineMoD 100003#definell Long Longusing namespacestd;ll m,n;ll Qpow (ll A,ll b) {ll C=1, d=a%MoD;  while(b>0)    {        if(b&1) C= (c%mod*d%mod)%MoD; b>>=1; D= (d%mod*d%mod)%MoD; }    returnC;}intMain () {scanf ("%lld%lld",&m,&N); Long Longans=Qpow (m,n); Ans=ans-m*qpow (M-1, N-1)%MoD; if(ans<0) ans+=MoD; printf ("%lld", ans); return 0;}

[HNOI2008] [BZOJ1008] Jailbreak

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.