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. 更新KB915597补丁后导致“您的windows副本不是正版”的解决方案

    更新KB915597补丁后导致“您的windows副本不是正版”的解决方案 解决方法: 运行cw.exe(https://pan.lanzou.com/i05ya8h),直至提示成功: 重新启动操作系 ...

  2. vue系列(一)子组件和父组件

    父组件传递数据到子组件props 父组件 <template> <div class="main"> <div class="top&quo ...

  3. SAP云平台运行环境Cloud Foundry和Neo的区别

    SAP云平台提供了两套运行环境:Cloud Foundry和Neo 从下图能发现,Cloud Foundry的运行环境,基础设施由第三方公司提供,比如Amazon亚马逊和Microsoft微软,SAP ...

  4. NYOJ-255-C小加 之 随机数

    原题链接 C小加 之 随机数 时间限制:3000 ms  |  内存限制:65535 KB 难度:1   描述 ACM队的“C小加”同学想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用 ...

  5. iOS Dispatch_sync 阻塞线程的原因

    大家的知道在主队列上使用dispatch_sync(), - (void)testSyncMainThread { dispatch_queue_t main = dispatch_get_main_ ...

  6. PS 厘米与像素切换

    方法一: 快捷键 ctrl + r   打开标尺将鼠标放在标尺刻度上右键 出现菜单里修改即可: 方法二: 编辑---首选项---单位与标尺 修改即可:

  7. python基础一 day16 匿名函数

    def add(x,y): return x+y add = lambda x,y:x+yprint(add(1,2)) dic={'k1':10,'k2':100,'k3':30}def func( ...

  8. AJAX进行分页

    新建数据集:PagingDataSet.xsd SELECT * from ( select id, areaID, area, father,Row_Number() over (order by ...

  9. display:inline-block解决文字有间隙问题

    定义:display:inline-block是使元素以块级元素的形式呈现在行内.意思就是说,让这个元素显示在同一行不换行,但是又可以控制高度和宽度,这相当于内联元素的增强. 但是display:in ...

  10. z-index、absolute、marquee滚动条的问题

    1.z-index 层次叠加 ,元素叠加,谁的权重大谁就在上面 1).父级出现position:relation:的时候,失效: 2).层叠元素出现float的时候失效: 3).层次元素也得设置pos ...