UVA 11582 Colossal Fibonacci Numbers! 数学


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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值