UVA 1546 - Complete the sequence!(差分法)

来源:互联网 发布:java web小米商城项目 编辑:程序博客网 时间:2024/05/17 14:29

UVA 1546 - Complete the sequence!

题目链接

题意:给定多项式前s项,求出后c项,要求尽量小

思路:利用差分法,对原序列求s - 1次差分,就可以发现规律,然后对于每多一项,就逆推回去即可

代码:

#include <stdio.h>#include <string.h>const int N = 205;int t, s, c, a[N][N];int main() {    scanf("%d", &t);    while (t--) {scanf("%d%d", &s, &c);memset(a, 0, sizeof(a));for (int i = 0; i < s; i++)    scanf("%d", &a[0][i]);for (int i = 1; i < s; i++)    for (int j = 0; j < s - i; j++)a[i][j] = a[i - 1][j + 1] - a[i - 1][j];for (int i = 0; i < c; i++) {    for (int j = s - 1; j >= 0; j--)a[j][s - j + i] = a[j + 1][s - j - 1 + i] + a[j][s - j - 1 + i];    printf("%d%c", a[0][s + i], (i == c - 1 ? '\n' : ' '));}    }    return 0;}

1 0
原创粉丝点击