171222 GCD of Polynomials(数学+思维)

这里写图片描述

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值