HDU 2817 A sequence of numbers

题目链接:A sequence of numbers

解题思路: (a * b) % m = ((a % m) * (b % m))  % m   等比数列就用快速幂,否则超时。

#include<stdio.h>
#include<math.h>
#define MAX 200907
__int64 seq[5];

void call(__int64 q, int k){
	k--;
	while(k){
		if(k & 1){
			seq[0] = ((seq[0] % MAX) * (q % MAX)) % MAX;
		}
		q = ((q % MAX) * (q % MAX)) % MAX;
		k >>= 1;
	}
	
} 

int main(){
	int i, k, n;
	__int64 q;
	scanf("%d", &n); 
	while(n--){
		for(i = 0; i < 3; i++){
			scanf("%I64d", &seq[i]);
		}
		scanf("%d", &k);
		if(seq[1] - seq[0] == seq[2] - seq[1]){
			q = seq[1] - seq[0];
			seq[0] = (seq[0] % MAX + ((k - 1) % MAX * q %MAX) % MAX) % MAX;
		}
		else{
			q = seq[1] / seq[0];
			call(q, k);
		}
		printf("%I64d\n", seq[0]);
	}
	return 0;
}


 

你可能感兴趣的:(HDU 2817 A sequence of numbers)