uva 11582 - Colossal Fibonacci Numbers!(整数快速幂)

Problem F: Colossal Fibonacci Numbers!

Oooh...pretty

The i'th Fibonacci number f (i) is recursively defined in the following way:

  • f (0) = 0 and f (1) = 1
  • f (i+2) = f (i+1) + f (i) for every i ≥ 0

Your task is to compute some values of this sequence.

Input begins with an integer t ≤ 10,000, the number of test cases. Each test case consists of three integers a,b,n where 0 ≤ a,b < 264 (a and b will not both be zero) and 1 ≤ n ≤ 1000.

For each test case, output a single line containing the remainder of f (ab) upon division by n.

Sample input

3
1 1 2
2 3 1000
18446744073709551615 18446744073709551615 1000

Sample output

1
21
250
 
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

#define ull unsigned long long int
const int maxn = 1010;
vector<int> F[maxn];
int C[maxn] , n;
string a;
ull b;

void get_cycle(int mod){
	F[mod].push_back(0%mod);
	F[mod].push_back(1%mod);
	int i = 2;
	while(true){
		F[mod].push_back((F[mod][i-1]+F[mod][i-2])%mod);
		if(F[mod][i] == F[mod][1] && F[mod][i-1] == F[mod][0]){
			C[mod] = i-1;
			return;
		}
		i++;
	}
}

void ini(){
	for(int i = 1; i <= 1000; i++){
		get_cycle(i);
	}
}

int deal_a(){
	int len = a.length() , r = 0 , mod = C[n];
	for(int i = 0; i < len; i++){
		r = (r*10%mod+(a[i]-'0')%mod)%mod;
	}
	return r;
}

string dec_to_two(ull num){
	string result;
	while(num != 0){
		result.push_back(char(num%2+'0'));
		num = num/2;
	}
	return result;
}

int deal_b(int r , string temp){
	int len = temp.length() , ans = 1 , mod = C[n];
	for(int i = 0; i < len; i++){
		if(temp[i] == '1') ans = (ans*r)%mod;
		r = (r*r)%mod;
	}
	return ans;
}

void computing(){
	int remaider = deal_a();
	string temp = dec_to_two(b);
	remaider = deal_b(remaider , temp);
	printf("%d\n" , F[n][remaider]);
}

int main(){
	ini();
	int t;
	scanf("%d" , &t);
	while(t--){
		cin >> a >> b >> n;
		computing();
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值