码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 11582 Colossal Fibonacci Numbers!(打表+快速幂)

时间:2014-08-15 17:55:09      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   os   io   for   div   amp   

Colossal Fibonacci Numbers!

bubuko.com,布布扣

The i‘th Fibonacci number f (i) is recursively defined in the following way:

  • f (0) = 0 and f (1) = 1
  • f (i+2) = f (i+1) + f (i)  for every i ≥ 0

Your task is to compute some values of this sequence.

Input begins with an integer t ≤ 10,000, the number of test cases.Each test case consists of three integers a,b,n where 0 ≤ a,b < 264(a and b will not both be zero)and 1 ≤ n ≤ 1000.

For each test case, output a single linecontaining the remainder of f (ab) upon division by n.

Sample input

3
1 1 2
2 3 1000
18446744073709551615 18446744073709551615 1000

Sample output

1
21
250
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
#include<vector>
using namespace std;
typedef unsigned long long ULL;//long long 挂
ULL n,m,MOD;
vector<int>f[1001];
void init()
{
    for(int i=2;i<=1000;i++)
    {
        int mod=i;
        int a=0,b=1,c=(a+b)%mod;
        f[i].push_back(a);
        f[i].push_back(b);
        f[i].push_back(c);
        while(!(b==0&&c==1))
        {
            a=b;
            b=c;
            c=(a%mod+b%mod)%mod;
            f[i].push_back(c);
        }
        f[i].pop_back();
        f[i].pop_back();
    }
}
ULL quick_mod(ULL a,ULL b,ULL m)//快速幂缩小区间
{
     ULL ans = 1;
     while(b)
     {
        if(b&1)
        {
            ans=((ans%m)*(a%m))%m;
            b--;
        }
        b/=2;
        a=((a%m)*(a%m))%m;
     }
     return ans;
}
int main()
{
    int t;
    cin>>t;
    init();//打表
    while(t--)
    {
        cin>>n>>m>>MOD;
        if(MOD==1)
        {
            cout<<0<<endl;
            continue;
        }
        ULL mm=f[MOD].size();
        ULL ans=quick_mod(n,m,mm);
        cout<<f[MOD][ans]<<endl;
    }
    return 0;
}

UVA 11582 Colossal Fibonacci Numbers!(打表+快速幂),布布扣,bubuko.com

UVA 11582 Colossal Fibonacci Numbers!(打表+快速幂)

标签:style   http   color   os   io   for   div   amp   

原文地址:http://blog.csdn.net/u013582254/article/details/38587479

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!