UVa 11582 huge Fibonacci sequence (preprocessing + fast power)

Source: Internet
Author: User



Test instructions


Test instructions very well understand, ask F (a^b)%n as much;

By listing the first few items can be found, there are regular (nonsense is 20 bit so large number of irregular only strange,)

More than 18 bits with unsigned long long input output with%llu


It can be found that there is a period when f[i-1] =f[1] f[i]=f[2] is present, and the cycle must be found within the n^2.


But it's easy to time out,

Pre-treatment of the table is possible


"Code Implementation"


#include <bits/stdc++.h> #include <iostream> #include <stdio.h> #include <algorithm> # Include <cmath> #include <math.h> #include <cstring> #include <string> #include <queue> # Include <deque> #include <stack> #include <stdlib.h> #include <list> #include <map> # Include <set> #include <bitset> #include <vector> #define MEM (b) memset (A,b,sizeof (a)) #define Findx (x) Lower_bound (b+1,b+1+bn,x)-B #define FIN freopen ("Input.txt", "R", stdin) #define FOUT freopen ("Output.txt", "W", STDOUT) #define S1 (n) scanf ("%d", &n) #define SL1 (n) scanf ("%i64d", &n) #define S2 (n,m) scanf ("%d%d", &AMP;N,&A MP;M) #define SL2 (n,m) scanf ("%i64d%i64d", &n,&m) #define PR (n) printf ("%d\n", N) #define Lson RT << 1, l
, MID #define Rson RT << 1|1, Mid + 1, r using namespace std;
typedef unsigned long long ll;
Const Double Pi=acos (-1);
const int inf=0x3f3f3f3f; Const double ESP=1e-6;
const int maxn=1e5+5;
const int maxn=50005;
const int mod=1e9+7;



int dir[5][2]={0,1,0,-1,1,0,-1,0};
ll F[1005][MAXN];
ll GO[MAXN];
ll A,b,n; ll Qpow (ll x,ll y,ll MOD) {ll res=1;for (; y;y>>=1) {if (y&1) res= (res*x)%mod;x= (x*x)%mod;}
return res;}
        void Init () {for (int i=2;i<=1000;i++) {f[i][0]=0,f[i][1]=1,f[i][2]=1; for (int j=3;;
            J + +) {f[i][j]= (f[i][j-1]%i+f[i][j-2]%i)%i;
                if (F[i][j]==f[i][1]&&f[i][j-1]==f[i][0]) {go[i]=j-1;
            Break
    }}}} int main () {init ();
    int T;
    cin>>t;
        while (t--) {scanf ("%llu%llu%llu", &a,&b,&n);
            if (n==1| |!a) {printf ("0\n");
        Continue
        }//printf ("%llu%llu \ n", a%go[n],b);
        ll T=qpow (A%go[n],b,go[n]);
    printf ("%llu\n", F[n][t%go[n]]);
} 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.