HDU1211 POJ1398 UVA1546 LA2155 Complete the Sequence【多次差分】

Complete the Sequence

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1036 Accepted Submission(s): 618

Problem Description
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.n^2-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. 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 X1, X2, … 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 i, Xi = 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.

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

Source
Central Europe 2000

问题链接HDU1211 POJ1398 UVA1546 LA2155 Complete the Sequence
问题简述:给定多项式前s项,计算后c项,要求尽量小。
问题分析:利用差分法,对原序列求s - 1次差分,然后逆推。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* HDU1211 POJ1398 UVA1546 LA2155 Complete the Sequence */

#include <iostream>
#include <cstdio>

using namespace std;

const int N = 100 * 2;
int d[N][N];

int main()
{
    int t, s, c;
    scanf("%d", &t);
    while(t--) {
        scanf("%d%d", &s, &c);
        for(int i = 0; i < s; i++) scanf("%d", &d[0][i]);

        for(int i = 1; i < s; i++)
            for(int j = 0; i + j < s; j++)
                d[i][j] = d[i - 1][j + 1] - d[i - 1][j];
        for(int i = 1; i <= c; i++) d[s - 1][i] = d[s - 1][0];
        for(int i = s - 2; i >= 0; i--)
            for(int j = 0; j < c; j++)
                d[i][s + j - i] = d[i + 1][s + j - i - 1] + d[i][s + j - i - 1];

        for(int i = s; i < s + c - 1; i++)
            printf("%d ", d[0][i]);
        printf("%d\n", d[0][s + c - 1]);
    }

    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值