Ultraviolet A 11582 colossal Fibonacci numbers! (Table hitting + quick power)

Source: Internet
Author: User

Colossal Fibonacci numbers!

TheI'Th Maid numberF (I)Is recursively defined in the following way:

    • F (0) = 0AndF (1) = 1
    • F (I + 2) = f (I + 1) + f (I)For everyI ≥ 0

Your task is to compute some values of this sequence.

Input begins with an integerT≤ 10,000, the number of test cases. Each test case consists of three IntegersA, B, nWhere 0 ≤A, B<1, 264 (AAndBWill not both be zero) and 1 ≤N≤ 1000.

For each test case, output a single linecontaining the remainderF (AB)Upon divisionN.

Sample Input
 
31 1 22 3 100018446744073709551615 18446744073709551615 1000
Sample output
 
121250
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <limits. h ># include <vector> using namespace STD; typedef unsigned long ull; // long hanging ull n, m, MOD; vector <int> F [1001]; void Init () {for (INT I = 2; I <= 1000; I ++) {int mod = I; int A = 0, B = 1, C = (a + B) % MOD; F [I]. push_back (a); F [I]. push_back (B); F [I]. push_back (c); While (! (B = 0 & C = 1) {A = B; B = C; C = (a % mod + B % mod) % MOD; f [I]. push_back (c);} f [I]. pop_back (); F [I]. pop_back () ;}} ull quick_mod (ull A, ull B, ull m) // fast power-down range {ull ans = 1; while (B) {If (B & 1) {ans = (ANS % m) * (a % m) % m; B --;} B/= 2; A = (a % m) * (a % m) % m;} return ans;} int main () {int t; CIN> T; Init (); // while (t --) {CIN> N> m> MOD; If (mod = 1) {cout <0 <Endl; continue ;} ull Mm = f [mod]. size (); ull ans = quick_mod (n, m, mm); cout <F [mod] [ANS] <Endl;} return 0 ;}

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.