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-斐波那契周期节的更多相关文章
- HDU 2814 斐波那契循环节 欧拉降幂
一看就是欧拉降幂,问题是怎么求$fib(a^b)$,C给的那么小显然还是要找循环节.数据范围出的很那啥..unsigned long long注意用防爆的乘法 /** @Date : 2017-09- ...
- Java Fibonacci 斐波那契亚
Java Fibonacci 斐波那契亚 /** * <html> * <body> * <P> Copyright 1994-2018 JasonInternat ...
- 递归算法之Fibonacci 斐波那契数列第n个数的求解
Fibonacci 斐波那契数列第n个数的求解,也可以用递归和非递归的形式实现,具体如下,dart语言实现. int fibonacci(int n) { if (n <= 0) throw S ...
- lintcode:Fibonacci 斐波纳契数列
题目: 斐波纳契数列 查找斐波纳契数列中第 N 个数. 所谓的斐波纳契数列是指: 前2个数是 0 和 1 . 第 i 个数是第 i-1 个数和第i-2 个数的和. 斐波纳契数列的前10个数字是: 0, ...
- hdu number number number 斐波那契数列 思维
http://acm.hdu.edu.cn/showproblem.php?pid=6198 F0=0,F1=1的斐波那契数列. 给定K,问最小的不能被k个数组合而成的数是什么. 赛后才突然醒悟,只要 ...
- 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区间 ...
- 算法导论-求(Fibonacci)斐波那契数列算法对比
目录 1.斐波那契数列(Fibonacci)介绍 2.朴素递归算法(Naive recursive algorithm) 3.朴素递归平方算法(Naive recursive squaring) 4 ...
- HDOJ/HDU 5686 Problem B(斐波拉契+大数~)
Problem Description 度熊面前有一个全是由1构成的字符串,被称为全1序列.你可以合并任意相邻的两个1,从而形成一个新的序列.对于给定的一个全1序列,请计算根据以上方法,可以构成多少种 ...
- HDU 1568 快速求斐波那契前四位
思路: 把斐波那契通项公式转化成log的形式,高中数学... //By SiriusRen #include <bits/stdc++.h> using namespace std; ], ...
- hdu 4099 字典树 + 斐波那契
题意: 给你一个串(最长40位)问你这个串是斐波那契F(n) n <= 99999中的那个数的前缀,如果存在多个输出最小的n否则输出-1. 思路: 给的串最长40位,那 ...
随机推荐
- NetBSD是个开源到源码的系统
How to get NetBSD NetBSD is an Open Source operating system, and as such it is freely available for ...
- [半原创]指纹识别+谷歌图片识别技术之C++代码
原地址:http://blog.csdn.net/guoming0000/article/details/8138223 以前看到一个http://topic.csdn.net/u/20120417/ ...
- UVAlive 2519 Radar Installation (区间选点问题)
Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. ...
- Graphical Shell with WebShell - WebOS Internals
Graphical Shell with WebShell - WebOS Internals Graphical Shell with WebShell From WebOS Internals J ...
- Spring集成XFire开发WebService
Spring是眼下最流行的JavaEE Framework,可是使用Spring的Spring-WS开发WebService却十分繁琐.XFire是一个简化WebService开发的开源项目.通过Sp ...
- OpenCV-Python教程(5、初级滤波内容)
本篇文章介绍如何用OpenCV-Python来实现初级滤波功能. 提示: 转载请详细注明原作者及出处,谢谢! 本文介绍使用OpenCV-Python实现基本的滤波处理 本文不介绍滤波处理的详细概念,所 ...
- 阿里2016实习offer五面经验与总结(转)
前言 目前楼主已经拿到阿里实习offer,一共经历了5次面试,其中4轮技术面,1轮HR面试.在这里分享一下自己的面试经验和学习总结.写这篇面经主要是希望能够帮助更多的小伙伴.我本科毕业于中南大学信管专 ...
- Android利用反射获取状态栏(StatusBar)高度
MainActivity如下: package cc.teststatusbarheight; import java.lang.reflect.Field; import android.os.Bu ...
- 〖Groovy〗语言使用贴士(Tips)(转)
[Groovy]是一门运行在[JVM]之上的动态语言.由[James Strachan]和[Bob McWhirter]于2003年启动开发,之后于2004年3月成为[JSR 241](Java Sp ...
- pro-engineer&UG
Pro/Engineer操作软件是美国参数技术公司(PTC)旗下的CAD/CAM/CAE一体化的三维软件.Pro/Engineer软件以参数化著称,是参数化技术的最早应用者,在目前的三维造型软件领域中 ...