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 题意 给一串珠子,我们可以在珠子的最右端或最左端加一些珠子 问做一条包含循环珠子的项链,最少还需要多少珠子 思路 ...
随机推荐
- char*、string、CString各种字符串之间转换
参考博客: http://blog.csdn.net/luoweifu/article/details/20242307 http://blog.csdn.net/luoweifu/article/d ...
- 64位系统中为VS2012添加OpenGL工具包
之前一直都是按照网上教程进行的添加,以前使用的系统是32位的,所以一直都没有问题.最近换了64位系统,要使用到OpenGL,于是就又进行了原来的工作,但进行测试时,老是失败: 但是在目录:" ...
- 手把手教你用Docker部署一个MongoDB集群
MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中最像关系数据库的.支持类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引 ...
- iOS界面设计切图小结
iOS界面设计切图小结 APR 12TH, 2013 1.基本尺寸 (1)界面 实际设计时按: iPhone4.4s:640px*960px iPhone5: 640px*1136px iPad:15 ...
- 编译防火墙——C++的Pimpl惯用法解析
http://blog.csdn.net/lihao21/article/details/47610309 Pimpl(pointer to implementation, 指向实现的指针)是一种常用 ...
- RxJava2 方法总结
RxJava2 方法总结 看了许多讲解RxJava的文章,有些文章讲解的内容是基于第一个版本的,有些文章的讲解是通过比较常用的一些API和基础的概念进行讲解的. 但是每次看到RxJava的类中的几十个 ...
- java定义一个Circle类,包含一个double型的radius属性代表圆的半径,一个findArea()方法返回圆的面积
需求如下:(1)定义一个Circle类,包含一个double型的radius属性代表圆的半径,一个findArea()方法返回圆的面积. (2)定义一个类PassObject,在类中定义一个方法pri ...
- nodejs 用户登录密码md5加密
jade文件 div.login ul.inp-content li span= '用户名:' input.ui-input1#input1(placeholder='请输入手机号') li sp ...
- django+xadmin在线教育平台(六)
4-1 使用py3.6和django1.11开发系统前注意事项 直接通过Python3.6和django最新版本来开发我们的系统的一些注意事项. 原版本: Python 2.7 & djang ...
- JZOJ 5777. 【NOIP2008模拟】小x玩游戏
5777. [NOIP2008模拟]小x玩游戏 (File IO): input:game.in output:game.out Time Limits: 1000 ms Memory Limits ...