`
bcyy
  • 浏览: 1831648 次
文章分类
社区版块
存档分类
最新评论

POJ - Complete the Sequence! - 生成完整序列数

 
阅读更多

这个是经典题目了,好像各大OJ网站都喜欢收录。我们考IQ题目的时候经常用到的知识,就是给定一个初始数列,请选择序列的下一个数是什么。

本题就是使用程序生成序列的下几个数的。

原题如下:

You probably know those quizzes in Sunday magazines: given the sequence 1, 2, 3, 4, 5, what is the next number? Sometimes it is very easy to answer, sometimes it could be pretty hard. Because these "sequence problems" are very popular, ACM wants to implement them into the "Free Time" section of their new WAP portal.

ACM programmers have noticed that some of the quizzes can be solved by describing the sequence by polynomials. For example, the sequence 1, 2, 3, 4, 5 can be easily understood as atrivial polynomial. The next number is 6. But even more complex sequences, like 1, 2, 4, 7, 11, can be described by apolynomial. In this case, 1/2.n2-1/2.n+1 can be used. Note that even if the members of the sequence are integers, polynomial coefficients may be any real numbers.

Polynomial is anexpression in the following form:

P(n) =aD.nD+aD-1.nD-1+...+a1.n+a0

IfaD<> 0, the numberDis called adegreeof the polynomial. Note that constant functionP(n) =Ccan be considered as polynomial of degree 0, and the zero functionP(n) = 0 is usually defined to have degree -1.

Input

There is asingle positive integerTon the first line of input (equal to about 5000). It stands for the number of test cases to follow. Each test case consists of two lines. First line of each test case contains two integer numbersSandCseparated by asingle space, 1 <=S< 100, 1 <=C< 100, (S+C) <= 100. The first number,S, stands for the length of the given sequence, the second number,Cis the amount of numbers you are to find to complete the sequence.

The second line of each test case containsSinteger numbersX1,X2, ...XSseparated by aspace. These numbers form the given sequence. The sequence can always be described by apolynomialP(n) such that for everyi,Xi=P(i). Among these polynomials, we can find the polynomialPminwith the lowest possible degree. This polynomial should be used for completing the sequence.

Output

For every test case, your program must print asingle line containingCinteger numbers, separated by aspace. These numbers are the values completing the sequence according to the polynomial of the lowest possible degree. In other words, you are to print valuesPmin(S+1),Pmin(S+2), ....Pmin(S+C).

It is guaranteed that the resultsPmin(S+i) will be non-negative and will fit into the standardintegertype.

Example

Sample Input:

4
6 3
1 2 3 4 5 6
8 2
1 2 4 7 11 16 22 29
10 2
1 1 1 1 1 1 1 1 1 2
1 10
3

Sample Output:

7 8 9
37 46
11 56
3 3 3 3 3 3 3 3 3 3

引用一个博客的结题报告吧:http://rchardx.is-programmer.com/posts/16142.html

不过说的也比较简单,这里补充解说一下:

举例:序列如果是2阶,那么 n^2 +6,使用n-1替代n就会得到:n^2 + 1 - 2n + 6, 那么两者相减:2n-1这个时候就变成1阶了,继续2(n-1)-1 = 2n-3,继续相减:2,这里得到的2就相当于这个序列的0阶初始值了,利用这个初始值反推,就能重新得到这个序列。因为是0阶的时候就可以知道n等于任何值的时候的序列值都等于2.那么可以利用2推导出一阶n等于任意值的序列值,那么继续可以利用一阶序列值推出任意二阶值,如此就可以得到任意阶的序列值了。

这里要说到用什么算法就是动态规划法,由低往上推导的算法。

说的有点绕口,不过填填表就很清楚了。

#include <iostream>
#include <vector>
#include <string>

using namespace std;

void CompleteTheSequence()
{
	int T = 0;
	cin>>T;
	int S = 0, C = 0;
	while (T--)
	{
		cin>>S>>C;
		if (S < 1) continue;

		vector<vector<int> > A(S, vector<int>(S+C));
		for (int i = 0; i < S; i++) cin>>A[0][i];

		for (int i = 1; i < S; i++)
		{
			for (int j = 0; j < S-i; j++)
			{
				A[i][j] = A[i-1][j+1] - A[i-1][j];
			}
		}
		for (int i = 1; i <= C; i++) A[S-1][i] = A[S-1][0];
		for (int i = S - 2; i >= 0 ; i--)
		{
			for (int j = S-i; j < S-i+C; j++)
			{
				A[i][j] = A[i][j-1] + A[i+1][j-1];
			}
		}
		
		for (int i = S; i < S+C; i++)
		{
			cout<<A[0][i]<<' ';
		}
		cout<<endl;
	}
}

int main()
{
	CompleteTheSequence();
	return 0;
}




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics