HDU 3221 Brute-force Algorithm

Brute-force Algorithm

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2922    Accepted Submission(s): 802


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
 

Source
 
Recommend
zhuweicong


题意:给一个公式,计算函数被运行多少次
很容易算出来的一个公式为f(n)=f(n-1)*f(n-2)(n>=3),a ,b的系数很容易算出来是斐波拉契数列,对指数取模要用到公式:a^x%p=a^(x%phi(p)+phi(p))%p,计算出a,b的指数快速幂一下就好了



#pragma comment(linker,"/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<cmath>
#include<vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;

#define pi acos(-1.0)
#define eps 1e-10
#define pf printf
#define sf scanf
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
#define e tree[rt]
#define _s second
#define _f first
#define all(x) (x).begin,(x).end
#define mem(i,a) memset(i,a,sizeof i)
#define for0(i,a) for(int (i)=0;(i)<(a);(i)++)
#define for1(i,a) for(int (i)=1;(i)<=(a);(i)++)
#define mi ((l+r)>>1)
#define sqr(x) ((x)*(x))

const int inf=0x3f3f3f3f;
int t;
ll a,b,p,phi,n,f[3][3],g[3][3],c,d,m[3][3],ans[3][3];//c,d分别保存a,b的系数,m存斐波拉契的矩阵,ans存最后结果的矩阵,phi保存p的欧拉函数值

ll ouler(ll x)
{
    ll ans=x;
    for(ll i=2;sqr(i)<=x;i++)
    {
        if(!(x%i))
        {
            ans=ans/i*(i-1);
            while(!(x%i))x/=i;
        }
    }
    if(x>1)ans=ans/x*(x-1);
    return ans;
}

void multi(ll a[][3],ll b[][3])//矩阵快速幂计算斐波拉契数列的第n项
{
    ll tmp[3][3];
    mem(tmp,0);
    for1(i,2)
        for1(j,2)
            for1(k,2)
            {
                tmp[i][j]=tmp[i][j]+a[i][k]*b[k][j];
                if(tmp[i][j]>phi)//当系数大于phi(p)是才能取模运算
                    tmp[i][j]=tmp[i][j]%phi+phi;
            }
    for1(i,2)
        for1(j,2)
            a[i][j]=tmp[i][j];
}

ll quick(ll x,ll y)//快速幂乘积mod p,不是phi
{
    ll ans=1;
    while(y)
    {
        if(y&1)ans=ans*x%p;
        x=x*x%p;
        y>>=1;
    }
    return ans;
}

void quick_pow(ll x)
{
    while(x)
    {
        if(x&1)multi(ans,m);
        multi(m,m);
        x>>=1;
    }
}

void init()//矩阵初始化
{
    mem(ans,0);
    ans[1][1]=ans[2][2]=1;
    f[1][1]=1,f[1][2]=0,g[1][1]=0,g[1][2]=1;
    m[1][1]=m[1][2]=m[2][1]=1,m[2][2]=0;
}

int main()
{
    sf("%d",&t);
    for1(i,t)
    {
        sf("%I64d%I64d%I64d%I64d",&a,&b,&p,&n);
        phi=ouler(p);
        init();
        if(n<=2)c=f[1][n],d=g[1][n];
        else
        {
            quick_pow(n-2);
            c=ans[1][2],d=ans[1][1];
        }
        ll q=quick(a,c)*quick(b,d)%p;
        pf("Case #%d: %I64d\n",i,q);
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值