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

来源:互联网 发布:还珠格格五阿哥和知画 编辑:程序博客网 时间:2024/05/07 21:05

这个是经典题目了,好像各大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 a trivial polynomial. The next number is 6. But even more complex sequences, like 1, 2, 4, 7, 11, can be described by a polynomial. 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 an expression in the following form: 

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

If aD <> 0, the number D is called a degree of the polynomial. Note that constant function P(n) = C can be considered as polynomial of degree 0, and the zero function P(n) = 0 is usually defined to have degree -1.

Input

There is a single positive integer T on 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 numbers S and C separated by a single 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, C is the amount of numbers you are to find to complete the sequence.

The second line of each test case contains S integer numbers X1X2, ... XS separated by a space. These numbers form the given sequence. The sequence can always be described by a polynomial P(n) such that for every iXi = P(i). Among these polynomials, we can find the polynomial Pmin with the lowest possible degree. This polynomial should be used for completing the sequence.

Output

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

It is guaranteed that the results Pmin(S+i) will be non-negative and will fit into the standard integer type.

Example

Sample Input:

46 31 2 3 4 5 68 21 2 4 7 11 16 22 2910 21 1 1 1 1 1 1 1 1 21 103

Sample Output:

7 8 937 4611 563 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;}




1 0
原创粉丝点击