Hdu 2814 Interesting Fibonacci

Source: Internet
Author: User

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 DescriptionIn mathematics, the maid of Pisa, known as maid (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 previusly 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 maid by drawing arcs connecting the opposite corners of squares in the maid; 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
InputThe 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)
OutputFor 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

AuthorAekdyCoin
SourceHDU 1st "Old-Vegetable-Birds Cup" Programming Open Contest

Question and code:

# Include
 
  
# Include
  
   
# Include
   
    
Using namespace std; typedef unsigned long _ int65; int f [2000]; // records the Fibonacci number int eular (int n) // Euler's function {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) // calculate the cyclic section {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, __int65 B, int M) // fast power modulo {_ 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 <
    
     




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.