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位,那 ...
随机推荐
- Java线程状态及Thread类中的主要方法
要想实现多线程,就必须在主线程中创建新的线程对象. 不论什么线程一般具有5种状态,即创建,就绪,执行,堵塞,终止. 创建状态: 在程序中用构造方法创建了一个线程对象后,新的线程对象便处于新建状态,此时 ...
- Androidproject夹
创建一个Android应用 File -> New -> Android Application Project 填写应用名称.project名称.包名 设置project的相关信息.默认 ...
- spring中bean的设计模式
默认的是单例的. 如果不想单例需要如下配置: <bean id="user" class="..." singleton="false" ...
- 用Qt开发Web和本地混合的应用
QtWebkit 模块使得Qt widget能够通过HTML的object标签嵌入到web页面中,并通过JavaScript代码进行访问,而Qt对象也能相应的访问web页面元素. 将Qt对象插入到we ...
- [Xcode]使用target进行协同开发
协同开发时候发现难免会因为某些条件宏导致上传到SVN的代码影响到其他同时,但是每一次去修很多条件编译也不是很方便,所以可以通过新建自己的target来控制product. 一.创建自己的target: ...
- hdu4105 Electric wave
Electric wave Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hdu2102(bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 分析:bfs求最短时间到达'P'点,不过本题有好几个trick,我都踩到了,自己还是太嫩了... ...
- iBeacon怎样工作
原文地址 iBeacons iBeacons近期是一个趋势的话题,它们同意室内定位,让你的电话知道你在基站的范围.这个能有很多应用:在停车场帮你找到你的车,零售商通过优惠券和基于位置的特别优惠,以至很 ...
- xcode Workspaces
A workspace is an Xcode document that groups projects and other documents so you can work on them to ...
- 服务器编程入门(4)Linux网络编程基础API
问题聚焦: 这节介绍的不仅是网络编程的几个API 更重要的是,探讨了Linux网络编程基础API与内核中TCP/IP协议族之间的关系. 这节主要介绍三个方面的内容:套接字( ...