UVA 11582 Colossal Fibonacci Numbers(数学)

Colossal Fibonacci Numbers

想先说下最近的状态吧,已经考完试了,这个暑假也应该是最后刷题的暑假了,打完今年acm就应该会退了,但是还什么都不会呢? +_+ 所以这个暑假,一定要竭尽全力地去刷题,当然,也是能好好刷题的最后时间了.

【题目链接】Colossal Fibonacci Numbers

【题目类型】数学

&题意:

求Fi(f(a^b)%n) Fi()是斐波那契

&题解:

注意:unsigned时 要把%d全换成%u

数学大法好. 首先你要能看出来这是有循环节的,因为f(i)=f(i-1)+f(i-2) 那么只要f(i-1),f(i-2)和以前出现过的f(x-1),f(x-2)对应相等,那么就答案就一定会出现循环.而且循环节长度不会超过1000x1000,因为取余后一共有1000种情况,那个连着的2个在一起就是1000x1000
之后,打个表,做一次快速幂,对循环节取余,就可以a了.

&代码:

#include <cstdio>
#include <bitset>
#include <iostream>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
#define ull unsigned long long
#define fo(i,a,b) for(int i=(a);i<=(b);i++)
#define fd(i,a,b) for(int i=(a);i>=(b);i--)
#define cle(a,v) memset(a,(v),sizeof(a))
const int maxn = 1e3 + 7;
int t, n, cycle[maxn];
ull a, b;
vector<int> tab[maxn];
void pre() {
    for(int i = 2; i <= 1000; i++) {
        tab[i].push_back(0);
        tab[i].push_back(1);
        for(int j = 2;; j++) {
            tab[i].push_back((tab[i][j - 1] + tab[i][j - 2]) % i);
            if(tab[i][j] == 1 && tab[i][j - 1] == 0) {
                cycle[i] = j - 1;
                break;
            }
        }
    }
}
int pow(ull a, ull b, int m) {
    int ans = 1;
    while(b) {
        if(b & 1) ans = (ans * a) % m;
        a = (a * a) % m;
        b >>= 1;
    }
    return ans;
}
int main() {
    freopen("E:1.in", "r", stdin);
    pre();
    scanf("%d", &t);
    while(t--) {
        scanf("%llu%llu%d", &a, &b, &n);
        if(a == 0 || n == 1) printf("0\n");
        else printf("%d\n", tab[n][pow(a % cycle[n], b, cycle[n]) % cycle[n]]);
    }
    return 0;
}

转载于:https://www.cnblogs.com/s1124yy/p/7087223.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值