哇,其实我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. C语言盲点笔记1

    寥寥数笔,记录我的C语言盲点笔记,仅仅为以前经历过,亦有误,可交流. 1.int* a和int *a有差别吗? 没有不论什么差别,都表示a是int指针 建议这么写int *a;这样明显一点 理由例如以 ...

  2. TCP连接的建立(二)

    被动打开 SYN cookies TCP协议开辟了一个比較大的内存空间请求连接队列来存储连接请求块,当SYN请求不断添加,请求连接数目到达上限时,会致使系统丢弃SYN连接请求.SYN cookies技 ...

  3. tar.gz文件命名和压缩解压方法

    tar.gz文件命名 tar这是文件打成一个包,无压缩; gz同gzip标记的包.tar文件压缩; 所以它成为一个.tar.gz档 压缩 # tar cvfz backup.tar.gz /xxx/ ...

  4. NetAnalyzer2016使用方法

    NetAnalyzer笔记 之 八 NetAnalyzer2016使用方法(2)   [创建时间:2016-05-06 22:07:00] NetAnalyzer下载地址 在写本篇的时候,NetAna ...

  5. UDP vs. TCP

    UDP vs. TCP 原文:UDP vs. TCP,作者是Glenn Fiedler,专注于游戏网络编程相关工作多年. 说在最前面的话 翻译这篇文章的初衷:我在工作中根本接触不到网络游戏编程,但是我 ...

  6. deque,list,queue,priority_queue

    1.deque deque双端队列容器与vector一样,采用线性表顺序存储结构,但与vector唯一不同的是,deque采用分块的线性存储结构来存 储数据,每块的大小一般为512字节,称为一个deq ...

  7. 微信公众平台PHP开发

    p=932" style="color: rgb(255, 153, 0); text-decoration: none;">微信公众平台PHP开发 2013.05 ...

  8. hdu 1536(博弈)

    传送门:S-Nim 题意:给n个数的集合s, 再给m 组数据,每组表示 k 堆石子,每次可以取的个数只能是集合s中的数量.问先手胜还是输? 分析:sg函数的经典运用,先预处理出所有数量为0~10000 ...

  9. (1)cocos2d-x-2.2.4搭建windows开发环境

    Cocos2d-x-2.2.4搭建windows环境 软件需求 Windows系统(windows7或之后的系统): cocos2d-x-2.2.4压缩包. python安装包(推荐使用2.7.3版本 ...

  10. windows 2003 自动安全设置

    @echo offecho.echo.echo.echo 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓echo.echo.echo windows 2003 自动安全设置程序 echo. ec ...