hdu 3977 Evil teacher 求fibonacci数列的循环节

Problem Description
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

//My Code  暴力

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define bignum unsigned long long
//求fibonacci数列的循环节  f[0]=f[1]=1;
//s[p^c]=s[p]*p^(c-1) p素数
//s[n]=lcm(s[pi^ci]);
//求s[p] p<=1e6 暴力求解即可
const bignum maxn=1100000;
bignum flag[maxn],prime[maxn],pl;
void _prime()
{
    for(int i=2;i<maxn;i++)
    {
        if(flag[i]==0) prime[pl++]=i;
        for(int j=0;j<pl&&i*prime[j]<maxn;j++)
        {
            flag[i*prime[j]]=1;
            if(i%prime[j]==0) break;
        }
    }
}
//求素数p的斐波那契数列的循环节
bignum getP(bignum p)
{
    bignum a=1,b=1;
    bignum c=(a+b)%p;
    bignum cnt=1;
    while(b!=1||c!=1)
    {
        a=b;
        b=c;
        c=(a+b)%p;
        cnt++;
    }
    return cnt;
}
bignum gcd(bignum a,bignum b)
{
    return b==0?a:gcd(b,a%b);
}
bignum lcm(bignum a,bignum b)
{
    return a/gcd(a,b)*b;
}
int main()
{
    _prime();
    int ci,cases=1;scanf("%d",&ci);
    while(ci--)
    {
        bignum n;scanf("%I64d",&n);
        bignum ans=1;
        for(int i=0;prime[i]*prime[i]<=n;i++)
        {
            if(n==1) break;
            if(n%prime[i]==0)
            {
                int cnt=0;
                while(n%prime[i]==0) cnt++,n/=prime[i];
                ans=lcm(ans,getP(prime[i])*(bignum)pow(prime[i]+0.0,cnt-1));
            }
        }
        if(n>1)
        {
            ans=lcm(ans,getP(n));
        }
        printf("Case #%d: %I64d\n",cases++,ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值