Uva11582 Colossal Fibonacci Numbers!

传送门:https://vjudge.net/problem/UVA-11582

这道题比较烦的事时unsigned long long和long long的区别,很坑呀

如果是long long,由于数据太大,会出问题·

而unsigned long long,由于一定是正数,即使溢出,也是对2<<64的求模运算的结果

(该题具体分析紫书上有)

#include <bits/stdc++.h>
using namespace std;
typedef  long long ll;
const int maxn=1000+5;
int f[maxn][maxn*6],period[maxn];
int fast_pow(ll a,ll b,int mod)
{
	ll ans=1;
	while(b){
		if(b&1)ans=ans*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ans;
}
void ini()
{
	for(int n = 2; n <= 1000; n++) {
    f[n][0] = 0; f[n][1] = 1;
    for(int i = 2; ; i++) {
      f[n][i] = (f[n][i-1] + f[n][i-2]) % n;
      if(f[n][i-1] == 0 && f[n][i] == 1) {
        period[n] = i - 1;
        break;
      }
    }
  }
}
int solve(ll a, ll b, int n) {
  if(a == 0 || n == 1) return 0; // attention!
  int p = fast_pow(a % period[n], b, period[n]);
  return f[n][p];
}
int main()
{
	std::ios::sync_with_stdio(false);
	ini();
	int T;
	cin>>T;
	while(T--){
		int n;
		ll a,b;
		cin>>a>>b>>n;
		cout<<solve(a,b,n)<<endl;
	}
	return 0;
} 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值