hdu 1121 Complete the Sequence(插值差分)
2015-09-05 11:25
375 查看
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1121
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.
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
Sample Output
分析:这题AC率挺高的,但是我还是觉得不简单啊。刚开始真不知道如何入手,后来看到了差分解法:http://rchardx.is-programmer.com/posts/16142.html 原来是这样,这让我想起了计算方法中的插值法(呵呵~~)。
对于一个n长度数列,需要做n-1阶差分,然后逆向推回0阶(原来的)数列,这样才能求得原数列后面的(预测)数字。
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.
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
分析:这题AC率挺高的,但是我还是觉得不简单啊。刚开始真不知道如何入手,后来看到了差分解法:http://rchardx.is-programmer.com/posts/16142.html 原来是这样,这让我想起了计算方法中的插值法(呵呵~~)。
对于一个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; }
相关文章推荐
- 关于divide and conquer的两道Leetcode
- HDU1711 Number Sequence(KMP模版题)
- parseInt和valueOf
- Lexical or preprocessor 'XXX/XXX.h' issue file not found
- POJ2299 Ultra-QuickSort(树状数组求逆序数+离散化)
- ueditor样式过滤问题
- UIday0803:UIDatePicker的属性和用法
- UIday0802:UINavigationController 传值(界面通信)
- UIDatePicker
- stack && queue && priority_queue 使用方法 (栈, 队列, 优先队列)
- leetcode_Unique Binary Search Trees_easy
- 【UI初级 连载四】--------UI常用控件
- uetitor上传图片及附件问题
- UI连载十-----------表视图单元格定制
- 【UI初级 连载三】--------UIWindow 和 UIView 的使用
- Number Sequence(hdu 5014)
- IEnumberable与IQueryable区别
- UI:自定义键盘的实现
- 常规功能和模块自定义系统 (cfcmms)—007Extjs的配置文件和自定义ui
- [LeetCode]Longest Consecutive Sequence