HDU 1121 Complete the Sequence 差分

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1121

Complete the Sequence

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

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.n^D+aD-1.n^D-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

题解:

参考别人写的,跑了下样例帮助理解>-<:http://blog.csdn.net/wangjie_wang/article/details/9149683

ac代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4
 5 using namespace std;
 6
 7 const int maxn = 111;
 8
 9 int s, c;
10 int f[maxn][maxn];
11
12 void init() {
13     memset(f, 0, sizeof(f));
14 }
15
16 int main() {
17     int tc;
18     scanf("%d", &tc);
19     while (tc--) {
20         init();
21         scanf("%d%d", &s, &c);
22         for (int i = 0; i < s; i++) scanf("%d", &f[0][i]);
23         for (int i = 1; i <= s - 1; i++) {
24             for (int j = 0; j < s - i; j++) {
25                 f[i][j] = f[i - 1][j + 1] - f[i - 1][j];
26             }
27         }
28         for (int i = 1; i <= c; i++) f[s - 1][i] = f[s - 1][i - 1];
29         for (int i = s - 2; i >= 0; i--) {
30             for (int j = s - i; j < s + c - i; j++) {
31                 f[i][j] = f[i][j - 1] + f[i + 1][j - 1];
32             }
33         }
34         printf("%d", f[0][s]);
35         for (int i = s + 1; i < s + c; i++) printf(" %d", f[0][i]);
36         printf("\n");
37     }
38     return 0;
39 }

时间: 2024-08-03 08:45:43

HDU 1121 Complete the Sequence 差分的相关文章

HDU 5783 Divide the Sequence(数列划分)

HDU 5783 Divide the Sequence(数列划分) Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Problem Description - 题目描述 Alice has a sequence A, She wants to split A into as much as possible continuous subsequences, satisfy

HDU 5063 Operation the Sequence(暴力)

HDU 5063 Operation the Sequence 题目链接 把操作存下来,由于只有50个操作,所以每次把操作逆回去运行一遍,就能求出在原来的数列中的位置,输出即可 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int N = 100005; const ll MOD = 100000

hdu 4893 Wow! Such Sequence!(线段树)

题目链接:hdu 4983 Wow! Such Sequence! 题目大意:就是三种操作 1 k d, 修改k的为值增加d 2 l r, 查询l到r的区间和 3 l r, 间l到r区间上的所以数变成最近的斐波那契数,相等的话取向下取. 解题思路:线段树,对于每个节点新增一个bool表示该节点以下的位置是否都是斐波那契数. #include <cstdio> #include <cstring> #include <cstdlib> #include <algor

HDU Wow! 4893 Such Sequence!(线段树)

HDU 4893 Wow! Such Sequence! 题目链接 题意:给定一个序列,3种操作,单点添加值,查询区间和,把区间和变成最接近的婓波那契数 思路:线段树,就是第三个操作麻烦,就在结点添加一个值,标记它区间是不是都是婓波那契数了,然后修改区间的时候,如果区间是了就不用修改,如果不是就继续往后一层推即可 代码: #include <cstdio> #include <cstring> #include <cstdlib> #define lson(x) ((x

HDU 4893 Wow! Such Sequence! 水线段树

思路: 线段树走起.. 写完这题就退役T^T 单点更新的时候直接找到这个点的最近fib,然后维护当前和 和 fib的和 #include<stdio.h> #include<string.h> #include<iostream> #include<math.h> #include<algorithm> #include<queue> #include<map> #include<set> #include&l

hdu 4893 Wow! Such Sequence!(线段树功能:单点更新,区间更新相邻较小斐波那契数)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4893 --------------------------------------------------------------------------------------------------------------------------------------------

HDU 4893 Wow! Such Sequence! (线段树)

Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 838    Accepted Submission(s): 245 Problem Description Recently, Doge got a funny birthday present from his new friend, Prot

HDU 5288 OO’s Sequence (暴力枚举因子)

题目链接:HDU 5288 OO's Sequence 题意:给出一个n,表示n个数的序列,函数f(l,r)定义,在l,r区间中存在多少个数,不能被其他数整除.求累加所有子区间的函数值 思路:从ai的小范围入手 1.a不能被b整除,即a的所有因子中不存在b,所以打表枚举所有的数的因子. 2.找到一个数(位置为i)满足条件时最左端l和最右端r,(i-l)*(r-i)就是对答案的贡献. AC代码: #include <stdio.h> #include <algorithm> #inc

判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence

1 // 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence 2 // 题意:三种操作,1增加值,2开根,3求和 3 // 思路:这题与HDU 4027 和HDU 5634 差不多 4 // 注意开根号的话,遇到极差等于1的,开根号以后有可能还是差1.如 5 // 2 3 2 3... 6 // 8 9 8 9... 7 // 2 3 2 3... 8 // 8 9 8 9... 9 // 剩下就是遇到区间相等的话,就直接开根号不往下传 10 11 12