Snail Alice
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Problem Description
Snail Alice is a snail indulged in math. One day, when she was walking on the grass, suddenly an accident happened. Snail Alice fell into a bottomless hole, which was deep enough that she kept falling for a very long time. In the end, she caught the lateral wall of the hole and stop falling down. She named the place where she stopped “lucky place” immediately.
Snail Alice decided to climb up along the wall from the “lucky place”. The first day she climbed up q0 (q is a positive constant integer) metres, but at night when she fell asleep, she fell down q1 metres. She was shocked when she woke up, and she decided to make an extra effort. The second day she finally climbed up q2 metres. To her surprise, she fell down faster because of the tiredness. She fell down q3 metres at night. The longer she climbed up the longer she fell down. But finally, she still climbed out of the hole and slept on the ground.
Lying on the grass safe, she was curious about a question. How many metres was the “lucky place” down under the ground? She remembered that the sum of the times of her climbing up and falling down is n(of course, n is odd), so the distance between the ground and the “lucky place” must be 1-q+q2-q3+...+(-1)n-1qn-1. Snail Alice simplified that long formula and get a beautiful result: (qn+1)/(q+1). But as a math professor, she wouldn’t stop. She came up with a good problem to test her students. Here is the problem:
A function f(n), n is a positive integer, and
Given q and n, please calculate the value of f(n).Please note that q and n could be huge.
Snail Alice decided to climb up along the wall from the “lucky place”. The first day she climbed up q0 (q is a positive constant integer) metres, but at night when she fell asleep, she fell down q1 metres. She was shocked when she woke up, and she decided to make an extra effort. The second day she finally climbed up q2 metres. To her surprise, she fell down faster because of the tiredness. She fell down q3 metres at night. The longer she climbed up the longer she fell down. But finally, she still climbed out of the hole and slept on the ground.
Lying on the grass safe, she was curious about a question. How many metres was the “lucky place” down under the ground? She remembered that the sum of the times of her climbing up and falling down is n(of course, n is odd), so the distance between the ground and the “lucky place” must be 1-q+q2-q3+...+(-1)n-1qn-1. Snail Alice simplified that long formula and get a beautiful result: (qn+1)/(q+1). But as a math professor, she wouldn’t stop. She came up with a good problem to test her students. Here is the problem:
A function f(n), n is a positive integer, and
Given q and n, please calculate the value of f(n).Please note that q and n could be huge.
Input
Input consists of multiple test cases, and ends with a line of “-1 -1 -1”.
For each test case:
The first line contains three integers x1, y1 and z1, representing q. q=x1^y1+z1.
The second line contains two integers: y2 and z2, representing n. n=2^y2+z2.
The third line contains a single integer P, meaning that what you really should output is the formula’s value mod P.
Note: 0<=x1,y1,z1,y2,z2<=50000, z1>0, 0<P<100000000
For each test case:
The first line contains three integers x1, y1 and z1, representing q. q=x1^y1+z1.
The second line contains two integers: y2 and z2, representing n. n=2^y2+z2.
The third line contains a single integer P, meaning that what you really should output is the formula’s value mod P.
Note: 0<=x1,y1,z1,y2,z2<=50000, z1>0, 0<P<100000000
Output
For each test case, print one line containing an integer which equals to the formula’s value mod P.
Sample Input
2 1 3 0 0 32551 3 0 5 0 2 70546 -1 -1 -1
Sample Output
1 31
这题就是求f(n),然后q=x1^y1+z1,n=2^y2+z2。所以q和n都很大。
之后,因为n是2^y2+z2,相当大,因此可以先减去1,得到2^y2-1+z2。根据矩阵快速幂,对于指数的二进制是1的,需要将底数和答案乘一次,如果二进制是0,则不需要。因此观察2^y2-1,所有位上都是1,因此只需要模拟一遍y2-1位的矩阵快速幂,最后加上z2即可。
之后,因为n是2^y2+z2,相当大,因此可以先减去1,得到2^y2-1+z2。根据矩阵快速幂,对于指数的二进制是1的,需要将底数和答案乘一次,如果二进制是0,则不需要。因此观察2^y2-1,所有位上都是1,因此只需要模拟一遍y2-1位的矩阵快速幂,最后加上z2即可。
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#define C 0.57721566490153286060651209
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF=0x3f3f3f3f;
const double eps=1e-10;
const double PI=acos(-1.0);
const int maxn=1000009;
int x1,y11,z1,y2,z2;
LL p,n,q;
struct mat
{
LL m[3][3];
}a,b,aa;
LL fpow(int n,int m)
{
LL ret=1;
LL N=n;
while(m)
{
if(m&1)
ret=(ret*N)%p;
m>>=1;
N=(N*N)%p;
}
return ret;
}
mat mul(mat aa,mat bb)
{
mat ans;
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
ans.m[i][j]=0;
for(int k=0;k<2;k++)
{
ans.m[i][j]+=aa.m[i][k]*bb.m[k][j];
}
ans.m[i][j]%=p;
}
}
return ans;
}
int main()
{
while(~scanf("%d%d%d",&x1,&y11,&z1))
{
if(x1==-1&&y11==-1&&z1==-1)
break;
scanf("%d%d",&y2,&z2);
scanf("%I64d",&p);
q=(fpow(x1,y11)+z1)%p;
a.m[0][0]=(q-1+p)%p;
a.m[0][1]=q;
a.m[1][0]=1;
a.m[1][1]=0;
b.m[0][0]=1;
b.m[1][0]=0;
aa=a;
while(z2)
{
if(z2&1)
b=mul(aa,b);
z2>>=1;
aa=mul(aa,aa);
}
for(int i=0;i<y2;i++)
{
b=mul(a,b);
a=mul(a,a);
}
printf("%I64d\n",b.m[0][0]);
}
return 0;
}