hdu 2814 Interesting Fibonacci
Interesting Fibonacci
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 712 Accepted Submission(s): 137
Problem Description
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
Input
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)
Output
For each test case, print a line containing the test case number( beginning with 1) followed by a integer which is the value of G(N) mod C
Sample Input
1
17 18446744073709551615 1998 139
Sample Output
Case 1: 120
Author
AekdyCoin
Source
HDU 1st “Old-Vegetable-Birds
Cup” Programming Open Contest
題解及代碼:
#include
#include
#include
using namespace std;
typedef unsigned long long __int65;
int f[2000]; //記錄斐波那契數
int eular(int n) //歐拉函數
{
int cnt=n;
for(int i=2;i*i<=n;i++)
if(n%i==0)
{
cnt-=cnt/i;
while(n%i==0)
{
n/=i;
}
}
if(n>1)
cnt-=cnt/n;
return cnt;
}
int circle(int M) //求循環節
{
f[0]=0;
f[1]=1;
f[2]=1;
for(int i=3;;i++)
{
f[i]=(f[i-1]+f[i-2])%M;
if(f[i]==f[1]&&f[i-1]==f[0])
return i-1;
}
}
__int65 quick_mod(__int65 a,__int65 b,int M) //快速冪取模
{
__int65 t=1;
a=a%M;
while(b)
{
if(b&1) t=t*a%M;
b/=2;
a=a*a%M;
}
return t;
}
int main()
{
__int65 A,B,N,C;
int t,cas=1;
cin>>t;
while(t--)
{
cin>>A>>B>>N>>C;
if(C==1)
{
printf("Case %d: 0\n",cas++);
continue;
}
__int65 oula=eular(C);
int c=circle(C);
__int65 cl=quick_mod(A,B,c);
cl=f[cl];
if(oula==1)
{
printf("Case %d: ",cas++);
cout<