UVa 11582 巨大斐波那契数列 (预处理 + 快速幂)



【题意】


题意很好 理解,  求 f(a^b) %n 为多少;

通过 列出前几项可以发现,  是有规律的  (废话  都  20位了  这么大的数 没规律才怪,  )

超过18位  用 unsigned long long   输入 输出 用 %llu


可以发现  当 出现  F[I-1] =F[1]  F[I]=F[2]  时 是 有周期的,  周期在  n^2 内 一定能找到


但是  很容易超时,

可以提前打表预处理


【代码实现】


//#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <vector>
#define mem(a,b) memset(a,b,sizeof(a))
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define FIN      freopen("input.txt","r",stdin)
#define FOUT     freopen("output.txt","w",stdout)
#define S1(n)    scanf("%d",&n)
#define SL1(n)   scanf("%I64d",&n)
#define S2(n,m)  scanf("%d%d",&n,&m)
#define SL2(n,m)  scanf("%I64d%I64d",&n,&m)
#define Pr(n)     printf("%d\n",n)
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r

using namespace std;
typedef unsigned long long ll;
const double PI=acos(-1);
const int INF=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e5+5;
const int MAXN=50005;
const int mod=1e9+7;
int dir[5][2]={0,1,0,-1,1,0,-1,0};



ll f[1005][maxn];
ll go[maxn];
ll a,b,n;
ll qpow(ll x,ll y,ll MOD){ll res=1;for(;y;y>>=1){if(y&1)res=(res*x)%MOD;x=(x*x)%MOD;}return res;}
void init()
{
    for(int i=2;i<=1000;i++)
    {
        f[i][0]=0,f[i][1]=1,f[i][2]=1;
        for(int j=3;;j++)
        {
            f[i][j]=(f[i][j-1]%i+f[i][j-2]%i)%i;
            if(f[i][j]==f[i][1]&&f[i][j-1]==f[i][0])
            {
                go[i]=j-1;
                break;
            }

        }
    }
}
int main()
{
    init();
    int T;
    cin>>T;
    while(T--)
    {
        scanf("%llu%llu%llu",&a,&b,&n);
        if(n==1|| !a)
        {
            printf("0\n");
            continue;
        }
        //printf("%llu %llu \n",a%go[n],b);
        ll t=qpow(a%go[n],b,go[n]);
        printf("%llu\n",f[n][t%go[n]]);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值