UVA 11582 Colossal Fibonacci Numbers!(数论)



题意:输 入两个非负整数a、b和正整数n(0<=a,b<=2^64,1<=n<=1000),让你计算f(a^b)对n取模的值,其中f(0) = 0,f(1) =  1;且对任意非负整数i,f(i+2)= f(i+1)+f(i)。

思路:因为斐波那契序列要对n取模,余数只有n种,所以最多n^2项序列就开始重复,所以问题转化成了求周期然后大整数取模。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<ctime>
#define eps 1e-6
#define LL long long
#define ULL unsigned long long
#define pii (pair<int, int>)
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

const int maxn = 1000 + 100;
//const int INF = 0x3f3f3f3f;
ULL a, b;
int n;
int f[maxn*maxn];

ULL pow_mod(ULL a, ULL p, ULL n) {
	if(p == 0) return 1;
	ULL ans = pow_mod(a, p/2, n);
	ans = ans * ans % n;
	if(p%2 == 1) ans = ans * a % n;
	return ans;
} 

int main() {
    //freopen("input.txt", "r", stdin);
	int T; cin >> T;
	while(T--) {
		cin >> a >> b >> n;
		f[0] = 0, f[1] = 1%n;
		int loop = 1;
		for(int i = 2; i <= n*n+100; i++) {
			f[i] = (f[i-1]+f[i-2]) % n;
			if(f[i]==f[1] && f[i-1]==f[0]) {
				loop = i - 1;
				break;
			}
		}
		ULL ans = pow_mod(a%loop, b, (ULL)loop);
		cout << f[ans] << endl;
	} 
    return 0;
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值