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 反射机制[Method反射]
Java 反射机制[Method反射] 接着上一篇Java 反射机制[Field反射],通过调用Person类的setName方法将obj的name字段的Value设置为"callPerso ...
- 超人学院Hadoop大数据技术资源分享
超人学院Hadoop大数据技术资源分享 http://bbs.superwu.cn/forum.php?mod=viewthread&tid=807&fromuid=645 很多其它精 ...
- 做SEO所要具备的四种能力
1,不为失败找借口 既然我们选择了做SEO,那么发生网站被降权.被K是常常的事.当这样的情况发生时,大部分站长首先将责任推给百度机制,由于百度更新算法调整遭降权,不是由于他们的优化没有 ...
- VSTO学习笔记(十四)Excel数据透视表与PowerPivot
原文:VSTO学习笔记(十四)Excel数据透视表与PowerPivot 近期公司内部在做一种通用查询报表,方便人力资源分析.统计数据.由于之前公司系统中有一个类似的查询使用Excel数据透视表完成的 ...
- FOJ 1591 —— Coral的烦恼
#include<stdio.h> int main() { __int64 n,i,sum,l,r; while(scanf("%I64d",&n)!=EOF ...
- 使用CXF创建REST WEBSERVICE
简单小结下CXF跟REST搭配webservice的做法,直接举代码为样例: 1 order.java package com.example.rest; import javax.xml.bin ...
- 百度map android sdk3.5实现定位 并跳转的指定坐标,加入标记
前几天又下载了新的百度地图sdk,3.5版本号.发现百度地图api有了较大变化 定位和3.0版本号差点儿相同 可是设置地图中心和加入maker标记有较大变化 设置地图中心点 // 定义地图状态zoom ...
- extern int *a与extern int a[]
extern int *a与int a[] Table of Contents 1. 问题: 2. 解答: 1 问题: 以下的声明取自某个源文件: int a[10]; int *b=a; 但在还有一 ...
- PHPExcel融入ZF2
下载PHPExcel至vendor下一个 在public\index.php加拿大 require './vendor/Classes/PHPExcel.php'; 之后就能够在不论什么地方按例如以下 ...
- 【译】ASP.NET MVC 5 教程 - 2:添加控制器
原文:[译]ASP.NET MVC 5 教程 - 2:添加控制器 MVC 表示 模型-视图-控制器.MVC 是一种用于开发应用程序的模式,具备良好架构,可测试和易于维护.基于 MVC 应用程序中包含: ...