hdu 2814
来源:互联网 发布:博客助赢软件cpzyrj 编辑:程序博客网 时间:2024/04/29 17:07
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 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
117 18446744073709551615 1998 139
Sample Output
Case 1: 120
Author
AekdyCoin
Source
HDU 1st “Old-Vegetable-Birds Cup” Programming Open Contest
公式推导,要非常非常的仔细,不然你懂得()
#include <iostream>#include <stdio.h>using namespace std;int quickmod (unsigned long long a,unsigned long long b,int c){ unsigned long long tmp=1; while(b>1) { if(b%2==0) { a=a*a%c; b=b/2; } else { tmp=tmp*a%c; b--; } } return a*tmp%c;}int eular(int n){ int ret=1; for(int i=2;i*i<=n;i++) { if(n%i==0) { n/=i,ret*=i-1; while(n%i==0) { n/=i; ret*=i; } } } if(n>1) ret*=n-1; return ret;}int main(){ int data[90010],data1[90010],len,len1,c1,g1,g2,n1,n2; unsigned long long a,b,n; int c,t; data[0]=0; data[1]=1; data1[0]=0; data1[1]=1; cin>>t; for(int p=1;p<=t;p++) { cin>>a>>b>>n>>c; if(c==1) { printf("Case %d: 0\n",p); continue; } for(int i=2;i<=90003;i++) { data[i]=(data[i-1]+data[i-2])%c; if(data[i]==1&&data[i-1]==0) { len=i-1; break; } } c1=eular(c); if(c1>1) { for(int i=2;i<=90003;i++) { data1[i]=(data1[i-1]+data1[i-2])%c1; if(data1[i]==1&&data1[i-1]==0) { len1=i-1; break; } } } n1=quickmod(a%len,b,len); g1=data[n1]; if(c1==1) g2=0; else { n2=quickmod(a%len1,b,len1); g2=data1[n2]; } int yu=quickmod(g2,n-1,c1); int yu1=quickmod(g1,yu+c1,c); if(n==1) printf("Case %d: %d\n",p,g1); else printf("Case %d: %d\n",p,yu1); } return 0;}
0 0
- hdu 2814
- hdu 2814 Interesting Fibonacci
- hdu 2814 Interesting Fibonacci
- hdu 2814 Interesting Fibonacci
- hdu 2814 Interesting Fibonacci
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- Light 1008
- hdu 1130 How Many Trees?(卡特兰数,大数的乘法与除法)
- Cocos2dx——瓦片地图(Tiled Map)
- 黑马程序员——Java基础03
- HDU 1501 Zipper
- hdu 2814
- HDU5391-Zball in Tina Town
- HDU 4450 Draw Something
- JAVA基础(二)
- 去哪网实习总结:java读写excel表格(JavaWeb)
- Javascript将数字转为中文大写金额形式
- Django之第一个app<1>
- 【React】热热身:Hello World
- 2.5 寻找最大的k个数