编程介的小学生 2019-01-26 16:09 采纳率: 20.5%
浏览 359

C语言的函数算法去解决一个矩阵的问题,涉及到大数的计算怎么实现的?

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

  • 写回答

2条回答 默认 最新

  • JW12138 2019-01-26 19:55
    关注

    这个不是大数,用unsigned long long就能装下,运算用快速幂套模板就行

    #include<iostream>
    #include<cstdio>
    using namespace std;
    int pow_mod(int a,int n,int m)//快速幂,a为底数,n为幂数,m为mod数;
    {
        if(n==0) return 1;
        int x=pow_mod(a,n/2,m);
        long long ans=(long long)x*x%m;
        if(n%2==1) ans=ans*a%m;
        return (int)ans;
    }
    int main()
    {
        int a,n,m;
        cin>>a>>n>>m;
        cout<<pow_mod(a,n,m)<<endl;
        return 0;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)