hdu 1121 Complete the Sequence(插值差分)
来源:互联网 发布:知乎 英文新闻网站 编辑:程序博客网 时间:2024/05/18 13:25
题目: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): 406 Accepted Submission(s): 247
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.
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.
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.
It is guaranteed that the results Pmin(S+i) will be non-negative and will fit into the standard integer type.
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
对于一个n长度数列,需要做n-1阶差分,然后逆向推回0阶(原来的)数列,这样才能求得原数列后面的(预测)数字。
#include <iostream>#include <cstdio>using namespace std;int f[110][110];int main(){ //freopen("cin.txt","r",stdin); int t,s,c; cin>>t; while(t--){ scanf("%d%d",&s,&c); int i,j; for(j=0;j<s;j++) scanf("%d",&f[0][j]); for(i=1;i<=s-1;i++){ for(j=0;j+i<s;j++) f[i][j]=f[i-1][j+1]-f[i-1][j]; } for(j=1;j<=c;j++) f[s-1][j]=f[s-1][0]; for(i=s-2;i>=0;i--){ for(j=0;j<c;j++) f[i][s-i+j]=f[i][s-i+j-1]+f[i+1][s-i+j-1]; } for(j=0;j<c-1;j++) printf("%d ",f[0][s+j]); printf("%d\n",f[0][s+c-1]); } return 0;}
0 0
- hdu 1121 Complete the Sequence(插值差分)
- HDU 1121 Complete the Sequence
- 【HDU】 1121 Complete the Sequence
- HDU 1121 Complete the Sequence 数学题
- hdu 1121 Complete the Sequence(DP多项式差分)
- 杭电ACM HDU 1121 Complete the Sequence
- hdu 1121 差分计算 Complete the Sequence
- hdu1121 Complete the Sequence
- HDU 1211 Complete the Sequence(差分方法)
- UVA 1546 - Complete the sequence!(差分法)
- uva 1546 - Complete the sequence!(牛顿插值法)
- HDU1121——Complete the Sequence
- 数论+差分 hdu1121 Complete the Sequence
- POJ - Complete the Sequence! - 生成完整序列数
- hdu-5063 Operation the Sequence
- HDU - 5036 Operation the Sequence
- Hdu 5063 Operation the Sequence
- Hdu 5063 Operation the Sequence
- 聊聊实际开发中经常遇到的DOM中Property和Attribute问题
- Sublime Text3可用注册码
- c语言连珠五子棋(未完成)
- 计算机网络重要知识点总结 面试必备
- 放开和心宽
- hdu 1121 Complete the Sequence(插值差分)
- 企业项目权限管理设计思路详解
- TT 哈夫曼编码
- EventBus使用和说明
- JS事件代理
- 数组指针
- 零基础学python-11.3 代码分隔符
- RAC启动和关闭(oracle 11g)
- 《git权威指南》第10章基本命令--读书笔记