2014
03-23

# Snail Alice

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.

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

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

2 1 3
0 0
32551
3 0 5
0 2
70546
-1 -1 -1

1
31

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>

#define N 50005
#define M 30
#define cl(a) memset(a,0,sizeof(a))
#define ll long long

ll x[M],f[3],r[N][3][3],q;
int p;

using namespace std;

ll bin(ll t)
{
ll res=1;
int i=0;
while (t>0)
{
if (t&1) res=(res*x[i])%p;
i++;
t>>=1;
}
return res;
}

void init()
{
r[0][1][1]=(q-1+p)%p;
r[0][1][2]=q;
r[0][2][1]=1;
r[0][2][2]=0;
f[1]=1;f[2]=0;
}

void mul(int t)
{
int i,j,k;
for (i=1;i<=2;i++)
for (j=1;j<=2;j++)
{
r[t][i][j]=0;
for (k=1;k<=2;k++) r[t][i][j]=r[t][i][j]+(r[t-1][i][k]*r[t-1][k][j])%p;
}
}

void zmul(int i)
{
ll g1,g2;
g1=(f[1]*r[i][1][1])%p+(f[2]*r[i][1][2])%p;
g2=(f[1]*r[i][2][1])%p+(f[2]*r[i][2][2])%p;
f[1]=g1%p;f[2]=g2%p;
}

void bin2(int t)
{
int i=0;
while (t>0)
{
if (t&1) zmul(i);
t>>=1;
i++;
}
}

int main()
{
int x1,y1,z1,y2,z2,i,j;
{
if (x1==-1&&y1==-1&&z1==-1) break;
cl(x);
x[0]=x1%p;
for (i=1;i<M;i++) x[i]=(x[i-1]*x[i-1])%p;
q=(bin(y1)+z1)%p;
init();
for (i=1;i<N;i++) mul(i);
zmul(y2);
if (z2>0) bin2(z2-1);
if (z2>=1) printf("%d\n",f[1]);
else printf("%d\n",f[2]);
}
return 0;
}

1. 约瑟夫也用说这么长……很成熟的一个问题了，分治的方法解起来o(n)就可以了，有兴趣可以看看具体数学的第一章，关于约瑟夫问题推导出了一系列的结论，很漂亮

2. #include <cstdio>

int main() {
int n, u, d;
while(scanf("%d%d%d",&n,&u,&d)==3 && n>0) {
if(n<=u) { puts("1"); continue; }
n-=u; u-=d; n+=u-1; n/=u;
n<<=1, ++n;
printf("%dn",n);
}
return 0;
}

3. #include <cstdio>
#include <cstring>

const int MAXSIZE=256;
//char store[MAXSIZE];
char str1[MAXSIZE];
/*
void init(char *store) {
int i;
store['A']=’V', store['B']=’W',store['C']=’X',store['D']=’Y',store['E']=’Z';
for(i=’F';i<=’Z';++i) store =i-5;
}
*/
int main() {
//freopen("input.txt","r",stdin);
//init(store);
char *p;
while(fgets(str1,MAXSIZE,stdin) && strcmp(str1,"STARTn")==0) {
if(p=fgets(str1,MAXSIZE,stdin)) {
for(;*p;++p) {
//*p=store[*p]
if(*p<’A’ || *p>’Z') continue;
if(*p>’E') *p=*p-5;
else *p=*p+21;
}
printf("%s",str1);
}
fgets(str1,MAXSIZE,stdin);
}
return 0;
}