In the math class, the evil teacher gave you one unprecedented problem!

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 ;
}

数字变大呢,就是解特征值了,不会啊

1195 斐波那契数列的循环节

HDU3977 Evil teacher 求fib数列模p的最小循环节的更多相关文章

  1. HDU3977(斐波那契数列模n的循环节长度)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3977 题意:求斐波那契数列模p的循环节长度,注意p最大是2*10^9,但是它的素因子小于10^6. 分析过 ...

  2. poj2406--Power Strings(KMP求最小循环节)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33178   Accepted: 13792 D ...

  3. The Minimum Length - HUST 1010(求最小循环节)

    题意:有个一字符串A(本身不是循环串),然后经过很多次自增变成AAAAA,然后呢从自增串里面切出来一部分串B,用这个串B求出来A的长度.   分析:其实就是求最小循环节.......串的长度 - 最大 ...

  4. KMP 求最小循环节

    转载自:https://www.cnblogs.com/chenxiwenruo/p/3546457.html KMP模板,最小循环节   下面是有关学习KMP的参考网站 http://blog.cs ...

  5. 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 ...

  6. kmp的next数组的运用(求字符串的最小循环节)

    hdu3746 Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  7. hdu 3746 Cyclic Nacklace(next数组求最小循环节)

    题意:给出一串字符串,可以在字符串的开头的结尾添加字符,求添加最少的字符,使这个字符串是循环的(例如:abcab 在结尾添加1个c变为 abcabc 既可). 思路:求出最小循环节,看总长能不能整除. ...

  8. poj 2185 Milking Grid(next数组求最小循环节)

    题意:求最小的循环矩形 思路:分别求出行.列的最小循环节,乘积即可. #include<iostream> #include<stdio.h> #include<stri ...

  9. HDU-3746 Cyclic Nacklace 字符串匹配 KMP算法 求最小循环节

    题目链接:https://cn.vjudge.net/problem/HDU-3746 题意 给一串珠子,我们可以在珠子的最右端或最左端加一些珠子 问做一条包含循环珠子的项链,最少还需要多少珠子 思路 ...

随机推荐

  1. C# 字符串转组件名、变量名

    字符串转组件名 (Controls["button1"] as Button).Text = "Hello";//单独组件 (Controls[].Contro ...

  2. java Vamei快速教程05 实施接口

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在封装与接口中,private关键字封装了对象的内部成员.经过封装,产品隐藏了内部 ...

  3. StackOverflow之旅<2>------{HashMap和Hashtable的区别}

    问题 在Java中HashMap和Hashtable的区别? 哪一个对于多线程应用程序更好? 回答 Hashtable是同步的,加了synchronized锁,而HashMap不是.没有加synchr ...

  4. js 对象字面量

    对象字面量的输出方式以及定义好处 1.对象字面量的输出方式有两种:传统的'.' 例如:box.name 以及数组方式,只不过用数组方式输出时,方括号里面要用引号括起来 例如:box['name'] v ...

  5. JPA将查询结果转换为DTO对象

    前言 JPA支持使用@Query自定义查询,查询的结果需要字节用DTO对象接收,如果使用HQL的查询语句,可以将直接将DTO对象的构造方法传入hql中,直接转为DTO对象:而如果使用native sq ...

  6. DeepLearning tutorial(3)MLP多层感知机原理简介+代码详解

    本文介绍多层感知机算法,特别是详细解读其代码实现,基于python theano,代码来自:Multilayer Perceptron,如果你想详细了解多层感知机算法,可以参考:UFLDL教程,或者参 ...

  7. 仅用移动开发服务:开发native应用

    不花一分钱,就可以做native应用开发,这在以前是根本不敢想象的事儿.然而在今天,移动开发工具和服务已经五花八门,聪明的开发者只要随心所欲的抓取几个顺手的,就能完成native开发.今天给大家介绍的 ...

  8. Bootstrap 历练实例 - 折叠(Collapse)插件事件

    事件 下表列出了折叠(Collapse)插件中要用到的事件.这些事件可在函数中当钩子使用. 事件 描述 实例 show.bs.collapse 在调用 show 方法后触发该事件. $('#ident ...

  9. django+xadmin在线教育平台(六)

    4-1 使用py3.6和django1.11开发系统前注意事项 直接通过Python3.6和django最新版本来开发我们的系统的一些注意事项. 原版本: Python 2.7 & djang ...

  10. 自动化运维工具——ansible剧本playbook(三)

    一.Playbook--Ansible剧本 playbook是由一个或多个 "play"组成的列表 play的主要功能在于将事先归并为一组的主机装扮成事先通过ansible中的ta ...