uva 11582(大fib,打表找循环节)

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

Sample input

three integers a,b,n where 0 ≤ a,b < 264 (a and b will not both be zero) and 1 ≤ n ≤ 1000.

T

a  b  n 
3
1 1 2
2 3 1000
18446744073709551615 18446744073709551615 1000

Sample output

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

1
21
250


其实这题的主要知识,还是找循环节,打表。

卡点:2^64-1 --->   unsigned long long


#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#define bug(a) cout<<a<<"--->\n";
using namespace std;

typedef unsigned long long ULL;

vector<int>f[1005];
int period[1005];

int qpow(ULL a,ULL b,int p)
{
    int ans=1;
    while(b)
    {
        if(b&1) ans=int((ans*a)%p);
        a=(a*a)%p;
        b>>=1;
    }
    return ans;
}

void pre_fib()
{
    for(int n=2;n<=1000;n++)
    {
        f[n].push_back(0);f[n].push_back(1);
        for(int i=2;;i++)
        {
            f[n].push_back((f[n][i-1]+f[n][i-2])%n);
            if(f[n][i-1]==0&&f[n][i-2]==1)
            {
                period[n]=i-1;
                break;
            }
        }
    }
}

int main()
{
    int T;
    pre_fib();
    scanf("%d",&T);
    while(T--)
    {
        ULL a,b;
        int p;
        int c;
        scanf("%llu%llu%d",&a,&b,&p);
        if(a==0||p==1) printf("0\n");
        else
        {
            c=qpow(a%period[p],b,period[p]);
            printf("%d\n",f[p][c]);
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值