HDU3977 Evil teacher 求fib数列模p的最小循环节
Here f(n) is the n-th fibonacci number (n >= 0)! Where f(0) = f(1) = 1 and for any n > 1, f(n) = f(n - 1) + f(n - 2). For example, f(2) = 2, f(3) = 3, f(4) = 5 ...
The teacher used to let you calculate f(n) mod p where n <= 10^18 and p <= 10^9, however , as an ACMER, you may just kill it in seconds! The evil teacher is mad about this. Now he let you find the smallest integer m (m > 0) such that for ANY non-negative integer n ,f(n) = f(n + m) (mod p) . For example, if p = 2, then we could find know m = 3 , f(0) = f(3) = 1(mod 2), f(1) = f(4) (mod 2) ....
Now the evil teacher will only give you one integer p( p <= 2* 10^9), will you tell him the smallest m you can find ?
Input
The first line is one integer T indicates the number of the test cases. (T <=20)
Then for every case, only one integer P . (1 <= P <= 2 * 10^9, the max prime factor for P is no larger than 10^6)
Output
Output one line.
First output “Case #idx: ”, here idx is the case number count from 1.Then output the smallest m you can find. You can assume that the m is always smaller than 2^64 .
Sample Input
5
11
19
61
17
67890
Sample Output
Case #1: 10
Case #2: 18
Case #3: 60
Case #4: 36
Case #5: 4440
题目让我们做什么呢,求fib数列模p的最小循环节,但是这个题目的素数一定是小于10^6,所以有下面这个水水的做法
对每一个形如p^k的数计算循环节,它们的最小公倍数就是n的循环节长度(当然这里面涉及到CRT等等方面的基础)。那么现在问题就是计算p^k的循环节,这个问题可以进一步简化成求G(p)*p^(k-1)
求fib数列模p(p是素数)的最小循环节方法:
暴力枚举fib[i]%p==0的最小的i,然后记录pos=i+1,设a为fib[i]%p==0的前一位数,即a=fib[i-1]
那么我们知道fib数列模p的最小循环节长度一定是pos*x,那么也就是说现在要求一个最小的数x
满足,
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll Lcm(ll a,ll b)
{
return a/__gcd(a,b)*b;
}
inline ll Pow(ll a,ll b,ll p)
{
ll ans=;
for(; b; b>>=,a=a*a%p)if(b&)ans=ans*a%p;
return ans;
}
const int N=1e6+;
int prime[N],tot,vis[N];
inline void Pre()
{
for(int i=; i<N; i++)
{
if(!vis[i]) prime[++tot]=i;
for(int j=; j<=tot&&1LL*prime[j]*i<N; j++)
{
vis[prime[j]*i]=;
if(i%prime[j]==)break;
}
}
}
inline ll calc(ll p)
{
ll a=,f1=,f2=,f3=%p;
while(f3) f1=f2,f2=f3,f3=(f1+f2)%p,a++;
ll ans=p-;
for(int i=; 1LL*i*i<p; i++)
if((p-)%i==)
{
if(Pow(f2,i,p)==) ans=min(ans,1LL*i);
if(Pow(f2,(p-)/i,p)==) ans=min(ans,1LL*(p-)/i);
}
return ans*a;
}
inline ll Solve(ll n)
{
ll ans=;
for(int i=; prime[i]<=n; i++)
if(n%prime[i]==)
{
ll tmp=;
while (n%prime[i]==) n/=prime[i],tmp*=prime[i];
tmp=tmp/prime[i]*calc(prime[i]);
ans=!ans?tmp:Lcm(ans,tmp);
}
return ans;
}
int main()
{
int T,n,ca=;
Pre(),scanf("%d",&T);
while(T--)scanf("%d",&n),printf("Case #%d: %I64d\n",++ca,n==?:Solve(n));
return ;
}
数字变大呢,就是解特征值了,不会啊
HDU3977 Evil teacher 求fib数列模p的最小循环节的更多相关文章
- HDU3977(斐波那契数列模n的循环节长度)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3977 题意:求斐波那契数列模p的循环节长度,注意p最大是2*10^9,但是它的素因子小于10^6. 分析过 ...
- poj2406--Power Strings(KMP求最小循环节)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 33178 Accepted: 13792 D ...
- The Minimum Length - HUST 1010(求最小循环节)
题意:有个一字符串A(本身不是循环串),然后经过很多次自增变成AAAAA,然后呢从自增串里面切出来一部分串B,用这个串B求出来A的长度. 分析:其实就是求最小循环节.......串的长度 - 最大 ...
- KMP 求最小循环节
转载自:https://www.cnblogs.com/chenxiwenruo/p/3546457.html KMP模板,最小循环节 下面是有关学习KMP的参考网站 http://blog.cs ...
- HDU 3746 - Cyclic Nacklace & HDU 1358 - Period - [KMP求最小循环节]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3746 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- kmp的next数组的运用(求字符串的最小循环节)
hdu3746 Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- hdu 3746 Cyclic Nacklace(next数组求最小循环节)
题意:给出一串字符串,可以在字符串的开头的结尾添加字符,求添加最少的字符,使这个字符串是循环的(例如:abcab 在结尾添加1个c变为 abcabc 既可). 思路:求出最小循环节,看总长能不能整除. ...
- poj 2185 Milking Grid(next数组求最小循环节)
题意:求最小的循环矩形 思路:分别求出行.列的最小循环节,乘积即可. #include<iostream> #include<stdio.h> #include<stri ...
- HDU-3746 Cyclic Nacklace 字符串匹配 KMP算法 求最小循环节
题目链接:https://cn.vjudge.net/problem/HDU-3746 题意 给一串珠子,我们可以在珠子的最右端或最左端加一些珠子 问做一条包含循环珠子的项链,最少还需要多少珠子 思路 ...
随机推荐
- pat乙级1060
将数组排序后从大到小遍历,设置一个递增的变量t,当v[i] > t的时候,说明有t个数大于t,最后一个满足v[i] > t的t即为所求结果. #include <iostream&g ...
- IOS plist的数据 存 取(沙河目录)
应用沙盒目录的常见获取方式 沙盒根目录:NSString *home = NSHomeDirectory(); Documents:(2种方式) 1.利用沙盒根目录拼接”Documents”字符串 N ...
- MVC文件下载和webform也能使用的下载方法
public ActionResult Index() { DownloadMethod("text/plain", "C:/Users/sunny/Pictures/S ...
- 【BZOJ4698】[SDOI2008] Sandy的卡片(后缀数组+二分)
点此看题面 大致题意: 给你\(N\)个序列,若定义两个相同子串为一个子串内所有数加上一个数后能变成另一个串,求所有序列中的最长相同子串的长度. 简单的转化 首先,我们对题目进行一个简单的转化. 要求 ...
- 【洛谷4884】多少个1?(BSGS)
点此看题面 大致题意: 求满足\(个111...111(N\text{个}1)\equiv K(mod\ m)\)的最小\(N\). 题目来源 这题是洛谷某次极不良心的月赛的\(T1\),当时不会\( ...
- WARNING: The TCP backlog setting of 511.解决
redis启动警告问题:WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/so ...
- AngularJS 数组
AngularJS数组就像Javascript数组 <!DOCTYPE html><html><head><meta http-equiv="Con ...
- 第十五篇、OC_同一个View实现两个手势响应
#pragma mark-UIGestureRecognizerDelegate Methods // 只要实现这个方法,就可以实现两个手势同时响应 - (BOOL)gestureRecognizer ...
- SummerVocation_Learning--java的线程死锁
public class Test_DeadLock implements Runnable { ; static Object o1 = new Object(),o2 = new Object() ...
- 3170: [Tjoi2013]松鼠聚会
Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1804 Solved: 968[Submit][Status][Discuss] Descript ...