哇,其实我2A该。。。。否1A纯脑损伤。。

乞讨:F(a^b)^(F(a^b) ^ (n-1))%c 

既是求F(a^b)^(F(a^b) ^ (n-1)%phi[c]+phi[c])%c

先求x=F(a^b)%phi[c],有循环节,直接找到循环节就OK。

然后求y=F(a^b)%c,同求x,循环节。

然后问题就变成求y^(x^(n-1)%phi[c]+phi[c])

直接套两次高速幂取模就OK。

#include <iostream>

#include<stdio.h>

#include<vector>

#include<queue>

#include<stack>

#include<string.h>

#include<algorithm>

#include<math.h>

using namespace std;

#define LL unsigned __int64

#define lcm(a,b) (a*b/gcd(a,b))

//O(n)求素数,1-n的欧拉数

#define N 110000

struct math_use

{

    LL euler(LL x)

    {

        LL i, res = x;

        for (i = 2; i*i <= x; i++)

            if (x%i == 0)

            {

                res = res / i*(i - 1);

                while (x%i == 0)

                    x /= i;

            }

        if (x > 1)

            res = res / x*(x - 1);

        return res;

    }

//a^b%c

    LL q_mod(LL a,LL b,LL n)

    {

        LL ret=1;

        LL tmp=a;

        while(b)

        {

            //基数存在

            if(b&0x1) ret=ret*tmp%n;

            tmp=tmp*tmp%n;

            b>>=1;

        }

        return ret;

    }

} M;

int smod[330];

int eur[330];

LL s_mod(int mod)

{

    LL a1,a2,a3,tmp;

    a1=0;

    a2=1;

    a3=1;

    LL ans=1;

    while(a2!=0||a3!=1)

    {

        tmp=(a2+a3)%mod;

        a2=a3;

        a3=tmp;

        ans++;

    }

    return ans;

}

void init()

{

    smod[1]=1;

    eur[1]=M.euler(1);

    for(int i=2; i<=300; i++)

    {

        smod[i]=s_mod(i);

        eur[i]=M.euler(i);

    }

}

LL get_fib(int x,int mod)

{

    if(x==0)return 0;

    LL a1,a2,a3,tmp;

    a1=0;

    a2=a3=1;

    x--;

    while(x--)

    {

        tmp=(a2+a3)%mod;

        a2=a3;

        a3=tmp;

    }

    return a2;

}

LL fib(LL a,LL b,LL mod)

{

    LL ans=1;

    int yu=smod[mod];

    LL s=M.q_mod(a%yu,b,yu);

    return get_fib(s,mod);

}

int main()

{

    LL a,b,n,c;

    init();

    LL T;

    cin>>T;

    int cas=0;

    while(T--)

    {

        cas++;

        cin>>a>>b>>n>>c;

        if(c==1)

        {

            printf("Case %d: 0\n",cas);

            continue;

        }

        LL x,y;

        LL mod,mod1;

        mod=c;

        mod1=eur[c];

        x=fib(a,b,mod1);

        y=fib(a,b,mod);

        LL p=M.q_mod(x,(n-1)%eur[mod1]+eur[mod1],mod1);

        LL ans=M.q_mod(y,p+mod1,mod);

        printf("Case %d: ",cas);

        cout<<ans<<endl;

    }

    return 0;

}

版权声明:本文博主原创文章,博客,未经同意不得转载。

hdu-2814-Interesting Fibonacci-斐波那契周期节的更多相关文章

  1. HDU 2814 斐波那契循环节 欧拉降幂

    一看就是欧拉降幂,问题是怎么求$fib(a^b)$,C给的那么小显然还是要找循环节.数据范围出的很那啥..unsigned long long注意用防爆的乘法 /** @Date : 2017-09- ...

  2. Java Fibonacci 斐波那契亚

    Java Fibonacci 斐波那契亚 /** * <html> * <body> * <P> Copyright 1994-2018 JasonInternat ...

  3. 递归算法之Fibonacci 斐波那契数列第n个数的求解

    Fibonacci 斐波那契数列第n个数的求解,也可以用递归和非递归的形式实现,具体如下,dart语言实现. int fibonacci(int n) { if (n <= 0) throw S ...

  4. lintcode:Fibonacci 斐波纳契数列

    题目: 斐波纳契数列 查找斐波纳契数列中第 N 个数. 所谓的斐波纳契数列是指: 前2个数是 0 和 1 . 第 i 个数是第 i-1 个数和第i-2 个数的和. 斐波纳契数列的前10个数字是: 0, ...

  5. hdu number number number 斐波那契数列 思维

    http://acm.hdu.edu.cn/showproblem.php?pid=6198 F0=0,F1=1的斐波那契数列. 给定K,问最小的不能被k个数组合而成的数是什么. 赛后才突然醒悟,只要 ...

  6. hdu 4983 线段树+斐波那契数

    http://acm.hdu.edu.cn/showproblem.php?pid=4893 三种操作: 1 k d, 修改k的为值增加d 2 l r, 查询l到r的区间和 3 l r, 从l到r区间 ...

  7. 算法导论-求(Fibonacci)斐波那契数列算法对比

    目录 1.斐波那契数列(Fibonacci)介绍 2.朴素递归算法(Naive recursive algorithm) 3.朴素递归平方算法(Naive recursive squaring) 4 ...

  8. HDOJ/HDU 5686 Problem B(斐波拉契+大数~)

    Problem Description 度熊面前有一个全是由1构成的字符串,被称为全1序列.你可以合并任意相邻的两个1,从而形成一个新的序列.对于给定的一个全1序列,请计算根据以上方法,可以构成多少种 ...

  9. HDU 1568 快速求斐波那契前四位

    思路: 把斐波那契通项公式转化成log的形式,高中数学... //By SiriusRen #include <bits/stdc++.h> using namespace std; ], ...

  10. hdu 4099 字典树 + 斐波那契

    题意:       给你一个串(最长40位)问你这个串是斐波那契F(n)  n <= 99999中的那个数的前缀,如果存在多个输出最小的n否则输出-1. 思路:       给的串最长40位,那 ...

随机推荐

  1. 14.4.3.6 Fine-tuning InnoDB Buffer Pool Flushing 微调 InnoDB Buffer Pool 刷新:

    14.4.3.6 Fine-tuning InnoDB Buffer Pool Flushing 微调 InnoDB Buffer Pool 刷新: innodb_flush_neighbors an ...

  2. PageHeap,调试Heap问题的工具

    <Windows用户态程序高效排错>第二章主要介绍用户态调试相关的知识和工具.本文主要讲了PageHeap,调试Heap问题的工具. AD:51CTO学院:IT精品课程在线看! 2.4.2 ...

  3. php中如何开启GD库

    php中开启GD库 在浏览器输入启用wamp下的GD库(否则验证码可能不能用) D:\lamp\php\php.ini 文件

  4. Delphi 的绘图功能(29篇博客)

    http://www.cnblogs.com/del/category/123038.html

  5. Selenium来抓取动态加载的页面

    一般的爬虫都是直接使用http协议,下载指定url的html内容,并对内容进行分析和抽取.在我写的爬虫框架webmagic里也使用了HttpClient来完成这样的任务. 但是有些页面是通过js以及a ...

  6. mahout入门指南之基于mahout的itembased算法

    基于mahout的itembased算法 事实上mahout分布式上仅仅是实现了部分算法.比方推荐算法中Item-based和slopone都有hadoop实现和单机版实现,User-based没有分 ...

  7. 如何解决ORA-12547: TNS:lost contact错

    执行环境:ubuntu+oracle 11.2.0 为了启动oracle时间,出现ORA-12547: TNS:lost contact错误. 中午好好的纳,下午就无论了.以为是链接失效,关机重新启动 ...

  8. A Game of Thrones(17) - Bran

    It seemed as though he had been falling for years. Fly, a voice whispered in the darkness, but Bran ...

  9. projecteuler----&gt;problem=9----Special Pythagorean triplet

    title: A Pythagorean triplet is a set of three natural numbers, a b c, for which, a2 + b2 = c2 For e ...

  10. struts2文件上传限制大小问题

    struts2默认文件上传大小为2M,如需改动默认大小,解决方法例如以下: <struts> <constant name="struts.multipart.maxSiz ...