hdu-2814-Interesting Fibonacci-斐波那契循环节

哇塞,我竟然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-斐波那契循环节

时间: 2024-08-25 11:14:07

hdu-2814-Interesting Fibonacci-斐波那契循环节的相关文章

斐波那契循环节

斐波那契循环节 从一道题引出一个算法:斐波那契数列 这道题并没有什么花里胡哨的条件,就是很简单的计算\(F(n)\ mod\ p\). 但是这题的\(n\)达到了\(10^{30000000}\)级别,很显然不能直接用矩阵快速幂做. 因此我们要引入一个概念:斐波那契循环节. 显而易见的是通过看题解我们知道,斐波那契数列在模一个数时会出现循环,而这个周期的长度就称为斐波那契循环节. 所以我们只需要求出斐波那契循环节\(m\),然后用矩阵快速幂计算\(F(n\ mod\ m)\ mod\ p\)就行

hdu 2814 Interesting Fibonacci

Interesting Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 712    Accepted Submission(s): 137 Problem Description In mathematics, the Fibonacci numbers are a sequence of numbers name

Fibonacci斐波拉契数列----------动态规划DP

n==10 20 30 40 50 46 体验一下,感受一下,运行时间 #include <stdio.h>int fib(int n){ if (n<=1)     return 1; else            return fib(n-1)+fib(n-2); }int main( ){ int n; scanf("%d",&n); printf("%d\n" ,fib(n) );} 先 n==10 20 30 40 50 46

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

目录 1.斐波那契数列(Fibonacci)介绍 2.朴素递归算法(Naive recursive algorithm) 3.朴素递归平方算法(Naive recursive squaring) 4 .自底向上算法(Bottom-up) 5. 递归平方算法(Recursive squaring) 6.完整代码(c++) 7.参考资料 内容 1.斐波那契数列(Fibonacci)介绍 Fibonacci数列应该也算是耳熟能详,它的递归定义如上图所示. 下面2-6分别说明求取Fibonacci数列的

UVA10299- Modular Fibonacci(斐波那契数列+矩阵快速幂)

题目链接 题意:给出n和m,求出f(n) % m, f(x)为斐波那契数列. 思路:因为n挺大的,如果直接利用公式计算很有可能会TLE,所以利用矩阵快速幂求解,|(1, 1), (1, 0)| * |f(n - 1), f(n - 2)| = |f(n), f(n - 1)|,所以求f(n)相当于|f(1), f(0)|乘上n - 1次的|(1, 1), (1, 0)|. 代码: #include <iostream> #include <cstdio> #include <

【转】Fibonacci 斐波纳契堆优化 Dijkstra 最短路径算法

话不多说,拿来主义,直接上代码! PS:打印最短路径我还不晓得怎么加,如有哪位大神知道,还请mark一下! 1 /*********************************************************************** 2 * File: FibonacciHeap.java 3 * Author: Keith Schwarz ([email protected]) 4 * 5 * An implementation of a priority queue

fibonacci 斐波那契数列

1.小兔子繁殖问题  (有该问题的详细来由介绍) 2.台阶问题 题目:一个人上台阶可以一次上一个或者两个,问这个人上n层的台阶,一共有多少种走法. 递归的思路设计模型: i(台阶阶数)        n(走法种数)                                          组合情况 1                        1                                                         {1} 2        

递归:阶乘、斐波那契数列

阶乘 public static void main(String[] args) { System.out.println(factorial(5));; } //factorial 阶乘 public static long factorial(int n) { if (n == 1) { return 1; } return n*factorial(n-1); } 阶乘画图理解 斐波那契数列递归实现: public static void main(String[] args) { Sys

递归思想之---斐波拉契数列

斐波那契数列中的递归思想 ??如果上述的分析都明白了,那就说明你已掌握了递归,但为了加深对递归的理解,我们再来看一个思考题(来自程序员的数学思考题),题目是这样的,假如动物中有一种特殊的种类,它出生2天后就开始以每天1只的速度繁殖后代.假设第1天,有1只这样的动物(该动物刚出生,从第3天开始繁殖后代).那么到第11天,共有多少只呢? 我们先来按一般顺序思考,先不要考虑第11天,先从第1天开始,看能不能找出规律: [第1天]只有1只动物 [第2天]只有1只动物,还没有繁殖后代,总量为1 [第3天]