UVA 11582 - Colossal Fibonacci Numbers!

数学题。也感觉算是一个综合题吧。


Fibonacci 数列  一般是用大数解决。 但是这个地方 出现了 大数的n次方。 必然不会用到大数。 因为数列必然取不到这一项。


所以既然数列数组开不到那么大 就会必然有周期性的存在。 因为数据是达到了  2^64  long long 的数据范围是  2^63 - 1  所以longlong 是存不下来的。


用unsigned  long long才行。  又因为 如果 高次幂的话 循环  肯定会超时。 所以用到了  快速幂运算。 超快。 这个题 RE了 3次。


一开始 还以为数组的问题。 开大点之后。还是不行。就读题 读了一遍。在数组里面发现问题了。在运算中 一定注意一些细节处理。 


周期的寻找很简单。 再一次 出现  0 1的时候 就会是  循环的开始。


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <string>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <cctype>
using namespace std;
#define ll long long
typedef unsigned long long ull;
#define maxn 10000000+10
#define INF 1<<30
ull f[maxn];
ull pow_(ull a, ull b , ull time){
    if(b == 0)
        return 1;
    ull x = pow_(a, b/2 , time);
    ull ans = ((ull)(x % time) * (ull)(x % time)) % time;
    if(b % 2 == 1)
        ans = ((ans % time) * (a % time)) % time;
    return ans;
}
int main (){
    int counts ;
    scanf("%d",&counts);
    while(counts--){
        ull a,b,n;
        f[0] = 0;
        f[1] = 1;
        scanf("%llu%llu%llu",&a,&b,&n);
        ull time;
        if(a == 0 || n == 1){
            printf("0\n");
            continue;
        }
        for(ull i = 2; i <= n * n; i++){
            f[i] = (f[i-1] + f[i-2]) % n;
            if(f[i-1] == 0 && f[i] == 1){
                time = i-1;
                break;
            }
        }
        ull sum = pow_(a % time, b, time);
        printf("%llu\n",f[sum % time]);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值