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