Bzoj 1008 Jailbreak

Source: Internet
Author: User

The topics are as follows:

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 Input

2 3

Sample Output

6

This problem, a look at the data, first think of the log-level algorithm. Then look at the topic, the data structure is certainly not expected. Is the number of States, so consider the principle of recursion and multiplication. If you try recursion, you will find that both recursive and general formulas are hard to come by. So let's change an angle. Just ask for it, and try it backwards. It is clear that the number of =MN is------all states. If the method is inverted, the next need to calculate is that the number of cases without jailbreak =m* (M-1) N-1. Why is it? For each religion (M) there will be M-1 religions that are adjacent to him and will not be jailbroken, so how many of these m-1 religions are arranged? That is (M-1) N-1 species, so there is m* (M-1) N-1 kind of situation will not occur jailbreak. That's clear. So the answer is that all cases minus the number of cases without jailbreak--------mn-m* (M-1) N-1. Fast power can be.

#include <cstdio>#include<iostream>#defineMoD 100003#definell Long Long intusing namespaceStd;ll M,n;ll Quick (ll a,ll N) {ll B=1;  while(n>0){        if(n&1) {b=b*a%MoD; } A=a*a%MoD; N=n>>1; }    returnb;}intMain () {scanf ("%lld%lld",&m,&N); M%=MoD; ll Ans1=quick (m,n)%MoD; ll Ans2=m*quick (M-1, N-1)%MoD; ll ans=ans1+mod-Ans2; Ans%=MoD; printf ("%lld", ans);}

Bzoj 1008 Jailbreak

Related Article

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.