现在的位置: 首页 > 综合 > 正文

UVA 11582 Colossal Fibonacci Numbers! 数学

2017年11月23日 ⁄ 综合 ⁄ 共 1348字 ⁄ 字号 评论关闭

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

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

 Status

Description

Download as PDF

Problem F: Colossal Fibonacci Numbers!

Oooh...pretty

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,nwhere 0 â‰¤ a,b < 264 (a and b will
not both be zero) and 1 â‰¤ n â‰¤ 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

#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;
}

抱歉!评论已关闭.