【HDU 2814 扩展欧拉 a^b ≡ (a mod c)^b mod ϕ(c)+ϕ(c) modc,b>=ϕ(c) 】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sxy201658506207/article/details/84677168

G(1)=F(ab)G(1)=F(ab) 
G(n)=G(n−1)F(ab)(n>=2)G(n)=G(n−1)F(ab)(n>=2)
求G(n)modc

具体:
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 <bits/stdc++.h>
#define X 10005
#define inF 0x3f3f3f3f
#define PI 3.141592653589793238462643383
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
typedef long long ll;   //2^32
typedef unsigned long long Ull; //2^64
const int maxn = 1e6 + 10;
const int Times = 10;
const ll inf = 9223372036854775807;
int N = 1e6 + 10;
int primer[maxn], a[maxn];
Ull f[20005];                   //注意要使用无符号形
Ull phi(Ull n){   //欧拉函数值
    ll sum=n,i;
    for(i=2;i*i<=n;i++){
        if(n%i==0){
            sum-=sum/i;
            while(n%i==0)
            n/=i;
        }
    }
    if(n>1)
    sum-=sum/n;
    return sum;
}
Ull quickmod(Ull a,Ull b,Ull  m){
    Ull sum=1;                   //快速幂
    a=a%m;
    while(b){
        if(b&1)
        sum=sum*a%m;
        b>>=1;
        a=a*a%m;
    }
    return sum;
}
Ull loop(Ull m){
    ll i;                       //求斐波那契数的循环节
    f[0]=0;f[1]=1;f[2]=1;
    for(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;
    }
}
int main(){                                     //这道题主要运用了两个性质:
    Ull a,b,n,c,i,j,l,ph,cur,sum;//1.a^b ≡ (a mod c)^b mod ϕ(c)+ϕ(c) modc,b>=ϕ(c) ϕ(c)为c的欧拉函数值
    int k,t;                                    //2.斐波那契数列有一个性质,它的n次方取模会出现一个循环节.假设循环节长度为len,
                                                //       则F(a^b) modc = F(a^bmod len)modc

    cin>>t;
    for(k=1;k<=t;k++){
        cin>>a>>b>>n>>c;
        printf("Case %d: ",k);
        if(c==1){                               //c等于0时,模一定为0
            printf("0\n");
            continue;
        }                                       //根据以上性质本题变为求F(a^b)^(F(a^b)^(n-1))%c(F()为斐波那契)
        ph=phi(c);                              //先求出欧拉函数值
        l=loop(c);                              //求循环
        cur=quickmod(a,b,l);
        cur=f[cur];                             //求出F[a^b%len]
        if(ph==1){                              //欧拉函数为1时单独考虑
            cout<<quickmod(cur,n-1,c)<<endl;
            continue;
        }
        l=loop(ph);
        sum=quickmod(a,b,l);
        sum=f[sum];                             //求出关于欧拉函数值F[a^b%len]
        sum=quickmod(sum,n-1,ph)+ph;
        cur=quickmod(cur,sum,c);                //从而求出结果
        cout<<cur<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sxy201658506207/article/details/84677168
今日推荐