hdu 1121 Coplete the Sequence(補間差分)


タイトル:http://acm.hdu.edu.cn/showproblem.php?pid=1121
Coplete the Sequence
Time Limit:3000/1000 MS(Java/Others)    メモリLimit:65536/32768 K(Java/Others)Total Submission(s):406    Acceepted Submission(s):247
Problem Description
You probably know those quizes in Sunday magazinness:given the sequence 1,2,3,4,5,what is the next number?Sometimes it is very easure to answer,sometimes it could be pretty hard.Because these「sequence probles」are very popur,ACM wants to implement the m into the「Free Time」section of their new WAPポロポロ.
ACM programmers have have noticed that some of the quizzzzzs can soved by describing the sequence by polynomials.For example,the sequence 1,2,3,4,5 can mysundededestood as a trivaatrivapomimimimilyexxxt.The.The.The.emimimimimimiaaaaaaaaaaaaabebeberererererererererererererererererererererererereemmmmtttttttttttttttttttttttttttttpolynomial.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 cofficients may be any real numbers.
Polynomial is an expression in the follwing form:
P(n)=aD.n^D+a D-1.n^D-1+…+a 1.n+a 0
If aD<>0,the number D is caled a degree of the polynomial.Note that constant function P(n)=C can be considedeed as polynomial of degree 0,and the zerofunction P(n)=0 is usualy defined have.1.
 
Input
The e e is a single positive integer T the first line of input.It stands for the number of test cases to follows.Each test case consists.First line of each test case contains.tments.tinteger nfingers's.parts。stands for the length of the given sequence、the second number、C is the amount of numbers Youre to find to complette e the sequence.
The second line of each test case contains S integer numbers X 1,X 2,…XS separated by a space.The numbers form the given sequence.The sequence can always be described by a polynomial P(n)such fore=Amable.eiwe can find the polynomial Pmin with the lowest possible degree.This polynomial shoud be used for comppleting the sequence.
 
Output
For evertest case,your program must print a single line containing C integer numbers,separated by a space.The numbers are the values completing the sequence accoding to the polynomial of the lowest possible+Inlutes.Intres
It is gative 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
",f[0][s+c-1]); } return 0; }