UVa11582 huge Fibonacci numbers.

Source: Internet
Author: User

1#include <cstdio>2#include <cstring>3#include <cstdlib>4#include <cmath>5#include <vector>6#include <iostream>7#include <algorithm>8 using namespacestd;9typedef unsignedLong Longull;Ten Const intINF =1<< -; One  A intT,n; -vector<int> fib[1005];///The table, the two-dimensional array subscript represents the corresponding divisor, to take the remainder - intcyc[1005];///The corresponding period of the deposit divisor p the ull A, b; -  - voidpre_process () { -      for(intp =2; P <= +; ++p) {///p = N (that is, divisor) +Fib[p].push_back (0); -Fib[p].push_back (1); +          for(inti =2; ; ++i) { AFib[p].push_back (Fib[p][i-1] + fib[p][i-2]) %p);///The remainder is stored in the "one array" corresponding to P at             if(Fib[p][i] = =1&& Fib[p][i-1] ==0) {///Find a cycle cycle -CYC[P] = i-1;/// record period End position -                  Break; -             } -         } -     } in } -  to intQuick_pow_mod (ull x,ull y,intMoD) {//Template code +     intAns =1; -      while(y) { the         if(Y &1) ans = (int) ((ans * x)%MoD); *x = (x * x)%MoD; $Y >>=1;Panax Notoginseng     } -     returnans; the } +  A intMain () { the pre_process ();///Play Watch +scanf"%d",&t); -      while(t--){ $scanf"%llu%llu%d",&a,&b,&n); $         if(A = =0|| n = =1) printf ("0\n"); -         Elseprintf"%d\n", Fib[n][quick_pow_mod (a%cyc[n],b,cyc[n]); -     } the     return 0; -}

UVa11582 huge Fibonacci numbers.

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.