171222 GCD of Polynomials(数学+思维)

来源:互联网 发布:smtp默认端口 编辑:程序博客网 时间:2024/05/16 23:01

这里写图片描述

Input

You are given a single integer n (1 ≤ n ≤ 150) — the number of steps of the algorithm you need to reach.

Output

Print two polynomials in the following format.

In the first line print a single integer m (0 ≤ m ≤ n) — the degree of the polynomial.

In the second line print m + 1 integers between  - 1 and 1 — the coefficients of the polynomial, from constant to leading.

The degree of the first polynomial should be greater than the degree of the second polynomial, the leading coefficients should be equal to 1. Euclid’s algorithm should perform exactly n steps when called using these polynomials.

If there is no answer for the given n, print -1.

If there are multiple answer, print any of them.

Examples

input
1
output
1
0 1
0
1
input
2
output
2
-1 0 1
1
0 1
Note
In the second example you can print polynomials x2 - 1 and x. The sequence of transitions is

(x2 - 1, x) → (x,  - 1) → ( - 1, 0).
There are two steps in it.

注意:

1、审题! 审题! 审题! 读懂题意是做题的基础和关键!
2、理思路
a)为什么 f[i] = x*f[i-1] +/- f[i-2] 是f[i] mod f[i-1] 唯一的分解方式?
b) 递推的关系,该怎么用程序处理比较好?
c) 为什么 +/- 中必有一种方案能使系数的绝对值小于2?

审题! 审题! 审题! !!!!!

#include <bits/stdc++.h>using namespace std;const int maxn = 200;int f[maxn][maxn];int main(){    int n;    cin >> n;    f[0][0] = 1;    f[1][0] = 0;    f[1][1] = 1;    f[2][0] = -1;    f[2][1] = 0;    f[2][2] = 1;    for(int i = 3; i <= n; i++){        for(int j = i; j > 0; j--)            f[i][j] = f[i-1][j-1];        f[i][0] = 0;        for(int j = i-2; j >= 0; j--)            f[i][j] += f[i-2][j];        bool flag = false;        for(int j = 0; j <= i; j++)            if(abs(f[i][j]) >= 2){                flag = true;break;  // +时不能保证系数为-1, 0, 1            }        if(flag)            for(int j = 0; j <= i-2; j++)                f[i][j] -= 2*f[i-2][j];    }    // 输出, 第一个坐标表示最高的次数    cout << n << endl;    for(int i = 0; i <= n; i++)        cout << f[n][i] << " ";    cout << endl;    cout << n-1 << endl;    for(int i = 0; i <= n-1; i++)        cout << f[n-1][i] << " ";    cout << endl;    return 0;}
原创粉丝点击