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

UVA 11582 Colossal Fibonacci Numbers! 数学

时间:2014-11-09 11:16:44      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   color   ar   os   sp   


n比较小,最多n*n就回出现循环节....


Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

 Status

Description

bubuko.com,布布扣

Problem F: 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 a‰¥ 0

Your task is to compute some values of this sequence.

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

For each test case, output a single line containing 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

Zachary Friggstad

Source

Root :: AOAPC II: Beginning Algorithm Contests (Second Edition) (Rujia Liu) :: Chapter 10. Maths :: Examples
Root :: Prominent Problemsetters :: Zachary Friggstad

 Status

bubuko.com,布布扣


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef unsigned long long int uLL;

uLL d[2000200],dn;
uLL A,B,M;

uLL quickpow()
{
    uLL e=1;
    A=A%dn;
    while(B)
    {
        if(B%2)
        {
            e=(e*A)%dn;
        }
        A=(A*A)%dn;
        B/=(uLL)2;
    }
    return e;
}

int main()
{
    int T_T;
    cin>>T_T;
    while(T_T--)
    {
        dn=0;
        cin>>A>>B>>M;
        if(M==1)
        {
            puts("0"); continue;
        }
        uLL a=(uLL)1,b=(uLL)1;
        uLL la=a,lb=b;
        d[dn++]=a;d[dn++]=b;
        while(true)
        {
            a=(la+lb)%M;
            b=(lb+a)%M;
            if(d[0]==a&&d[1]==b) break;
            d[dn++]=a;d[dn++]=b;
            la=a; lb=b;
        }
        cout<<d[quickpow()-1]<<endl;
    }
    return 0;
}




UVA 11582 Colossal Fibonacci Numbers! 数学

标签:des   style   blog   http   io   color   ar   os   sp   

原文地址:http://blog.csdn.net/ck_boss/article/details/40948011

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