HDU 3221 Brute-force Algorithm(歐拉公式降冪)

Problem Description
Professor Brute is not good at algorithm design. Once he was asked to solve a path finding problem. He worked on it for several days and finally came up with the following algorithm:

Any fool but Brute knows that the function “funny” will be called too many times. Brute wants to investigate the number of times the function will be called, but he is too lazy to do it.

Now your task is to calculate how many times the function “funny” will be called, for the given a, b and n. Because the answer may be too large, you should output the answer module by P.
 

Input
There are multiple test cases. The first line of the input contains an integer T, meaning the number of the test cases.

For each test cases, there are four integers a, b, P and n in a single line.
You can assume that 1≤n≤1000000000, 1≤P≤1000000, 0≤a, b<1000000.
 

Output
For each test case, output the answer with case number in a single line.
 

Sample Input
3 3 4 10 3 4 5 13 5 3 2 19 100
 

Sample Output
Case #1: 2 Case #2: 11 Case #3: 12
 
分析:可以發現歐拉公式降冪
A^B%C=A^( B%Phi[C] + Phi[C] )%C   (B>=Phi[C])
如果某個出現次數大於phi[c]個可以直接降冪
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int INF=0x3f3f3f3f;
typedef long long LL;
int mod,p;
LL f[110];
struct Matrix{
    LL mat[2][2];
    void Clear()
    {
        CLEAR(mat,0);
    }
};
Matrix A;
Matrix mult(Matrix m1,Matrix m2)
{
    Matrix ans;
    for(int i=0;i<2;i++)
    {
        for(int j=0;j<2;j++)
        {
            ans.mat[i][j]=0;
            for(int k=0;k<2;k++)
                ans.mat[i][j]=(ans.mat[i][j]+m1.mat[i][k]*m2.mat[k][j]%mod)%mod;
        }
    }
    return ans;
}
Matrix Pow(Matrix m1,LL b)
{
    Matrix ans;ans.Clear();
    for(int i=0;i<2;i++)
        ans.mat[i][i]=1;
    while(b)
    {
        if(b&1)
            ans=mult(ans,m1);
        b>>=1;
        m1=mult(m1,m1);
    }
    return ans;
}
int Euler(int x)
{
    int res=x,a=x;
    for(int i=2;i*i<=a;i++)
    {
        if(a%i==0)
        {
            res=res/i*(i-1);
            while(a%i==0)
                a/=i;
        }
    }
    if(a>1) res=res/a*(a-1);
    return res;
}
LL POW(LL a,LL b)
{
    LL ans=1;
    while(b)
    {
        if(b&1)
            ans=(ans*a)%p;
        a=(a*a)%p;
        b>>=1;
    }
    return ans;
}
LL solve(LL a,LL n)
{
    f[1]=1;f[2]=1;int i;
    for(i=3;i<=n;i++)
    {
        f[i]=f[i-1]+f[i-2];
        if(f[i]>=mod)
            break;
    }
    if(i>n)
       return POW(a,f[n]);
    else
    {
       // cout<<"fuck  "<<n<<endl;
        Matrix ans=Pow(A,n-2);
        LL c=ans.mat[0][1]+ans.mat[0][0]+mod;
        return POW(a,c);
    }
}
int main()
{
    int t,n,a,b,cas=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d%d",&a,&b,&p,&n);
        if(n<=3)
        {
            if(n<=2) printf("Case #%d: %lld\n",cas++,n==1?a%p:b%p);
            else  printf("Case #%d: %lld\n",cas++,1LL*a*b%p);
            continue;
        }
        A.Clear();
        A.mat[0][0]=A.mat[0][1]=1;
        A.mat[1][0]=1;A.mat[1][1]=0;
        mod=Euler(p);
        printf("Case #%d: %lld\n",cas++,solve(a,n-2)*solve(b,n-1)%p);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章