2014
02-17

# Interesting Fibonacci

In mathematics, the Fibonacci numbers are a sequence of numbers named after Leonardo of Pisa, known as Fibonacci (a contraction of filius Bonaccio, "son of Bonaccio"). Fibonacci’s 1202 book Liber Abaci introduced the sequence to Western European mathematics, although the sequence had been previously described in Indian mathematics.
The first number of the sequence is 0, the second number is 1, and each subsequent number is equal to the sum of the previous two numbers of the sequence itself, yielding the sequence 0, 1, 1, 2, 3, 5, 8, etc. In mathematical terms, it is defined by the following recurrence relation:

That is, after two starting values, each number is the sum of the two preceding numbers. The first Fibonacci numbers (sequence A000045 in OEIS), also denoted as F[n];
F[n] can be calculate exactly by the following two expressions:

A Fibonacci spiral created by drawing arcs connecting the opposite corners of squares in the Fibonacci tiling; this one uses squares of sizes 1, 1, 2, 3, 5, 8, 13, 21, and 34;

So you can see how interesting the Fibonacci number is.
Now AekdyCoin denote a function G(n)

Now your task is quite easy, just help AekdyCoin to calculate the value of G (n) mod C

The input consists of T test cases. The number of test cases (T is given in the first line of the input. Each test case begins with a line containing A, B, N, C (10<=A, B<2^64, 2<=N<2^64, 1<=C<=300)

The input consists of T test cases. The number of test cases (T is given in the first line of the input. Each test case begins with a line containing A, B, N, C (10<=A, B<2^64, 2<=N<2^64, 1<=C<=300)

1
17 18446744073709551615 1998 139

Case 1: 120

#include <iostream> //������Ϊ�����ҵ�,�ύ��Ϊ����֤����Ƿ���ȷ.
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXN = 400;

unsigned long long A, B, N;
int C;
int phi[MAXN];
int d[MAXN];
void init_phi()
{
int i, j;
for (i = 1; i < MAXN; i ++)
phi[i] = i;
for (i = 2; i < MAXN; i += 2)
phi[i] /= 2;
for (i = 3; i < MAXN; i += 2)
if (phi[i] == i)
{
for (j = i; j < MAXN; j += i)
{
phi[j] = phi[j] / i * (i - 1);
}
}
}

int Fib_T (int mod)
{
if (mod == 1)
return 1;
int t0 = 0, t1 = 1;
int i;
for (i = 1; ; i ++)
{
t0 = (t0 + t1) % mod;
swap (t0, t1);
if (t0 == 0 && t1 == 1)
return i;
}
}

int get_Fib (int n, int mod)
{
int t0 = 0, t1 = 1;
int i;
for (i = 0; ; i ++)
{
if (i == n)
return t0;
t0 = (t0 + t1) % mod;
swap (t0, t1);
}
}

int bspow (int a, unsigned long long b, int mod)
{
int ans = 1;
while (b)
{
if (b % 2)
ans = (ans * a) % mod;
a = (a * a) % mod;
b /= 2;
}
return ans;
}

int cal_G(int mod)
{
if (mod == 1)
return 0;
int FT = Fib_T (mod);
int ab = bspow (A % FT, B, FT);
int t = get_Fib (ab, mod);
return t;
}

int solve ()
{
if (C == 1)
return 0;
int temp = cal_G (phi[C]);
d[1] = cal_G (C);
int i;
for (i = 2; i < MAXN; i ++)
{
d[i] = bspow (d[i - 1], temp + phi[C], C);
}
int T;
for (i = MAXN - 2; i >= 1; i --)
if (d[i] == d[MAXN - 1])
{
T = MAXN - 1 - i;
break;
}
int j = N % T;
while (j + T < MAXN)
j += T;
return d[j];
}

int main ()
{
//freopen ("2.txt", "r", stdin);
//freopen ("3.txt", "w", stdout);
int ca = 1, t;
scanf ("%d", &t);
init_phi();
while (t --)
{
scanf ("%I64u%I64u%I64u%d", &A, &B, &N, &C);
int ans = solve ();
printf ("Case %d: %d\n", ca ++, ans);
}
}

1. 学算法中的数据结构学到一定程度会乐此不疲的，比如其中的2－3树，类似的红黑树，我甚至可以自己写个逻辑文件系统结构来。