(UVA - 11582)Colossal Fibonacci Numbers!(递推+快速幂,无符号长整型)

链接: https://vjudge.net/problem/UVA-11582

The i’th Fibonacci number f(i) is
recursively defined in the following
way:
• f(0) = 0 and f(1) = 1
• f(i + 2) = f(i + 1) + f(i) for
every i ≥ 0
Your task is to compute some
values of this sequence.
Input
Input begins with an integer t ≤
10, 000, the number of test cases.
Each test case consists of three integers
a, b, n where 0 ≤ a, b < 2
64
(a and b will not both be zero) and
1 ≤ n ≤ 1000.
Output
For each test case, output a single line containing the remainder of f(a
b
) upon division by n.
Sample Input
3
1 1 2
2 3 1000
18446744073709551615 18446744073709551615 1000
Sample Output
1
21
250

分析:给出了递推式: f[0]=0,f[1]=1;
f[i+2]=f[i]+f[i+1],i>=0

然后求f[a^b]%n
求出的结果一定是

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define mem(a,n) memset(a,n,sizeof(a))
typedef unsigned long long uLL;
const int INF=0x3f3f3f3f;
const uLL N=1e3+5;
uLL F[N*N];
uLL pow_mod(uLL a,uLL b,uLL mod)
{
    uLL ans=1;
    a%=mod;
    while(b)
    {
        if(b&1) ans=ans*a%mod;
        b>>=1;
        a=a*a%mod;
    }
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        uLL a,b,n;
        scanf("%llu%llu%llu",&a,&b,&n);
//        printf("%llu %llu %llu\n",a,b,n);
        F[0]=0,F[1]=1%n;
        uLL T=1;
        for(int i=2; i<=n*n; i++)
        {
            F[i]=(F[i-1]%n+F[i-2]%n)%n;
            if(F[i]==1&&F[i-1]==0)
            {
                T=i-1;
                break;
            }
        }
        uLL tmp=pow_mod(a%T,b,T);
        printf("%llu\n",F[tmp]);
    }
    return 0;
}

 

上一篇:[ SDOI 2016 ] 模式字符串


下一篇:算法训练 结点选择 动态规划