题意:

给出 a, b ,n。

求 f(a^b)%n的值。f()是斐波那契数列。

UVA 11582

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<bits/stdc++.h>
typedef unsigned long long ll;
using namespace std;
ll f[1000005];
ll pow_mod(ll a,ll n,ll mod)
{
   ll res=1;
    while(n>0)
    {
        if(n&1)
            res=res*a%mod;
        a=(a%mod)*(a%mod)%mod;
        n>>=1;
    }
    return res;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
       ll a,b,n,mod;
       cin>>a>>b>>n;
       f[1]=1%n,f[2]=1%n;
       for(int i=3;;i++)
       {
           f[i]=f[i-1]+f[i-2];
           f[i]%=n;
           if(f[i]==f[2]&&f[i-1]==f[1])
           {
               mod=i-2;
               break;
           }
       }
       ll x=pow_mod(a,b,mod);
       cout<<f[x]<<endl;
    }
    return 0;
}

 

相关文章:

  • 2021-09-13
  • 2021-10-03
  • 2022-12-23
  • 2022-12-23
  • 2022-01-18
  • 2022-12-23
  • 2022-01-23
  • 2022-12-23
猜你喜欢
  • 2021-07-28
  • 2022-02-24
  • 2021-12-02
  • 2022-12-23
  • 2021-11-19
  • 2021-10-13
相关资源
相似解决方案