UVA 11582 Colossal Fibonacci Numbers!

题目分析

大家可以发现,如果f[i] = 1且f[i-1] = 0,那么数组f进入循环。接下来我们就需要找出f数组的循环周期,同时我们会发现,循环周期最大只可能为1000*1000,因为取模之后之后1000*1000种选择。同时a的b次方对周期进行取模,暴力显然会超时,只运用快速幂,logn的时间就能得到结果。这道题需要打表存下来,因为有1000次询问,但是只有1000种情况,所以打表就可以减少时间,查表就可以得到结果。hint:因为没有在uva上写过几次题,我不知道输出64位无符号整数是llu,我用的I64u,结果答案一直wa,后来看了别人的代码才发现别的都一样就这个不同,改了之后果然过了,真是坑呀!!

#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 1005;
typedef unsigned long long ull;
vector <int> vec[maxn];
int len[maxn];

void init()
{
    memset(len, 0, sizeof(len));
    for(int i = 2; i <= 1000; i++)
    {
        int a = 0,b = 1,c = 1;
        vec[i].push_back(a);
        vec[i].push_back(b);
        vec[i].push_back(c);
        while(!(b == 0 && c == 1))
        {
            a = b;
            b = c;
            c = (a+b)%i;
            vec[i].push_back(c);
        }
        len[i] = vec[i].size()-2;
    }
}

ull pow_mod(ull a, ull b, ull MOD)  //快速幂
{
    int ans = 1%MOD;
    a = a%MOD;
    while(b)
    {
        if(b&1)
            ans = (int)(ans*a)%MOD;
        a = (a*a)%MOD;
        b = b/2;
    }
    return ans;
}

int main()
{
    init();
    int n,t;
    ull a,b;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%llu%llu%d", &a, &b, &n);
        if(n == 1)
        {
            printf("0\n");
            continue;
        }
        ull ans = pow_mod(a, b, len[n]);
        printf("%d\n", vec[n][ans]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值