UVA11582-Colossal Fibonacci Numbers

题目链接

UVA11582


题意

输入两个非负整数a,b和正整数n(0<=a,b<2^64,1<=n<=1000),计算f(a^b)对n的余数。f为fibonacci数论。


题解

fibonacci数论取余存在周期性。求出数论对n取余的周期M,再通过快速幂计算 k = a^b%M。即ans = f[k];


代码

/**
 *  Give you a,b,n; a,b<=2^64, and n <= 1000;
 *
 *  f(0) = 0, f(1) = 1;
 *
 *  you should ouput f(a^b) % n;
 *
 *  now we kown that f(k)%n has a circle while length is M , (when n == 1000 , M just 1500- Circle length )
 *
 *  ans when n == 750 - Circle length is 3000;
 *
 *  then we can quick mod to calculate the k = (a^b)%M;
 *
 *  then the answer is f(k) % n;
 *
 *
 */
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 3001;
typedef unsigned long long ll; /// because a , b <= 2^64 so we should ues (unsigned long long) !!!;
ll MOD;

class Matrix
{
    public:
        ll maze[2][2];
        Matrix(){memset(maze,0,sizeof(maze));}
        void einit() { maze[0][0] = maze[1][1] = 1; }
        void pinit() { maze[0][0] = maze[1][0] = maze[0][1] = 1; }
        void ainit() { maze[0][0] = 1; }
        Matrix operator * (const Matrix &b) const;
};

Matrix Matrix::operator * (const Matrix &b) const
{
    Matrix ans;
    for(int k=0;k<2;k++)
    {
        for(int i=0;i<2;i++) if(maze[i][k])
        {
            ll temp;
            for(int j=0;j<2;j++) if(b.maze[k][j])
            {
                temp = maze[i][k] * b.maze[k][j] % MOD;
                ans.maze[i][j] = (ans.maze[i][j] + temp) % MOD;
            }
        }
    }
    return ans;
}

Matrix Mpower(Matrix a, ll b )
{
    Matrix ans;
    ans.einit();
    while(b)
    {
        if(b & 1) ans = ans * a;
        b >>= 1;
        a = a * a;
    }
    return ans;
}

ll solve (ll n, ll mod)
{
    if( n <= 1 ) return n;
    MOD = mod;
    Matrix ans,mp;
    mp.pinit();
    ans.ainit();
    mp = Mpower(mp,n-1);
    ans = mp * ans;
    return ans.maze[0][0];
}
ll fib[maxn];


ll Circle(ll n)
{
    memset(fib,0,sizeof(fib));
    fib[1] = 1;
    for(ll i=2;;i++)
    {
        fib[i] = (fib[i-1]+fib[i-2]) % n;
        if(fib[i] == 1 && fib[i-1] == 0){
            return i-1;
        }
    }
}
ll power(ll a,ll b,ll mod)
{
    ll ans = 1;
    a %= mod;                   /// there you should mod First !! because a * a will overflow!!
    while( b )
    {
        if(b & 1) ans = ans * a % mod;
        b >>= 1;
        a = a * a % mod;
    }
    return ans;
}
int main()
{
    /**for(int i=2;i<=1000;i++) {
        ll it = Circle(i);
        if(it >= 2000 )cout<<i<<" "<<it<<endl;

    }*/
    int caset;
    cin>>caset;
    while(caset--)
    {
        ll a,b,n;
        cin>>a>>b>>n;
        if( n == 1 ) {    /// so we should special judge it when n == 1;
            cout<<"0"<<endl;
            continue;
        }
        ll M = Circle(n); /// this is a BUG while n == 1; and when n==1000, M just 1500;
        ///printf("%lld\n",M);
        ll k = power(a,b,M);
        cout<<solve(k,n)<<endl;   /// there used Matrix fast power;
        ///cout<<fib[k]<<endl;     // there used fibonacci define;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值