"Bzoj 1008" [HNOI2008] Jailbreak

Source: Internet
Author: User

1008: [HNOI2008] Jailbreak time limit:1 Sec Memory limit:162 MB
submit:5535 solved:2370
[Submit] [Status] [Discuss] 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 a simple combinatorial math problem. In all cases, there are MN species schemes. There are m* (m-1) (n-1) species that are unlikely to occur. The former is good to understand, the latter I want to explain why. We abstract the problem into a model, lattice staining. There are different kinds of schemes for the color of two adjacent lattices. First, the first lattice is capable of dyeing m colors. The second lattice in order not to conflict with the previous lattice can be dyed m-1 color, and so on. It is concluded that except for the first lattice, the rest of the lattice can only dye m-1 color. Finally, the answer is mn-m* (m-1) (n-1), and finally take a model. We can use a fast power to calculate n times. Time complexity O (log2n)
#include <cstdio>#include<cstring>#include<iostream>using namespacestd;Const intMOD =100003; Long LongN;Long Longm;Long LongpwLong LongALong Longb) {    if(A = =0|| A = =1)returnA; BOOLp[ -]; intLen =0; Long Longans;  Do{P[len+ +] = (b &1); b>>=1; }  while(b); Ans=1;  for(inti = len-1; I >=0; --i) {ans= ans * ans%MOD; if(P[i]) ans = (ans * (a% MOD))%MOD; }    returnans;} intMain () {CIN>> m >>N; cout<< ((PW (M, N)-(m% MOD) * PW (M-1N1)% mod + MoD) <<Endl; return 0;}

"Bzoj 1008" [HNOI2008] 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.