hdu 2814 Interesting Fibonacci

点击此处即可传送 hdu 2814
题目大意:就是给你两个函数,一个是F(n) = F(n-1) + F(n-2),
F(0) = 0, F(1) = 1;
另一个是 G(n) = G(n-1)^F(a^b);
G(1) = F(a^b);
求G(n) % c;
范围:A, B, N, C (10<=A, B<2^64, 2<=N<2^64, 1<=C<=300)

注意了:c的范围是1<= C <= 300,所以说它一定会有循环 节:
解题思路: 首先算G(1) = F(a^b),设a^b的循环节是len;
F(a^b)%c = F(a^b%len)%c;
一边加一边取余

然后算G(n)%c = F(a^b)^(F(a^b)^(n-1)) % c;
G(n)%c = F(a^b)^(F(a^b)^(n-1)%phi(c)+phi(c))%c;
F(a^b)^(n-1)%phi(c)+phi(c) == (F(a^b)%phi(c)^(n-1))+phi(c)
F(a^b)%phi(c) 有循环节,同上,具体详见代码

上代码:

/*
2015 - 8 - 16 下午
Author: ITAK

今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
*/
#include <iostream>
#include <cstdio>

using namespace std;

//快速幂取余
int quick_mod(int a, unsigned long long b, int c)
{
    int ans = 1;
    a %= c;
    while(b)
    {
        if(b & 1)
            ans = (ans*a) % c;
        b >>= 1;
        a = (a*a) % c;
    }
    return ans;
}
//欧拉函数
int Phi(int m)
{
    int ans = m;
    for(int i=2; i*i<=m; i++)
    {
        if(m%i == 0)
            ans -= ans/i;
        while(m%i == 0)
            m /= i;
    }
    if(m > 1)
        ans -= ans/m;
    return ans;
}
//公式:x^y % c == x^(y%phi(c)+phi(c))%c;
int data[90005],data1[90005];
int main()
{
    //注意不要用long long,用unsigned long long
    unsigned long long a, b, n;
    int c, c1, t, n1, n2, tmp;
    int g[10], len=0, len_c=0, len_e=0;
    scanf("%d",&t);
    for(int k=1; k<=t; k++)
    {
        //cin>>a>>b>>n>>c;
        scanf("%lld%lld%lld%d",&a,&b,&n,&c);
        if(c == 1)
        {
            printf("Case %d: 0\n",k);
            continue;
        }
        data[0]=0, data[1]=1;
        data1[0]=0, data1[1]=1;
        for(int i=2; i<90005; i++)
        {
            data[i] = (data[i-1]+data[i-2])%c;
            if(data[i]==1 && data[i-1]==0)
            {
                len = i-1;//c的循环节
                break;
            }
        }
        c1 = Phi(c);
        if(c1 > 1)
        {
            for(int i=2; i<90005; i++)
            {
                data1[i] = (data1[i-1]+data1[i-2])%c1;
                if(data1[i]==1 && data1[i-1]==0)
                {
                    len_c = i-1;//Phi(c)的循环节
                    break;
                }
            }
        }

        n1 = quick_mod(a%len, b, len);
        g[1] = data[n1];
        if(c1 == 1)
            g[2] = 0;
        else
        {
            n2 = quick_mod(a%len_c, b, len_c);
            g[2] = data1[n2];
        }
        int ans1 = quick_mod(g[2], n-1, c1);
        int ans = quick_mod(g[1], ans1+c1, c);
        if(n == 1)
            printf("Case %d: %d\n",k,g[1]);
        else
            printf("Case %d: %d\n",k,ans);
    }
    return 0;
}
时间: 2024-10-31 02:33:33

hdu 2814 Interesting Fibonacci的相关文章

hdu 1588 Gauss Fibonacci

点击此处即可传送hdu 1588 **Gauss Fibonacci** Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2849 Accepted Submission(s): 1179 Problem Description Without expecting, Angel replied quickly.She says: "I'v h

hdu 3306 Another kind of Fibonacci

点击此处即可传送hdu 3306 **Another kind of Fibonacci** Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2005 Accepted Submission(s): 787 Problem Description As we all known , the Fibonacci series : F(0) =

hdu 1250 Hat&amp;#39;s Fibonacci

点击此处即可传送hdu 1250 Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4) Your

hdu 3117 Fibonacci Numbers

点击此处即可传送到hdu 3117 **Fibonacci Numbers** Problem Description The Fibonacci sequence is the sequence of numbers such that every element is equal to the sum of the two previous elements, except for the first two elements f0 and f1 which are respectively

hdu 1568 Fibonacci

点击此处即可传送hdu 1568 **Fibonacci** Problem Description 2007年到来了.经过2006年一年的修炼,数学神童zouyu终于把0到100000000的Fibonacci数列 (f[0]=0,f[1]=1;f[i] = f[i-1]+f[i-2](i>=2))的值全部给背了下来. 接下来,CodeStar决定要考考他,于是每问他一个数字,他就要把答案说出来,不过有的数字太长了.所以规定超过4位的只要说出前4位就可以了,可是CodeStar自己又记不住.于

hdu 1021 Fibonacci Again

点击此处即可传送hdu 1021 Fibonacci Again Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 44439 Accepted Submission(s): 21214 Problem Description There are another kind of Fibonacci numbers: F(0) = 7, F(1)

【HDU 4786 Fibonacci Tree】最小生成树

一个由n个顶点m条边(可能有重边)构成的无向图(可能不连通),每条边的权值不是0就是1. 给出n.m和每条边的权值,问是否存在生成树,其边权值和为fibonacci数集合{1,2,3,5,8...}中的一个. 求最小生成树和最大生成树,得到生成树边权和的下界left和上界right.这道题由于每条边权值不是0就是1,因此生成树边权和可以覆盖到[left,right]的每一个数.那么求得[left,right],看是否有fibonacci数在区间内就可以了. 1 #include <cstdio>

hdu 3509 Buge&amp;#39;s Fibonacci Number Problem

点击此处即可传送 hdu 3509 题目大意:F1 = f1, F2 = f2;; F(n) = a*F(n-1) + b*F(n-2); S(n) = F1^k + F2^k +-.+Fn^k; 求S(n) mod m; 解题思路: 1:首先一个难题就是怎么判断矩阵的维数(矩阵的维数是个变量) 解决方法:开一个比较大的数组,然后再用一个公有变量记一下就行了,具体详见代码: 2:k次方,找规律: 具体上代码吧: /* 2015 - 8 - 16 晚上 Author: ITAK 今日的我要超越昨日

HDOJ/HDU 1250 Hat&amp;#39;s Fibonacci(大数~斐波拉契)

Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4) Your task is to take