1. 程式人生 > >【HDU 2814 擴充套件尤拉 a^b ≡ (a mod c)^b mod ϕ(c)+ϕ(c) modc,b>=ϕ(c) 】

【HDU 2814 擴充套件尤拉 a^b ≡ (a mod c)^b mod ϕ(c)+ϕ(c) modc,b>=ϕ(c) 】

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;
}