HDU 2814 斐波那契数列的循环节问题

本文介绍了如何通过快速幂求余的算法来解决Fibonacci数列的问题,并提供了两种解题思路。其中包括了欧拉函数的应用、快速幂算法的实现、Fibonacci数列的循环节寻找等关键步骤。

http://acm.hdu.edu.cn/showproblem.php?pid=2814

Problem Description
In mathematics, the Fibonacci numbers are a sequence of numbers named after Leonardo of Pisa, known as Fibonacci (a contraction of filius Bonaccio, "son of Bonaccio"). Fibonacci's 1202 book Liber Abaci introduced the sequence to Western European mathematics, although the sequence had been previously described in Indian mathematics.
  The first number of the sequence is 0, the second number is 1, and each subsequent number is equal to the sum of the previous two numbers of the sequence itself, yielding the sequence 0, 1, 1, 2, 3, 5, 8, etc. In mathematical terms, it is defined by the following recurrence relation:

That is, after two starting values, each number is the sum of the two preceding numbers. The first Fibonacci numbers (sequence A000045 in OEIS), also denoted as F[n]; 
F[n] can be calculate exactly by the following two expressions:


A Fibonacci spiral created by drawing arcs connecting the opposite corners of squares in the Fibonacci tiling; this one uses squares of sizes 1, 1, 2, 3, 5, 8, 13, 21, and 34;

So you can see how interesting the Fibonacci number is.
Now AekdyCoin denote a function G(n)

Now your task is quite easy, just help AekdyCoin to calculate the value of G (n) mod C
 

Input
The input consists of T test cases. The number of test cases (T is given in the first line of the input. Each test case begins with a line containing A, B, N, C (10<=A, B<2^64, 2<=N<2^64, 1<=C<=300)
 

Output
For each test case, print a line containing the test case number( beginning with 1) followed by a integer which is the value of G(N) mod C
 

Sample Input
  
1 17 18446744073709551615 1998 139
 

Sample Output
  
Case 1: 120
解题思路:

对于A^B%C 有一个公式 即
A^x = A^(x % Phi(C) + Phi(C)) (mod C)

              

             

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#define max 400
int quick_mod(int a,unsigned long long  b, int c)//快速幂
{
    int ans=1;
    a=a%c;
    while(b>0)
    {
        if (b%2==1)
            ans=(ans*a)%c;
        b=b/2;
        a=(a*a)%c;
    }
    return ans;
}
int eular(int n)  //欧拉函数
{
    int ret=1,i;
    for (i=2; i*i<=n; i++)
    {
        if (n%i==0)
        {
            n/=i,ret*=i-1;
            while (n%i==0)
                n/=i,ret*=i;
        }
    }
    if (n>1)
        ret*=n-1;
    return ret;
}
unsigned long long  a,b,n;
int c;
int data1[9003];
int data2[9003];
int g[9003];
int len,len_c,len_e;
int main()
{
    int T,tt=0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%I64d%I64d%I64d%d",&a,&b,&n,&c);
        if(c==1)
        {
            printf("Case %d: 0\n",++tt);
            continue;
        }
        data1[0]=0,data1[1]=1;
        for(int i=2;; i++)
        {
            data1[i]=(data1[i-1]+data1[i-2])%c;
            if(data1[i]==1&&data1[i-1]==0)
            {
                len=i-1;
                break;
            }
        }
        int o=eular(c);
        if(o==1)
            len_c=0;
        else
        {
            data2[0]=0,data2[1]=1;
            for(int i=2;;i++)
            {
                data2[i]=(data2[i-1]+data2[i-2])%o;
                if(data2[i]==1&&data2[i-1]==0)
                {
                    len_c=i-1;
                    break;
                }
            }
        }
        int n1=quick_mod(a%len,b,len);
        g[1]=data1[n1];
        int mi_zhi_shu;
        int n2;
        if(len_c==0)
            mi_zhi_shu=0;
        else
        {
            n2=quick_mod(a%len_c,b,len_c);
            mi_zhi_shu=data2[n2];
        }
        int x=quick_mod(mi_zhi_shu,n-1,o);
        x+=o;
        x=quick_mod(g[1],x,c);
        if(n==1)
            printf("Case %d: %d\n",++tt,g[1]);
        else
            printf("Case %d: %d\n",++tt,x);
    }
    return 0;
}
解法二:  在将n从1~max列举出来,在找循环节,陈老师是这么做的。

   

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#define max 400
int quick_mod(int a,unsigned long long  b, int c)//快速幂
{
    int ans=1;
    a=a%c;
    while(b>0)
    {
        if (b%2==1)
            ans=(ans*a)%c;
        b=b/2;
        a=(a*a)%c;
    }
    return ans;
}
int eular(int n)  //欧拉函数
{
    int ret=1,i;
    for (i=2; i*i<=n; i++)
    {
        if (n%i==0)
        {
            n/=i,ret*=i-1;
            while (n%i==0)
                n/=i,ret*=i;
        }
    }
    if (n>1)
        ret*=n-1;
    return ret;
}
unsigned long long  a,b,n;
int c;
int data1[9003];
int data2[9003];
int g[9003];
int len,len_c,len_e;
int main()
{
    int T,tt=0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%I64d%I64d%I64d%d",&a,&b,&n,&c);
        if(c==1)
        {
            printf("Case %d: 0\n",++tt);
            continue;
        }
        data1[0]=0,data1[1]=1;
        for(int i=2;; i++)
        {
            data1[i]=(data1[i-1]+data1[i-2])%c;
            if(data1[i]==1&&data1[i-1]==0)
            {
                len=i-1;
                break;
            }
        }
        int o=eular(c);
        if(o==1)
            len_c=0;
        else
        {
            data2[0]=0,data2[1]=1;
            for(int i=2;;i++)
            {
                data2[i]=(data2[i-1]+data2[i-2])%o;
                if(data2[i]==1&&data2[i-1]==0)
                {
                    len_c=i-1;
                    break;
                }
            }
        }
        int n1=quick_mod(a%len,b,len);
        g[1]=data1[n1];
        int mi_zhi_shu;
        int n2;
        if(len_c==0)
            mi_zhi_shu=0;
        else
        {
            n2=quick_mod(a%len_c,b,len_c);
            mi_zhi_shu=data2[n2];
        }
        mi_zhi_shu+=o;
        for(int i=2;i<=max;i++)
            g[i]=quick_mod(g[i-1]%c,mi_zhi_shu,c);
        for(int i=max-1;i>=1;i--)
        {
            if(g[i]==g[max])
            {
                len_e=max-i;
                break;
            }
        }
        int tmp=n%len_e;
        if(n>=len_e)
        {
            while(tmp+len_e<max)
                tmp=tmp+len_e;
        }
        if(n<len_e)
            tmp=n;
        if(n==1)
            printf("Case %d: %d\n",++tt,g[1]);
        else
            printf("Case %d: %d\n",++tt,g[tmp]);
    }
    return 0;
}

HDU2019的数列有序问题通常涉及到数组排序或搜索算法。这类题目一般会给出一个未排序的整数序列,然后需要检查这个序列是否能通过某种操作变得有序。常见的操作可能是交换两个元素、删除一个元素等。 例如,你可以考虑使用二分查找或者归并排序的思想。如果序列已经是升序排列,直接返回true;如果是降序排列,也需要检查能否通过一次交换将整个序列变为升序;对于其他情况,可以尝试从中间元素开始向两边遍历,看能否通过有限次的操作使序列有序。 下面是一个简单的Python示例,假设我们有一个函数`checkSorted(nums)`,它接受一个整数列表`nums`: ```python def checkSorted(nums): n = len(nums) # 如果只有一个元素或者已经有序 if n <= 1 or nums == sorted(nums): return True # 检查是否存在逆序对 for i in range(1, n): if nums[i] < nums[i - 1]: left, right = i, n - 1 while left < right: mid = (left + right) // 2 if nums[mid] > nums[i - 1]: left = mid + 1 else: right = mid # 如果找到了逆序对并且右边界小于等于左边界,说明可以通过一次交换修复 if right <= i - 1: nums[left], nums[i - 1] = nums[i - 1], nums[left] if checkSorted(nums): return True # 否则无法修复,返回false else: return False # 所有操作都尝试过了,还是有序的 return True # 测试 nums = [4, 2, 3, 1] # 这个例子应该返回True,因为可以通过一次交换变成升序 print(checkSorted(nums)) ``` 请注意,这只是一种基本思路,实际解题时可能需要根据题目给出的具体条件进行调整。如果你遇到具体的题目,请提供题目详细描述以便我能给出更精确的帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值