一直WA了好多次,而且还没有搞懂没啥fib数列的循环节一定出现在最开始,然后找下一个0 1出现的位置,然后今天吧啦吧啦证明一下,感觉还阔以,然后一发AC!开心!
Uva-11582Colossal Fibonacci Numbers!

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef unsigned long long ll;
const int Max_n=1e6+10;

int t,n;
ll a,b;
int f[Max_n];

int pow(ll base,ll exp,int mod){
    base%=mod;
    ll ans=1;
    while(exp){
        if(exp&1)ans=ans*base%mod;
        exp>>=1;
        base=base*base%mod;
    }
    return ans;
}

int main()
{
    scanf("%d",&t);
    while(t--){
        scanf("%llu%llu%d",&a,&b,&n);
        if(n==1||a==0){
            printf("0\n");continue;
        }
        f[0]=0;f[1]=1;
        int M;
        for(int i=2;;i++){
            f[i]=(f[i-1]+f[i-2])%n;
            if(f[i]==1&&f[i-1]==0){
                M=i-1;break;
            }
        }
        printf("%d\n",f[pow(a,b,M)]);
    }
    return 0;
}

相关文章:

  • 2022-12-23
  • 2021-09-16
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2018-03-03
猜你喜欢
  • 2021-07-28
  • 2021-06-14
  • 2021-06-01
  • 2021-07-05
  • 2022-12-23
  • 2021-08-15
  • 2021-04-30
相关资源
相似解决方案