hdu2814(斐波那契,费马小定理,快速幂)

202 篇文章 0 订阅
154 篇文章 0 订阅

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





思路容易理解,代码不好写,这就是数学难的地方啊……

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef unsigned long long ll;

int oula(int n)
{
    int res=1;
    for(int i=2;i*i<=n;i++)
    {
        if(n%i==0)
        {
            n/=i;res*=i-1;
            while(n%i==0)
                n/=i,res*=i;
        }
    }
    if(n>1)res*=n-1;
    return res;
}

int quick(int a,ll b,int c)
{
    int ans=1;
    a=a%c;
    while(b>0)
    {
        if(b&1)
            ans=(ans*a)%c;
        b/=2;
        a=(a*a)%c;
    }
    return ans;
}

int main()
{
    int t,o=1;
    cin>>t;
    while(t--)
    {
        int c,len1;
        ll a,b,n;
       scanf("%I64d%I64d%I64d%d",&a,&b,&n,&c);
       if(c==1)
        {
            printf("Case %d: 0\n",o++);continue;
        }
        ll f[5000]={0};
        f[1]=1;
        for(int i=2;i<5000;i++)
        {
            f[i]=(f[i-1]+f[i-2])%c;
            if(f[i]==f[1]&&f[i-1]==f[0])
            {
                len1=i-1;break;
            }
        }
        int c1=oula(c),len2=0; ll f1[5000]={0};
        if(c1>1)
        {
            f1[1]=1;
            for(int i=2;i<5000;i++)
        {
            f1[i]=(f1[i-1]+f1[i-2])%c1;
            if(f1[i]==f1[1]&&f1[i-1]==f1[0])
            {
                len2=i-1;break;
            }
        }
        }
        int n1=quick(a%len1,b,len1);
        int g1=f[n1],g2;
        if(c1==1)
            g2=0;
        else
        {
            int n2=quick(a%len2,b,len2);
            g2=f1[n2];
        }
        int yu=quick(g2,n-1,c1);
        int yu1=quick(g1,yu+c1,c);
        if(n==1)
            printf("Case %d: %d\n",o++,g1);
        else
            printf("Case %d: %d\n",o++,yu1);
    }
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值