hdu 2814 Interesting Fibonacci

斐波那契数列的高级应用

Interesting Fibonacci

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 712    Accepted Submission(s): 137


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
 

Author
AekdyCoin
 

Source
 


题解及代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef unsigned long long __int65;

int f[2000];         //记录斐波那契数

int eular(int n)       //欧拉函数
{
    int cnt=n;
    for(int i=2;i*i<=n;i++)
    if(n%i==0)
    {
        cnt-=cnt/i;
        while(n%i==0)
        {
            n/=i;
        }
    }
    if(n>1)
        cnt-=cnt/n;
    return cnt;
}

int circle(int M)     //求循环节
{
    f[0]=0;
    f[1]=1;
    f[2]=1;
    for(int i=3;;i++)
    {
        f[i]=(f[i-1]+f[i-2])%M;
        if(f[i]==f[1]&&f[i-1]==f[0])
            return i-1;
    }
}

__int65 quick_mod(__int65 a,__int65 b,int M)  //快速幂取模
{
    __int65 t=1;
    a=a%M;
    while(b)
    {
        if(b&1) t=t*a%M;
        b/=2;
        a=a*a%M;
    }
    return t;
}


int main()
{
    __int65 A,B,N,C;
    int t,cas=1;
    cin>>t;
    while(t--)
    {
        cin>>A>>B>>N>>C;
        if(C==1)
        {
           printf("Case %d: 0\n",cas++);
           continue;
        }

       __int65 oula=eular(C);

       int c=circle(C);
       __int65 cl=quick_mod(A,B,c);
       cl=f[cl];

       if(oula==1)
       {
          printf("Case %d: ",cas++);
          cout<<quick_mod(cl,1,C)<<endl;
          continue;
       }

       int t=circle(oula);
       __int65 tl=quick_mod(A,B,t);
       tl=f[tl];

       __int65 mi=quick_mod(tl,N-1,oula)+oula;
       __int65 di=cl;

       printf("Case %d: ",cas++);
       cout<<quick_mod(di,mi,C)<<endl;
    }
    return 0;
}
/*
设t=a^b;G(1)=F[t];
根据递推公式G(n)=G(n-1)^F[t];可知:G(2)=F[t]^F[t];G(3)=(F[t]^F[t])^F[t]=F[t]^(F[t]*F[t]);
这样我们能看出G(n)=F[t]^(F[t]^(n-1));
首先我们在这里要先解决F[t]的值,由于a,b的值都比较大,所以求a^b是不现实的(java你可以试试)
但是我们取余的C是比较小的,所以可以使用循环节来求出F[t]%C的值,设A=F[t]%C;
接下来求A^(F[t]^(n-1))%C的值,这里还是由于F[t]的值是没有办法求的,所以需要用到取余来简化计算,
那么我们会想到费马小定理,欧拉公式,但是这里没说C是素数,所以要用到万能公式来求解,
即首先要求出φ(C),然后再求出循环节,得到F[t]%φ(C)的值,这样我们就能求解了。
注意C=1和欧拉函数值为1的情况。
*/





### 使用多种编程语言实现输出斐波那契数列的前四项 以下是几种常见编程语言实现输出斐波那契数列前四项的方法: #### C++ 实现 在C++中可以通过简单的循环来计算并打印斐波那契数列的前几项。 ```cpp #include <iostream> using namespace std; int main() { cout << "Fibonacci数列的前4项如下:" << endl; int a = 1, b = 1; // 初始化前两项 cout << a << " " << b << " "; // 打印前两项 for (int i = 1; i <= 2; ++i) { // 计算并打印后续两项 int nextTerm = a + b; cout << nextTerm << " "; a = b; b = nextTerm; } cout << endl; return 0; } ``` 此代码片段基于引用中的逻辑[^1],简化为仅输出前四项。 --- #### Python 实现 Python 提供了一种简洁的方式来生成斐波那契数列。通过列表推导或其他方法可轻松完成任务。 ```python def fibonacci_four_terms(): terms = [1, 1] # 初始两个值 for _ in range(2): # 添加接下来的两项 terms.append(terms[-1] + terms[-2]) return terms[:4] result = fibonacci_four_terms() print("Fibonacci数列的前4项:", result) ``` 上述代码利用了动态数组的概念,类似于引用中的描述[^2],但调整为了只生成四个数值。 --- #### Java 实现 Java 中可以借助 `ArrayList` 来存储和操作斐波那契序列。 ```java import java.util.ArrayList; public class FibonacciFourTerms { public static void main(String[] args) { ArrayList<Integer> fabList = new ArrayList<>(); fabList.add(1); fabList.add(1); for (int i = 2; i < 4; i++) { fabList.add(fabList.get(i - 1) + fabList.get(i - 2)); } System.out.println("Fibonacci数列的前4项:"); for (Integer num : fabList) { System.out.print(num + " "); } } } ``` 这段代码参考了 Java 的实现方式[^5],并对范围进行了修改以便适应当前需求。 --- #### C 实现 对于更基础的语言如C,则可以直接采用数组或者变量交换的方式处理。 ```c #include <stdio.h> void print_fibonacci_first_four() { int first = 1, second = 1; printf("%d %d ", first, second); // 输出前两项目 for(int i = 3; i <= 4; i++) { // 继续计算剩余部分直到第四项为止 int third = first + second; printf("%d ", third); first = second; second = third; } } int main(){ print_fibonacci_first_four(); return 0; } ``` 该版本遵循传统迭代模式构建结果集,并且保持简单明了结构设计思路来自其他例子[^3]^。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值