zoukankan      html  css  js  c++  java
  • UVA 11582 Colossal Fibonacci Numbers! (巨大的斐波那契数!)

    题意:输入两个非负整数a、b和正整数n(0<=a,b<264,1<=n<=1000),你的任务是计算f(ab)除以n的余数,f(0) = 0, f(1) = 1,且对于所有非负整数i,f(i + 2) = f(i + 1) + f(i)。

    分析:

    1、对于某个n取余的斐波那契序列总是有周期的,求出每个取值的n下的斐波那契序列和周期。

    2、ab对T[n]取余,即可确定对n取余的斐波那契序列中f(ab)的位置。

    #pragma comment(linker, "/STACK:102400000, 102400000")
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<cmath>
    #include<iostream>
    #include<sstream>
    #include<iterator>
    #include<algorithm>
    #include<string>
    #include<vector>
    #include<set>
    #include<map>
    #include<stack>
    #include<deque>
    #include<queue>
    #include<list>
    #define Min(a, b) ((a < b) ? a : b)
    #define Max(a, b) ((a < b) ? b : a)
    const double eps = 1e-8;
    inline int dcmp(double a, double b) {
        if(fabs(a - b) < eps)  return 0;
        return a < b ? -1 : 1;
    }
    typedef long long LL;
    typedef unsigned long long ULL;
    const int INT_INF = 0x3f3f3f3f;
    const int INT_M_INF = 0x7f7f7f7f;
    const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
    const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
    const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
    const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
    const int MOD = 1e9 + 7;
    const double pi = acos(-1.0);
    const int MAXN = 1000 + 10;
    const int MAXT = 10000 + 10;
    using namespace std;
    vector<int> v[MAXN];
    int T[MAXN];//周期
    void init(){
        for(int i = 2; i <= 1000; ++i){
            v[i].push_back(0);
            v[i].push_back(1);
            for(int j = 2; ; ++j){
                v[i].push_back((v[i][j - 1] + v[i][j - 2]) % i);
                if(v[i][j] == 1 && v[i][j - 1] == 0){
                    T[i] = j - 1;
                    break;
                }
            }
        }
    }
    ULL Q_POW(ULL a, ULL b, int n){
        ULL ans = 1ULL;
        ULL tmp = a;
        while(b){
            if(b & 1){
                ans = (ans * tmp) % n;
            }
            tmp = (tmp * tmp) % n;
            b >>= 1;
        }
        return ans;
    }
    int main(){
        int N;
        scanf("%d", &N);
        init();
        while(N--){
            ULL a, b, n;
            scanf("%llu%llu%llu", &a, &b, &n);
            if(a == 0 || n == 1){
                printf("0\n");
                continue;
            }
            ULL ans = Q_POW(a % T[n], b, T[n]);
            printf("%d\n", v[n][ans]);
        }
        return 0;
    }
    

      

  • 相关阅读:
    ACM学习历程——POJ3321 Apple Tree(搜索,线段树)
    ACM学习历程——NOJ1113 Game I(贪心 || 线段树)
    ACM学习历程——HDU5202 Rikka with string(dfs,回文字符串)
    ACM学习历程——hihoCoder挑战赛10A 01串(策略)
    ACM学习历程——POJ3468 A Simple Problem with Integers(线段树)
    ACM学习历程——POJ1260 Pearls(动态规划)
    java入门了解09
    java入门了解08
    java入门了解之快捷键
    java入门了解07
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/6384656.html
Copyright © 2011-2022 走看看