Hduoj2817【数学二分】

/*A sequence of numbers 
Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 28   Accepted Submission(s) : 11
Font: Times New Roman | Verdana | Georgia 
Font Size: ← →
Problem Description
Xinlv wrote some sequences on the paper a long time ago, they might be arithmetic or geometric sequences. The numbers are not very clear now, 
and only the first three numbers of each sequence are recognizable. Xinlv wants to know some numbers in these sequences, and he needs your help. 
Input
The first line contains an integer N, indicting that there are N sequences. Each of the following N lines contain four integers. The first three 
indicating the first three numbers of the sequence, and the last one is K, indicating that we want to know the K-th numbers of the sequence.

You can assume 0 < K <= 10^9, and the other three numbers are in the range [0, 2^63). All the numbers of the sequences are integers. And the 
sequences are non-decreasing.

Output
Output one line for each test case, that is, the K-th number module (%) 200907. 
Sample Input
2
1 2 3 5
1 2 4 5

Sample Output
5
16

Source
2009 Multi-University Training Contest 1 - Host by TJU */
#include<stdio.h>
#define M 200907
int main()
{
	__int64 i, j, k, n, a, b, c, d,ans;
	scanf("%I64d", &n);
	while(n--)
	{
		scanf("%I64d%I64d%I64d%I64d", &a, &b, &c, &k);
		if(b-a == c-b)
		{
			d = b-a;
			ans = (a + ( ((k-1) % M) * ( d%M )) % M) % M;
		}
		else
		{
			d = (b/a) % M;//倍数 
			a %= M;
			k--;
			__int64 t = 1;
			while(k)
			{
				if(k & 1)//如果指数是奇数 拿出一个 
				{
					t *= d;
					t %= M;
				}
				d = (d*d)%M;//二分求取余值 
				k /= 2;//指数减半 
			}
			ans = (a * t) % M;
		}
		printf("%I64d\n", ans);
	}
	return 0;
}


题意:给出n个数列,每个数列要么是等差数列,要么是等比数列,并且数的范围特别大,需要对200907取余,给出前三个数字,要你求第k个数字。

思路:就是根据公式来求解,但这里等比数列时需要用到二分去求q^(k-1),即奇数时取出一个q保存进t,然后再二分q^(),求出一个新的q继续二分或者保存进t,t就是q^(k-1)的最终值。

难点:这里有2个难点,其一是题意。。。不懂的人完全不知道arithmetic or geometric就是等差和等比的意思...其次就是二分了。

你可能感兴趣的:(Hduoj2817【数学二分】)