Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const double eps = 1e-7;
const long long mod = 1e9 + 7;
int n;
vector<int> ans1, ans2, ans3;
int main() {
cin >> n;
ans1.clear();
ans2.clear();
ans1.push_back(1);
ans2.push_back(1);
ans2.push_back(0);
for (int t = 2; t <= n; t++) {
ans3 = ans2;
ans2.push_back(0);
int flag = 0;
for (int i = 0; i < ans1.size(); i++) {
if (ans1[i] + ans2[i + ans2.size() - ans1.size()] > 1 ||
ans1[i] + ans2[i + ans2.size() - ans1.size()] < -1) {
flag = 1;
break;
}
}
if (flag == 0) {
for (int i = 0; i < ans1.size(); i++) {
ans2[i + ans2.size() - ans1.size()] += ans1[i];
}
} else {
for (int i = 0; i < ans1.size(); i++) {
ans2[i + ans2.size() - ans1.size()] -= ans1[i];
}
}
ans1 = ans3;
}
cout << ans2.size() - 1 << endl;
for (int i = ans2.size() - 1; i >= 0; i--) cout << ans2[i] << " ";
cout << "\n" << ans1.size() - 1 << "\n";
for (int i = ans1.size() - 1; i >= 0; i--) cout << ans1[i] << " ";
cout << "\n";
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int main() {
int i, j, k, n;
cin >> n;
int c[151][151];
for (int i = 0; i <= 150; i++) {
for (int j = 0; j <= 150; j++) {
c[i][j] = 0;
}
}
c[0][0] = 1;
c[1][1] = 1;
for (j = 2; j <= n; j++) {
for (k = 0; k <= j; k++) {
if (c[j - 1][k] == 1) c[j][k + 1] = 1;
c[j][k] = (c[j][k] + c[j - 2][k]) % 2;
}
}
cout << n << endl;
for (int i = 0; i <= n; i++) {
cout << c[n][i] << " ";
}
cout << endl;
cout << n - 1 << endl;
for (int i = 0; i <= n - 1; i++) {
cout << c[n - 1][i] << " ";
}
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
vector<int> a[200];
vector<int> b;
int main() {
a[0].push_back(1);
a[1].push_back(0);
a[1].push_back(1);
int n;
cin >> n;
for (int i = 2; i <= n; ++i) {
b = a[i - 1];
b.insert(b.begin(), 0);
int l = b.size() - 2;
for (int j = 0; j < l; ++j) {
a[i].push_back(b[j] + a[i - 2][j]);
if (a[i][j] == 2) {
a[i][j] = 0;
}
}
a[i].push_back(b[l]);
a[i].push_back(b.back());
}
cout << a[n].size() - 1 << endl;
for (auto i : a[n]) {
cout << i << " ";
}
cout << endl << a[n - 1].size() - 1 << endl;
for (auto i : a[n - 1]) {
cout << i << " ";
}
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
vector<int> m[200];
int main() {
int n;
cin >> n;
m[0] = {1};
m[1] = {0, 1};
for (int i = 2; i <= n; i++) {
m[i] = m[i - 1];
m[i].insert(m[i].begin(), 0);
for (int j = 0; j < m[i - 2].size(); j++) {
m[i][j] = abs(m[i][j] + m[i - 2][j]) > 1 ? m[i][j] - m[i - 2][j]
: m[i][j] + m[i - 2][j];
}
}
cout << n << endl;
for (int i = 0; i < m[n].size(); i++) cout << m[n][i] << " ";
cout << endl;
cout << n - 1 << endl;
for (int i = 0; i < m[n - 1].size(); i++) cout << m[n - 1][i] << " ";
cout << endl;
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int N = 150;
vector<int> p[N + 1];
int main() {
p[0].push_back(1);
p[1].push_back(0);
p[1].push_back(1);
for (int i = 2; i <= N; i++) {
p[i].push_back(0);
for (int j = 0; j < p[i - 1].size(); j++) p[i].push_back(p[i - 1][j]);
bool f = 1;
vector<int> tmp = p[i];
for (int j = 0; j < p[i - 2].size(); j++) {
tmp[j] += p[i - 2][j];
if (abs(tmp[j]) > 1) {
f = 0;
break;
}
}
f = f && (tmp[tmp.size() - 1] == 1);
if (f) {
p[i].swap(tmp);
continue;
}
for (int j = 0; j < p[i - 2].size(); j++) p[i][j] -= p[i - 2][j];
}
int n;
scanf("%d", &n);
printf("%d\n", p[n].size() - 1);
for (int i = 0; i < p[n].size(); i++) printf("%d ", p[n][i]);
printf("\n%d\n", p[n - 1].size() - 1);
for (int i = 0; i < p[n - 1].size(); i++) printf("%d ", p[n - 1][i]);
puts("");
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const long double EPS = 1e-9;
const int N = 1e5 + 5;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
int n, p[200][200];
int main() {
scanf("%d", &n);
p[0][0] = 1;
p[1][0] = 0;
p[1][1] = 1;
for (int i = 2; i <= n; i++) {
p[i][0] = p[i - 2][0];
for (int j = 1; j < 200; j++) {
p[i][j] = (p[i - 1][j - 1] + p[i - 2][j]) % 2;
}
}
int d1 = 0;
for (int i = 0; i < 200; i++) d1 = max(d1, p[n][i] ? i : 0);
int d2 = 0;
for (int i = 0; i < 200; i++) d2 = max(d2, p[n - 1][i] ? i : 0);
printf("%d\n", d1);
for (int i = 0; i <= d1; i++) printf("%d ", p[n][i]);
printf("\n");
printf("%d\n", d2);
for (int i = 0; i <= d2; i++) printf("%d ", p[n - 1][i]);
printf("\n");
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
vector<int> a;
vector<int> b;
int n;
void Swap(vector<int> &a, vector<int> &b) {
vector<int> tmp = a;
a = b;
b = tmp;
}
int main() {
scanf("%d", &n);
a.push_back(0);
a.push_back(1);
b.push_back(1);
for (int j = 1; j < n; j++) {
b.push_back(0);
b.push_back(0);
for (int i = 0; i < (int)a.size(); i++) {
b[i + 1] += a[i];
}
for (int i = 0; i < (int)b.size(); i++) {
b[i] %= 2;
}
a.swap(b);
}
printf("%d\n", (int)a.size() - 1);
for (int i = 0; i < (int)a.size(); i++) {
printf("%d ", a[i]);
}
printf("\n");
printf("%d\n", (int)b.size() - 1);
for (int i = 0; i < (int)b.size(); i++) {
printf("%d ", b[i]);
}
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
int n;
struct poly {
int deg;
int a[155];
poly() { memset(a, 0, sizeof(a)); }
void mul_x() {
deg++;
for (int i = deg; i >= 1; i--) a[i] = a[i - 1];
a[0] = 0;
}
void nega() {
for (int i = 0; i <= deg; i++) a[i] = -a[i];
}
void output() {
printf("%d\n%d", deg, a[0]);
for (int i = 1; i <= deg; i++) printf(" %d", a[i]);
printf("\n");
}
} B, AmodB;
poly operator+(poly a, poly b) {
int mx = max(a.deg, b.deg);
poly ret;
ret.deg = mx;
for (int i = 0; i <= mx; i++) ret.a[i] = a.a[i] + b.a[i];
return ret;
}
int main() {
scanf("%d", &n);
AmodB.deg = 0;
AmodB.a[0] = 0;
B.deg = 0;
B.a[0] = 1;
for (int i = 1; i <= n; i++) {
poly tmp = B;
B.mul_x();
for (int i = 0; i <= AmodB.deg; i++)
if (B.a[i] == AmodB.a[i] && B.a[i]) {
AmodB.nega();
break;
}
B = B + AmodB;
AmodB = tmp;
}
B.output();
AmodB.output();
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | n = input()
a, b = 1, 2
for i in range(1,n):
c = (b << 1) ^ a
a = b
b = c
print n
for i in range(n+1):
x = 1 if (b & 1) else 0
print x,
b >>= 1
print
print n-1
for i in range(n):
x = 1 if (a & 1) else 0
print x,
a >>= 1 | PYTHON |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | import java.lang.*;
import java.util.*;
import java.io.*;
public class Main {
void solve() {
int n=ni();
int p1[]=new int[n];
int p2[]=new int[n+1];
int tmp[]=new int[n+1];
p1[0]=1; p2[1]=1;
for(int i=2;i<=n;i++){
for(int j=0;j<=n;j++) tmp[j]=p2[j];
for(int j=i;j>=1;j--){
p2[j]=p2[j-1];
}
p2[0]=0;
for(int j=0;j<i;j++){
p2[j]=(p2[j]+p1[j])%2;
}
for(int j=0;j<n;j++) p1[j]=tmp[j];
}
pw.println(n);
for(int i=0;i<=n;i++) pw.print(p2[i]+" ");
pw.println("\n"+(n-1));
for(int i=0;i<n;i++) pw.print(p1[i]+" ");
pw.println("");
}
long M = (long)1e9+7;
// END
PrintWriter pw;
StringTokenizer st;
BufferedReader br;
void run() throws Exception {
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
pw.flush();
}
public static void main(String[] args) throws Exception {
new Main().run();
}
String ns() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
String nextLine() throws Exception {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
throw new Exception(e.toString());
}
return str;
}
int ni() {
return Integer.parseInt(ns());
}
long nl() {
return Long.parseLong(ns());
}
double nd() {
return Double.parseDouble(ns());
}
} | JAVA |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 150 + 10;
int a[maxn][maxn];
int n;
int main() {
scanf("%d", &n);
a[0][0] = 1;
a[1][1] = 1;
for (int i = 2; i <= n; i++) {
for (int j = 0; j <= i; j++) {
if (j > 0) a[i][j] = a[i - 1][j - 1];
if (j <= i - 2) {
if (a[i][j] + a[i - 2][j] > 1)
a[i][j] -= a[i - 2][j];
else
a[i][j] += a[i - 2][j];
}
}
}
printf("%d\n", n);
for (int i = 0; i <= n; i++) {
printf("%d ", a[n][i]);
}
printf("\n");
printf("%d\n", n - 1);
for (int i = 0; i < n; i++) {
printf("%d ", a[n - 1][i]);
}
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long linf = 1e18;
const int N = 200 + 10;
const double eps = 1e-5;
const int mo = 1e9 + 7;
int n;
struct pol {
int d;
int a[N];
void operator=(pol &b) {
d = b.d;
memset(a, 0, sizeof a);
for (int i = 0; i <= d; i++) a[i] = b.a[i];
}
} A, B, C, D;
void mul(pol &b) {
for (int i = b.d; i >= 0; i--) {
b.a[i + 1] = b.a[i];
}
b.a[0] = 0;
b.d++;
}
void add(pol &b, pol &c) {
for (int i = 0; i <= b.d; i++) b.a[i] += c.a[i];
}
void flip(pol &b) {
for (int i = 0; i <= b.d; i++) b.a[i] = -b.a[i];
}
void sub(pol &b, pol &c) {
for (int i = 0; i <= b.d; i++) b.a[i] -= c.a[i];
}
bool ok(pol &b) {
for (int i = 0; i <= b.d; i++)
if (b.a[i] > 1 || b.a[i] < -1) return 0;
return 1;
}
void print(pol &b) {
printf("%d\n", b.d);
for (int i = 0; i <= b.d; i++) {
printf("%d ", b.a[i]);
}
printf("\n");
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
if (i == 1) {
A.d = 1, A.a[1] = 1;
B.d = 0, B.a[0] = 1;
} else {
D = A;
C = D;
mul(C);
add(C, B);
bool flag = 0;
if (!ok(C)) {
flag = 1;
sub(C, B);
sub(C, B);
}
A = C, B = D;
}
}
print(A);
print(B);
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int N = 201;
const int M = 1e9 + 7;
const double eps = 1e-6;
const double PI(acos(-1.0));
bitset<155> a, b, c;
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
a[1] = 1;
b[0] = 1;
int n;
cin >> n;
for (int i = 0; i < (n - 1); i++) {
c = (a << 1) ^ b;
b = a, a = c;
}
cout << n << endl;
for (int j = (0); j <= (n); j++) cout << a[j] << " ";
cout << endl;
cout << n - 1 << endl;
for (int j = (0); j <= (n - 1); j++) cout << b[j] << " ";
cout << endl;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline T bigmod(T p, T e, T M) {
long long ret = 1;
for (; e > 0; e >>= 1) {
if (e & 1) ret = (ret * p) % M;
p = (p * p) % M;
}
return (T)ret;
}
template <class T>
inline T gcd(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
template <class T>
inline T modinverse(T a, T M) {
return bigmod(a, M - 2, M);
}
template <class T>
inline T lcm(T a, T b) {
a = abs(a);
b = abs(b);
return (a / gcd(a, b)) * b;
}
template <class T, class X>
inline bool getbit(T a, X i) {
T t = 1;
return ((a & (t << i)) > 0);
}
template <class T, class X>
inline T setbit(T a, X i) {
T t = 1;
return (a | (t << i));
}
template <class T, class X>
inline T resetbit(T a, X i) {
T t = 1;
return (a & (~(t << i)));
}
inline long long getnum() {
char c = getchar();
long long num, sign = 1;
for (; c < '0' || c > '9'; c = getchar())
if (c == '-') sign = -1;
for (num = 0; c >= '0' && c <= '9';) {
c -= '0';
num = num * 10 + c;
c = getchar();
}
return num * sign;
}
inline long long power(long long a, long long b) {
long long multiply = 1;
for (int i = (0); i < (b); i++) {
multiply *= a;
}
return multiply;
}
int n;
long long c[202][202];
void go(int x) {
int p = x - 1, q = 0;
int l = 0;
vector<int> coeffs;
cout << x - 1 << "\n";
for (int i = x; i > 0; i -= 2) {
coeffs.push_back(c[p][q]);
coeffs.push_back(0);
q++;
p--;
}
if (x % 2) coeffs.pop_back();
reverse(coeffs.begin(), coeffs.end());
for (int J = (0); J < (coeffs.size()); J++) cout << coeffs[J] << " ";
cout << "\n";
}
int main() {
int test, cases = 1;
cin >> n;
for (int i = (0); i < (n + 1); i++) c[i][0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) & 1;
}
}
go(n + 1);
go(n);
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
vector<int> p[155];
int main() {
int n;
cin >> n;
p[0].push_back(1);
p[1].push_back(0);
p[1].push_back(1);
for (int i = 2; i <= n; i++) {
p[i] = p[i - 2];
p[i][0] = p[i - 2][0];
for (int j = 1; j <= p[i - 1].size() - 2; j++)
p[i][j] = (p[i - 1][j - 1] + p[i - 2][j]) % 2;
p[i].push_back(p[i - 1][p[i - 1].size() - 2]);
p[i].push_back(p[i - 1][p[i - 1].size() - 0 - 1]);
}
cout << n << endl;
for (auto x : p[n]) cout << x << " ";
cout << endl;
cout << n - 1 << endl;
for (auto x : p[n - 1]) cout << x << " ";
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int N = 200;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int n, cnt, flag = 1;
int a[N], b[N], c[N];
int main() {
cnt = n = read();
a[0] = -1, b[0] = 0;
while (cnt--) {
for (int i = 0; i < N - 1; i++) c[i + 1] = a[i];
c[0] = 0;
for (int i = 0; i < N; i++) c[i] += b[i];
for (int i = 0; i < N; i++)
if (abs(c[i]) > 1) {
for (int i = 0; i < N - 1; i++) c[i + 1] = a[i] * (-1);
c[0] = 0;
for (int i = 0; i < N; i++) c[i] += b[i];
break;
}
for (int i = 0; i < N; i++) b[i] = a[i], a[i] = c[i];
}
cout << n << endl;
if (a[n] < 0)
flag = -1;
else
flag = 1;
for (int i = 0; i <= n; i++) cout << a[i] * flag << " ";
cout << endl << n - 1 << endl;
if (b[n - 1] < 0)
flag = -1;
else
flag = 1;
for (int i = 0; i <= n - 1; i++) cout << b[i] * flag << " ";
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | a_coeffs = [1, 0, -1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, -1, 0, 1, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, -1, 0, 1, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, -1, 0, 1, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 1, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 1, 0, -1, 0, 0, 0, 1]
b_coeffs = [1, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, -1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, -1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, -1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, -1, 0]
def rem(a, b):
assert len(a) == len(b) + 1
coeff = a[0] * b[0]
if a[0] == -1:
a = [-c for c in a]
if b[0] == -1:
b = [-c for c in b]
b = b + [0]
assert len(a) == len(b)
r = [c1 - c2 for (c1, c2) in zip(a, b)]
assert r[0] == 0 and r[1] == 0 and r[2] != 0
r = r[2:]
r = [coeff * c for c in r]
return r
def solve(n):
a, b = a_coeffs, b_coeffs
for _ in range(n, 150):
a, b = b, rem(a, b)
if a[0] == -1:
a = [-c for c in a]
if b[0] == -1:
b = [-c for c in b]
return a, b
n = int(input())
a, b = solve(n)
print(len(a) - 1)
print(' '.join(str(c) for c in reversed(a)))
print(len(b) - 1)
print(' '.join(str(c) for c in reversed(b)))
| PYTHON3 |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | import java.util.*;
import java.io.*;
public class D {
static InputReader in = new InputReader(System.in);
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) {
int n = in.nextInt();
int[] poly1 = new int[n+1];
int[] poly2 = new int[n+1];
poly1[0] = 1;
for (int i = 1; i <= n; i++) {
int minus = -1;
for (int j = 1; j < i; j++) {
if (poly1[j-1] == -poly2[j] && poly2[j] != 0) {
minus = 1;
}
}
int[] poly1old = poly1.clone();
poly1[0] = poly2[0] * minus;
for (int j = 1; j <= i; j++) {
poly1[j] = poly1old[j-1] + poly2[j] * minus;
}
poly2 = poly1old;
}
out.println(n);
for (int i = 0; i <= n; i++) {
out.print(poly1[i] + " ");
}
out.println();
out.println(n-1);
for (int i = 0; i <= n-1; i++) {
out.print(poly2[i] + " ");
}
finish();
}
public static void finish() {
out.close();
in.close();
System.exit(0);
}
static class InputReader implements Iterator<String>, Closeable {
// Fast input reader. Based on Kattio.java from open.kattis.com
// but has method names to match Scanner
private BufferedReader r;
private String line;
private StringTokenizer st;
private String token;
public InputReader(InputStream i) {
r = new BufferedReader(new InputStreamReader(i));
}
public boolean hasNext() {
return peekToken() != null;
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public String next() {
return nextToken();
}
public String nextLine() {
try {
line = r.readLine();
} catch (IOException e) {
line = null;
}
token = null;
st = null;
return line;
}
public void close() {
try {
r.close();
} catch (IOException e) {
}
}
private String peekToken() {
if (token == null)
try {
while (st == null || !st.hasMoreTokens()) {
line = r.readLine();
if (line == null)
return null;
st = new StringTokenizer(line);
}
token = st.nextToken();
} catch (IOException e) {
}
return token;
}
private String nextToken() {
String ans = peekToken();
token = null;
return ans;
}
}
} | JAVA |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int N = 200 + 5;
int a[N][N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
a[0][1] = 1;
a[1][1] = 0;
a[1][2] = 1;
a[2][1] = -1;
a[2][2] = 0;
a[2][3] = 1;
for (int i = 3; i <= n; i++) {
for (int j = 1; j <= i + 1; j++) a[i][j] = a[i - 1][j - 1];
for (int j = 1; j <= i + 1; j++) a[i][j] += a[i - 2][j];
for (int j = 1; j <= i; j++) {
if (abs(a[i][j]) >= 2) a[i][j] %= 2;
}
}
cout << n << endl;
for (int i = 1; i <= n + 1; i++) cout << a[n][i] << ' ';
cout << endl;
cout << n - 1 << endl;
for (int i = 1; i <= n; i++) cout << a[n - 1][i] << ' ';
cout << endl;
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int N = 200;
const int inf = 1791791791;
const int mod = 1e9 + 7;
int c[N][N];
int main() {
int n;
scanf("%d", &n);
c[0][0] = c[1][1] = 1;
c[1][0] = 0;
for (int i = 2; i <= n; i++) {
for (int j = 0; j <= i; j++) {
if (j != 0) c[i][j] += c[i - 1][j - 1];
c[i][j] += c[i - 2][j];
c[i][j] %= 2;
}
}
for (int i = n; i >= n - 1; i--) {
printf("%d\n", i);
for (int j = 0; j <= i; j++) {
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
#pragma comment(linker, "/stack:16777216")
using namespace std;
const int INF = 1000000000;
const int BASE = 1000000007;
const int MAX = 100007;
const int MAX2 = 10007;
const int MAXE = 100000;
const int ADD = 1000000;
const int MOD = 1000000007;
const int CNT = 0;
vector<int> a[151];
int main() {
a[0].push_back(1);
a[1].push_back(0);
a[1].push_back(1);
for (int i = (2); i < (151); i++) {
a[i].push_back(0);
for (int j = (0); j < (a[i - 1].size()); j++) {
a[i].push_back(a[i - 1][j]);
}
bool reverse = false;
for (int j = (0); j < (a[i - 2].size()); j++) {
a[i][j] += a[i - 2][j];
if (abs(a[i][j]) >= 2) reverse = true;
}
if (reverse) {
for (int j = (0); j < (a[i - 2].size()); j++) {
a[i][j] -= 2 * a[i - 2][j];
}
}
}
int n;
cin >> n;
;
cout << a[n].size() - 1 << endl;
cout << a[n][0];
for (int i = (1); i < (a[n].size()); i++) {
cout << " " << a[n][i];
}
cout << endl << a[n - 1].size() - 1 << endl;
cout << a[n - 1][0];
for (int i = (1); i < (a[n - 1].size()); i++) {
cout << " " << a[n - 1][i];
}
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int maxn = 1e4 + 5;
vector<int> a[maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int i, j, k, m, n;
cin >> n;
a[0] = {1};
a[1] = {0, 1};
for (int i = 2; i <= n; ++i) {
a[i].push_back(0);
for (auto it : a[i - 1]) a[i].push_back(it);
int mode = 1;
for (int j = 0; j < a[i - 2].size(); ++j) a[i][j] += a[i - 2][j];
for (auto it : a[i])
if (abs(it) >= 2) mode = 0;
if (mode) continue;
for (int j = 0; j < a[i - 2].size(); ++j) a[i][j] -= a[i - 2][j] * 2LL;
}
cout << a[n].size() - 1 << endl;
for (auto it : a[n]) cout << it << " ";
cout << endl;
cout << a[n - 1].size() - 1 << endl;
for (auto it : a[n - 1]) cout << it << " ";
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, x = 0;
cin >> n;
vector<vector<int>> dp(n + 1, vector<int>(2, 0));
dp[0][0] = 1, dp[1][1] = 1;
for (int i = 2; i <= n; i++, x ^= 1) {
for (int j = 1; j <= i; j++) {
dp[j][x] = (dp[j][x] + dp[j - 1][x ^ 1]) % 2;
}
}
cout << n << '\n';
for (int i = 0; i <= n; i++) {
cout << dp[i][x ^ 1] << (i == n ? '\n' : ' ');
}
cout << n - 1 << '\n';
for (int i = 0; i < n; i++) {
cout << dp[i][x] << (i == n - 1 ? '\n' : ' ');
}
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | n = int(raw_input())
l1 = [0 for i in range(200)]
l2 = [0 for i in range(200)]
l2[0] = 1
for i in range(n):
l1, l2 = l2, [([0] + l2[:-1])[i]+l1[i] for i in range(200)]
print(n)
print " ".join([str(k%2) for k in l2[:n+1]])
print(n-1)
print " ".join([str(k%2) for k in l1[:n]])
#print l1[:10], l2[:10]
| PYTHON |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | B = [1, 0] # 1x + 0
R = [1] # 0x + 1
A = list(B)
n = int(input())
for i in range(1, n):
A += [0]
# print('A =', A)
# print('R =', R)
# print('B =', B)
for j in range(-1, -len(R)-1, -1):
A[len(A)+j] += R[len(R)+j]
if A[len(A)+j] == 2:
A[len(A)+j] = 0
R = list(B)
B = list(A)
# print(i, A, R)
print(len(B)-1)
for u in reversed(B):
print(u, end=' ')
print()
print(len(R)-1)
for u in reversed(R):
print(u, end=' ')
print()
| PYTHON3 |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
vector<int> a = {0, 1};
vector<int> b = {1};
while (--n) {
vector<int> c = {0};
vector<int> d = {0};
for (auto k : a) c.push_back(k);
for (auto k : a) d.push_back(k);
for (int i = 0; i < b.size(); i++) {
c[i] += b[i];
d[i] -= b[i];
}
int flag1 = 0;
int flag2 = 0;
for (auto k : c)
if (abs(k) > 1) flag1 = 1;
for (auto k : d)
if (abs(k) > 1) flag2 = 1;
if (flag1 && flag2)
printf("fuck!\n");
else if (flag1) {
b = a;
a = d;
} else {
b = a;
a = c;
}
}
printf("%d\n", a.size() - 1);
for (auto k : a) printf("%d ", k);
printf("\n");
printf("%d\n", b.size() - 1);
for (auto k : b) printf("%d ", k);
printf("\n");
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
ostream& operator<<(ostream& os, vector<long long>& x) {
for (long long i = 0; i < x.size(); i++) os << x[i] << ' ';
return os;
}
ostream& operator<<(ostream& os, pair<long long, long long> x) {
cout << x.first << ' ' << x.second << ' ';
return os;
}
ostream& operator<<(ostream& os, vector<pair<long long, long long> >& x) {
for (long long i = 0; i < x.size(); i++) os << x[i] << '\n';
return os;
}
long long a[155], b[155];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
a[1] = 1;
b[0] = -1;
for (long long step = 2; step <= n; step++) {
if (step % 2 == 0) {
bool neg = 0;
for (long long i = 0; i <= 150; i++) {
b[i + 1] += a[i];
if (b[i + 1] > 1) neg = true;
}
if (neg) {
for (long long i = 0; i <= 150; i++) b[i + 1] -= a[i];
for (long long i = 0; i <= 150; i++) b[i + 1] -= a[i];
}
} else {
bool neg = 0;
for (long long i = 0; i <= 150; i++) {
a[i + 1] += b[i];
if (a[i + 1] > 1) neg = true;
}
if (neg) {
for (long long i = 0; i <= 150; i++) a[i + 1] -= b[i];
for (long long i = 0; i <= 150; i++) a[i + 1] -= b[i];
}
}
}
if (a[n] == 0) {
if (b[n] == -1)
for (long long i = 0; i <= n; i++) b[i] = -b[i];
cout << n << '\n';
for (long long i = 0; i <= n; i++) cout << b[i] << ' ';
cout << '\n';
if (a[n - 1] == -1)
for (long long i = 0; i <= n; i++) a[i] = -a[i];
cout << n - 1 << '\n';
for (long long i = 0; i <= n - 1; i++) cout << a[i] << ' ';
cout << '\n';
} else {
if (a[n] == -1)
for (long long i = 0; i <= n; i++) a[i] = -a[i];
cout << n << '\n';
for (long long i = 0; i <= n; i++) cout << a[i] << ' ';
cout << '\n';
if (b[n - 1] == -1)
for (long long i = 0; i <= n; i++) b[i] = -b[i];
cout << n - 1 << '\n';
for (long long i = 0; i <= n - 1; i++) cout << b[i] << ' ';
cout << '\n';
}
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 200;
int v[maxn][maxn];
int main() {
int n;
cin >> n;
v[0][0] = 1;
v[1][1] = 1;
for (int i = 2; i <= n; i++) {
for (int j = 0; j <= i; j++) {
if (j == 0) {
v[i][j] = v[i - 2][j];
continue;
}
v[i][j] = v[i - 1][j - 1];
v[i][j] += v[i - 2][j];
v[i][j] %= 2;
}
}
cout << n << endl;
for (int i = 0; i <= n; i++) cout << v[n][i] << " ";
cout << endl << n - 1 << endl;
for (int i = 0; i < n; i++) cout << v[n - 1][i] << " ";
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long linf = 1e18;
const int N = 2000 + 10;
const double eps = 1e-5;
const int mo = 1e9 + 7;
int n;
int a[N][N];
int f[N][N];
int main() {
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
scanf("%d", &n);
n++;
f[1][1] = 1;
f[2][1] = 0, f[2][2] = 1;
for (int i = 3; i <= n; i++)
for (int j = 1; j <= i; j++) {
if (j == 1)
f[i][j] = i % 2;
else {
f[i][j] = f[i - 1][j - 1] ^ f[i - 2][j];
}
}
a[1][1] = 1;
a[2][1] = 0, a[2][2] = 1;
for (int i = 3; i <= n; i++) {
int k = 1;
for (int j = 1; j <= i; j++) {
if (a[i - 1][j - 1] && a[i - 1][j - 1] + a[i - 2][j] == 0) {
k = 1;
} else if (a[i - 1][j - 1] && a[i - 1][j - 1] == a[i - 2][j]) {
k = -1;
}
}
for (int j = 1; j <= i; j++) {
if (j == 1)
a[i][j] = k * a[i - 2][j];
else {
a[i][j] = a[i - 1][j - 1] + k * a[i - 2][j];
}
}
}
n--;
cout << n << endl;
for (int i = 1; i <= n + 1; i++) cout << a[n + 1][i] << " ";
cout << endl;
cout << n - 1 << endl;
for (int i = 1; i <= n; i++) cout << a[n][i] << " ";
cout << endl;
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | import sys
#f = open('input', 'r')
f = sys.stdin
n = f.readline()
n = int(n)
t = [[0], [1]]
for j in range(n):
cur = [0] + t[-1]
for i, x in enumerate(t[-2]):
cur[i] += x
if min(cur) < -1 or max(cur) > 1:
cur = [0] + t[-1]
for i, x in enumerate(t[-2]):
cur[i] -= x
t.append(cur)
print(len(t[n+1])-1)
print(' '.join([str(x) for x in t[n+1]]))
print(len(t[n])-1)
print(' '.join([str(x) for x in t[n]]))
| PYTHON3 |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | import java.io.*;
import java.util.*;
/*
TASK: gcd
LANG: JAVA
*/
public class gcd {
static int n;
static long[][] poly;
public static void main(String[] args) throws IOException {
FastScanner in = new FastScanner(System.in);
n = in.nextInt();
poly = new long[n+1][n+2];
poly[0][0] = 1;
poly[1][1] = 1;
for(int i = 2;i < n+1; i++){
for(int j = 1; j < n+2; j++){
poly[i][j] = poly[i-1][j-1];
}
for (int j = 0; j < n + 2; j++) {
poly[i][j] += poly[i - 2][j];
}
for(int j = 0;j < n+2; j++){
poly[i][j] %= 3;
if(poly[i][j] == 2)poly[i][j] = -1;
if(poly[i][j] == -2)poly[i][j] = 1;
}
}
System.out.println(n);
for(int i = 0; i <= n; i++){
System.out.print(poly[n][i] + " ");
}
System.out.println();
System.out.println(n-1);
for(int i = 0; i < n; i++){
System.out.print(poly[n-1][i] + " ");
}
System.out.println();
}
private static class FastScanner {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastScanner(InputStream stream) {
this.stream = stream;
}
int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) return -1;
}
return buf[curChar++];
}
boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
boolean isEndline(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String next() {
int c = read();
while (isSpaceChar(c)) c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = read();
while (isEndline(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndline(c));
return res.toString();
}
}
}
| JAVA |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int a[155][155], b[155][155];
int main() {
int n;
scanf("%d", &n);
a[1][1] = 1;
b[1][0] = 1;
for (int i = 2; i <= n; i++) {
for (int j = 0; j <= i - 1; j++) b[i][j] = a[i - 1][j];
for (int j = 0; j <= i - 1; j++) {
if (b[i][j] == 1 && b[i - 1][j + 1] != 1)
a[i][j + 1] = 1;
else if (b[i][j] == 0 && b[i - 1][j + 1] == 1)
a[i][j + 1] = 1;
}
if (b[i - 1][0] == 1) a[i][0] = 1;
}
printf("%d\n", n);
for (int i = 0; i <= n; i++) printf("%d ", a[n][i]);
printf("\n%d\n", n - 1);
for (int i = 0; i <= n - 1; i++) printf("%d ", b[n][i]);
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | import java.io.*;
import java.util.*;
/*
TASK: gcd
LANG: JAVA
*/
public class gcd {
static int n;
static long[][] poly;
public static void main(String[] args) throws IOException {
FastScanner in = new FastScanner(System.in);
n = in.nextInt();
poly = new long[n+1][n+2];
poly[0][0] = 1;
poly[1][1] = 1;
for(int i = 2;i < n+1; i++){
for(int j = 1; j < n+2; j++){
poly[i][j] = poly[i-1][j-1];
}
for (int j = 0; j < n + 2; j++) {
poly[i][j] += poly[i - 2][j];
}
for(int j = 0;j < n+2; j++){
poly[i][j] %= 2;
}
}
System.out.println(n);
for(int i = 0; i <= n; i++){
System.out.print(poly[n][i] + " ");
}
System.out.println();
System.out.println(n-1);
for(int i = 0; i < n; i++){
System.out.print(poly[n-1][i] + " ");
}
System.out.println();
}
private static class FastScanner {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastScanner(InputStream stream) {
this.stream = stream;
}
int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) return -1;
}
return buf[curChar++];
}
boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
boolean isEndline(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String next() {
int c = read();
while (isSpaceChar(c)) c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = read();
while (isEndline(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndline(c));
return res.toString();
}
}
}
| JAVA |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.*;
import java.lang.Math.*;
import java.math.*;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main
{
static int n;
public static void main(String[] args) throws Exception
{
// InputStream inputStream = new FileInputStream (new File("kitchen.in"));
// OutputStream outputStream = new FileOutputStream(new File("kitchen.out"));
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
solver.solve(1, in, out);
out.close();
}
static class Task {
static long INF = Long.MAX_VALUE;
static int N = (int) 3e5;
ArrayList<Integer> a = new ArrayList<Integer>();
ArrayList<Integer> b = new ArrayList<Integer>();
public void solve(int testNumber, InputReader in, PrintWriter out)
{
int n = in.nextInt();
int cnt = 0;
a.add(0);
a.add(1);
b.add(1);
while (b.size() != n)
{
ArrayList<Integer> c = new ArrayList<Integer>();
c.add(0);
for (int i = 0; i < (int)a.size(); i++)
{
c.add(a.get(i));
}
boolean fl = true;
for (int i = 0; i < (int)b.size(); i++)
{
if (-1 <= c.get(i) + b.get(i) && c.get(i) + b.get(i) <= 1);
else
{
fl = false;
break;
}
}
if (!fl)
{
fl = true;
for (int i = 0; i < (int)b.size(); i++)
{
if (-1 <= c.get(i) - b.get(i) && c.get(i) - b.get(i) <= 1);
else
{
fl = false;
break;
}
}
if (!fl) {
out.println(b.size());
out.close();
return;
}
for (int i = 0; i < (int)b.size(); i++) {
c.set(i , c.get(i) - b.get(i));
}
}
else
{
for (int i = 0; i < (int)b.size(); i++)
{
c.set(i , c.get(i) + b.get(i));
}
}
b = a;
a = c;
}
if (a.get(a.size() - 1) < 0)
{
for (int i = 0; i < (int)a.size(); i++)
{
a.set(i , -a.get(i));
}
}
if (b.get(b.size() - 1) < 0)
{
for (int i = 0; i < (int)b.size(); i++)
{
b.set(i , -b.get(i));
}
}
out.println(a.size() - 1);
for (int i = 0; i < (int)a.size(); i++)
{
out.print(a.get(i) + " ");
}
out.println();
out.println(b.size() - 1);
for (int i = 0; i < (int)b.size(); i++)
{
out.print(b.get(i) + " ");
}
out.println();
out.close();
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public float nextFloat() {
return Float.parseFloat(next());
}
}
} | JAVA |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int N = 200 + 5;
int a[N][N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
a[0][1] = 1;
a[1][1] = 0;
a[1][2] = 1;
a[2][1] = -1;
a[2][2] = 0;
a[2][3] = 1;
for (int i = 3; i <= n; i++) {
for (int j = 1; j <= i + 1; j++) a[i][j] = a[i - 1][j - 1];
for (int j = 1; j <= i + 1; j++) a[i][j] += a[i - 2][j];
for (int j = 1; j <= i; j++) {
a[i][j] = -a[i][j];
if (abs(a[i][j]) >= 2) a[i][j] %= 2;
}
}
cout << n << endl;
for (int i = 1; i <= n + 1; i++) cout << a[n][i] << ' ';
cout << endl;
cout << n - 1 << endl;
for (int i = 1; i <= n; i++) cout << a[n - 1][i] << ' ';
cout << endl;
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int n;
int arr[155][155];
int lebih1 = false;
int main() {
memset(arr, 0, sizeof(arr));
arr[0][0] = -1;
arr[1][0] = 0;
arr[1][1] = 1;
for (int i = 2; i <= 150; i++) {
lebih1 = false;
for (int j = 0; j <= i - 1; j++) {
arr[i][j + 1] = arr[i - 1][j];
}
for (int j = 0; j <= i; j++) {
if (abs(arr[i][j] + arr[i - 2][j]) > 1) {
lebih1 = true;
}
}
if (!lebih1) {
for (int j = 0; j <= i; j++) {
arr[i][j] += arr[i - 2][j];
}
} else {
for (int j = 0; j <= i; j++) {
arr[i][j] -= arr[i - 2][j];
}
}
}
cin >> n;
if (n == 1) {
cout << "1\n0 1\n0\n1\n";
return 0;
}
cout << n << '\n';
for (int i = 0; i <= n; i++) {
cout << arr[n][i] << " ";
}
cout << "\n";
cout << n - 1 << '\n';
for (int i = 0; i <= n - 1; i++) {
cout << arr[n - 1][i] << " ";
}
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
long long x[155][155];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, a, b, c, d, m, i, j, k;
cin >> n;
if (n == 1) {
cout << "1\n0 1\n0\n1";
return 0;
}
x[1][0] = 1;
x[2][1] = 1;
x[2][0] = 0;
for (i = 3; i <= n + 2; i++) {
for (j = i - 1; j >= 1; j--) {
x[i][j] = x[i - 1][j - 1];
}
for (j = i - 3; j >= 0; j--) {
x[i][j] += x[i - 2][j];
}
k = 0;
for (j = i - 3; j >= 0; j--) {
if (x[i][j] > 1 || x[i][j] < -1) {
k = 1;
break;
}
}
if (k == 1) {
for (j = i - 3; j >= 0; j--) {
x[i][j] -= 2 * x[i - 2][j];
}
}
}
cout << n << "\n";
for (i = 0; i <= n; i++) {
cout << x[n + 1][i] << " ";
}
cout << "\n";
cout << n - 1 << "\n";
for (i = 0; i <= n - 1; i++) {
cout << x[n][i] << " ";
}
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
long long x, y, z, a, b, c, m, n, k, q[10000], w[1000000], d, ww[1000],
www[10000];
long long w1[200][200];
map<long long, long long> M, M1;
map<pair<long long, long long>, bool> h;
vector<pair<long long, long long> > v1;
long long inf = 1e9;
long long poww(long long x, long long y) {
if (x == 0) return 0;
if (x == 1) return 1;
if (y == 0) return 1;
if (y == 1) return x;
long long d = poww(x, y / 2);
if (y % 2) return x * d * d;
return d * d;
}
long long ans;
long long w2[10003][103];
vector<long long> v[100030];
char s[100][1000];
void build(long long n1) {
if (n1 > n) return;
for (int i = n1; i >= 1; i--) {
w1[n1][i] = w1[n1 - 1][i - 1];
}
for (int i = 0; i <= n1 - 2; i++) {
w1[n1][i] += w1[n1 - 2][i];
w1[n1][i] %= 2;
}
build(n1 + 1);
}
int main() {
cin >> n;
w1[0][0] = 1, w1[1][1] = 1;
if (n >= 2) build(2);
cout << n << endl;
for (int i = 0; i <= n; i++) {
cout << w1[n][i] << ' ';
}
cout << endl << n - 1 << endl;
for (int i = 0; i <= n - 1; i++) {
cout << w1[n - 1][i] << ' ';
}
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
MyScanner sc = new MyScanner();
int n = sc.nextInt();
int[][] arr = new int[n+1][n+1];
arr[0][0] = 1;
arr[1][1] = 1;
for (int i = 2; i <= n; ++i){
int z = i;
for (int k = z; k >= 1; --k){
arr[z][k] = arr[z-1][k-1];
}
for (int k = z; k >= 0; --k){
arr[z][k] = (arr[z][k] + arr[z-2][k]) % 2;
}
}
out = new PrintWriter(new BufferedOutputStream(System.out));
out.println(n);
for (int i = 0; i <= n; ++i){
out.print(arr[n][i] + " ");
}
out.println();
out.println(n-1);
for (int i = 0; i < n; ++i){
out.print(arr[n-1][i] + " ");
}
out.close();
}
public static long GCD(long a, long b){
while (b > 0L){
long tmp = b;
b = a % b;
a = tmp;
}
return a;
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| JAVA |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int A[155], B[155], C[155];
void copy_(int *x, int *y) {
for (int i = 0; i <= 150; ++i) *(x + i) = *(y + i);
}
int main() {
int N, j, i = 1;
scanf("%d", &N);
A[1] = 1;
B[0] = 1;
while (i < N) {
for (j = 1; j <= i + 1; ++j) C[j] = (A[j - 1] + B[j]) % 2;
++i;
C[0] = B[0];
copy_(B, A);
copy_(A, C);
}
printf("%d\n", N);
for (i = 0; i <= N; ++i) printf("%d ", A[i]);
puts("");
printf("%d\n", N - 1);
for (i = 0; i <= N - 1; ++i) printf("%d ", B[i]);
puts("");
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int block_size = 320;
const long long mod = 1e9 + 7;
const long long inf = 1e9 + 7;
const long double eps = 1e-9;
const double PI = atan(1) * 4;
template <typename T>
inline int sign(const T &a) {
if (a < 0) return -1;
if (a > 0) return 1;
return 0;
}
template <typename T>
inline void in(T &x) {
x = 0;
T f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
x *= f;
}
long long twop(int x) { return 1LL << x; }
template <typename A, typename B>
inline void in(A &x, B &y) {
in(x);
in(y);
}
template <typename A, typename B, typename C>
inline void in(A &x, B &y, C &z) {
in(x);
in(y);
in(z);
}
template <typename A, typename B, typename C, typename D>
inline void in(A &x, B &y, C &z, D &d) {
in(x);
in(y);
in(z);
in(d);
}
template <class T>
void upd(T &a, T b) {
a = max(a, b);
}
vector<vector<int>> fib_poly;
int main() {
fib_poly.push_back({1});
fib_poly.push_back({0, 1});
for (long long i = 0; i < 200; i++) {
auto b = fib_poly[((int)fib_poly.size()) - 2];
auto a = fib_poly[((int)fib_poly.size()) - 1];
a.insert(a.begin(), 0);
for (long long i = 0; i < ((int)b.size()); i++) {
a[i] += b[i];
a[i] %= 2;
}
fib_poly.push_back(a);
}
int n;
cin >> n;
cout << ((int)fib_poly[n].size()) - 1 << endl;
for (auto i : fib_poly[n]) cout << i << " ";
cout << endl;
cout << ((int)fib_poly[n - 1].size()) - 1 << endl;
for (auto i : fib_poly[n - 1]) cout << i << " ";
cout << endl;
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 155;
int m1[maxn][maxn];
void init() {
memset(m1, 0, sizeof(m1));
m1[1][1] = 1;
m1[2][1] = 0;
m1[2][2] = 1;
for (int i = 3; i <= 151; i++) {
int res = 1;
for (int j = 1; j <= i; j++) {
if (abs(m1[i - 2][j]) && m1[i - 1][j - 1] == 0) {
m1[i][j] = m1[i - 2][j];
continue;
}
if (m1[i - 2][j] == 0 && m1[i - 1][j - 1] != 0) {
m1[i][j] = res * m1[i - 1][j - 1];
continue;
}
if (m1[i - 2][j] == m1[i - 1][j - 1]) {
res = -1;
}
}
}
}
int main() {
int n;
init();
while (cin >> n) {
cout << n << endl;
int rr = 1;
if (m1[n + 1][n + 1] < 0) rr = -1;
for (int i = 1; i < n + 1; i++) cout << m1[n + 1][i] * rr << " ";
cout << m1[n + 1][n + 1] * rr << endl;
rr = 1;
if (m1[n][n] < 0) rr = -1;
cout << n - 1 << endl;
for (int i = 1; i < n; i++) cout << m1[n][i] * rr << " ";
cout << m1[n][n] * rr << endl;
}
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
vector<int> g[222];
vector<int> get(int n, vector<int> &a, vector<int> &b) {
vector<int> res(n, 0);
for (int i = 1; i < n; i++) res[i] = a[i - 1];
for (int i = 0; i < (int)(b.size()); i++) res[i] += b[i];
for (int i = 0; i < n; i++) res[i] = res[i] % 2;
return res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
g[0] = {1};
g[1] = {0, 1};
for (int i = 2; i < 151; i++) g[i] = get(i + 1, g[i - 1], g[i - 2]);
int n;
cin >> n;
cout << (int)(g[n].size()) - 1 << '\n';
for (int e : g[n]) cout << e << " ";
cout << '\n';
cout << (int)(g[n - 1].size()) - 1 << '\n';
for (int e : g[n - 1]) cout << e << " ";
cout << '\n';
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
bitset<1000> a[1005];
int main() {
int n, i, j;
cin >> n;
a[1] = 1;
for (i = 2; i <= n + 1; i++) a[i] = (a[i - 1] << 1) ^ a[i - 2];
cout << n << endl;
for (i = 0; i <= n; i++) cout << a[n + 1][i] << ' ';
cout << endl;
cout << n - 1 << endl;
for (i = 0; i <= n - 1; i++) cout << a[n][i] << ' ';
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | import java.util.Scanner;
public class ArrayList {
private static int a[][]=new int[151][151];
static {
a[0][0]=1;
a[1][1]=1;
}
public static void main(String[] args) {
int n=new Scanner(System.in) .nextInt();
for (int i=2;i<=n;i++) {
a[i][0] = a[i - 2][0];
for (int j=1;j<=i;j++)
a[i][j]=(a[i-1][j-1]+a[i-2][j])%2;
}
print(n);
print(n-1);
}
public static void print(int n){
System.out.println(n);
for (int i=0;i<=n;i++)
System.out.print(a[n][i]+" ");
System.out.println();
}
} | JAVA |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int a[1000], b[1000], c[1000];
int main() {
int n;
cin >> n;
a[0] = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) c[j] = a[j];
if (i % 2 == 0) {
for (int j = i + 1; j >= 0; j--) {
if (j) {
if (a[j - 1] && !b[j])
a[j] = a[j - 1];
else if (!a[j - 1] && b[j])
a[j] = b[j];
else
a[j] = 0;
} else
a[j] = b[j];
}
} else {
for (int j = i + 1; j >= 0; j--) {
if (j) {
if (a[j - 1] && !b[j])
a[j] = a[j - 1];
else if (!a[j - 1] && b[j])
a[j] = -b[j];
else
a[j] = 0;
} else
a[j] = -b[j];
}
}
for (int j = 0; j <= i; j++) b[j] = c[j];
}
cout << n << endl;
for (int i = 0; i <= n; i++) cout << a[i] << " ";
cout << endl;
cout << n - 1 << endl;
for (int i = 0; i < n; i++) cout << b[i] << " ";
cout << endl;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int main() {
int n;
scanf("%d", &n);
int p[256][256];
memset(p, 0, sizeof p);
p[0][0] = 1, p[1][1] = 1;
for (int i = 2; i <= n; i++) {
p[i][0] = p[i - 2][0];
for (int j = 1; j < 256; j++) {
p[i][j] = (p[i - 1][j - 1] + p[i - 2][j]) % 2;
}
}
printf("%d\n", n);
for (int i = 0; i <= n; i++) printf("%d ", p[n][i]);
printf("\n%d\n", n - 1);
for (int i = 0; i < n; i++) printf("%d ", p[n - 1][i]);
printf("\n");
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | import java.util.*;
import java.io.*;
public class PolyGCD {
static BufferedReader br;
static StringTokenizer tokenizer;
public static void main(String[] args) throws Exception {
br = new BufferedReader(new InputStreamReader(System.in));
int n = nextInt();
Poly a = new Poly();
Poly b = new Poly();
Poly t = new Poly();
a.coeff.add(1);
b.coeff.add(0);
for(int i = 0; i < n; i++) {
t = a.duplicate();
a.next(b);
b = t;
}
System.out.println(a.coeff.size() - 1);
System.out.println(a);
System.out.println(b.coeff.size() - 1);
System.out.println(b);
}
public static String next() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
String line = br.readLine();
if (line == null)
throw new IOException();
tokenizer = new StringTokenizer(line);
}
return tokenizer.nextToken();
}
public static int nextInt() throws IOException {
return Integer.parseInt(next());
}
public static long nextLong() throws IOException {
return Long.parseLong(next());
}
public static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
class Poly {
ArrayList<Integer> coeff;
public Poly() {
coeff = new ArrayList<Integer>();
}
public Poly(ArrayList<Integer> coeff) {
this.coeff = coeff;
}
public void next(Poly r) {
coeff.add(0, 0);
boolean flag = true;
for(int i = 0; i < r.coeff.size(); i++) {
if(coeff.get(i) + r.coeff.get(i) >= 2 ||coeff.get(i) + r.coeff.get(i) <= -2) {
flag = false;
break;
}
}
if(flag) {
for(int i = 0; i < r.coeff.size(); i++)
coeff.set(i, coeff.get(i) + r.coeff.get(i));
return;
}
for(int i = 0; i < r.coeff.size(); i++)
coeff.set(i, coeff.get(i) - r.coeff.get(i));
}
public Poly duplicate() {
ArrayList<Integer> c = new ArrayList<Integer>();
for(int i = 0; i < coeff.size(); i++)
c.add(coeff.get(i));
return new Poly(c);
}
@Override
public String toString() {
String s = "";
for(int i = 0; i < coeff.size(); i++)
s += coeff.get(i) + " ";
s = s.substring(0, Math.max(0, s.length() - 1));
return s;
}
} | JAVA |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
n--;
vector<int> p1 = {0, 1}, p0 = {1};
while (n) {
vector<int> temp = p1;
temp.insert(temp.begin(), 0);
for (int i = 0; i < (int)p0.size(); i++) {
temp[i] ^= p0[i];
}
swap(p1, p0);
swap(p1, temp);
n--;
}
cout << (int)p1.size() - 1 << '\n';
for (auto &x : p1) cout << x << ' ';
cout << '\n';
cout << (int)p0.size() - 1 << '\n';
for (auto &x : p0) cout << x << ' ';
cout << '\n';
cin.ignore(2);
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int f[160][160];
int n;
int main() {
cin >> n;
memset(f, 0, sizeof(f));
f[0][0] = 1;
f[1][1] = 1;
for (int i = 2; i <= n; i++) {
int t[160];
for (int j = 1; j <= n; j++) t[j] = f[i - 1][j - 1];
t[0] = 0;
for (int j = 0; j <= n; j++) t[j] = t[j] + f[i - 2][j];
bool ok = true;
for (int j = 0; j <= n; j++)
if (t[j] > 1 || t[j] < -1) {
ok = false;
break;
}
if (ok) {
for (int j = 0; j <= n; j++) f[i][j] = t[j];
} else {
for (int j = 1; j <= n; j++) t[j] = f[i - 1][j - 1];
t[0] = 0;
for (int j = 0; j <= n; j++) t[j] = t[j] - f[i - 2][j];
bool ok1 = true;
for (int j = 0; j <= n; j++)
if (t[j] > 1 || t[j] < -1) {
ok1 = false;
break;
}
if (ok1) {
for (int j = 0; j <= n; j++) f[i][j] = t[j];
} else {
cout << -1;
return 0;
}
}
}
int deg1, deg2;
for (int i = n; i >= 0; i--)
if (f[n][i] != 0) {
deg1 = i;
break;
}
cout << deg1 << endl;
for (int i = 0; i <= deg1; i++) cout << f[n][i] << " \n"[i == deg1];
for (int i = n; i >= 0; i--)
if (f[n - 1][i] != 0) {
deg2 = i;
break;
}
cout << deg2 << endl;
for (int i = 0; i <= deg2; i++) cout << f[n - 1][i] << " \n"[i == deg2];
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int main() {
int i, j, n;
cin >> n;
vector<long long> v[155];
v[0].push_back(1);
v[1].push_back(0);
v[1].push_back(1);
for (i = 2; i <= n; i++) {
v[i].push_back(0);
for (j = 0; j < v[i - 1].size(); j++) {
v[i].push_back(v[i - 1][j]);
}
for (j = 0; j < v[i - 2].size(); j++) {
v[i][j] = v[i][j] + v[i - 2][j];
if (v[i][j] == 2) v[i][j] = -1;
if (v[i][j] == -2) v[i][j] = 1;
}
}
cout << n << endl;
for (j = 0; j < v[n].size(); j++) cout << v[n][j] << " ";
cout << endl << n - 1 << endl;
for (j = 0; j < v[n - 1].size(); j++) cout << v[n - 1][j] << " ";
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int n;
int res[150 + 1][150 + 1];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
res[0][0] = 1, res[1][0] = 1, res[1][1] = 1;
for (int i = (2); i < (n + 1); i++) {
for (int j = (1); j < (i + 1); j++) res[i][j] = res[i - 1][j - 1];
for (int j = 0; j < (i); j++) res[i][j] += res[i - 2][j], res[i][j] %= 2;
}
cout << n << endl;
for (int i = 0; i < (n + 1); i++) cout << res[n][i] << " ";
cout << endl;
cout << n - 1 << endl;
for (int i = 0; i < (n); i++) cout << res[n - 1][i] << " ";
cout << endl;
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
vector<int> p[151];
void pre() {
p[0] = {1};
p[1] = {0, 1};
for (int i = (2); i <= (150); ++i) {
vector<int> np;
np.emplace_back(0);
for (int j = (0); j <= (((int)(p[i - 1]).size()) - 1); ++j)
np.emplace_back(p[i - 1][j]);
for (int j = (0); j <= (((int)(p[i - 2]).size()) - 1); ++j)
np[j] = (np[j] + p[i - 2][j]) % 2;
p[i] = np;
}
}
void print(int id) {
for (int i = (0); i <= (((int)(p[id]).size()) - 1); ++i)
cout << p[id][i] << " \n"[i == ((int)(p[id]).size()) - 1];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
pre();
int n;
cin >> n;
cout << n << '\n';
print(n);
cout << n - 1 << '\n';
print(n - 1);
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int N = 155;
int a[N], b[N], c[N];
int main() {
int n;
cin >> n;
a[0] = 1, b[0] = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) c[j] = a[j];
a[0] = b[0] % 2;
for (int j = 1; j <= i + 1; j++) {
a[j] = (c[j - 1] + b[j]) % 2;
}
for (int j = 0; j <= i; j++) b[j] = c[j];
}
cout << n << endl;
for (int i = 0; i <= n; i++) cout << a[i] << " ";
cout << endl;
cout << n - 1 << endl;
for (int i = 0; i < n; i++) cout << b[i] << " ";
cout << endl;
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int N = 157;
int f[N][N];
int main() {
int n;
scanf("%d", &n);
f[0][0] = 1;
f[1][0] = 0;
f[1][1] = 1;
for (int i = 2; i <= n; i++) {
for (int j = 0; j <= i - 2; j++) f[i][j] = f[i - 2][j];
for (int j = 0; j <= i - 1; j++) {
f[i][j + 1] = (f[i][j + 1] + f[i - 1][j]) & 1;
}
}
printf("%d\n", n);
for (int i = 0; i <= n; i++) printf("%d%c", f[n][i], i == n ? '\n' : ' ');
n--;
printf("%d\n", n);
for (int i = 0; i <= n; i++) printf("%d%c", f[n][i], i == n ? '\n' : ' ');
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | """
NTC here
"""
import sys
inp= sys.stdin.readline
input = lambda : inp().strip()
flush= sys.stdout.flush
# import threading
# sys.setrecursionlimit(10**6)
# threading.stack_size(2**25)
def iin(): return int(input())
def lin(): return list(map(int, input().split()))
# range = xrange
# input = raw_input
def main():
n = iin()
ans = [0]*(n+1)
ans[0]=1
ans1 = [0]*(n+1)
sol = 0
# print(ans, ans1)
for i in range(n):
su = ans1[:]
for j in range(n):
su[j+1]+=ans[j]
if su[j+1]>1:su[j+1]=0
# print(1, su, ans)
ans, ans1= su, ans
# print(ans, ans1, mx)
if sol:
print(-1)
else:
m1, m2=0, 0
for i in range(n+1):
if abs(ans[i]):
m1 = i+1
if abs(ans1[i]):
m2 = i+1
print(m1-1)
print( *ans[:m1])
print( m2-1)
print( *ans1[:m2])
main()
# threading.Thread(target=main).start()
| PYTHON3 |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 10;
const long long mod = 1e9 + 7;
const double PI = acos(-1);
int v[2][N];
int len[2];
int main() {
int n;
scanf("%d", &n);
v[1][0] = 0, v[1][1] = 1;
v[0][0] = 1;
len[1] = 1, len[0] = 0;
int op = 1;
for (int i = 2; i <= n; ++i) {
op = 1;
len[i & 1] = len[(i - 1) & 1] + 1;
for (int j = 1; j <= len[i & 1]; ++j)
if (v[(i - 1) & 1][j - 1] + v[i & 1][j] >= 2 ||
v[(i - 1) & 1][j - 1] + v[i & 1][j] <= -2)
op = 0;
for (int j = 1; j <= len[i & 1]; ++j) {
if (op)
v[i & 1][j] += v[(i - 1) & 1][j - 1];
else
v[i & 1][j] -= v[(i - 1) & 1][j - 1];
}
}
int now = n & 1;
printf("%d\n", len[now]);
if (v[now][len[now]] == -1) {
for (int i = 0; i <= len[now]; ++i) v[now][i] = -v[now][i];
}
for (int i = 0; i <= len[now]; ++i)
printf("%d%c", v[now][i], " \n"[i == len[now]]);
now ^= 1;
printf("%d\n", len[now]);
if (v[now][len[now]] == -1) {
for (int i = 0; i <= len[now]; ++i) v[now][i] = -v[now][i];
}
for (int i = 0; i <= len[now]; ++i)
printf("%d%c", v[now][i], " \n"[i == len[now]]);
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int a[160][160], b[160][160], t[160];
int main() {
int n;
scanf("%d", &n);
a[0][0] = -1;
a[1][1] = 1;
for (int i = 2; i <= 150; i++) {
for (int j = 1; j <= 150; j++) a[i][j] = a[i - 1][j - 1];
bool flag = 0;
for (int j = 0; j <= 150; j++) {
t[j] = a[i][j] + a[i - 2][j];
if (t[j] > 1 || t[j] < -1) flag = 1;
}
if (flag) {
flag = 0;
for (int j = 0; j <= 150; j++) a[i][j] = -a[i][j];
for (int j = 0; j <= 150; j++) {
t[j] = a[i][j] + a[i - 2][j];
if (t[j] > 1 || t[j] < -1) flag = 1;
}
}
if (flag) {
flag = 0;
for (int j = 0; j <= 150; j++) a[i][j] = 2 * a[i][j];
for (int j = 0; j <= 150; j++) {
t[j] = a[i][j] + a[i - 2][j];
if (t[j] > 1 || t[j] < -1) flag = 1;
}
}
if (flag) {
flag = 0;
for (int j = 0; j <= 150; j++) a[i][j] = -a[i][j];
for (int j = 0; j <= 150; j++) {
t[j] = a[i][j] + a[i - 2][j];
if (t[j] > 1 || t[j] < -1) flag = 1;
}
}
if (flag) {
assert(1 == 0);
break;
}
for (int j = 0; j <= 150; j++) a[i][j] += a[i - 2][j];
}
b[0][0] = 1;
b[1][1] = 1;
for (int i = 2; i <= 150; i++) {
for (int j = 1; j <= 150; j++) b[i][j] = b[i - 1][j - 1];
bool flag = 0;
for (int j = 0; j <= 150; j++) {
t[j] = b[i][j] + b[i - 2][j];
if (t[j] > 1 || t[j] < -1) flag = 1;
}
if (flag) {
flag = 0;
for (int j = 0; j <= 150; j++) b[i][j] = -b[i][j];
for (int j = 0; j <= 150; j++) {
t[j] = b[i][j] + b[i - 2][j];
if (t[j] > 1 || t[j] < -1) flag = 1;
}
}
if (flag) {
flag = 0;
for (int j = 0; j <= 150; j++) b[i][j] = 2 * b[i][j];
for (int j = 0; j <= 150; j++) {
t[j] = b[i][j] + b[i - 2][j];
if (t[j] > 1 || t[j] < -1) flag = 1;
}
}
if (flag) {
flag = 0;
for (int j = 0; j <= 150; j++) b[i][j] = -b[i][j];
for (int j = 0; j <= 150; j++) {
t[j] = b[i][j] + b[i - 2][j];
if (t[j] > 1 || t[j] < -1) flag = 1;
}
}
if (flag) {
assert(1 == 0);
break;
}
for (int j = 0; j <= 150; j++) b[i][j] += b[i - 2][j];
}
if (1) {
printf("%d\n", n);
if (a[n][n] == 1)
for (int i = 0; i <= n; i++) printf("%d ", a[n][i]);
else
for (int i = 0; i <= n; i++) printf("%d ", -a[n][i]);
puts("");
printf("%d\n", n - 1);
if (a[n - 1][n - 1] == 1)
for (int i = 0; i < n; i++) printf("%d ", a[n - 1][i]);
else
for (int i = 0; i < n; i++) printf("%d ", -a[n - 1][i]);
} else
puts("-1");
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
vector<int> divide(vector<int> A, vector<int> B) {}
const vector<int> err(1, 999999999);
vector<int> operator-(vector<int> A, vector<int> B) {
for (int i = 0; i < A.size(); i++) {
if (i >= B.size()) break;
A[i] += (-1 * B[i]);
}
return A;
}
vector<int> operator+(vector<int> A, vector<int> B) {
for (int i = 0; i < A.size(); i++) {
if (i >= B.size()) break;
A[i] += B[i];
}
return A;
}
vector<vector<int> > P(250);
int n;
void outp() {
cout << P[n].size() - 1 << endl;
for (auto a : P[n]) {
cout << a << " ";
}
cout << endl;
cout << P[n - 1].size() - 1 << endl;
for (auto a : P[n - 1]) {
cout << a << " ";
}
cout << endl;
}
bool chk(const vector<int> P) {
for (auto a : P)
if (a > 1 || a < -1) return 0;
return 1;
}
int main() {
cin >> n;
P[0].push_back(1);
P[1].push_back(0);
P[1].push_back(1);
for (int i = 2; i <= 150; i++) {
P[i] = P[i - 1];
reverse(P[i].begin(), P[i].end());
P[i].push_back(0);
reverse(P[i].begin(), P[i].end());
vector<int> ans = P[i] + P[i - 2];
if (!chk(ans)) {
ans = P[i] - P[i - 2];
}
P[i] = ans;
}
outp();
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
std::mt19937 rng(
(int)std::chrono::steady_clock::now().time_since_epoch().count());
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int n;
std::cin >> n;
std::vector<std::bitset<155>> a(n + 1);
a[0][0] = 1;
a[1][1] = 1;
for (int i = 2; i <= n; i++) {
a[i] = a[i - 2] ^ (a[i - 1] << 1);
}
std::cout << n << '\n';
for (int i = 0; i <= n; i++) {
std::cout << (a[n][i] ? 1 : 0) << (i == n ? '\n' : ' ');
}
std::cout << n - 1 << '\n';
for (int i = 0; i < n; i++) {
std::cout << (a[n - 1][i] ? 1 : 0) << (i == n - 1 ? '\n' : ' ');
}
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | import java.io.* ; import java.util.* ; import java.math.* ;
import static java.lang.Math.min ;
import static java.lang.Math.max ;
public class Codeshefcode{
public static void main(String[] args) throws IOException{
/*Solver Machine = new Solver() ;
Machine.Solve() ; Machine.Finish() ;*/
new Thread(null,new Runnable(){
public void run(){
Solver Machine = new Solver() ;
try{
Machine.Solve() ; Machine.Finish() ;
}catch(Exception e){
e.printStackTrace() ; System.out.flush() ;
System.exit(-1) ;
}catch(Error e){
e.printStackTrace() ; System.out.flush() ;
System.exit(-1) ;
}
}
},"Solver",1l<<27).start() ;
}
}
class mylist extends ArrayList<Integer>{}
class myset extends TreeSet<Integer>{}
class mystack extends Stack<Integer>{}
class mymap extends TreeMap<Long,Integer>{}
class pair implements Comparable<pair>{
int x ; int y ;
pair(int x,int y){ this.x=x ; this.y=y ;}
public int compareTo(pair p){
return (this.x<p.x ? -1 : (this.x>p.x ? 1 : (this.y<p.y ? -1 : (this.y>p.y ? 1 : 0)))) ;
}
}
class Solver{
Reader ip = new Reader(System.in) ;
PrintWriter op = new PrintWriter(System.out) ;
int p[][] ;
public void Solve() throws IOException{
fill() ;
int n = ip.i() ;
pln(n) ;
for(int j=0 ; j<=n ; j++) p(((p[n][n]>=0 ? 1 : -1)*p[n][j])+" ") ;
pln("") ;
pln(n-1) ;
for(int j=0 ; j<n ; j++) p(((p[n-1][n-1]>=0 ? 1 : -1)*p[n-1][j])+" ") ;
pln("") ;
}
void fill(){
p = new int[151][] ;
int tmp[] = {1} ;
p[0] = tmp ;
int tmp1[] = {0,1} ;
p[1] = tmp1 ;
for(int i=2 ; i<=150 ; i++){
int res[] = new int[i+1] ;
for(int j=1 ; j<=i ; j++) res[j] = p[i-1][j-1] ;
int res1[] = new int[i+1] ;
int res2[] = new int[i+1] ;
for(int j=0 ; j<=i ; j++){
res1[j] = res[j] ;
res2[j] = res[j] ;
}
for(int j=0 ; j<(i-1) ; j++){
res1[j]+=p[i-2][j] ;
res2[j]-=p[i-2][j] ;
}
p[i] = which(res1,res2) ;
}
}
int[] which(int a1[],int a2[]){
for(int elem : a1) if(elem<-1 || elem>1) return a2 ;
return a1 ;
}
void Finish(){ op.flush(); op.close(); }
void p(Object o){ op.print(o) ; }
void pln(Object o){ op.println(o) ;}
}
class Mod{
static long mod=1000000007 ;
static long d(long a,long b){ return (a*inv(b))%mod ; }
static long m(long a,long b){ return (a*b)%mod ; }
static long inv(long a){ return pow(a,mod-2) ; }
static long pow(long a,long b){
if(b<0) return pow(inv(a),-b) ;
long val=a ; long ans=1 ;
while(b!=0){
if((b&1)==1) ans = (ans*val)%mod ;
val = (val*val)%mod ;
b/=2 ;
}
return ans ;
}
}
class Reader{
BufferedReader reader;
StringTokenizer tkz;
Reader(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input)) ;
tkz = new StringTokenizer("") ;
}
String s() throws IOException{
while (!tkz.hasMoreTokens()) tkz = new StringTokenizer(reader.readLine()) ;
return tkz.nextToken() ;
}
int i() throws IOException{ return Integer.parseInt(s()) ;}
long l() throws IOException{ return Long.parseLong(s()) ;}
double d() throws IOException{ return Double.parseDouble(s()) ;}
} | JAVA |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.*;
public class CFC {
BufferedReader br;
PrintWriter out;
StringTokenizer st;
boolean eof;
final long MOD = 1000L * 1000L * 1000L + 7;
void solve() throws IOException {
int n = nextInt();
int[] a = {1};
int[] b = {0};
for (int i = 0; i < n; i++) {
int[] tmpa = Arrays.copyOf(a, a.length);
int[] tmpb = Arrays.copyOf(b, b.length);
int[] acopy = new int[tmpa.length + 1];
for (int j = 0; j < tmpa.length; j++) {
acopy[j + 1] = tmpa[j];
}
for (int j = 0; j < tmpb.length; j++) {
acopy[j] += tmpb[j];
acopy[j] %= 2;
}
a = acopy;
b = tmpa;
}
outln(a.length - 1);
for (int i = 0; i < a.length; i++) {
out(a[i] + " ");
}
outln("");
outln(b.length -1);
for (int i = 0; i < b.length; i++) {
out(b[i] + " ");
}
}
int gcd(int a, int b) {
while(a != 0 && b != 0) {
int c = b;
b = a % b;
a = c;
}
return a + b;
}
void shuffle(int[] a) {
int n = a.length;
for(int i = 0; i < n; i++) {
int r = i + (int) (Math.random() * (n - i));
int tmp = a[i];
a[i] = a[r];
a[r] = tmp;
}
}
private void outln(Object o) {
out.println(o);
}
private void out(Object o) {
out.print(o);
}
public CFC() throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
}
public static void main(String[] args) throws IOException {
new CFC();
}
public long[] nextLongArr(int n) throws IOException{
long[] res = new long[n];
for(int i = 0; i < n; i++)
res[i] = nextLong();
return res;
}
public int[] nextIntArr(int n) throws IOException {
int[] res = new int[n];
for(int i = 0; i < n; i++)
res[i] = nextInt();
return res;
}
public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
eof = true;
return null;
}
}
return st.nextToken();
}
public String nextString() {
try {
return br.readLine();
} catch (IOException e) {
eof = true;
return null;
}
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
public double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
} | JAVA |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.Collections;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author darkhan imangaliyev
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
MyReader in = new MyReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskD solver = new TaskD();
solver.solve(1, in, out);
out.close();
}
static class TaskD {
public void solve(int testNumber, MyReader in, PrintWriter out) {
int n = in.nextInt();
Polynomial a = new Polynomial(Arrays.asList(1));
Polynomial b = new Polynomial(Arrays.asList(1, 0));
for (int i = 1; i < n; ++i) {
Polynomial c = b.multByX();
c = c.sub(a);
if (check(c)) {
a = b;
b = c;
continue;
}
c = b.multByX();
c = c.add(a);
if (check(c)) {
a = b;
b = c;
continue;
}
out.println(-1);
return;
}
out.println(b.power());
Collections.reverse(b.c);
for (int i : b.c) {
out.print(i + " ");
}
out.println();
out.println(a.power());
Collections.reverse(a.c);
for (int i : a.c) {
out.print(i + " ");
}
out.println();
}
private boolean check(Polynomial p) {
for (int i : p.c) {
if (Math.abs(i) > 1) {
return false;
}
}
return true;
}
class Polynomial {
List<Integer> c;
public Polynomial() {
c = new ArrayList<>();
}
public Polynomial(List<Integer> c) {
this.c = c;
}
public Polynomial multByX() {
List<Integer> coef = new ArrayList<>(c);
coef.add(0);
return new Polynomial(coef);
}
public int power() {
return c.size() - 1;
}
public Polynomial add(Polynomial p) {
return add(this, p, true);
}
public Polynomial sub(Polynomial p) {
return add(this, p, false);
}
private Polynomial add(Polynomial a, Polynomial b, boolean add) {
if (a.power() < b.power()) {
return add(b, a, add);
}
List<Integer> c = new ArrayList<>(a.c);
for (int i = c.size() - 1, j = b.c.size() - 1; j >= 0; --j, --i) {
if (add) {
c.set(i, c.get(i) + b.c.get(j));
} else {
c.set(i, c.get(i) - b.c.get(j));
}
}
return new Polynomial(c);
}
}
}
static class MyReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public MyReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| JAVA |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
long long cnt[170][170] = {0};
void init() {
cnt[1][0] = 1;
for (int i = 2; i <= 160; i++)
for (int j = 0; j <= i; j++) {
cnt[i][j] = (cnt[i - 1][j - 1] + cnt[i - 2][j]) % 2;
}
}
int main() {
int n;
init();
scanf("%d", &n);
printf("%d\n", n);
for (int i = 0; i <= n; i++) printf("%lld ", cnt[n + 1][i]);
printf("\n%d\n", n - 1);
for (int i = 0; i < n; i++) printf("%lld ", cnt[n][i]);
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
class Solver {
public:
int n;
vector<int> a, b, c;
Solver() {}
void input_data() { cin >> n; }
void solve() {
b.push_back(1);
a.push_back(0);
a.push_back(1);
vector<int> c;
for (int i = 1; i < n; ++i) {
c = get_new_a(a, b);
b = a;
a = c;
}
if (a.back() == -1) {
for (int i = 0; i < a.size(); ++i) {
a[i] = -a[i];
}
}
if (b.back() == -1) {
for (int i = 0; i < b.size(); ++i) {
b[i] = -b[i];
}
}
}
void output_answer() {
cout << a.size() - 1 << endl;
for (int i = 0; i < a.size(); ++i) {
cout << a[i] << " ";
}
cout << endl;
cout << b.size() - 1 << endl;
for (int i = 0; i < b.size(); ++i) {
cout << b[i] << " ";
}
cout << endl;
}
private:
vector<int> get_new_a(vector<int> a, vector<int> b) {
vector<int> res;
res.push_back(0);
for (int i = 0; i < a.size(); ++i) {
res.push_back(a[i]);
}
for (int i = 0; i < b.size(); ++i) {
res[i] += b[i];
}
for (int i = 0; i < res.size(); ++i) {
if (abs(res[i]) > 1) {
break;
}
if (i + 1 == res.size()) {
return res;
}
}
res.resize(0);
res.push_back(0);
for (int i = 0; i < a.size(); ++i) {
res.push_back(-a[i]);
}
for (int i = 0; i < b.size(); ++i) {
res[i] += b[i];
if (abs(res[i]) > 1) {
cout << "fail" << endl;
}
}
return res;
}
};
int main() {
Solver solver;
solver.input_data();
solver.solve();
solver.output_answer();
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
long long min(long long a, long long b) {
if (a < b)
return a;
else
return b;
}
static long long gcd(long long a, long long b) {
long long r;
while (b != 0) {
r = a % b;
a = b;
b = r;
}
return a;
}
long long lcm(long long a, long long b) {
long long temp = gcd(a, b);
return temp ? (a / temp * b) : 0;
}
unsigned long long PowMod(unsigned long long x, unsigned long long e,
unsigned long long mod) {
unsigned long long res;
if (e == 0) {
res = 1;
} else if (e == 1) {
res = x;
} else {
res = PowMod(x, e / 2, mod);
res = res * res % mod;
if (e % 2) res = res * x % mod;
}
return res;
}
int a[152][152];
int r[152][152];
bool check(int x, int y) {
a[x + 1][0] = 0;
for (int i = 0; i <= x; i++) {
a[x + 1][i + 1] = a[x][i];
r[x + 1][i] = a[x][i];
}
for (int i = 0; i <= x; i++) {
a[x + 1][i] += (y * r[x][i]);
}
for (int i = 0; i <= x + 1; i++) {
if (a[x + 1][i] > 1 || a[x + 1][i] < (-1)) {
return false;
}
}
return true;
}
int solve() {
int n;
cin >> n;
memset(a, 0, sizeof(a));
memset(r, 0, sizeof(r));
a[1][0] = 0;
a[1][1] = 1;
r[1][0] = 1;
a[2][0] = -1;
a[2][1] = 0;
a[2][2] = 1;
r[2][0] = 0;
r[2][1] = 1;
for (int i = 3; i <= n; i++) {
if (check(i - 1, 1) == true) {
} else {
check(i - 1, -1);
}
}
cout << n << endl;
for (int i = 0; i <= n; i++) {
cout << a[n][i] << " ";
}
cout << endl << n - 1 << endl;
for (int i = 0; i < n; i++) {
cout << r[n][i] << " ";
}
cout << endl;
return 0;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
for (int i = 1; i <= t; i++) {
solve();
}
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
int main() {
int ans[200][200];
for (int i = 0; i < 200; i++) {
for (int j = 0; j < 200; j++) {
ans[i][j] = 0;
}
}
ans[0][0] = 1;
ans[1][0] = 0;
ans[1][1] = 1;
for (int i = 2; i <= 150; i++) {
bool jud = true;
ans[i][0] = ans[i - 2][0];
for (int j = 1; j <= i; j++) {
ans[i][j] = ans[i - 1][j - 1] + ans[i - 2][j];
if (ans[i][j] * ans[i][j] > 1) {
jud = false;
break;
}
}
if (!jud) {
ans[i][0] = -ans[i - 2][0];
for (int j = 0; j <= i; j++) {
ans[i][j] = ans[i - 1][j - 1] - ans[i - 2][j];
}
}
}
int n;
while (scanf("%d", &n) != EOF) {
printf("%d\n", n);
for (int i = 0; i <= n; i++) {
printf("%d ", ans[n][i]);
}
printf("\n%d\n", n - 1);
for (int i = 0; i <= n - 1; i++) {
printf("%d ", ans[n - 1][i]);
}
printf("\n");
}
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | /*
*
* @Author Ajudiya_13(Bhargav Girdharbhai Ajudiya)
* Dhirubhai Ambani Institute of Information And Communication Technology
*
*/
import java.util.*;
import java.io.*;
import java.lang.*;
public class Code271
{
public static void print(int[] a)
{
for(int i=0;i<10;i++)
System.out.print(a[i] + " ");
System.out.println();
}
public static void copy(int[] a,int[] b)
{
for(int i=0;i<a.length;i++)
a[i] = b[i];
}
public static int getPower(int[] a)
{
int power = 0;
for(int i=0;i<a.length;i++)
{
if(a[i]!=0)
power = i;
}
return power;
}
public static void main(String[] args)
{
InputReader in = new InputReader(System.in);
PrintWriter pw = new PrintWriter(System.out);
int n = in.nextInt();
int[] rem = new int[1000];
int[] div = new int[1000];
//int power_of_rem;
int[] nextans = new int[1000];
int[] for_con = new int[1000];
rem[0] = 1;
div[1] = 1;
//power_of_rem = 0;
for(int i=1;i<n;i++)
{
for_con[1] = 1;
for(int j=0;j<1000;j++)
{
for(int k=0;k<1000;k++)
{
if(j+k<1000)
nextans[j+k] += div[j]*for_con[k];
}
}
for(int j=0;j<1000;j++)
nextans[j] += rem[j];
for(int j=0;j<1000;j++)
nextans[j]%=2;
copy(rem, div);
copy(div, nextans);
Arrays.fill(for_con,0);
Arrays.fill(nextans, 0);
//power_of_rem = getPower(rem);
}
int powerdiv = getPower(div);
pw.println(powerdiv);
for(int i=0;i<=powerdiv;i++)
pw.print(div[i] + " ");
pw.println();
int powerrem = getPower(rem);
pw.println(powerrem);
for(int i=0;i<=powerrem;i++)
pw.print(rem[i] + " ");
pw.flush();
pw.close();
}
static class InputReader
{
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int snext()
{
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars)
{
curChar = 0;
try
{
snumChars = stream.read(buf);
}
catch (IOException e)
{
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = snext();
}
int res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = snext();
}
long res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n)
{
int a[] = new int[n];
for (int i = 0; i < n; i++)
{
a[i] = nextInt();
}
return a;
}
public String readString()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine()
{
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c)
{
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}
public static long mod = 1000000007;
public static int d;
public static int p;
public static int q;
public static int[] suffle(int[] a,Random gen)
{
int n = a.length;
for(int i=0;i<n;i++)
{
int ind = gen.nextInt(n-i)+i;
int temp = a[ind];
a[ind] = a[i];
a[i] = temp;
}
return a;
}
public static void swap(int a, int b){
int temp = a;
a = b;
b = temp;
}
public static HashSet<Integer> primeFactorization(int n)
{
HashSet<Integer> a =new HashSet<Integer>();
for(int i=2;i*i<=n;i++)
{
while(n%i==0)
{
a.add(i);
n/=i;
}
}
if(n!=1)
a.add(n);
return a;
}
public static void sieve(boolean[] isPrime,int n)
{
for(int i=1;i<n;i++)
isPrime[i] = true;
isPrime[0] = false;
isPrime[1] = false;
for(int i=2;i*i<n;i++)
{
if(isPrime[i] == true)
{
for(int j=(2*i);j<n;j+=i)
isPrime[j] = false;
}
}
}
public static int GCD(int a,int b)
{
if(b==0)
return a;
else
return GCD(b,a%b);
}
public static long GCD(long a,long b)
{
if(b==0)
return a;
else
return GCD(b,a%b);
}
public static void extendedEuclid(int A,int B)
{
if(B==0)
{
d = A;
p = 1 ;
q = 0;
}
else
{
extendedEuclid(B, A%B);
int temp = p;
p = q;
q = temp - (A/B)*q;
}
}
public static long LCM(long a,long b)
{
return (a*b)/GCD(a,b);
}
public static int LCM(int a,int b)
{
return (a*b)/GCD(a,b);
}
public static int binaryExponentiation(int x,int n)
{
int result=1;
while(n>0)
{
if(n % 2 ==1)
result=result * x;
x=x*x;
n=n/2;
}
return result;
}
public static long binaryExponentiation(long x,long n)
{
long result=1;
while(n>0)
{
if(n % 2 ==1)
result=result * x;
x=x*x;
n=n/2;
}
return result;
}
public static int modularExponentiation(int x,int n,int M)
{
int result=1;
while(n>0)
{
if(n % 2 ==1)
result=(result * x)%M;
x=(x*x)%M;
n=n/2;
}
return result;
}
public static long modularExponentiation(long x,long n,long M)
{
long result=1;
while(n>0)
{
if(n % 2 ==1)
result=(result * x)%M;
x=(x*x)%M;
n=n/2;
}
return result;
}
public static int modInverse(int A,int M)
{
return modularExponentiation(A,M-2,M);
}
public static long modInverse(long A,long M)
{
return modularExponentiation(A,M-2,M);
}
public static boolean isPrime(int n)
{
if (n <= 1) return false;
if (n <= 3) return true;
if (n%2 == 0 || n%3 == 0)
return false;
for (int i=5; i*i<=n; i=i+6)
{
if (n%i == 0 || n%(i+2) == 0)
return false;
}
return true;
}
static class pair implements Comparable<pair>
{
Integer x, y;
pair(int x,int y)
{
this.x=x;
this.y=y;
}
public int compareTo(pair o) {
int result = x.compareTo(o.x);
if(result==0)
result = y.compareTo(o.y);
return result;
}
public String toString()
{
return x+" "+y;
}
public boolean equals(Object o)
{
if (o instanceof pair)
{
pair p = (pair)o;
return p.x == x && p.y == y ;
}
return false;
}
public int hashCode()
{
return new Long(x).hashCode()*31 + new Long(y).hashCode();
}
}
} | JAVA |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | n = int(input())
a = [1]
b = [0]
for i in range(n):
c = a.copy()
a.append(0)
for i in range(len(a) - 1, 0, -1):
a[i] = a[i - 1]
if i < len(b):
a[i] += b[i]
a[i] %= 2
a[0] = b[0]
b = c.copy()
print(len(a) - 1)
for i in a:
print(i, end=' ')
print('')
print(len(b) - 1)
for i in b:
print(i, end=' ') | PYTHON3 |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
bool cpr(int *a, int *b) { return (a[0] < b[0]); }
int main() {
int n;
cin >> n;
int *a = new int[n + 1], *b = new int[n + 1];
for (int i = 0; i <= n; i++) a[i] = b[i] = 0;
int *t;
a[0] = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
b[j + 1] += a[j];
b[j + 1] = ((b[j + 1] + 1) % 3 + 3) % 3 - 1;
}
t = a;
a = b;
b = t;
}
cout << (n) << endl;
for (int i = 0; i <= (n); i++) cout << a[i] << " ";
cout << endl << (n - 1) << endl;
for (int i = 0; i < n; i++) cout << b[i] << " ";
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author khokharnikunj8
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
ScanReader in = new ScanReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskD solver = new TaskD();
solver.solve(1, in, out);
out.close();
}
static class TaskD {
public void solve(int testNumber, ScanReader in, PrintWriter out) {
int n = in.scanInt();
int ans[][] = new int[n + 1][n + 1];
ans[0][0] = 1;
ans[1][1] = 1;
for (int i = 2; i <= n; i++) {
ans[i][0] = ans[i - 2][0];
for (int j = 1; j <= i; j++)
ans[i][j] = (ans[i - 1][j - 1] + ans[i - 2][j]) & 1;
}
out.println(n);
for (int i = 0; i <= n; i++) out.print(ans[n][i] + " ");
out.println();
out.println(n - 1);
for (int i = 0; i < n; i++) out.print(ans[n - 1][i] + " ");
}
}
static class ScanReader {
private byte[] buf = new byte[4 * 1024];
private int index;
private BufferedInputStream in;
private int total;
public ScanReader(InputStream inputStream) {
in = new BufferedInputStream(inputStream);
}
private int scan() {
if (index >= total) {
index = 0;
try {
total = in.read(buf);
} catch (Exception e) {
e.printStackTrace();
}
if (total <= 0) return -1;
}
return buf[index++];
}
public int scanInt() {
int integer = 0;
int n = scan();
while (isWhiteSpace(n)) n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
integer *= 10;
integer += n - '0';
n = scan();
}
}
return neg * integer;
}
private boolean isWhiteSpace(int n) {
if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
else return false;
}
}
}
| JAVA |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int data[155][155] = {};
data[0][0] = 1;
data[1][0] = 0;
data[1][1] = 1;
for (int i = 2; i <= n; i++) {
memcpy(&data[i][1], &data[i - 1][0], i * sizeof(int));
bool flag = true;
for (int j = 0; j < n - 1; j++) {
data[i][j] += data[i - 2][j];
if (data[i][j] == 2 || data[i][j] == -2) flag = false;
}
if (flag) continue;
for (int j = 0; j < n - 1; j++) {
data[i][j] -= 2 * data[i - 2][j];
}
}
cout << n << endl;
for (int i = 0; i <= n; i++) {
cout << data[n][i] << " ";
}
cout << endl << n - 1 << endl;
for (int i = 0; i <= n - 1; i++) {
cout << data[n - 1][i] << " ";
}
cout << endl;
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const long long N = 2e5 + 5;
long long n, m, k, cnt;
vector<long long> a, b;
vector<long long> fn(vector<long long> a, vector<long long> b) {
a.push_back(0);
long long lt = a.size() - 1;
for (long long i = b.size() - 1; i >= 0; i--)
a[lt] = (a[lt] + b[i]) % 2, lt--;
return a;
}
void solve() {
cin >> n;
a.push_back(1);
a.push_back(0);
b.push_back(1);
while (a.size() != n + 1) {
b = fn(a, b);
swap(a, b);
}
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
cout << a.size() - 1 << "\n";
for (auto x : a) cout << x << " ";
cout << "\n";
cout << b.size() - 1 << "\n";
for (auto x : b) cout << x << " ";
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long t = 1;
while (t--) solve();
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const long long mod = 1e9 + 7;
const int N = 1e5 + 10;
vector<int> f;
vector<int> s;
vector<int> temp;
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n;
cin >> n;
f.clear();
s.clear();
s.push_back(1);
f.push_back(1);
f.push_back(0);
for (int i = 2; i <= n; ++i) {
temp = f;
f.push_back(0);
int x = f.size() - 1;
int y = s.size() - 1;
while (x >= 0 and y >= 0) {
f[x] = f[x] + s[y];
f[x] %= 2;
if (f[x] < 0) f[x] += 2;
x--;
y--;
}
s = temp;
}
cout << f.size() - 1 << endl;
for (int i = (int)f.size() - 1; i >= 0; --i) cout << f[i] << " ";
cout << endl;
cout << s.size() - 1 << endl;
for (int i = (int)s.size() - 1; i >= 0; --i) cout << s[i] << " ";
cout << endl;
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int arr[157][157] = {0};
int main() {
arr[0][0] = 1;
arr[1][1] = 1;
arr[1][0] = 0;
for (int i = 2; i < 157; i++) {
arr[i][0] = arr[i - 2][0];
for (int j = 1; j <= i; j++) {
arr[i][j] = (arr[i - 1][j - 1] + arr[i - 2][j]) % 2;
}
}
int n;
scanf("%d", &n);
cout << n << endl;
cout << arr[n][0];
for (int i = 1; i <= n; i++) {
cout << " ";
cout << arr[n][i];
}
cout << endl;
cout << (n - 1) << endl;
cout << arr[n - 1][0];
for (int i = 1; i <= (n - 1); i++) {
cout << " ";
cout << arr[n - 1][i];
}
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int a[155][155], b[155][155];
int main() {
int n;
cin >> n;
a[1][0] = 0, a[1][1] = 1;
b[1][0] = 1;
for (int i = 2; i <= n; i++) {
int m[155] = {};
for (int j = 0; j <= i - 1; j++) {
b[i][j] = a[i - 1][j];
if (a[i - 1][j] == 1) m[j + 1] = 1;
if (b[i - 1][j] == 1) m[j] = 1;
}
for (int j = 0; j <= i - 1; j++) {
if (a[i - 1][j] == 1 && b[i - 1][j + 1] == 1) {
m[j + 1] = 0;
}
}
for (int j = 0; j <= i - 1; j++) {
if (m[j] == 1) a[i][j] = 1;
}
a[i][i] = 1;
}
cout << n << endl;
for (int i = 0; i <= n; i++) cout << a[n][i] << " ";
cout << endl;
cout << n - 1 << endl;
for (int i = 0; i <= n - 1; i++) cout << b[n][i] << " ";
cout << endl;
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int Inf = (int)1e9 + 7;
const long long LongInf = (long long)1e18 + 7;
namespace {
inline bool check(const vector<int>& a, const vector<int>& b) {
for (int i = 0; i < (int)b.size(); i++)
if (a[i] + b[i] > 1 || a[i] + b[i] < -1) return false;
return true;
}
void solve() {
int n;
cin >> n;
vector<int> a, b;
a.insert(a.begin(), 1);
a.insert(a.begin(), 0);
b.insert(b.begin(), 1);
for (int i = 1; i < n; i++) {
vector<int> aa = a;
a.insert(a.begin(), 0);
if (!check(a, b)) {
for (int j = 0; j < (int)b.size(); j++) b[j] *= -1;
}
for (int j = 0; j < (int)b.size(); j++) a[j] = a[j] + b[j];
b = aa;
}
cout << ((int)a.size() - 1) << "\n";
for (int ax : a) cout << ax << " ";
cout << "\n";
cout << ((int)b.size() - 1) << "\n";
for (int bx : b) cout << bx << " ";
cout << "\n";
}
} // namespace
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout.precision(9);
cout << fixed;
solve();
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;
long long powmod(long long a, long long b) {
long long res = 1;
a %= M;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1) res = res * a % M;
a = a * a % M;
}
return res % M;
}
const int N = 1e6;
int dp[151][151];
int main() {
int n;
cin >> n;
dp[0][0] = dp[1][1] = 1;
for (int i = 2; i <= n; ++i) {
dp[i][0] = dp[i - 2][0];
for (int j = 1; j <= i; ++j) {
dp[i][j] = (dp[i - 1][j - 1] + dp[i - 2][j]) % 2;
}
}
cout << n << endl;
for (int i = 0; i <= n; ++i) cout << dp[n][i] << " ";
cout << endl;
cout << n - 1 << endl;
for (int i = 0; i < n; ++i) cout << dp[n - 1][i] << " ";
cout << endl;
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<long long>;
using pii = pair<long long, long long>;
const long long DX[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0},
DY[9] = {-1, 0, 1, 0, -1, 1, 1, -1, 0};
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
for (auto i = (begin(v)); i != (end(v)); ++i)
os << *i << (i == end(v) - 1 ? "" : " ");
return os;
}
template <class T>
istream &operator>>(istream &is, vector<T> &v) {
for (auto i = (begin(v)); i != (end(v)); ++i) is >> *i;
return is;
}
template <class T>
istream &operator>>(istream &is, pair<T, T> &p) {
is >> p.first >> p.second;
return is;
}
template <class T>
bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <class T>
using heap = priority_queue<T, vector<T>, greater<T>>;
struct before_main_function {
before_main_function() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << setprecision(15) << fixed;
}
} before_main_function;
void output(vector<long long> &v) {
long long n = ((long long)v.size());
long long m = n - 1;
for (long long i = (long long)(n - 1); i >= 0; --i) {
if (v[i] != 0) {
m = i;
break;
}
}
cout << m << "\n";
for (long long i = 0, ilen = (long long)(m + 1); i < ilen; ++i) {
cout << v[i];
if (i != m)
cout << " ";
else
cout << "\n";
}
}
signed main() {
long long n;
cin >> n;
vector<long long> A(n + 1, 0), B(n + 1, 0);
A[1] = 1;
B[0] = 1;
for (long long i = 0, ilen = (long long)(n - 1); i < ilen; ++i) {
vector<long long> C(n + 1, 0);
for (long long j = 0, jlen = (long long)(n + 1); j < jlen; ++j) {
long long x = (j > 0 ? A[j - 1] : 0) + B[j];
long long y = (j > 0 ? -A[j - 1] : 0) + B[j];
if (abs(x) >= 2)
C[j] = y;
else
C[j] = x;
}
B = A;
A = C;
}
output(A);
output(B);
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.HashMap;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author OmarYasser
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskD solver = new TaskD();
solver.solve(1, in, out);
out.close();
}
static class TaskD {
static HashMap<Integer, TaskD.Result> map = new HashMap<Integer, TaskD.Result>();
static void solve(int[] A, int[] B, int steps, boolean add) {
if (steps == 151) return;
int[] nxtA1, nxtA2, nxtB;
nxtB = A;
nxtA1 = mult(A);
nxtA2 = mult(A);
nxtA1 = add(nxtA1, B, 1);
nxtA2 = add(nxtA2, B, -1);
for (int i = 0; i < 151; i++)
if (Math.abs(nxtA1[i]) >= 2) {
solve(nxtA2, nxtB, steps + 1, !add);
map.put(steps, new TaskD.Result(nxtA2, nxtB));
return;
}
solve(nxtA1, nxtB, steps + 1, !add);
map.put(steps, new TaskD.Result(nxtA1, nxtB));
}
static int[] add(int[] A, int[] B, int sign) {
int[] res = new int[151];
for (int i = 0; i < 151; i++)
res[i] = A[i] + sign * B[i];
return res;
}
static int[] mult(int[] A) {
int[] res = new int[151];
for (int i = 0; i < 150; i++)
res[i + 1] = A[i];
return res;
}
public void solve(int testNumber, Scanner sc, PrintWriter out) {
int n = sc.nextInt();
if (n == 1) out.println("1\n" +
"0 1\n" +
"0\n" +
"1");
else {
int[] f = new int[151];
int[] s = new int[151];
f[0] = 0;
f[1] = 1;
s[0] = 1;
solve(f, s, 2, false);
int[] A = map.get(n).A, B = map.get(n).B;
int lastA = 0, lastB = 0;
for (int i = 0; i < 151; i++) {
if (A[i] == 1) lastA = i;
if (B[i] == 1) lastB = i;
}
out.println(lastA);
for (int i = 0; i <= lastA; i++)
out.print(A[i] + " ");
out.println();
out.println(lastB);
for (int i = 0; i <= lastB; i++)
out.print(B[i] + " ");
out.println();
}
}
static class Result {
int[] A;
int[] B;
Result(int[] AA, int[] BB) {
A = AA;
B = BB;
}
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader f) {
br = new BufferedReader(f);
}
public String next() {
while (st == null || !st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| JAVA |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, j, i, p;
cin >> n;
long long a[200], b[200], la = 2, lb = 1;
a[0] = 0;
a[1] = 1;
b[0] = 1;
for (i = 2; i <= n; i++) {
long long c[la];
for (j = 0; j < la; j++) c[j] = a[j];
for (j = la; j > 0; j--) {
a[j] = a[j - 1];
}
a[0] = 0;
p = 0;
for (j = 0; j < lb; j++) {
if ((a[j] == 1 && b[j] == 1) || (a[j] == -1 && b[j] == -1)) {
p = 1;
break;
}
}
if (p == 0) {
for (j = 0; j < lb; j++) a[j] += b[j];
} else {
for (j = 0; j < lb; j++) a[j] -= b[j];
}
la++;
lb++;
for (j = 0; j < lb; j++) b[j] = c[j];
}
cout << la - 1 << endl;
for (i = 0; i < la; i++) cout << a[i] << " ";
cout << endl;
cout << lb - 1 << endl;
for (i = 0; i < lb; i++) cout << b[i] << " ";
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
vector<int> g[222];
vector<int> get(int n, vector<int> &a, vector<int> &b) {
vector<int> res(n, 0);
for (int i = 1; i < n; i++) res[i] = a[i - 1];
bool f = 1;
for (int i = 0; i < (int)(b.size()); i++) {
if (res[i] + b[i] > 1 || res[i] + b[i] < -1) {
f = 0;
break;
}
}
if (f)
for (int i = 0; i < (int)(b.size()); i++) res[i] += b[i];
else
for (int i = 0; i < (int)(b.size()); i++) res[i] -= b[i];
return res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
g[0] = {1};
g[1] = {0, 1};
for (int i = 2; i < 151; i++) g[i] = get(i + 1, g[i - 1], g[i - 2]);
int n;
cin >> n;
cout << (int)(g[n].size()) - 1 << '\n';
for (int e : g[n]) cout << abs(e) << " ";
cout << '\n';
cout << (int)(g[n - 1].size()) - 1 << '\n';
for (int e : g[n - 1]) cout << abs(e) << " ";
cout << '\n';
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int main() {
srand(time(nullptr));
int n;
scanf("%d", &n);
vector<vector<int>> f;
f.push_back({1});
f.push_back({0, 1});
for (int i = 2; i <= n; i++) {
f.push_back({});
f.back().push_back(f[i - 2][0]);
for (int j = 1; j <= i - 2; j++)
f.back().push_back(f[i - 2][j] + f[i - 1][j - 1]);
f.back().push_back(f[i - 1][i - 2]);
f.back().push_back(f[i - 1][i - 1]);
for (int &x : f.back()) x = x % 2;
}
printf("%d\n", n);
for (int a : f[n]) printf("%d ", a);
printf("\n%d\n", n - 1);
for (int a : f[n - 1]) printf("%d ", a);
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using i8 = char;
using u8 = unsigned char;
using i16 = short int;
using u16 = unsigned short int;
using i32 = long int;
using u32 = unsigned long int;
using i64 = long long int;
using u64 = unsigned long long int;
using f32 = float;
using f64 = double;
using f80 = long double;
using namespace std;
int main(void) {
i32 n;
cin >> n;
i32 p[160][160];
for (i32 i = 0; i <= n; i++) p[0][i] = p[1][i] = 0;
p[0][0] = p[1][1] = 1;
for (i32 i = 2; i <= n; i++) {
p[i][0] = p[i - 2][0];
for (i32 j = 1; j <= n; j++) {
p[i][j] = (p[i - 1][j - 1] + p[i - 2][j]) % 2;
}
}
cout << n << endl;
for (i32 i = 0; i <= n; i++) cout << p[n][i] << " ";
cout << endl;
cout << n - 1 << endl;
for (i32 i = 0; i < n; i++) cout << p[n - 1][i] << " ";
cout << endl;
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
static Scanner input = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
public static void main(String[] args) {
int n = input.nextInt();
int[] pp = {1}, p = {0, 1}, tmp;
for (int i = 1; i < n; i++) {
tmp = p;
p = nextP(pp, p);
pp = tmp;
}
System.out.println(p.length - 1);
String[] TMP = new String[p.length];
for (int i = 0; i < p.length; i++) {
TMP[i] = String.valueOf(p[i]);
}
System.out.println(String.join(" ", TMP));
System.out.println(pp.length - 1);
TMP = new String[pp.length];
for (int i = 0; i < pp.length; i++) {
TMP[i] = String.valueOf(pp[i]);
}
System.out.println(String.join(" ", TMP));
}
static int[] nextP(int[] first, int[] second) {
int[] res = new int[second.length + 1];
res[0] = first[0];
boolean firstRes = true;
for (int i = 1; i < first.length; i++) {
res[i] = second[i - 1] + first[i];
if (res[i] > 1 || res[i] < -1) {
firstRes = false;
break;
}
}
if (firstRes) {
for (int i = first.length; i < second.length + 1; i++) {
res[i] = second[i - 1];
}
return res;
}
res[0] = -first[0];
for (int i = 1; i < first.length; i++) {
res[i] = second[i - 1] - first[i];
}
for (int i = first.length; i < second.length + 1; i++) {
res[i] = second[i - 1];
}
return res;
}
} | JAVA |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | f = [[1], [0, 1]]
n = int(input())
for i in range(2, n + 1):
l = [0] + f[i - 1]
for j in range(len(f[i - 2])):
l[j] = (l[j] + f[i - 2][j]) & 1
f.append(l)
print(n)
print(*f[n])
print(n - 1)
print(*f[n - 1]) | PYTHON3 |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int readint() {
int t = 0, f = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) {
t = (t << 3) + (t << 1) + c - '0';
c = getchar();
}
return t * f;
}
const int maxn = 159;
int n, opt = 1, a[maxn][2];
int main() {
n = readint();
a[0][0] = a[1][0] = 1;
for (int i = 1; i <= (n); i++) {
a[0][opt] += 2;
for (int i = 1; i <= (a[0][opt ^ 1]); i++) {
if (a[i + 1][opt] == 1)
a[i + 1][opt] -= a[i][opt ^ 1];
else
a[i + 1][opt] += a[i][opt ^ 1];
}
opt ^= 1;
}
if (a[0][1] > a[0][0]) {
for (int i = 1; i <= (a[0][1]); i++) swap(a[i][0], a[i][1]);
swap(a[0][0], a[0][1]);
}
for (int i = 0; i <= (1); i++) {
cout << (a[0][i] - (a[0][i] != 0)) << endl;
for (int j = 1; j <= (a[0][i]); j++) {
cout << a[j][i];
putchar(j == a[0][i] ? '\n' : ' ');
}
}
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int64_t INF = 1e9, MOD = 1e9 + 7, MAXN = 1e5 + 7, MAXA = 1e5 + 5;
const long double EPS = 1e-9, PI = acos(-1);
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int64_t n;
cin >> n;
vector<int64_t> fibb1(n + 1), fibb2(n + 1);
fibb1[1] = 1;
fibb2[0] = 1;
for (int64_t i = 1; i < n; ++i) {
vector<int64_t> temp = fibb1;
for (int64_t j = n; j > -1; --j) fibb1[j] = fibb1[j - 1];
fibb1[0] = 0;
for (int64_t j = 0; j < n; ++j) {
fibb1[j] += fibb2[j];
fibb1[j] %= 2;
}
fibb2 = temp;
}
while (fibb1.back() == 0) fibb1.pop_back();
while (fibb2.back() == 0) fibb2.pop_back();
if (fibb1.size() < fibb2.size()) swap(fibb1, fibb2);
cout << fibb1.size() - 1 << endl;
for (int64_t elem : fibb1) cout << elem << " ";
cout << endl;
cout << fibb2.size() - 1 << endl;
for (int64_t elem : fibb2) cout << elem << " ";
cout << endl;
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
struct node {
int p[200];
void q() {
for (int i = 151; i > 0; i--) p[i] = p[i - 1];
p[0] = 0;
}
void f(int a[]) {
for (int i = 0; i <= 150; i++) {
p[i] += a[i];
}
}
void g(int a[]) {
for (int i = 0; i <= 150; i++) {
p[i] -= 2 * a[i];
}
}
bool vj() {
bool flag = true;
for (int i = 0; i <= 150; i++) {
if (p[i] > 1 || p[i] < -1) {
flag = false;
break;
}
}
return flag;
}
};
int main() {
int n;
scanf("%d", &n);
int m = n;
int ans1[200][200];
int ans2[200][200];
node a, b;
memset(a.p, 0, sizeof(a.p));
memset(b.p, 0, sizeof(b.p));
memset(ans2, 0, sizeof(ans2));
a.p[0] = 1;
memset(ans1, 0, sizeof(ans1));
ans1[0][0] = 1;
int tt = 1;
while (m--) {
int t[200];
for (int i = 0; i < tt; i++) {
t[i] = a.p[i];
}
a.q();
a.f(b.p);
if (!a.vj()) {
a.g(b.p);
}
for (int i = 0; i <= tt; i++) {
ans1[tt][i] = a.p[i];
ans2[tt][i] = t[i];
b.p[i] = t[i];
}
tt++;
}
printf("%d\n", n);
for (int i = 0; i <= n; i++) printf("%d%c", ans1[n][i], i == n ? '\n' : ' ');
printf("%d\n", n - 1);
for (int i = 0; i <= n - 1; i++)
printf("%d%c", ans2[n][i], (i == (n - 1)) ? '\n' : ' ');
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
deque<int> deq[200];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
deq[0].push_back(0);
deq[1].push_back(1);
for (int i = 2; i <= n + 1; i++) {
deq[i] = deq[i - 1];
deq[i].push_front(0);
for (int j = 0; j < deq[i - 2].size(); j++) deq[i][j] += deq[i - 2][j];
}
cout << n << '\n';
for (int i = 0; i < deq[n + 1].size(); i++) cout << deq[n + 1][i] % 2 << " ";
cout << '\n';
cout << n - 1 << '\n';
for (int i = 0; i < deq[n].size(); i++) cout << deq[n][i] % 2 << " ";
cout << '\n';
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
struct Poly {
vector<int> p;
int n;
Poly(){};
Poly(int _n) {
n = _n;
p.resize(n + 1, 0);
}
Poly operator+(Poly other) const {
int nm = max(n, other.n);
Poly sol(nm);
for (int i = 0; i <= n; ++i) sol.p[i] += p[i];
for (int i = 0; i <= other.n; ++i) sol.p[i] += other.p[i];
for (int i = 0; i <= nm; ++i) sol.p[i] %= 2;
return sol;
}
Poly shift() {
Poly sol(n + 1);
for (int i = 0; i <= n; ++i) sol.p[i + 1] = p[i];
return sol;
}
void print() {
printf("%d\n", n);
for (int i = 0; i <= n; ++i) {
if (i) printf(" ");
printf("%d", p[i]);
}
printf("\n");
}
};
const int N = 151;
Poly f[N];
int main() {
int n;
scanf("%d", &n);
f[0] = Poly(0);
f[0].p[0] = 1;
f[1] = Poly(1);
f[1].p[0] = 0;
f[1].p[1] = 1;
for (int i = 2; i <= n; ++i) {
f[i] = f[i - 1];
f[i] = f[i].shift();
f[i] = f[i] + f[i - 2];
}
f[n].print();
f[n - 1].print();
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | from random import getrandbits as R
rb=lambda:R(1)
def modu(p,q):
if len(q)==1:
return [0]
p=p[:]
for d in range(len(p)-1,len(q)-2,-1):
#print(d)
a = p.pop()
b = q[-1]
B = [-k*a/b for k in q[:-1]]
#print(B)
for i in range(len(B)):
p[i+1+len(p)-1-(len(q)-1)] += B[i]
while len(p)>1 and abs(p[-1])<1E-6:
p.pop()
return p
def gcd(p,q):
#print(p,q)
if len(q)==1 and abs(q[0])<1E-6:
return p,1
#if len(q)<=1:
# return q,1
else:
p,iters = gcd(q,modu(p,q))
return p,iters+1
#p,iters = gcd([5,3,2],[-10,1])
#print(p,iters)
#q,iters = gcd([2,5,4,1],[3,4,1])
#print(q,iters)
for n in [int(input())]:#range(150,0,-1):
while True:
p = [1]*(n+1)
q = [1]*(n)
for i in range(n):
p[i]=rb()
for i in range(n-1):
q[i]=rb()
Q,iters = gcd(p,q)
if iters<n+1:
continue
#print(Q,iters)
print(len(p)-1)
print(*p)
print(len(q)-1)
print(*q)
break
| PYTHON3 |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
deque<long long> d[2];
d[0].push_front(1);
d[1].push_front(1);
d[1].push_front(0);
deque<long long> help;
for (long long i = 0; i < n - 1; i++) {
help = d[1];
d[1].push_front(0);
for (long long j = 0; j < (d[0]).size(); j++) {
d[1][j] = (d[0][j] + d[1][j]) % 2;
}
d[0] = help;
}
cout << (d[1]).size() - 1 << endl;
for (long long i = 0; i < (d[1]).size(); i++) cout << d[1][i] << " ";
cout << endl << (d[0]).size() - 1 << endl;
for (long long i = 0; i < (d[0]).size(); i++) cout << d[0][i] << " ";
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskE solver = new TaskE();
solver.solve(1, in, out);
out.close();
}
static class TaskE {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n=in.nextInt();
int f[]=new int[151],s[]=new int[151];
f[0]=1;s[0]=0;
for(int i=1;i<=n;i++){
int t[]=new int[151];
for(int j=1;j<=150;j++)t[j]=f[j-1];
for(int j=0;j<=150;j++){
t[j]+=s[j];if(t[j]>1||t[j]<-1){t[j]=0;if(j>0)f[j-1]=-f[j-1];}
}
for(int j=0;j<151;j++){s[j]=f[j];f[j]=t[j];}
}
out.println(n);
for(int i=0;i<=n;i++)out.print(f[i]+" ");
out.println();out.println(n-1);
for(int i=0;i<=n-1;i++)out.print(s[i]+" ");
}
}
static class InputReader {
BufferedReader br;
StringTokenizer st;
public InputReader(InputStream stream) {
br = new BufferedReader(new InputStreamReader(stream));
st = null;
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
} | JAVA |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int size = 1e6 + 10;
long long a[size], n, m;
long long b[size], c[size];
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
vector<int> a, b;
b.push_back(0);
a.push_back(1);
while (n--) {
while (b.size() != a.size()) b.push_back(0);
b.push_back(0);
a.push_back(0);
vector<int> temp = a;
temp[0] = 0;
for (int i = 1; i < a.size(); i++) temp[i] = a[i - 1];
for (int i = 0; i < temp.size(); i++) temp[i] = (temp[i] + b[i]) % 2;
b = a;
a = temp;
}
cout << a.size() - 1 << endl;
for (int i : a) cout << i << " ";
cout << endl << a.size() - 2 << endl;
for (int i = 0; i < b.size() - 1; i++) cout << b[i] << " ";
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
int A[2][155] = {{1}, {0, 1}};
int main() {
int n;
std::cin >> n;
for (int i = 2; i <= n; ++i)
for (int j = i - 1; ~j; --j)
A[i & 1][j + 1] = (A[i & 1][j + 1] + A[~i & 1][j]) & 1;
printf("%d\n", n);
for (int i = 0; i <= n; ++i) printf(i == n ? "%d\n" : "%d ", A[n & 1][i]);
printf("%d\n", n - 1);
for (int i = 0; i <= n - 1; ++i)
printf(i == n - 1 ? "%d\n" : "%d ", A[~n & 1][i]);
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | #include <bits/stdc++.h>
using namespace std;
int main() {
const int max = 200;
int length;
scanf("%d", &length);
vector<int> divisor(max, 0), remainder(max, 0), dividend(max, 0),
subtrahend(max, 0);
divisor[0] = 1;
int power = 1;
while (length > 0) {
int factor = 1;
subtrahend[0] = 0;
for (int i = 0; i < power; i++) {
subtrahend[i + 1] = divisor[i] * factor;
}
for (int i = 0; i < power + 1; i++) {
dividend[i] = (subtrahend[i] + remainder[i]) % 2;
}
swap(remainder, divisor);
swap(dividend, divisor);
power++;
length--;
}
power--;
if (divisor[power] == -1 || remainder[power - 1] == -1) {
printf("-1");
} else {
printf("%d\n", power);
for (int i = 0; i <= power; i++) printf("%d ", divisor[i]);
printf("\n%d\n", power - 1);
for (int i = 0; i < power; i++) printf("%d ", remainder[i]);
}
return 0;
}
| CPP |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | /**
* @author Finn Lidbetter
*/
import java.util.*;
import java.io.*;
import java.awt.geom.*;
public class TaskD {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int maxDeg = 160;
int[][] polys = new int[maxDeg+1][maxDeg+1];
polys[0][0] = 0;
polys[1][0] = 1;
for (int i=2; i<=maxDeg; i++) {
getNextPoly(i,polys,maxDeg);
}
int n = Integer.parseInt(br.readLine());
sb.append(n+"\n");
for (int i=0; i<n+1; i++) {
sb.append(polys[n+1][i]);
sb.append((i==n) ? "\n":" ");
}
sb.append((n-1)+"\n");
for (int i=0; i<n; i++) {
sb.append(polys[n][i]);
sb.append((i==n-1) ? "\n":" ");
}
System.out.print(sb);
}
static void getNextPoly(int deg, int[][] polys, int maxDeg) {
int[] curr = new int[maxDeg+1];
for (int i=0; i<deg; i++) {
curr[i+1] = polys[deg-1][i];
}
for (int i=0; i<deg+1; i++) {
curr[i] -= polys[deg-2][i];
}
boolean bad = false;
for (int i=0; i<deg+1; i++) {
if (curr[i]>1 || curr[i]<-1) {
bad = true;
break;
}
}
if (bad) {
curr = new int[maxDeg+1];
for (int i=0; i<deg; i++) {
curr[i+1] = -1*polys[deg-1][i];
}
for (int i=0; i<deg+1; i++) {
curr[i] -= polys[deg-2][i];
}
}
boolean flip = false;
if (curr[deg-1]==-1) {
flip = true;
}
for (int i=0; i<deg+1; i++) {
polys[deg][i] = curr[i];
if (flip)
polys[deg][i] *= -1;
}
}
}
| JAVA |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskD solver = new TaskD();
solver.solve(1, in, out);
out.close();
}
static class TaskD {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int[][] ans = new int[n + 1][];
ans[0] = new int[]{1};
ans[1] = new int[]{0, 1};
for (int i = 2; i <= n; ++i) {
int[] prev = ans[i - 1];
int[] cur = new int[prev.length + 1];
for (int k = 0; k < prev.length; ++k) {
cur[k + 1] = prev[k];
}
int[] pprev = ans[i - 2];
for (int k = 0; k < pprev.length; ++k) {
cur[k] += pprev[k];
cur[k] %= 2;
}
ans[i] = cur;
}
int[] a;
int m;
a = ans[n];
m = a.length;
out.println(m - 1);
for (int i = 0; i < m; ++i) out.print(a[i] + " ");
out.println();
a = ans[n - 1];
m = a.length;
out.println(m - 1);
for (int i = 0; i < m; ++i) out.print(a[i] + " ");
out.println();
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| JAVA |
902_D. GCD of Polynomials | Suppose you have two polynomials <image> and <image>. Then polynomial <image> can be uniquely represented in the following way:
<image>
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, <image> denotes the degree of polynomial P(x). <image> is called the remainder of division of polynomial <image> by polynomial <image>, it is also denoted as <image>.
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials <image>. If the polynomial <image> is zero, the result is <image>, otherwise the result is the value the algorithm returns for pair <image>. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair <image> to pair <image>.
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. | 2 | 10 | n = int(input())
c = [0,1]
p = [1]
for i in range(1, n):
nc = [0] + c
rev=False
for j in range(len(p)):
nc[j] -= p[j]
if abs(nc[j]) > 1:
rev = True
if rev:
for j in range(len(p)):
nc[j] += 2*p[j]
if abs(nc[j]) > 1:
rev = True
p = c
c = nc
print(len(c)-1)
print(' '.join([str(i) for i in c]))
print(len(p)-1)
print(' '.join([str(i) for i in p])) | PYTHON3 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.