Bzoj 1008 hnoi2008 jailbreak quick power

Source: Internet
Author: User

Three jailbreaking officers have been arrested by the police after they have killed the prison guard and escaped from jail using the police server ...... Ah, it's far away.

The prison has n Rooms numbered 1... n consecutively. One prisoner is held in each room. There are m religions, and each prisoner may believe in one of them. If the prisoners in the adjacent room share the same religion, jailbreak may occur.

M <= 10 ^ 8, n <= 10 ^ 12

The data range is logn! So let's analyze it.

F [I] indicates the number of consecutive room jailbreaking events

There are two situations when the status is changed from F [I] to f [I + 1:

1. The original status will be jailbroken. If we leave another person in the room, the number of jailbroken cases will certainly be f [I] * m.

2. The original status will not be jailbroken, so we only need to plug in a person with the same beliefs as the person next to it. The number of jailbreak cases is m ^ I-f [I].
The recursive formula F [I + 1] = f [I] * (m-1) + m ^ I is obtained.

This is matrix multiplication! Trouble! Not written! Mom!

Let's change our thinking and find a complementary set.

F [I] indicates the number of consecutive room jailbreak failures

There are two situations when the status is changed from F [I] to f [I + 1:

1. The original status will be jailbroken. If we leave another person in the room, the number of jailbroken cases will certainly be 0.

2. The original status will not be jailbroken, so we only need to plug in a person with a different belief from the person next to it. The number of jailbroken cases is f [I] * m-1)

Therefore, F [I + 1] = f [I] * (m-1)

Initial Value f [1] = m. The final result is reduced by m ^ n.

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define M 100003llusing namespace std;typedef long long ll;ll m,n,ans;ll ksm(ll x,ll y){ll re=1;while(y){if(y&1)re*=x,re%=M;x*=x,x%=M;y>>=1;}return re;}int main(){cin>>m>>n;m%=M;ans=ksm(m,n);ans-=m*ksm(m-1,n-1);ans%=M;ans+=M;ans%=M;cout<<ans<<endl;}


Bzoj 1008 hnoi2008 jailbreak quick power

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.