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;
int main() {
long long n;
cin >> n;
if (n == 1) {
cout << "1\n0 1\n0\n1";
return 0;
}
vector<long long> p1, p2;
p1.push_back(1);
p1.push_back(0);
p2.push_back(1);
for (long long i = 1; i < n; i++) {
vector<long long> temp = p1;
p1.push_back(0);
for (long long j = 0; j < p2.size(); j++) {
p1[j + 2] += p2[j];
p1[j + 2] %= 2LL;
}
p2 = temp;
}
reverse(p1.begin(), p1.end());
reverse(p2.begin(), p2.end());
cout << p1.size() - 1 << "\n";
for (long long i = 0; i < p1.size(); i++) cout << p1[i] << " ";
cout << "\n";
cout << p2.size() - 1 << "\n";
for (long long i = 0; i < p2.size(); i++) cout << p2[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;
vector<int> sum(vector<int> a, vector<int> b) {
bool f = true;
vector<int> v(max(a.size(), b.size()));
for (int i = 0; i < v.size(); i++) {
if (i < a.size()) v[i] += a[i];
if (i < b.size()) v[i] += b[i];
if (abs(v[i]) > 1) f = false;
}
if (f) return v;
for (int i = 0; i < v.size(); i++) {
v[i] = 0;
if (i < a.size()) v[i] += a[i];
if (i < b.size()) v[i] -= b[i];
}
return v;
}
vector<int> gun(vector<int> a) {
vector<int> v;
v.push_back(0);
for (auto u : a) v.push_back(u);
return v;
}
vector<int> A[200], B[200];
void get(int idx) {
B[idx] = A[idx - 1];
A[idx] = sum(gun(B[idx]), B[idx - 1]);
return;
}
int main() {
A[1] = {0, 1};
B[1] = {1};
for (int i = 2; i <= 150; i++) get(i);
int n;
cin >> n;
cout << A[n].size() - 1 << '\n';
for (auto u : A[n]) cout << u << ' ';
cout << endl;
cout << B[n].size() - 1 << '\n';
for (auto u : B[n]) cout << u << ' ';
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;
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto& x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
const int N = 200;
int n, m, a[N], ans = 1, c[N];
vector<int> f[N];
vector<int> mul_by_x(vector<int> a) {
vector<int> ret;
ret.push_back(0);
for (auto it : a) ret.push_back(it);
return ret;
}
vector<int> operator+(const vector<int>& a, const vector<int>& b) {
vector<int> ret;
int i = 0;
for (; i < (int)b.size(); i++) {
ret.push_back(a[i] + b[i]);
}
for (; i < (int)a.size(); i++) {
ret.push_back(a[i]);
}
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
f[0] = {1}, f[1] = {0, 1};
for (int i = 2; i <= n; i++) {
f[i] = mul_by_x(f[i - 1]) + f[i - 2];
}
cout << f[n].size() - 1 << endl;
for (auto i : f[n]) cout << i % 2 << ' ';
cout << endl << f[n - 1].size() - 1 << endl;
for (auto i : f[n - 1]) cout << i % 2 << ' ';
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;
template <typename t>
void mi(t& a, const t& b) {
a = min(a, b);
}
template <typename t>
void ma(t& a, const t& b) {
a = max(a, b);
}
constexpr int si = 1024 * 1024, si_log = 10 + 10,
mod = static_cast<int>(1e9) + 7, partition_size = 400;
template <typename t, typename u>
void ad(t& a, const u& b) {
a += b;
if (mod <= a) a -= mod;
}
template <typename t, typename u>
void mu(t& a, const u& b) {
a = (a * b) % mod;
}
inline int64_t power(int64_t bas, int64_t p) {
int64_t r = 1;
while (p) {
if (p & 1) r = (r * bas) % mod;
bas = (bas * bas) % mod;
p >>= 1;
}
return r;
}
constexpr long double eps = 1.0e-9;
int n, m, k, ar[si], br[si], cr[2][si];
char bar[si], car[si];
vector<int> gr[si], gr2[si];
void fi(const int max_deg) {
for (int j = 0; j <= max_deg + 2; ++j) {
br[j] = 0;
}
for (int j = 0; j <= max_deg; ++j) {
br[j] = cr[1][j];
if (j) {
br[j] = (br[j] + cr[0][j - 1]) % 2;
}
}
}
int64_t sol() {
auto r = 0 * numeric_limits<int64_t>::max();
cr[0][1] = 1;
cr[1][0] = 1;
for (int i = 2; i <= n; ++i) {
fi(i);
for (int j = 0; j <= i; ++j) {
cr[1][j] = cr[0][j];
cr[0][j] = br[j];
}
}
int md0 = 0, md1 = 0;
for (int j = 0; j <= n; ++j) {
if (cr[0][j]) md0 = j;
if (cr[1][j]) md1 = j;
}
cout << md0 << '\n';
for (int j = 0; j <= md0; ++j) {
cout << ' ' << cr[0][j];
}
cout << '\n' << md1 << '\n';
for (int j = 0; j <= md1; ++j) {
cout << ' ' << cr[1][j];
}
cout << '\n';
return r;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
while (1 == scanf("%d", &n)) {
sol();
break;
}
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> f(vector<int> &a) {
vector<int> ans;
ans.resize(a.size() + 1);
for (int i = 1; i < (int)ans.size(); i++) ans[i] = a[i - 1];
return ans;
}
void add(vector<int> &a, vector<int> b) {
for (int i = 0; i < (int)b.size(); i++) {
if (i == (int)a.size()) a.push_back(0);
a[i] += b[i];
}
}
void euclid(vector<int> &a, vector<int> &b, int n) {
if (n == 0) return;
add(b, f(a));
euclid(b, a, n - 1);
}
int main() {
int n;
cin >> n;
vector<int> a;
a.push_back(1);
vector<int> b;
euclid(a, b, n);
if (a.size() < b.size()) swap(a, b);
cout << (int)a.size() - 1 << '\n';
for (int it : a) {
if (it % 2)
cout << 1;
else
cout << 0;
cout << ' ';
}
cout << '\n';
cout << (int)b.size() - 1 << '\n';
for (int it : b) {
if (it % 2)
cout << 1;
else
cout << 0;
cout << ' ';
}
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 << 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;
long long MOD = 1e9 + 7;
bool debug = 1;
const int N = 1e5 + 10;
vector<int> num = {0, 1}, divd = {1}, vez = {0, 1};
vector<int> mult(vector<int> a, vector<int> b) {
if (a.size() < b.size()) swap(a, b);
vector<int> ans(a.size() + b.size() - 1);
for (int ia = 0; ia < a.size(); ia++) {
for (int ib = 0; ib < b.size(); ib++) {
ans[ia + ib] += a[ia] * b[ib];
}
}
return ans;
}
vector<int> sub(vector<int> a, vector<int> b) {
assert(a.size() >= b.size());
vector<int> ans = a;
for (int i = 0; i < b.size(); i++) {
ans[i] -= b[i];
}
return ans;
}
vector<int> add(vector<int> a, vector<int> b) {
assert(a.size() >= b.size());
vector<int> ans = a;
for (int i = 0; i < b.size(); i++) {
ans[i] += b[i];
}
return ans;
}
bool go(int n) {
if (n == 0) return 1;
vector<int> numaux = add(mult(num, vez), divd);
bool ok = 1;
for (int i = 0; i < numaux.size(); i++) {
if (abs(numaux[i]) >= 2) ok = 0;
}
if (!ok) {
numaux = sub(mult(num, vez), divd);
ok = 1;
for (int i = 0; i < numaux.size(); i++) {
if (abs(numaux[i]) >= 2) ok = 0;
}
}
if (ok) {
divd = num;
num = numaux;
return go(n - 1);
} else {
return 0;
if (debug) cout << "erro" << endl;
}
}
int main() {
int n;
cin >> n;
num = {0, 1}, divd = {1}, vez = {0, 1};
if (!go(n - 1))
if (debug)
cout << "n"
<< " = " << (n) << endl;
printf("%d\n", n);
for (int i = 0; i < num.size(); i++) {
printf("%d ", num[i]);
}
printf("\n");
printf("%d\n", n - 1);
for (int i = 0; i < divd.size(); i++) {
printf("%d ", divd[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;
int main() {
const int MAXN = 150 + 7;
int p[MAXN][MAXN];
for (int i = 0; i < MAXN; ++i) {
for (int j = 0; j < MAXN; ++j) p[i][j] = 0;
}
p[0][0] = 1;
p[1][1] = 1;
int n;
cin >> n;
for (int i = 2; i <= n; ++i) {
for (int j = 0; j <= i - 2; ++j) p[i][j] = p[i - 2][j];
for (int j = 1; j <= i; ++j) p[i][j] = (p[i][j] + p[i - 1][j - 1]) % 2;
}
cout << n << '\n';
for (int i = 0; i <= n; ++i) cout << p[n][i] << ' ';
cout << '\n' << --n << '\n';
for (int i = 0; i <= n; ++i) cout << p[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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
/**
* @author Don Li
*/
public class GCDPolynomials {
int N = 155;
int[] a = new int[N], b = new int[N];
void solve() {
int n = in.nextInt();
a[1] = 1;
b[0] = 1;
for (int i = 1; i < n; i++) {
int[] na = Arrays.copyOf(a, a.length);
mul(na);
add(na, b);
b = a;
a = na;
}
printAns(a);
printAns(b);
}
void printAns(int[] a) {
int top = 0;
for (int i = N - 1; i >= 0; i--) {
if (a[i] != 0) {
top = i;
break;
}
}
out.println(top);
for (int i = 0; i <= top; i++) {
if (i > 0) out.print(' ');
out.print(a[i]);
}
out.println();
}
void mul(int[] a) {
for (int i = N - 1; i > 0; i--) {
a[i] = a[i - 1];
}
a[0] = 0;
}
void add(int[] a, int[] b) {
for (int i = 0; i < N; i++) a[i] = (a[i] + b[i]) % 2;
}
public static void main(String[] args) {
in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(System.out);
new GCDPolynomials().solve();
out.close();
}
static FastScanner in;
static PrintWriter out;
static class FastScanner {
BufferedReader in;
StringTokenizer st;
public FastScanner(BufferedReader in) {
this.in = in;
}
public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(in.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public double nextDouble() {
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 | #include <bits/stdc++.h>
using namespace std;
const int MAX = 1e3 + 1;
int a[MAX], b[MAX], c[MAX];
int main() {
ios::sync_with_stdio(0);
int n;
cin >> n;
a[1] = 1;
b[0] = 1;
for (int i = 1; i < n; ++i) {
for (int j = i; j >= 0; --j) {
c[j] = a[j];
a[j + 1] = a[j];
}
a[0] = 0;
int multiplier = 1;
for (int j = i - 1; j >= 0; --j) {
if (a[j] == b[j] && ((a[j] < 0) ? (-a[j]) : a[j]) == 1) multiplier = -1;
}
for (int j = i - 1; j >= 0; --j) a[j] += b[j] * multiplier;
for (int j = i; j >= 0; --j) b[j] = c[j];
}
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 | #include <bits/stdc++.h>
template <typename T>
inline void read(T &x) {
char c;
bool nega = 0;
while ((!isdigit(c = getchar())) && (c != '-'))
;
if (c == '-') {
nega = 1;
c = getchar();
}
x = c - 48;
while (isdigit(c = getchar())) x = x * 10 + c - 48;
if (nega) x = -x;
}
template <typename T>
inline void writep(T x) {
if (x > 9) writep(x / 10);
putchar(x % 10 + 48);
}
template <typename T>
inline void write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
writep(x);
putchar(' ');
}
template <typename T>
inline void writeln(T x) {
write(x);
putchar('\n');
}
using namespace std;
int n;
vector<int> p[1000005];
void setup() {
read(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 - 1];
p[i].insert(p[i].begin(), 0);
for (int j = (0); j <= (int(p[i - 2].size()) - 1); j++)
p[i][j] = (p[i][j] + p[i - 2][j]) % 2;
}
writeln(p[n].size() - 1);
for (int i = (0); i <= (int(p[n].size()) - 1); i++) write(p[n][i]);
putchar('\n');
n--;
writeln(p[n].size() - 1);
for (int i = (0); i <= (int(p[n].size()) - 1); i++) write(p[n][i]);
}
void work() {}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
setup();
work();
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.IOException;
import java.io.InputStream;
import java.util.InputMismatchException;
public class Main {
public static void main(String[] args) {
FastScanner input = new FastScanner(System.in);
int N = input.nextInt();
int[][] poly = new int[N + 1][N + 1];
poly[0][0] = 1;
poly[1][1] = 1;
for (int i = 2; i <= N; i++) {
poly[i][0] = poly[i - 2][0];
for (int j = 1; j <= N; j++)
poly[i][j] = (poly[i - 1][j - 1] + poly[i - 2][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 - 1; i++)
System.out.print(poly[N - 1][i] + " ");
System.out.println();
}
// Matt Fontaine's Fast IO
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;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
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();
}
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;
class customIO {
public:
istream& in;
ostream& out;
customIO(istream& _in, ostream& _out) : in(_in), out(_out) {}
string next() {
string s;
in >> s;
return s;
}
double nextDouble() { return stod(next()); }
int nextInt() { return stoi(next()); }
long long nextLong() { return stoll(next()); }
template <typename T>
void print(T t) {
out << t;
}
template <typename T, typename... U>
void print(T t, U... u) {
out << t;
print(u...);
}
template <typename T, typename... U>
void println(T t, U... u) {
print(t, u..., "\n");
}
void println() { print("\n"); }
};
class TaskD {
public:
void solve(customIO& io) {
int n = io.nextInt();
solve(n, io);
solve(n - 1, io);
return;
}
void solve(int n, customIO& io) {
vector<int> res = recurse(n);
io.println(res.size() - 1);
for (int i : res) {
io.print(i, " ");
}
io.println();
}
map<int, vector<int> > cache;
vector<int> recurse(int n) {
vector<int> res;
if (n == 0) {
res.push_back(1);
return res;
}
if (n == 1) {
res.push_back(0);
res.push_back(1);
return res;
}
if (cache.count(n) != 0) return cache[n];
vector<int> a = recurse(n - 1);
a.insert(a.begin(), 0);
vector<int> b = recurse(n - 2);
b.push_back(0);
b.push_back(0);
assert(a.size() == b.size());
for (int i = 0; i < a.size(); ++i) {
res.push_back((a[i] + b[i]) % 2);
}
return cache[n] = res;
}
};
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout << std::fixed;
std::cout.precision(20);
std::istream& in(std::cin);
std::ostream& out(std::cout);
customIO io(in, out);
TaskD solver;
solver.solve(io);
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);
for (int j = 0; j < (int)b.size(); j++) a[j] = (a[j] + b[j]) % 2;
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 N = 155;
int n;
int a[N][2][N];
void init() {
a[1][0][0] = 0;
a[1][0][1] = 1;
a[1][1][0] = 1;
for (int i = 2; i <= 150; i++) {
for (int j = 0; j <= 150; j++) {
a[i][1][j] = a[i - 1][0][j];
if (j != 0) a[i][0][j] = a[i - 1][0][j - 1];
a[i][0][j] += a[i - 1][1][j];
if (a[i][0][j] == 2 || a[i][0][j] == -2) a[i][0][j] = 0;
}
}
}
int main() {
init();
scanf("%d", &n);
printf("%d\n", n);
for (int i = 0; i <= n; i++) printf("%d ", a[n][0][i]);
printf("\n%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;
long long a[150], b[150], c[150], d[150];
int cnt = 0, n;
int main() {
cin >> n;
a[0] = 0;
a[1] = 1;
b[0] = 1;
cnt = 1;
for (int i = 2; i <= n; i++) {
memset(c, 0, sizeof(c));
memset(d, 0, sizeof(d));
for (int j = 0; j <= cnt; j++) d[j] = a[j];
for (int j = 1; j <= cnt + 1; j++) c[j] = (a[j - 1] + b[j]) % 2;
c[0] = +b[0];
cnt++;
for (int j = 0; j < cnt; j++) b[j] = d[j];
for (int j = 0; j <= cnt; j++) a[j] = c[j];
}
cout << cnt << endl;
for (int i = 0; i <= cnt; i++) cout << a[i] << " ";
cout << endl << cnt - 1 << endl;
for (int i = 0; i < cnt; 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 | def add(first,second):
l = max(map(len,[first,second]))
first = first+[0]*(l-len(first))
second = second+[0]*(l-len(second))
return [i+j for i,j in zip(first,second)]
def find_next(first,second):
# print 'fn ',first,second
first = list(first)
second = list(second)
second = [0]+second
p=1
for i,j in zip(first,second):
if(i==j and i!=0):
p=0
if(p == 0):
first = map(lambda x: -x, first)
p=1
for i,j in zip(first,second):
if(i==j and i!=0):
p=0
if(p==0):
print 'Error'
print 'First: ',first
print 'Second: ',second
return add(first,second)
# l0=[[1],[0,1]]
l=[[-1],[0,1]]
# print l[-2],l[-1]
# print l[-3:]
for i in range(150):
l += [find_next(l[-2],l[-1])]
l[0] = [1]
# for i in l[1:]:
# if i[-1]!=1:
# print i[-1]
n = input()
def p(x):
for i in x:
print i,
print ''
print n
p(l[n])
print n-1
p(l[n-1]) | PYTHON |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
void upd(long long &x, long long y) { x += y; }
struct node {
int nxt;
long long sta, v[10];
node() {
nxt = 0;
memset(v, 0, sizeof(v));
}
};
struct state {
vector<node> nodes;
int head[100007];
state() { memset(head, -1, sizeof(head)); }
size_t size() const { return nodes.size(); }
void sum() {
for (int i = 0; i < nodes.size(); i++)
for (int j = 1; j <= 9; j++) upd(nodes[i].v[j], nodes[i].v[j - 1]);
}
void update(int at, long long key, long long v) {
int tmp = key % 100007;
for (int i = head[tmp]; i != -1; i = nodes[i].nxt)
if (nodes[i].sta == key) {
upd(nodes[i].v[at], v);
return;
}
node now;
now.nxt = head[tmp];
head[tmp] = nodes.size();
now.v[at] = v;
now.sta = key;
nodes.push_back(now);
}
long long find(int at, long long key) {
int tmp = key % 100007;
for (int i = head[tmp]; i != -1; i = nodes[i].nxt)
if (nodes[i].sta == key) return nodes[i].v[at];
return 0;
}
} f[11][20];
long long c[2][20], bit[15];
bitset<200> vis[15], pre[200], tmp;
struct big {
int a[35], len;
void read() {
memset(a, 0, sizeof(a));
len = 0;
char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar())
;
for (; ch >= '0' && ch <= '9'; ch = getchar()) a[len++] = ch - 48;
}
long long mod() {
long long ans = 0;
for (int i = 0; i < len; i++)
ans = (10ll * ans + a[i]) % 1000000000000000007ll;
return ans;
}
void plus() {
a[len - 1]++;
for (int i = len - 1; i >= 1; i--) a[i - 1] += a[i] / 10, a[i] %= 10;
if (a[0] > 9) {
a[0] = 1;
for (int i = 1; i <= len; i++) a[i] = 0;
len++;
}
}
} L, R;
int sum[40], times = 0;
void dfs(int dep, int cnt, int sum, long long msk) {
int mx = sum + (18 - cnt) * 9;
tmp = pre[mx >> 1];
if (sum >> 1) tmp ^= pre[(sum >> 1) - 1];
if ((vis[dep - 1] & tmp) == tmp) return;
if (dep == 10) {
for (int i = sum >> 1; i >= 0; i--)
if (vis[dep - 1][i]) {
int dif = sum - i - i;
if (dif > 1) {
::sum[cnt]++;
f[dif][0].update(0, msk, 1);
}
break;
}
return;
}
vis[dep] = vis[dep - 1];
dfs(dep + 1, cnt, sum, msk);
for (; cnt < 18;) {
vis[dep] |= vis[dep] << dep;
dfs(dep + 1, ++cnt, sum += dep, msk += bit[dep]);
}
}
long long solve(int dif, big lim) {
long long ans = 0, msk = 0;
int len = lim.len;
state *F = f[dif];
for (int i = 0; i < len; i++)
if (len - i > 18) {
for (int j = 0; j < lim.a[i]; j++) ans += F[len - i - 1].find(9, msk);
} else if (lim.a[i]) {
ans += F[len - i].find(lim.a[i] - 1, msk);
msk += bit[lim.a[i]];
}
return ans;
}
long long solve2(big lim) {
int len = lim.len;
long long ans = 0;
int part = 0;
for (int i = 0; i < len; i++) {
int odd = lim.a[i] >> 1, even = lim.a[i] - odd;
ans += c[part][len - 1 - i] * odd + c[part ^ 1][len - 1 - i] * even;
part ^= lim.a[i] & 1;
}
return ans;
}
inline long long read() {
long long x = 0;
char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar())
;
for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 - 48 + ch;
return x;
}
int main() {
pre[0][0] = 1;
for (int i = 1; i <= 190; i++) pre[i] = pre[i - 1], pre[i][i] = 1;
c[0][0] = 1;
for (int i = 1; i <= 18; i++)
c[0][i] = c[1][i] =
5ll * (c[0][i - 1] + c[1][i - 1]) % 1000000000000000007ll;
bit[1] = 1;
for (int i = 2; i < 10; i++) bit[i] = bit[i - 1] << 5;
vis[0].set(0);
dfs(1, 0, 0, 0);
for (int i = 2; i < 10; i++) {
f[i][0].sum();
for (int j = 0; j < 18; j++) {
state &cur = f[i][j], &nxt = f[i][j + 1];
for (int id = 0, sz = cur.size(); id < sz; id++) {
int cnt = j;
long long way = cur.nodes[id].v[9];
long long msk = cur.nodes[id].sta, tmp = msk;
for (int k = 1; k < 10; k++, tmp >>= 5) {
int rem = tmp & 31;
if (!rem) continue;
cnt += rem;
nxt.update(k, msk - bit[k], way);
}
if (cnt < 18) nxt.update(0, msk, way);
}
nxt.sum();
}
}
int Q = read();
while (Q--) {
L.read();
R.read();
R.plus();
int k = read();
long long ans = R.mod() - L.mod();
if (!k) {
ans -= solve2(R) - solve2(L);
for (int i = 2; i < 10; i += 2) ans -= solve(i, R) - solve(i, L);
} else
for (int i = k + 1; i < 10; i++) ans -= solve(i, R) - solve(i, L);
printf("%lld\n", (ans % 1000000000000000007ll + 1000000000000000007ll) %
1000000000000000007ll);
}
return 0;
}
| CPP |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int B = 172;
const int S = 44000;
const int D = 20;
unordered_map<bitset<B>, int> H;
bool A[D][S];
bitset<B> a[S];
int go[S][10];
int n;
int d[20];
long long dp[20][10][S];
long long solve(long long n, int k) {
if (n == 0) return 1;
int m = 0;
while (n) d[m++] = n % 10, n /= 10;
int u = 1;
long long ret = 0;
for (int i = m - 1; i >= 0; i--) {
for (int j = 0; j < d[i] + (i == 0); j++) ret += dp[i][k][go[u][j]];
u = go[u][d[i]];
}
return ret;
}
int main() {
bitset<B> cur;
cur.set(0);
H[cur] = 1;
a[1] = cur;
A[0][1] = 1;
n = 1;
for (int i = 1; i < D; i++) {
for (int u = 1; u <= n; u++)
if (A[i - 1][u]) {
for (int j = 0; j < 10; j++) {
if (!go[u][j]) {
if (j == 0) {
go[u][j] = u;
} else {
cur.reset();
for (int k = j; k < B; k++)
if (a[u][k]) cur[k - j] = 1;
for (int k = 0; k < B - j; k++)
if (a[u][k]) cur[k + j] = 1;
for (int k = 1; k < j; k++)
if (a[u][k]) cur[j - k] = 1;
if (H.count(cur)) {
go[u][j] = H[cur];
} else {
go[u][j] = H[cur] = ++n;
a[n] = cur;
}
}
}
A[i][go[u][j]] = 1;
}
}
}
for (int j = 1; j <= n; j++) {
int k = 0;
for (k = 0; k < 10; k++)
if (a[j][k]) break;
for (; k < 10; k++) dp[0][k][j] = 1;
}
for (int j = 1; j < D - 1; j++) {
for (int k = 1; k <= n; k++)
if (A[D - 1 - j][k]) {
for (int i = 0; i < 10; i++) {
for (int l = 0; l < 10; l++) dp[j][l][k] += dp[j - 1][l][go[k][i]];
}
}
}
int ncase;
for (scanf("%d", &ncase); ncase--;) {
long long l, r;
int k;
scanf("%lld%lld%d", &l, &r, &k);
printf("%lld\n", solve(r, k) - solve(l - 1, k));
}
return 0;
}
| CPP |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
struct node {
int nxt;
long long sta, v[10];
node() {
nxt = 0;
memset(v, 0, sizeof(v));
}
};
struct state {
vector<node> nodes;
int head[100007];
state() { memset(head, -1, sizeof(head)); }
size_t size() const { return nodes.size(); }
void sum() {
for (int i = 0; i < nodes.size(); i++)
for (int j = 1; j <= 9; j++) nodes[i].v[j] += nodes[i].v[j - 1];
}
void update(int at, long long key, long long v) {
int tmp = key % 100007;
for (int i = head[tmp]; i != -1; i = nodes[i].nxt)
if (nodes[i].sta == key) {
nodes[i].v[at] += v;
return;
}
node now;
now.nxt = head[tmp];
head[tmp] = nodes.size();
now.v[at] += v;
now.sta = key;
nodes.push_back(now);
}
long long find(int at, long long key) {
int tmp = key % 100007;
for (int i = head[tmp]; i != -1; i = nodes[i].nxt)
if (nodes[i].sta == key) return nodes[i].v[at];
return 0;
}
} f[11][20];
long long c[2][20], bit[15];
bitset<90> vis[15], pre[90], tmp;
void dfs(int dep, int cnt, int sum, long long msk) {
int mx = sum + (18 - cnt) * 9;
tmp = pre[mx >> 1];
if (sum >> 1) tmp ^= pre[(sum >> 1) - 1];
if ((vis[dep - 1] & tmp) == tmp) return;
if (dep == 10) {
for (int i = sum >> 1; i >= 0; i--)
if (vis[dep - 1][i]) {
int dif = sum - i - i;
if (dif > 1) f[dif][0].update(0, msk, 1);
break;
}
return;
}
vis[dep] = vis[dep - 1];
dfs(dep + 1, cnt, sum, msk);
for (; cnt < 18;) {
vis[dep] |= vis[dep] << dep;
dfs(dep + 1, ++cnt, sum += dep, msk += bit[dep]);
}
}
long long solve(int dif, long long lim) {
char dig[22];
int len = 0;
for (; lim; dig[len++] = lim % 10, lim /= 10)
;
reverse(dig, dig + len);
long long ans = 0, msk = 0;
state *F = f[dif];
for (int i = 0; i < len; i++)
if (len - i > 18) {
for (int j = 0; j < dig[i]; j++) ans += F[len - i - 1].find(9, msk);
} else if (dig[i]) {
ans += F[len - i].find(dig[i] - 1, msk);
msk += bit[dig[i]];
}
return ans;
}
long long solve2(long long lim) {
char dig[22];
int len = 0;
for (; lim; dig[len++] = lim % 10, lim /= 10)
;
reverse(dig, dig + len);
long long ans = 0;
int part = 0;
for (int i = 0; i < len; i++) {
int odd = dig[i] >> 1, even = dig[i] - odd;
ans += c[part][len - 1 - i] * odd + c[part ^ 1][len - 1 - i] * even;
part ^= dig[i] & 1;
}
return ans;
}
inline long long read() {
long long x = 0;
char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar())
;
for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 - 48 + ch;
return x;
}
int main() {
pre[0][0] = 1;
for (int i = 1; i <= 85; i++) pre[i] = pre[i - 1], pre[i][i] = 1;
c[0][0] = 1;
for (int i = 1; i <= 18; i++)
c[0][i] = c[1][i] = 5 * (c[0][i - 1] + c[1][i - 1]);
bit[1] = 1;
for (int i = 2; i < 10; i++) bit[i] = bit[i - 1] << 5;
vis[0].set(0);
dfs(1, 0, 0, 0);
for (int i = 2; i < 10; i++) {
f[i][0].sum();
for (int j = 0; j < 18; j++) {
state &cur = f[i][j], &nxt = f[i][j + 1];
for (int id = 0, sz = cur.size(); id < sz; id++) {
int cnt = j;
long long way = cur.nodes[id].v[9];
long long msk = cur.nodes[id].sta, tmp = msk;
for (int k = 1; k < 10; k++, tmp >>= 5) {
int rem = tmp & 31;
if (!rem) continue;
cnt += rem;
nxt.update(k, msk - bit[k], way);
}
if (cnt < 18) nxt.update(0, msk, way);
}
nxt.sum();
}
}
int Q = read();
long long values = 3287462318;
while (Q--) {
long long L = read(), R = read();
int k = read();
long long ans = R - L + 1;
if (!k) {
ans -= solve2(R + 1) - solve2(L);
for (int i = 2; i < 10; i += 2) ans -= solve(i, R + 1) - solve(i, L);
} else
for (int i = k + 1; i < 10; i++) ans -= solve(i, R + 1) - solve(i, L);
printf("%lld\n", ans);
}
}
| CPP |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int B = 172;
const int maxn = 44000;
const int maxm = 20;
unordered_map<bitset<B>, int> mp;
bool vis[maxm][maxn];
bitset<B> a[maxn];
int nxt[maxn][10];
int cnt = 1;
int bit[20];
long long dp[20][10][maxn];
void DP() {
for (int i = 1; i <= cnt; i++) {
int j = 0;
for (; j < 10; j++)
if (a[i][j]) break;
for (; j < 10; j++) dp[0][j][i] = 1;
}
for (int i = 1; i < maxm - 1; i++)
for (int j = 1; j <= cnt; j++)
if (vis[maxm - 1 - i][j])
for (int k = 0; k < 10; k++)
for (int l = 0; l < 10; l++) dp[i][l][j] += dp[i - 1][l][nxt[j][k]];
}
long long solve(long long x, int k) {
if (x == 0) return 1;
int len = 0;
while (x) {
bit[len++] = x % 10;
x /= 10;
}
int u = 1;
long long ret = 0;
for (int i = len - 1; i >= 0; i--) {
for (int j = 0; j < bit[i] + (i == 0); j++) ret += dp[i][k][nxt[u][j]];
u = nxt[u][bit[i]];
}
return ret;
}
int main() {
bitset<B> Bit;
Bit.set(0);
mp[Bit] = 1;
a[1] = Bit;
vis[0][1] = true;
for (int i = 1; i < maxm; i++)
for (int u = 1; u <= cnt; u++)
if (vis[i - 1][u])
for (int j = 0; j < 10; j++) {
if (!nxt[u][j])
if (!j)
nxt[u][j] = u;
else {
Bit.reset();
for (int k = j; k < B; k++)
if (a[u][k]) Bit[k - j] = 1;
for (int k = 0; k < B - j; k++)
if (a[u][k]) Bit[k + j] = 1;
for (int k = 1; k < j; k++)
if (a[u][k]) Bit[j - k] = 1;
if (mp.count(Bit))
nxt[u][j] = mp[Bit];
else {
nxt[u][j] = mp[Bit] = ++cnt;
a[cnt] = Bit;
}
}
vis[i][nxt[u][j]] = true;
}
DP();
int kase;
scanf("%d", &kase);
while (kase--) {
long long l, r;
int k;
scanf("%I64d%I64d%d", &l, &r, &k);
printf("%I64d\n", solve(r, k) - solve(l - 1, k));
}
return 0;
}
| CPP |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int oo = 0x3f3f3f3f;
const long long ooo = 9223372036854775807ll;
const int _cnt = 1000 * 1000 + 7;
const int _p = 1000 * 1000 * 1000 + 7;
const int N = 100005;
const double PI = acos(-1.0);
const double eps = 1e-9;
int o(int x) { return x % _p; }
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
void file_put() {
freopen("filename.in", "r", stdin);
freopen("filename.out", "w", stdout);
}
bitset<165> B[N];
unordered_map<bitset<165>, int> M;
int T, tot = 0, no[N], k, go[N][10], d[20], top;
long long l, r, s[10][20][N], ans;
int I(const bitset<165> &x) {
if (M.count(x) == 0) B[M[x] = ++tot] = x;
return M[x];
}
void init() {
I(bitset<165>(1));
for (int i = (1); i <= (tot); ++i)
for (int j = (0); j <= (9); ++j) {
bitset<165> t = B[i] << j | B[i] >> j;
for (int k = (0); k <= (j - 1); ++k)
if (B[i][k]) t[j - k] = 1;
go[i][j] = I(t);
}
for (int i = (1); i <= (tot); ++i)
for (int k = (0); k <= (162); ++k) {
if (B[i][k]) {
no[i] = k;
break;
}
}
for (int i = (1); i <= (tot); ++i)
for (int k = (0); k <= (9); ++k) s[k][0][i] = (k >= no[i]);
for (int k = (0); k <= (9); ++k)
for (int i = (1); i <= (19); ++i)
for (int j = (1); j <= (tot); ++j)
for (int c = (0); c <= (9); ++c) s[k][i][j] += s[k][i - 1][go[j][c]];
}
long long Query(long long x, int k) {
for (top = 0; x; x /= 10) d[++top] = x % 10;
ans = 0;
for (int i = top, now = 1; i; now = go[now][d[i--]])
for (int j = (0); j <= (d[i] - 1); ++j) ans += s[k][i - 1][go[now][j]];
return ans;
}
int main() {
scanf("%d", &T), init();
while (T--) {
scanf("%I64d%I64d%d", &l, &r, &k);
printf("%I64d\n", Query(r + 1, k) - Query(l, k));
}
return 0;
}
| CPP |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int MAXX = 85;
const bitset<MAXX> unity(1);
bitset<MAXX> allOne;
hash<bitset<MAXX> > hash_fn;
bitset<MAXX> add(int sum, const bitset<MAXX> &p, int v) {
sum += v;
return (p | (p << v)) & allOne;
}
int getBest(int sum, const bitset<MAXX> &q) {
int best = 0;
for (int i = sum / 2; i >= 0; i--) {
if (q.test(i)) {
best = i;
break;
}
}
return best;
}
unordered_map<bitset<MAXX>, long long> fap[10][20][180];
long long solve(int target, int ps, int sum, const bitset<MAXX> &p) {
if (ps == -1) {
int best = getBest(sum, p);
assert(best + best <= sum);
return sum > 0 && sum - best - best <= target;
}
auto it = fap[target][ps][sum].find(p);
if (it != fap[target][ps][sum].end()) {
return it->second;
}
long long ans = solve(target, ps - 1, sum, p);
for (int d = 1; d < 10; d++) {
bitset<MAXX> q = add(sum, p, d);
ans += solve(target, ps - 1, sum + d, q);
}
return fap[target][ps][sum][p] = ans;
}
long long countLessThan(int target, long long x) {
string s = to_string(x);
int n = s.size();
long long ans = 0;
bitset<MAXX> run(unity);
int sum = 0;
for (char c : s) {
n--;
for (int d = 0; d < c - '0'; d++) {
if (d == 0) {
ans += solve(target, n - 1, sum, run);
} else {
int _best;
bitset<MAXX> p = add(sum, run, d);
ans += solve(target, n - 1, sum + d, p);
}
}
if (c != '0') {
run = add(sum, run, c - '0');
sum += c - '0';
}
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
for (int i = 0; i < MAXX; i++) allOne.set(i);
int t;
cin >> t;
while (t--) {
long long l, r, k;
cin >> l >> r >> k;
cout << countLessThan(k, r + 1) - countLessThan(k, l) << "\n";
}
return 0;
}
| CPP |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int B = 172;
const int maxn = 44000;
const int maxm = 20;
unordered_map<bitset<B>, int> mp;
bool vis[maxm][maxn];
bitset<B> a[maxn];
int nxt[maxn][10];
int cnt = 1;
int bit[20];
long long dp[20][10][maxn];
long long solve(long long x, int k) {
if (x == 0) return 1;
int len = 0;
while (x) {
bit[len++] = x % 10;
x /= 10;
}
int u = 1;
long long ret = 0;
for (int i = len - 1; i >= 0; i--) {
for (int j = 0; j < bit[i] + (i == 0); j++) ret += dp[i][k][nxt[u][j]];
u = nxt[u][bit[i]];
}
return ret;
}
int main() {
bitset<B> Bit;
Bit.set(0);
mp[Bit] = 1;
a[1] = Bit;
vis[0][1] = 1;
for (int i = 1; i < maxm; i++)
for (int u = 1; u <= cnt; u++)
if (vis[i - 1][u]) {
for (int j = 0; j < 10; j++) {
if (!nxt[u][j]) {
if (j == 0)
nxt[u][j] = u;
else {
Bit.reset();
for (int k = j; k < B; k++)
if (a[u][k]) Bit[k - j] = 1;
for (int k = 0; k < B - j; k++)
if (a[u][k]) Bit[k + j] = 1;
for (int k = 1; k < j; k++)
if (a[u][k]) Bit[j - k] = 1;
if (mp.count(Bit))
nxt[u][j] = mp[Bit];
else {
nxt[u][j] = mp[Bit] = ++cnt;
a[cnt] = Bit;
}
}
}
vis[i][nxt[u][j]] = 1;
}
}
for (int i = 1; i <= cnt; i++) {
int j = 0;
for (; j < 10; j++)
if (a[i][j]) break;
for (; j < 10; j++) dp[0][j][i] = 1;
}
for (int j = 1; j < maxm - 1; j++)
for (int k = 1; k <= cnt; k++)
if (vis[maxm - 1 - j][k])
for (int i = 0; i < 10; i++)
for (int l = 0; l < 10; l++) dp[j][l][k] += dp[j - 1][l][nxt[k][i]];
int kase;
scanf("%d", &kase);
while (kase--) {
long long l, r;
int k;
scanf("%I64d%I64d%d", &l, &r, &k);
printf("%I64d\n", solve(r, k) - solve(l - 1, k));
}
return 0;
}
| CPP |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int B = 172;
const int maxn = 44000;
const int maxm = 20;
unordered_map<bitset<B>, int> mp;
bool vis[maxm][maxn];
bitset<B> a[maxn];
int nxt[maxn][10];
int cnt = 1;
int bit[20];
long long dp[20][10][maxn];
long long solve(long long x, int k) {
if (x == 0) return 1;
int m = 0;
while (x) bit[m++] = x % 10, x /= 10;
int u = 1;
long long ret = 0;
for (int i = m - 1; i >= 0; i--) {
for (int j = 0; j < bit[i] + (i == 0); j++) ret += dp[i][k][nxt[u][j]];
u = nxt[u][bit[i]];
}
return ret;
}
int main() {
bitset<B> Bit;
Bit.set(0);
mp[Bit] = 1;
a[1] = Bit;
vis[0][1] = 1;
for (int i = 1; i < maxm; i++)
for (int u = 1; u <= cnt; u++)
if (vis[i - 1][u]) {
for (int j = 0; j < 10; j++) {
if (!nxt[u][j]) {
if (j == 0)
nxt[u][j] = u;
else {
Bit.reset();
for (int k = j; k < B; k++)
if (a[u][k]) Bit[k - j] = 1;
for (int k = 0; k < B - j; k++)
if (a[u][k]) Bit[k + j] = 1;
for (int k = 1; k < j; k++)
if (a[u][k]) Bit[j - k] = 1;
if (mp.count(Bit))
nxt[u][j] = mp[Bit];
else {
nxt[u][j] = mp[Bit] = ++cnt;
a[cnt] = Bit;
}
}
}
vis[i][nxt[u][j]] = 1;
}
}
for (int j = 1; j <= cnt; j++) {
int k = 0;
for (k = 0; k < 10; k++)
if (a[j][k]) break;
for (; k < 10; k++) dp[0][k][j] = 1;
}
for (int j = 1; j < maxm - 1; j++)
for (int k = 1; k <= cnt; k++)
if (vis[maxm - 1 - j][k])
for (int i = 0; i < 10; i++)
for (int l = 0; l < 10; l++) dp[j][l][k] += dp[j - 1][l][nxt[k][i]];
int kase;
scanf("%d", &kase);
while (kase--) {
long long l, r;
int k;
scanf("%I64d%I64d%d", &l, &r, &k);
printf("%I64d\n", solve(r, k) - solve(l - 1, k));
}
return 0;
}
| CPP |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
int sum;
vector<long long> vec[11];
const int moo = 100017;
struct Hashset {
long long key[40005];
long long val[40005];
int nxt[40005];
int head[moo], nd;
long long &operator[](const long long &x) {
for (int i = head[x % moo]; i; i = nxt[i])
if (key[i] == x) return val[i];
key[++nd] = x;
nxt[nd] = head[x % moo];
head[x % moo] = nd;
return val[nd];
}
long long findv(const long long &x) const {
for (int i = head[x % moo]; i; i = nxt[i])
if (key[i] == x) return val[i];
return 0;
}
} vf[20][11];
void dfs(int x, int y, int s, long long st, bitset<100> S) {
if (!S[s / 2]) {
int p = s / 2;
for (; !S[p]; --p)
;
vec[s - p * 2].push_back(st);
}
if (x > 18) return;
int pl = s / 2, pr = s / 2;
for (; pl >= 0 && S[pl]; --pl)
;
for (; pr < 100 && S[pr]; ++pr)
;
if (pr - pl - 1 >= 9) return;
for (int i = y; i <= 9; i++)
dfs(x + 1, i, s + i, st + (1ll << (5 * i)), S | (S << i));
}
void init() {
bitset<100> S;
S[0] = 1;
dfs(1, 1, 0, 0, S);
for (int i = (int)(2); i <= (int)(9); i++) {
sort(vec[i].begin(), vec[i].end());
for (auto j : vec[i]) ++vf[0][i][j];
}
for (int i = (int)(1); i <= (int)(18); i++)
for (int j = (int)(2); j <= (int)(9); j++)
for (int p = (int)(1); p <= (int)(vf[i - 1][j].nd); p++) {
pair<long long, long long> k(vf[i - 1][j].key[p], vf[i - 1][j].val[p]);
vf[i][j][k.first] += k.second;
for (int l = (int)(1); l <= (int)(9); l++)
if ((k.first >> (5 * l)) & 31)
vf[i][j][k.first - (1ll << (5 * l))] += k.second;
}
}
long long FFF(int i, long long j, int k) { return vf[i][k].findv(j); }
long long FF(long long l, int k) {
if (l % 2 == 1) return (l + 1) / 2;
int tmp = 0;
for (long long x = l; x; tmp += x % 10, x /= 10)
;
return (l + 1) / 2 + (tmp % 2 == k);
}
long long F(long long l, int k) {
static int a[20], top;
l++;
for (top = 0; l; a[++top] = l % 10, l /= 10)
;
long long st = 0, ans = 0;
for (int i = (int)(top); i >= (int)(1); i--) {
for (int j = (int)(0); j <= (int)(a[i] - 1); j++)
ans += FFF(i - 1, st + (j ? 1ll << (5 * j) : 0), k);
if (a[i]) st += 1ll << (5 * a[i]);
}
return ans;
}
int main() {
init();
int Q;
scanf("%d", &Q);
while (Q--) {
long long l, r, k, ans = 0;
scanf("%lld%lld%lld", &l, &r, &k);
if (k == 0) {
ans = FF(r, 0) - FF(l - 1, 0);
for (k += 2; k <= 9; k += 2) ans -= F(r, k) - F(l - 1, k);
} else {
ans = r - l + 1;
for (k++; k <= 9; k++) ans -= F(r, k) - F(l - 1, k);
}
printf("%lld\n", ans);
}
}
| CPP |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const long long MX = 6e4, C = 10, L = 20;
long long nxt[MX][C], sz;
bitset<170> b[MX];
unordered_map<bitset<170>, long long> id;
inline long long low_bit(bitset<170> s) {
for (long long i = 0; i < 10; i++)
if (s.test(i)) return i;
return -1;
}
inline long long extend(bitset<170> s) {
if (id.count(s)) return id[s];
id[s] = sz;
b[sz] = s;
return sz++;
}
void find_states() {
bitset<170> s;
s.reset();
s.set(0);
extend(s);
for (long long i = 0; i < sz; i++) {
for (long long j = 0; j < C; j++) {
bitset<170> s = (b[i] << j | b[i] >> j);
for (long long k = 0; k <= j; k++)
if (b[i].test(k)) s.set(j - k);
nxt[i][j] = extend(s);
}
}
}
long long dp[L][MX][C];
void calc_dps() {
for (long long state = 0; state < sz; state++)
for (long long k = 0; k < C; k++)
dp[0][state][k] = (low_bit(b[state]) <= k);
for (long long len = 1; len < L; len++) {
for (long long state = 0; state < sz; state++) {
for (long long k = 0; k < C; k++) {
for (long long c = 0; c < C; c++) {
dp[len][state][k] += dp[len - 1][nxt[state][c]][k];
}
}
}
}
}
long long q;
long long digit[L];
inline long long get(long long lim, long long k) {
long long cp = lim;
for (long long i = 0; i < L; i++, cp /= 10) {
digit[i] = cp % 10;
}
long long res = 0;
long long state = 0;
for (long long i = L - 1; i >= 0; i--) {
for (long long j = 0; j < digit[i]; j++) {
res += dp[i][nxt[state][j]][k];
}
state = nxt[state][digit[i]];
}
return res;
}
int32_t main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
find_states();
calc_dps();
cin >> q;
while (q--) {
long long l, r, k;
cin >> l >> r >> k;
r++;
cout << get(r, k) - get(l, k) << '\n';
}
}
| CPP |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
bool debug = 0;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string direc = "RDLU";
long long ln, lk, lm;
void etp(bool f = 0) {
puts(f ? "YES" : "NO");
exit(0);
}
void addmod(int &x, int y, int mod = 1000000007) {
assert(y >= 0);
x += y;
if (x >= mod) x -= mod;
assert(x >= 0 && x < mod);
}
void et() {
puts("-1");
exit(0);
}
long long fastPow(long long x, long long y, int mod = 1000000007) {
long long ans = 1;
while (y > 0) {
if (y & 1) ans = (x * ans) % mod;
x = x * x % mod;
y >>= 1;
}
return ans;
}
long long gcd1(long long x, long long y) {
long long z = y;
while (x % y != 0) {
z = x % y;
x = y;
y = z;
}
return z;
}
const int B = 165;
int nxt[41005][10], cnt;
bool vis[20][41005];
unordered_map<bitset<B>, int> id;
bitset<B> rid[41005];
long long dp[20][10][41005];
void init() {
bitset<B> bs;
bs.reset();
bs[0] = 1;
id[bs] = ++cnt;
rid[cnt] = bs;
vis[0][1] = 1;
for (int(i) = 1; (i) <= (int)(19); (i)++) {
for (int u = 1; u <= cnt; u++)
if (vis[i - 1][u]) {
for (int(j) = 0; (j) < (int)(10); (j)++) {
if (nxt[u][j] == 0) {
if (j == 0)
nxt[u][j] = u;
else {
bs.reset();
bs |= rid[u] >> j;
bs |= rid[u] << j;
for (int k = 1; k < j; k++)
if (rid[u][k]) bs[j - k] = 1;
if (id.count(bs))
nxt[u][j] = id[bs];
else {
id[bs] = ++cnt;
rid[cnt] = bs;
nxt[u][j] = cnt;
}
}
}
vis[i][nxt[u][j]] = 1;
}
}
}
for (int(i) = 1; (i) <= (int)(cnt); (i)++) {
int j = 0;
for (; j < 10; j++)
if (rid[i][j] == 1) break;
for (; j < 10; j++) dp[0][j][i] = 1;
}
for (int(i) = 1; (i) <= (int)(19); (i)++) {
for (int(k) = 1; (k) <= (int)(cnt); (k)++)
if (vis[19 - i][k]) {
for (int(j) = 0; (j) < (int)(10); (j)++)
for (int(l) = 0; (l) < (int)(10); (l)++) {
dp[i][l][k] += dp[i - 1][l][nxt[k][j]];
}
}
}
}
long long cal(long long x, int k) {
if (x == 0) return 1;
vector<int> bs;
while (x) {
bs.push_back(x % 10);
x /= 10;
}
long long ans = 0;
int u = 1;
for (int i = (int)bs.size() - 1; ~i; i--) {
for (int j = 0; j < bs[i] + (i == 0); j++) ans += dp[i][k][nxt[u][j]];
u = nxt[u][bs[i]];
}
return ans;
}
void fmain(int ID) {
init();
scanf("%d", &n);
for (int(i) = 1; (i) <= (int)(n); (i)++) {
long long l, r;
scanf("%lld%lld%d", &l, &r, &k);
printf("%lld\n", cal(r, k) - cal(l - 1, k));
}
}
int main() {
int t = 1;
for (int(i) = 1; (i) <= (int)(t); (i)++) {
fmain(i);
}
return 0;
}
| CPP |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int maxd = 10, maxl = 18, maxs = 81, BLEN = 5, BMSK = 31, maxv = 32175,
maxv2 = 191791;
int htot, cnt[maxv], inc[maxv][maxd + 1], sub[maxv][maxd + 1], ptot,
pos[maxl + 1][maxv];
map<long long, int> Hash;
long long bit[maxd + 1], c[2][maxl + 1];
long long seq[maxv], f[maxv2][maxd + 1][maxd - 1];
bitset<maxs + 1> vis[maxd + 1];
inline int getID(long long msk) {
map<long long, int>::iterator it = Hash.find(msk);
if (it != Hash.end()) return it->second;
seq[htot] = msk;
return Hash[msk] = htot++;
}
void dfs(int dep, int cnt, int sum, long long msk) {
if (dep == maxd) {
for (int i = sum >> 1; i >= 0; --i)
if (vis[dep - 1].test(i)) {
int dif = sum - i - i;
if (dif > 1) {
int idx = getID(msk);
pos[0][idx] = idx;
f[idx][maxd - 1][dif - 1] = 1;
}
break;
}
return;
}
vis[dep] = vis[dep - 1];
dfs(dep + 1, cnt, sum, msk);
while (cnt < maxl) {
vis[dep] |= vis[dep] << dep;
dfs(dep + 1, ++cnt, sum += dep, msk += bit[dep]);
}
}
long long solve(long long lim, int dif) {
if (dif >= maxd - 1) return lim;
char dig[maxl + 3];
int len = sprintf(dig, "%lld", lim), hidx = getID(0), part = 0;
long long ret = lim;
for (int i = 0; i < len; ++i) {
dig[i] -= '0';
if (!dif) {
int odd = dig[i] >> 1, even = (dig[i] + 1) >> 1;
ret -= c[part][len - 1 - i] * odd + c[part ^ 1][len - 1 - i] * even;
part ^= dig[i] & 1;
}
if (hidx >= 0) {
if (len - i > maxl) {
for (int j = 0; j < dig[i]; ++j) {
int hidx2 = inc[hidx][j];
if (hidx2 >= 0) {
int pidx = pos[len - i - 1][hidx2];
if (pidx >= 0) ret -= f[pidx][maxd - 1][dif];
}
}
} else if (dig[i]) {
int pidx = pos[len - i][hidx];
if (pidx >= 0) ret -= f[pidx][dig[i] - 1][dif];
}
hidx = inc[hidx][dig[i]];
}
}
return ret;
}
int main() {
bit[1] = 1;
for (int i = 2; i < maxd; ++i) bit[i] = bit[i - 1] << BLEN;
c[0][0] = 1;
for (int i = 0; i < maxl; ++i) {
int odd = maxd >> 1, even = (maxd + 1) >> 1;
c[0][i + 1] = c[0][i] * even + c[1][i] * odd;
c[1][i + 1] = c[0][i] * odd + c[1][i] * even;
}
memset(inc, -1, sizeof inc);
memset(sub, -1, sizeof sub);
memset(pos, -1, sizeof pos);
vis[0].set(0);
dfs(1, 0, 0, 0);
ptot = htot;
for (int i = 0; i < htot; ++i) {
long long msk = seq[i], tmp = msk;
inc[i][0] = sub[i][0] = i;
for (int j = 1; j < maxd; ++j, tmp >>= BLEN) {
int rem = tmp & BMSK;
if (!rem) continue;
cnt[i] += rem;
int idx = getID(msk - bit[j]);
inc[idx][j] = i;
sub[i][j] = idx;
}
}
for (int i = 0; i <= maxl; ++i) {
for (int hidx = 0; hidx < htot; ++hidx) {
int pidx = pos[i][hidx];
if (pidx < 0) continue;
for (int k = 1; k < maxd; ++k)
for (int d = 1; d < maxd - 1; ++d) f[pidx][k][d] += f[pidx][k - 1][d];
if (i + cnt[hidx] < maxl) {
int pidx2 = pos[i + 1][hidx];
if (pidx2 < 0) pos[i + 1][hidx] = pidx2 = ptot++;
for (int d = 0; d < maxd - 1; ++d)
f[pidx2][0][d] += f[pidx][maxd - 1][d];
}
for (int k = 1; k < maxd; ++k) {
int hidx2 = sub[hidx][k];
if (hidx2 < 0) continue;
int pidx2 = pos[i + 1][hidx2];
if (pidx2 < 0) pos[i + 1][hidx2] = pidx2 = ptot++;
for (int d = 1; d < maxd - 1; ++d)
f[pidx2][k][d] += f[pidx][maxd - 1][d];
}
for (int k = 0; k < maxd; ++k) {
for (int d = 1; d < maxd - 1; d += 2) f[pidx][k][0] += f[pidx][k][d];
for (int d = maxd - 2; d > 1; --d) f[pidx][k][d - 1] += f[pidx][k][d];
}
}
}
int t;
scanf("%d", &t);
for (int Case = 1; Case <= t; ++Case) {
long long L, R;
int k;
scanf("%lld%lld%d", &L, &R, &k);
printf("%lld\n", solve(R + 1, k) - solve(L, k));
}
return 0;
}
| CPP |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int B = 172, N = 44005;
using ll = long long;
using Bit = bitset<B>;
unordered_map<Bit, int> mp;
Bit aa, a[N];
bool vis[20][N];
int cnt = 1, nxt[N][10];
ll dp[20][10][N];
int b[20];
ll solve(ll n, int kk) {
if (!n) return 1;
int m = 0, pos = 1;
while (n) b[m++] = n % 10, n /= 10;
ll ret = 0;
for (int i = m - 1; i >= 0; --i) {
for (int j = 0; j < b[i] + !i; j++) ret += dp[i][kk][nxt[pos][j]];
pos = nxt[pos][b[i]];
}
return ret;
}
int main() {
ios::sync_with_stdio(false);
aa[0] = 1;
a[mp[aa] = 1] = aa;
vis[0][1] = 1;
for (int i = 1; i < 20; i++)
for (int p = 1; p <= cnt; p++)
if (vis[i - 1][p])
for (int j = 0; j < 10; j++) {
if (!nxt[p][j]) {
if (!j)
nxt[p][j] = p;
else {
aa.reset();
for (int k = j; k < B; k++)
if (a[p][k]) aa[k - j] = 1;
for (int k = 0; k < B - j; k++)
if (a[p][k]) aa[k + j] = 1;
for (int k = 1; k < j; k++)
if (a[p][k]) aa[j - k] = 1;
if (mp.count(aa))
nxt[p][j] = mp[aa];
else
a[nxt[p][j] = mp[aa] = ++cnt] = aa;
}
}
vis[i][nxt[p][j]] = 1;
}
for (int j = 1; j <= cnt; j++) {
int k = 0;
while (k < 10)
if (!a[j][k])
++k;
else
break;
while (k < 10) dp[0][k++][j] = 1;
}
for (int j = 1; j < 19; j++)
for (int k = 1; k <= cnt; k++)
if (vis[19 - j][k])
for (int i = 0; i < 10; i++)
for (int l = 0; l < 10; l++) dp[j][l][k] += dp[j - 1][l][nxt[k][i]];
int T;
cin >> T;
while (T--) {
ll l, r;
cin >> l >> r;
int kk;
cin >> kk;
cout << solve(r, kk) - solve(l - 1, kk) << endl;
}
return 0;
}
| CPP |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int B = 172, maxn = 44000, maxm = 20;
unordered_map<bitset<B>, int> mp;
bool vis[maxm][maxn];
bitset<B> a[maxn];
int nxt[maxn][10], cnt = 1, bit[20];
long long dp[20][10][maxn];
long long solve(long long x, int k) {
if (x == 0) return 1;
int len = 0;
while (x) {
bit[len++] = x % 10;
x /= 10;
}
int u = 1;
long long ret = 0;
for (int i = len - 1; i >= 0; i--) {
for (int j = 0; j < bit[i] + (i == 0); j++) ret += dp[i][k][nxt[u][j]];
u = nxt[u][bit[i]];
}
return ret;
}
int main() {
bitset<B> Bit;
Bit.set(0);
mp[Bit] = 1;
a[1] = Bit;
vis[0][1] = 1;
for (int i = 1; i < maxm; i++)
for (int u = 1; u <= cnt; u++)
if (vis[i - 1][u]) {
for (int j = 0; j < 10; j++) {
if (!nxt[u][j]) {
if (j == 0)
nxt[u][j] = u;
else {
Bit.reset();
for (int k = j; k < B; k++)
if (a[u][k]) Bit[k - j] = 1;
for (int k = 0; k < B - j; k++)
if (a[u][k]) Bit[k + j] = 1;
for (int k = 1; k < j; k++)
if (a[u][k]) Bit[j - k] = 1;
if (mp.count(Bit))
nxt[u][j] = mp[Bit];
else {
nxt[u][j] = mp[Bit] = ++cnt;
a[cnt] = Bit;
}
}
}
vis[i][nxt[u][j]] = 1;
}
}
for (int i = 1; i <= cnt; i++) {
int j = 0;
for (; j < 10; j++)
if (a[i][j]) break;
for (; j < 10; j++) dp[0][j][i] = 1;
}
for (int j = 1; j < maxm - 1; j++)
for (int k = 1; k <= cnt; k++)
if (vis[maxm - 1 - j][k])
for (int i = 0; i < 10; i++)
for (int l = 0; l < 10; l++) dp[j][l][k] += dp[j - 1][l][nxt[k][i]];
int kase;
scanf("%d", &kase);
while (kase--) {
long long l, r;
int k;
scanf("%I64d%I64d%d", &l, &r, &k);
printf("%I64d\n", solve(r, k) - solve(l - 1, k));
}
return 0;
}
| CPP |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int B = 172;
const int S = 44000;
const int D = 20;
unordered_map<bitset<B>, int> H;
bool A[D][S];
bitset<B> a[S];
int go[S][10];
int n;
int d[20];
long long dp[20][10][S];
long long solve(long long n, int k) {
if (n == 0) return 1;
int m = 0;
while (n) d[m++] = n % 10, n /= 10;
int u = 1;
long long ret = 0;
for (int i = m - 1; i >= 0; i--) {
for (int j = 0; j < d[i] + (i == 0); j++) ret += dp[i][k][go[u][j]];
u = go[u][d[i]];
}
return ret;
}
int main() {
bitset<B> cur;
cur.set(0);
H[cur] = 1;
a[1] = cur;
A[0][1] = 1;
n = 1;
for (int i = 1; i < D; i++) {
for (int u = 1; u <= n; u++)
if (A[i - 1][u]) {
for (int j = 0; j < 10; j++) {
if (!go[u][j]) {
if (j == 0) {
go[u][j] = u;
} else {
cur.reset();
for (int k = j; k < B; k++)
if (a[u][k]) cur[k - j] = 1;
for (int k = 0; k < B - j; k++)
if (a[u][k]) cur[k + j] = 1;
for (int k = 1; k < j; k++)
if (a[u][k]) cur[j - k] = 1;
if (H.count(cur)) {
go[u][j] = H[cur];
} else {
go[u][j] = H[cur] = ++n;
a[n] = cur;
}
}
}
A[i][go[u][j]] = 1;
}
}
}
for (int j = 1; j <= n; j++) {
int k = 0;
for (k = 0; k < 10; k++)
if (a[j][k]) break;
for (; k < 10; k++) dp[0][k][j] = 1;
}
for (int j = 1; j < D - 1; j++) {
for (int k = 1; k <= n; k++)
if (A[D - 1 - j][k]) {
for (int i = 0; i < 10; i++) {
for (int l = 0; l < 10; l++) dp[j][l][k] += dp[j - 1][l][go[k][i]];
}
}
}
int ncase;
for (scanf("%d", &ncase); ncase--;) {
long long l, r;
int k;
scanf("%lld%lld%d", &l, &r, &k);
printf("%lld\n", solve(r, k) - solve(l - 1, k));
}
return 0;
}
| CPP |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
struct node {
int nxt;
long long sta, v[10];
node() {
nxt = 0;
memset(v, 0, sizeof(v));
}
};
struct state {
vector<node> nodes;
int head[100007];
state() { memset(head, -1, sizeof(head)); }
size_t size() const { return nodes.size(); }
void sum() {
for (int i = 0; i < nodes.size(); i++)
for (int j = 1; j <= 9; j++) nodes[i].v[j] += nodes[i].v[j - 1];
}
void update(int at, long long key, long long v) {
int tmp = key % 100007;
for (int i = head[tmp]; i != -1; i = nodes[i].nxt)
if (nodes[i].sta == key) {
nodes[i].v[at] += v;
return;
}
node now;
now.nxt = head[tmp];
head[tmp] = nodes.size();
now.v[at] += v;
now.sta = key;
nodes.push_back(now);
}
long long find(int at, long long key) {
int tmp = key % 100007;
for (int i = head[tmp]; i != -1; i = nodes[i].nxt)
if (nodes[i].sta == key) return nodes[i].v[at];
return 0;
}
} f[11][20];
long long c[2][20], bit[15];
bitset<90> vis[15];
void dfs(int dep, int cnt, int sum, long long msk) {
if (dep == 10) {
for (int i = sum >> 1; i >= 0; i--)
if (vis[dep - 1][i]) {
int dif = sum - i - i;
if (dif > 1) f[dif][0].update(0, msk, 1);
break;
}
return;
}
vis[dep] = vis[dep - 1];
dfs(dep + 1, cnt, sum, msk);
for (; cnt < 18;) {
vis[dep] |= vis[dep] << dep;
dfs(dep + 1, ++cnt, sum += dep, msk += bit[dep]);
}
}
long long solve(int dif, long long lim) {
char dig[22];
int len = sprintf(dig, "%lld", lim);
long long ans = 0, msk = 0;
state *F = f[dif];
for (int i = 0; i < len; i++) {
dig[i] -= 48;
if (len - i > 18) {
for (int j = 0; j < dig[i]; j++) ans += F[len - i - 1].find(9, msk);
} else if (dig[i]) {
ans += F[len - i].find(dig[i] - 1, msk);
msk += bit[dig[i]];
}
}
return ans;
}
long long solve2(long long lim) {
char dig[22];
int len = sprintf(dig, "%lld", lim);
long long ans = 0;
int part = 0;
for (int i = 0; i < len; i++) {
dig[i] -= 48;
int odd = dig[i] >> 1, even = dig[i] - odd;
ans += c[part][len - 1 - i] * odd + c[part ^ 1][len - 1 - i] * even;
part ^= dig[i] & 1;
}
return ans;
}
int main() {
c[0][0] = 1;
for (int i = 1; i <= 18; i++)
c[0][i] = c[1][i] = 5 * (c[0][i - 1] + c[1][i - 1]);
bit[1] = 1;
for (int i = 2; i < 10; i++) bit[i] = bit[i - 1] << 5;
vis[0].set(0);
dfs(1, 0, 0, 0);
for (int i = 2; i < 10; i++) {
f[i][0].sum();
for (int j = 0; j < 18; j++) {
state &cur = f[i][j], &nxt = f[i][j + 1];
for (int id = 0, sz = cur.size(); id < sz; id++) {
int cnt = j;
long long way = cur.nodes[id].v[9];
long long msk = cur.nodes[id].sta, tmp = msk;
for (int k = 1; k < 10; k++, tmp >>= 5) {
int rem = tmp & 31;
if (!rem) continue;
cnt += rem;
nxt.update(k, msk - bit[k], way);
}
if (cnt < 18) nxt.update(0, msk, way);
}
nxt.sum();
}
}
int Q;
scanf("%d", &Q);
long long values = 3287462318;
while (Q--) {
long long L, R;
int k;
scanf("%lld%lld%d", &L, &R, &k);
long long ans = R - L + 1;
if (!k) {
ans -= solve2(R + 1) - solve2(L);
for (int i = 2; i < 10; i += 2) ans -= solve(i, R + 1) - solve(i, L);
} else
for (int i = k + 1; i < 10; i++) ans -= solve(i, R + 1) - solve(i, L);
printf("%lld\n", ans);
}
}
| CPP |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
const long long MAX = 1000000000000000000ll;
int tot[20], to[20][12300][10], rev[10][1 << 10];
std::map<std::pair<unsigned long long, unsigned>, int> M[20];
int abs(int x) { return x < 0 ? -x : x; }
int init(int i, std::pair<unsigned long long, unsigned> s) {
if (!s.first && !s.second) return -1;
if (!i) {
while (i < 10 && !(s.first >> i & 1)) i++;
return i;
}
if (M[i].count(s)) return M[i][s];
int o = M[i][s] = tot[i]++, l = i * 9 + 1;
for (int x = 0; x < 10; x++) {
std::pair<unsigned long long, unsigned> t =
x ? std::make_pair(
s.first << x | s.first >> x |
(unsigned long long)(s.second << 32 - x) << 32 |
rev[x][s.first & (1 << x) - 1],
unsigned(s.first >> 64 - x | s.second << x | s.second >> x))
: s;
if (l < 64)
t.first &= (1ull << l) - 1, t.second = 0;
else if (l < 96)
t.second &= (1u << l - 64) - 1;
to[i][o][x] = init(i - 1, t);
}
return o;
}
long long f[10][20][12300];
long long dfs(int k, int i, int o) {
if (o < 0) return 0;
if (!i) return o <= k;
if (f[k][i][o]) return f[k][i][o] - 1;
for (int x = 0; x < 10; x++) f[k][i][o] += dfs(k, i - 1, to[i][o][x]);
return f[k][i][o]++;
}
long long cal(long long r, int k) {
long long s = 0;
int d[20], l = 0, o = 0;
if (r > MAX) k ? s++ : 1, r--;
while (r) d[l++] = r % 10, r /= 10;
while (l-- && o > -1) {
for (int x = 0; x < d[l]; x++) s += dfs(k, l, to[l + 1][o][x]);
o = to[l + 1][o][d[l]];
}
return s;
}
int main() {
int q;
scanf("%d", &q);
for (int i = 1; i < 10; i++)
for (int s = 1; s < 1 << i; s++)
rev[i][s] = rev[i - 1][s >> 1] | (s & 1) << i;
init(18, std::make_pair(1ull, 0u));
while (q--) {
long long l, r;
int k;
scanf("%lld%lld%d", &l, &r, &k);
printf("%lld\n", cal(r + 1, k) - cal(l, k));
}
}
| CPP |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
void upd(long long &x, long long y) { x += y; }
struct node {
int nxt;
long long sta, v[10];
node() {
nxt = 0;
memset(v, 0, sizeof(v));
}
};
struct state {
vector<node> nodes;
int head[100007];
state() { memset(head, -1, sizeof(head)); }
size_t size() const { return nodes.size(); }
void sum() {
for (int i = 0; i < nodes.size(); i++)
for (int j = 1; j <= 9; j++) upd(nodes[i].v[j], nodes[i].v[j - 1]);
}
void update(int at, long long key, long long v) {
int tmp = key % 100007;
for (int i = head[tmp]; i != -1; i = nodes[i].nxt)
if (nodes[i].sta == key) {
upd(nodes[i].v[at], v);
return;
}
node now;
now.nxt = head[tmp];
head[tmp] = nodes.size();
now.v[at] = v;
now.sta = key;
nodes.push_back(now);
}
long long find(int at, long long key) {
int tmp = key % 100007;
for (int i = head[tmp]; i != -1; i = nodes[i].nxt)
if (nodes[i].sta == key) return nodes[i].v[at];
return 0;
}
} f[11][20];
long long c[2][20], bit[15];
bitset<200> vis[15], pre[200], tmp;
struct big {
int a[35], len;
void read() {
memset(a, 0, sizeof(a));
len = 0;
char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar())
;
for (; ch >= '0' && ch <= '9'; ch = getchar()) a[len++] = ch - 48;
}
long long mod() {
long long ans = 0;
for (int i = 0; i < len; i++)
ans = (10ll * ans + a[i]) % 1000000000000000007ll;
return ans;
}
void plus() {
a[len - 1]++;
for (int i = len - 1; i >= 1; i--) a[i - 1] += a[i] / 10, a[i] %= 10;
if (a[0] > 9) {
a[0] = 1;
for (int i = 1; i <= len; i++) a[i] = 0;
len++;
}
}
} L, R;
void dfs(int dep, int cnt, int sum, long long msk) {
int mx = sum + (18 - cnt) * dep;
if (!dep) {
for (int i = sum >> 1; i >= 0; i--)
if (vis[dep + 1][i]) {
int dif = sum - i - i;
if (dif > 1) f[dif][0].update(0, msk, 1);
break;
}
return;
}
tmp = pre[mx >> 1];
if (sum >> 1) tmp ^= pre[(sum >> 1) - 1];
if ((vis[dep + 1] & tmp) == tmp) return;
vis[dep] = vis[dep + 1];
dfs(dep - 1, cnt, sum, msk);
for (; cnt < 18;) {
vis[dep] |= vis[dep] << dep;
dfs(dep - 1, ++cnt, sum += dep, msk += bit[dep]);
}
}
long long solve(int dif, big lim) {
long long ans = 0, msk = 0;
int len = lim.len;
state *F = f[dif];
for (int i = 0; i < len; i++)
if (len - i > 18) {
for (int j = 0; j < lim.a[i]; j++) ans += F[len - i - 1].find(9, msk);
} else if (lim.a[i]) {
ans += F[len - i].find(lim.a[i] - 1, msk);
msk += bit[lim.a[i]];
}
return ans;
}
long long solve2(big lim) {
int len = lim.len;
long long ans = 0;
int part = 0;
for (int i = 0; i < len; i++) {
int odd = lim.a[i] >> 1, even = lim.a[i] - odd;
ans += c[part][len - 1 - i] * odd + c[part ^ 1][len - 1 - i] * even;
part ^= lim.a[i] & 1;
}
return ans;
}
inline long long read() {
long long x = 0;
char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar())
;
for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 - 48 + ch;
return x;
}
int main() {
pre[0][0] = 1;
for (int i = 1; i <= 190; i++) pre[i] = pre[i - 1], pre[i][i] = 1;
c[0][0] = 1;
for (int i = 1; i <= 18; i++)
c[0][i] = c[1][i] =
5ll * (c[0][i - 1] + c[1][i - 1]) % 1000000000000000007ll;
bit[1] = 1;
for (int i = 2; i < 10; i++) bit[i] = bit[i - 1] << 5;
vis[10].set(0);
dfs(9, 0, 0, 0);
for (int i = 2; i < 10; i++) {
f[i][0].sum();
for (int j = 0; j < 18; j++) {
state &cur = f[i][j], &nxt = f[i][j + 1];
for (int id = 0, sz = cur.size(); id < sz; id++) {
int cnt = j;
long long way = cur.nodes[id].v[9];
long long msk = cur.nodes[id].sta, tmp = msk;
for (int k = 1; k < 10; k++, tmp >>= 5) {
int rem = tmp & 31;
if (!rem) continue;
cnt += rem;
nxt.update(k, msk - bit[k], way);
}
if (cnt < 18) nxt.update(0, msk, way);
}
nxt.sum();
}
}
int Q = read();
while (Q--) {
L.read();
R.read();
R.plus();
int k = read();
long long ans = R.mod() - L.mod();
if (!k) {
ans -= solve2(R) - solve2(L);
for (int i = 2; i < 10; i += 2) ans -= solve(i, R) - solve(i, L);
} else
for (int i = k + 1; i < 10; i++) ans -= solve(i, R) - solve(i, L);
printf("%lld\n", (ans % 1000000000000000007ll + 1000000000000000007ll) %
1000000000000000007ll);
}
return 0;
}
| CPP |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int B = 172;
const int maxn = 44000;
const int maxm = 20;
unordered_map<bitset<B>, int> mp;
bool vis[maxm][maxn];
bitset<B> a[maxn];
int nxt[maxn][10];
int cnt;
int bit[20];
long long dp[20][10][maxn];
long long solve(long long cnt, int k) {
if (cnt == 0) return 1;
int m = 0;
while (cnt) bit[m++] = cnt % 10, cnt /= 10;
int u = 1;
long long ret = 0;
for (int i = m - 1; i >= 0; i--) {
for (int j = 0; j < bit[i] + (i == 0); j++) ret += dp[i][k][nxt[u][j]];
u = nxt[u][bit[i]];
}
return ret;
}
int main() {
bitset<B> cur;
cur.set(0);
mp[cur] = 1;
a[1] = cur;
vis[0][1] = 1;
cnt = 1;
for (int i = 1; i < maxm; i++) {
for (int u = 1; u <= cnt; u++)
if (vis[i - 1][u]) {
for (int j = 0; j < 10; j++) {
if (!nxt[u][j]) {
if (j == 0) {
nxt[u][j] = u;
} else {
cur.reset();
for (int k = j; k < B; k++)
if (a[u][k]) cur[k - j] = 1;
for (int k = 0; k < B - j; k++)
if (a[u][k]) cur[k + j] = 1;
for (int k = 1; k < j; k++)
if (a[u][k]) cur[j - k] = 1;
if (mp.count(cur)) {
nxt[u][j] = mp[cur];
} else {
nxt[u][j] = mp[cur] = ++cnt;
a[cnt] = cur;
}
}
}
vis[i][nxt[u][j]] = 1;
}
}
}
for (int j = 1; j <= cnt; j++) {
int k = 0;
for (k = 0; k < 10; k++)
if (a[j][k]) break;
for (; k < 10; k++) dp[0][k][j] = 1;
}
for (int j = 1; j < maxm - 1; j++) {
for (int k = 1; k <= cnt; k++)
if (vis[maxm - 1 - j][k]) {
for (int i = 0; i < 10; i++) {
for (int l = 0; l < 10; l++) dp[j][l][k] += dp[j - 1][l][nxt[k][i]];
}
}
}
int kase;
scanf("%d", &kase);
while (kase--) {
long long l, r;
int k;
scanf("%I64d%I64d%d", &l, &r, &k);
printf("%I64d\n", solve(r, k) - solve(l - 1, k));
}
return 0;
}
| CPP |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
struct state {
vector<long long> keys, vals[11];
void insert(long long key) { keys.push_back(key); }
void init(long long val = 0) {
sort(keys.begin(), keys.end());
keys.erase(unique(keys.begin(), keys.end()), keys.end());
for (int i = 0; i < 10; i++) vals[i].assign(keys.size(), val);
}
void sum() {
for (int i = 1; i < 10; i++) {
vector<long long>::iterator j = vals[i - 1].begin(), k = vals[i].begin();
for (; k != vals[i].end(); j++, k++) *k += *j;
}
}
size_t size() const { return keys.size(); }
void update(int at, long long key, long long v) {
int pos = lower_bound(keys.begin(), keys.end(), key) - keys.begin();
vals[at][pos] += v;
}
long long find(int at, long long key) {
vector<long long>::iterator it = lower_bound(keys.begin(), keys.end(), key);
if (it == keys.end() || *it != key) return 0;
return vals[at][it - keys.begin()];
}
} f[11][20];
long long c[2][20], bit[15];
bitset<90> vis[15];
void dfs(int dep, int cnt, int sum, long long msk) {
if (dep == 10) {
for (int i = sum >> 1; i >= 0; i--)
if (vis[dep - 1][i]) {
int dif = sum - i - i;
if (dif > 1) f[dif][0].insert(msk);
break;
}
return;
}
vis[dep] = vis[dep - 1];
dfs(dep + 1, cnt, sum, msk);
for (; cnt < 18;) {
vis[dep] |= vis[dep] << dep;
dfs(dep + 1, ++cnt, sum += dep, msk += bit[dep]);
}
}
long long solve(int dif, long long lim) {
char dig[22];
int len = sprintf(dig, "%lld", lim);
long long ans = 0, msk = 0;
state *F = f[dif];
for (int i = 0; i < len; i++) {
dig[i] -= 48;
if (len - i > 18) {
for (int j = 0; j < dig[i]; j++) ans += F[len - i - 1].find(9, msk);
} else if (dig[i]) {
ans += F[len - i].find(dig[i] - 1, msk);
msk += bit[dig[i]];
}
}
return ans;
}
long long solve2(long long lim) {
char dig[22];
int len = sprintf(dig, "%lld", lim);
long long ans = 0;
int part = 0;
for (int i = 0; i < len; i++) {
dig[i] -= 48;
int odd = dig[i] >> 1, even = dig[i] - odd;
ans += c[part][len - 1 - i] * odd + c[part ^ 1][len - 1 - i] * even;
part ^= dig[i] & 1;
}
return ans;
}
int main() {
c[0][0] = 1;
for (int i = 1; i <= 18; i++)
c[0][i] = c[1][i] = 5 * (c[0][i - 1] + c[1][i - 1]);
bit[1] = 1;
for (int i = 2; i < 10; i++) bit[i] = bit[i - 1] << 5;
vis[0].set(0);
dfs(1, 0, 0, 0);
for (int i = 2; i < 10; i++) {
f[i][0].init(1);
for (int j = 0; j < 18; j++) {
state &cur = f[i][j], &nxt = f[i][j + 1];
for (int id = 0, sz = cur.size(); id < sz; id++) {
int cnt = j;
long long msk = cur.keys[id], tmp = msk;
for (int k = 1; k < 10; k++, tmp >>= 5) {
int rem = tmp & 31;
if (!rem) continue;
cnt += rem, nxt.insert(msk - bit[k]);
}
if (cnt < 18) nxt.insert(msk);
}
nxt.init(0);
for (int id = 0, sz = cur.size(); id < sz; id++) {
int cnt = j;
long long way = cur.vals[9][id];
long long msk = cur.keys[id], tmp = msk;
for (int k = 1; k < 10; k++, tmp >>= 5) {
int rem = tmp & 31;
if (!rem) continue;
cnt += rem;
nxt.update(k, msk - bit[k], way);
}
if (cnt < 18) nxt.update(0, msk, way);
}
nxt.sum();
}
}
int Q;
scanf("%d", &Q);
while (Q--) {
long long L, R;
int k;
scanf("%lld%lld%d", &L, &R, &k);
long long ans = R - L + 1;
if (!k) {
ans -= solve2(R + 1) - solve2(L);
for (int i = 2; i < 10; i += 2) ans -= solve(i, R + 1) - solve(i, L);
} else
for (int i = k + 1; i < 10; i++) ans -= solve(i, R + 1) - solve(i, L);
printf("%lld\n", ans);
}
}
| CPP |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int B = 172;
const int maxn = 44000;
const int maxm = 20;
unordered_map<bitset<B>, int> mp;
bool vis[maxm][maxn];
bitset<B> a[maxn];
int nxt[maxn][10];
int n;
int bit[20];
long long dp[20][10][maxn];
long long solve(long long n, int k) {
if (n == 0) return 1;
int m = 0;
while (n) bit[m++] = n % 10, n /= 10;
int u = 1;
long long ret = 0;
for (int i = m - 1; i >= 0; i--) {
for (int j = 0; j < bit[i] + (i == 0); j++) ret += dp[i][k][nxt[u][j]];
u = nxt[u][bit[i]];
}
return ret;
}
int main() {
bitset<B> cur;
cur.set(0);
mp[cur] = 1;
a[1] = cur;
vis[0][1] = 1;
n = 1;
for (int i = 1; i < maxm; i++) {
for (int u = 1; u <= n; u++)
if (vis[i - 1][u]) {
for (int j = 0; j < 10; j++) {
if (!nxt[u][j]) {
if (j == 0) {
nxt[u][j] = u;
} else {
cur.reset();
for (int k = j; k < B; k++)
if (a[u][k]) cur[k - j] = 1;
for (int k = 0; k < B - j; k++)
if (a[u][k]) cur[k + j] = 1;
for (int k = 1; k < j; k++)
if (a[u][k]) cur[j - k] = 1;
if (mp.count(cur)) {
nxt[u][j] = mp[cur];
} else {
nxt[u][j] = mp[cur] = ++n;
a[n] = cur;
}
}
}
vis[i][nxt[u][j]] = 1;
}
}
}
for (int j = 1; j <= n; j++) {
int k = 0;
for (k = 0; k < 10; k++)
if (a[j][k]) break;
for (; k < 10; k++) dp[0][k][j] = 1;
}
for (int j = 1; j < maxm - 1; j++) {
for (int k = 1; k <= n; k++)
if (vis[maxm - 1 - j][k]) {
for (int i = 0; i < 10; i++) {
for (int l = 0; l < 10; l++) dp[j][l][k] += dp[j - 1][l][nxt[k][i]];
}
}
}
int ncase;
for (scanf("%d", &ncase); ncase--;) {
long long l, r;
int k;
scanf("%lld%lld%d", &l, &r, &k);
printf("%lld\n", solve(r, k) - solve(l - 1, k));
}
return 0;
}
| CPP |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
using ll = long long;
using ld = long double;
using namespace std;
vector<ll> splits;
const ll B = 20;
const int D = 10;
const int M = 19;
ll pw[D];
const int C = 100000;
const int NO = C - 1;
vector<ll> vals;
int go[C][D];
bool good[C][D];
ll goodCount[D][M][C];
int cn;
using BS = bitset<9 * 20 + 2>;
unordered_map<BS, int> bss;
BS q[C];
int ln[C];
int upd(const BS& b, int dst) {
auto it = bss.emplace(b, cn);
if (it.second) {
ln[cn] = dst;
q[cn++] = b;
}
return it.first->second;
}
void precalc() {
BS st{};
st[0] = true;
upd(st, 0);
for (int qb = 0; qb < cn; ++qb) {
auto b = q[qb];
int d = ln[qb];
fill(begin(go[qb]), end(go[qb]), NO);
if (d < M) {
for (int i = 0; i < D; ++i) {
auto nb = (b << i) | (b >> i);
for (int j = 0; j < i; ++j) {
if (b[j]) {
nb[i - j] = true;
}
}
go[qb][i] = upd(nb, d + 1);
}
}
}
good[0][0] = true;
for (int i = 0; i < cn; ++i) {
for (int j = 0; j < D; ++j) {
if (!good[i][j]) {
continue;
}
for (int l = 1; l < D; ++l) {
good[go[i][l]][abs(l - j)] = true;
}
}
}
for (int i = 0; i < cn; ++i) {
for (int j = 1; j < D; ++j) {
good[i][j] |= good[i][j - 1];
}
}
for (int k = 0; k < D; ++k) {
for (int i = 0; i < cn; ++i) {
goodCount[k][0][i] = good[i][k];
}
for (int j = 1; j < M; ++j) {
for (int i = 0; i < cn; ++i) {
for (int l = 0; l < D; ++l) {
goodCount[k][j][i] += goodCount[k][j - 1][go[i][l]];
}
}
}
}
}
ll calc(ll x, int k) {
int a[M];
for (int i = 0; i < M; ++i) {
a[i] = x % 10;
x /= 10;
}
ll ans = 0;
int cur = 0;
for (int i = M - 1; i >= 0; --i) {
for (int j = 0; j < a[i]; ++j) {
ans += goodCount[k][i][go[cur][j]];
}
cur = go[cur][a[i]];
}
return ans;
}
void solve() {
ll l, r;
int k;
cin >> l >> r >> k;
ll rc = calc(r + 1, k);
ll lc = calc(l, k);
cout << rc - lc << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cout.setf(ios::fixed);
cout.precision(20);
cout.tie(nullptr);
cin.tie(nullptr);
precalc();
int t;
cin >> t;
for (int i = 0; i < t; ++i) {
solve();
}
}
| CPP |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
const long long MAX = 1000000000000000000ll;
int tot[20], to[20][12300][10], rev[10][1 << 10];
std::map<std::pair<unsigned long long, unsigned>, int> M[20];
int abs(int x) { return x < 0 ? -x : x; }
int init(int i, std::pair<unsigned long long, unsigned> s) {
if (!s.first && !s.second) return -1;
if (!i) {
while (i < 10 && !(s.first >> i & 1)) i++;
return i;
}
if (M[i].count(s)) return M[i][s];
int o = M[i][s] = tot[i]++, l = i * 9 + 1;
for (int x = 0; x < 10; x++) {
std::pair<unsigned long long, unsigned> t =
x ? std::make_pair(
s.first << x | s.first >> x |
(unsigned long long)(s.second << 32 - x) << 32 |
rev[x][s.first & (1 << x) - 1],
unsigned(s.first >> 64 - x | s.second << x | s.second >> x))
: s;
if (l < 64)
t.first &= (1ull << l) - 1, t.second = 0;
else if (l < 96)
t.second &= (1u << l - 64) - 1;
to[i][o][x] = init(i - 1, t);
}
return o;
}
long long f[10][20][12300];
long long dfs(int k, int i, int o) {
if (o < 0) return 0;
if (!i) return o <= k;
if (f[k][i][o]) return f[k][i][o] - 1;
for (int x = 0; x < 10; x++) f[k][i][o] += dfs(k, i - 1, to[i][o][x]);
return f[k][i][o]++;
}
long long cal(long long r, int k) {
long long s = 0;
int d[20], l = 0, o = 0;
if (r > MAX) k ? s++ : 1, r--;
while (r) d[l++] = r % 10, r /= 10;
while (l-- && o > -1) {
for (int x = 0; x < d[l]; x++) s += dfs(k, l, to[l + 1][o][x]);
o = to[l + 1][o][d[l]];
}
return s;
}
int main() {
int q;
scanf("%d", &q);
for (int i = 1; i < 10; i++)
for (int s = 1; s < 1 << i; s++)
rev[i][s] = rev[i - 1][s >> 1] | (s & 1) << i;
init(18, std::make_pair(1ull, 0u));
while (q--) {
long long l, r;
int k;
scanf("%lld%lld%d", &l, &r, &k);
printf("%lld\n", cal(r + 1, k) - cal(l, k));
}
}
| CPP |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
using ll = long long;
using ld = long double;
using namespace std;
vector<ll> splits;
const ll B = 20;
const int D = 10;
const int M = 19;
ll pw[D];
const int C = 100000;
const int NO = C - 1;
vector<ll> vals;
int go[C][D];
bool good[C][D];
ll goodCount[D][M][C];
int cn;
using BS = bitset<9 * 20 + 2>;
unordered_map<BS, int> bss;
BS q[C];
int ln[C];
int upd(const BS& b, int dst) {
auto it = bss.emplace(b, cn);
if (it.second) {
ln[cn] = dst;
q[cn++] = b;
}
return it.first->second;
}
void precalc() {
BS st{};
st[0] = true;
upd(st, 0);
for (int qb = 0; qb < cn; ++qb) {
auto b = q[qb];
int d = ln[qb];
fill(begin(go[qb]), end(go[qb]), NO);
if (d < M) {
for (int i = 0; i < D; ++i) {
auto nb = (b << i) | (b >> i);
for (int j = 0; j < i; ++j) {
if (b[j]) {
nb[i - j] = true;
}
}
go[qb][i] = upd(nb, d + 1);
}
}
}
good[0][0] = true;
for (int i = 0; i < cn; ++i) {
for (int j = 0; j < D; ++j) {
if (!good[i][j]) {
continue;
}
for (int l = 1; l < D; ++l) {
good[go[i][l]][abs(l - j)] = true;
}
}
}
for (int i = 0; i < cn; ++i) {
for (int j = 1; j < D; ++j) {
good[i][j] |= good[i][j - 1];
}
}
for (int k = 0; k < D; ++k) {
for (int i = 0; i < cn; ++i) {
goodCount[k][0][i] = good[i][k];
}
for (int j = 1; j < M; ++j) {
for (int i = 0; i < cn; ++i) {
for (int l = 0; l < D; ++l) {
goodCount[k][j][i] += goodCount[k][j - 1][go[i][l]];
}
}
}
}
}
ll calc(ll x, int k) {
int a[M];
for (int i = 0; i < M; ++i) {
a[i] = x % 10;
x /= 10;
}
ll ans = 0;
int cur = 0;
for (int i = M - 1; i >= 0; --i) {
for (int j = 0; j < a[i]; ++j) {
ans += goodCount[k][i][go[cur][j]];
}
cur = go[cur][a[i]];
}
return ans;
}
void solve() {
ll l, r;
int k;
cin >> l >> r >> k;
ll rc = calc(r + 1, k);
ll lc = calc(l, k);
cout << rc - lc << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cout.setf(ios::fixed);
cout.precision(20);
cout.tie(nullptr);
cin.tie(nullptr);
precalc();
int t;
cin >> t;
for (int i = 0; i < t; ++i) {
solve();
}
}
| CPP |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int maxD = 10, maxl = 18, maxs = 81, BLEN = 5, BMSK = 31;
struct States {
vector<long long> keys, vals[maxD + 1];
void insert(long long key) { keys.push_back(key); }
void initialize(long long val = 0) {
sort(keys.begin(), keys.end());
keys.erase(unique(keys.begin(), keys.end()), keys.end());
for (int i = 0; i < maxD; ++i) vals[i].assign(keys.size(), val);
}
void summation() {
for (int i = 1; i < maxD; ++i)
for (vector<long long>::iterator it = vals[i - 1].begin(),
jt = vals[i].begin();
jt != vals[i].end(); ++it, ++jt)
*jt += *it;
}
size_t size() const { return keys.size(); }
void update(int upp, long long key, long long adt) {
vals[upp][lower_bound(keys.begin(), keys.end(), key) - keys.begin()] += adt;
}
long long find(int upp, long long key) {
vector<long long>::iterator it = lower_bound(keys.begin(), keys.end(), key);
if (it == keys.end() || *it != key) return 0;
return vals[upp][it - keys.begin()];
}
} f[maxD + 1][maxl + 1];
long long c[2][maxl + 1], bit[maxD + 1];
bitset<maxs + 1> vis[maxD + 1];
void dfs(int dep, int cnt, int sum, long long msk) {
if (dep == maxD) {
for (int i = sum >> 1; i >= 0; --i)
if (vis[dep - 1].test(i)) {
int dif = sum - i - i;
if (dif > 1) f[dif][0].insert(msk);
break;
}
return;
}
vis[dep] = vis[dep - 1];
dfs(dep + 1, cnt, sum, msk);
while (cnt < maxl) {
vis[dep] |= vis[dep] << dep;
dfs(dep + 1, ++cnt, sum += dep, msk += bit[dep]);
}
}
long long solve(int dif, long long lim) {
char dig[maxl + 3];
int len = sprintf(dig, "%lld", lim);
long long ret = 0, msk = 0;
States *F = f[dif];
for (int i = 0; i < len; ++i) {
dig[i] -= '0';
if (len - i > maxl) {
for (int j = 0; j < dig[i]; ++j)
ret += F[len - i - 1].find(maxD - 1, msk);
} else if (dig[i]) {
ret += F[len - i].find(dig[i] - 1, msk);
msk += bit[dig[i]];
}
}
return ret;
}
long long solve2(long long lim) {
char dig[maxl + 3];
int len = sprintf(dig, "%lld", lim), part = 0;
long long ret = 0;
for (int i = 0; i < len; ++i) {
dig[i] -= '0';
int odd = dig[i] >> 1, even = (dig[i] + 1) >> 1;
ret += c[part][len - 1 - i] * odd + c[part ^ 1][len - 1 - i] * even;
part ^= dig[i] & 1;
}
return ret;
}
int main() {
c[0][0] = 1;
for (int i = 0; i < maxl; ++i) {
int odd = maxD >> 1, even = (maxD + 1) >> 1;
c[0][i + 1] = c[0][i] * even + c[1][i] * odd;
c[1][i + 1] = c[0][i] * odd + c[1][i] * even;
}
bit[1] = 1;
for (int i = 2; i < maxD; ++i) bit[i] = bit[i - 1] << BLEN;
vis[0].set(0);
dfs(1, 0, 0, 0);
for (int i = 2; i < maxD; ++i) {
f[i][0].initialize(1);
for (int j = 0; j < maxl; ++j) {
States &cur = f[i][j], &nxt = f[i][j + 1];
for (int idx = 0, sz = cur.size(); idx < sz; ++idx) {
int cnt = j;
long long msk = cur.keys[idx], tmp = msk;
for (int k = 1; k < maxD; ++k, tmp >>= BLEN) {
int rem = tmp & BMSK;
if (!rem) continue;
cnt += rem;
nxt.insert(msk - bit[k]);
}
if (cnt < maxl) nxt.insert(msk);
}
nxt.initialize(0);
for (int idx = 0, sz = cur.size(); idx < sz; ++idx) {
int cnt = j;
long long msk = cur.keys[idx], ways = cur.vals[maxD - 1][idx],
tmp = msk;
for (int k = 1; k < maxD; ++k, tmp >>= BLEN) {
int rem = tmp & BMSK;
if (!rem) continue;
cnt += rem;
nxt.update(k, msk - bit[k], ways);
}
if (cnt < maxl) nxt.update(0, msk, ways);
}
nxt.summation();
}
}
int t;
scanf("%d", &t);
for (int Case = 1; Case <= t; ++Case) {
long long L, R;
int k;
scanf("%lld%lld%d", &L, &R, &k);
long long ans = R + 1 - L;
if (!k) {
ans -= solve2(R + 1) - solve2(L);
for (int i = 2; i < maxD; i += 2) ans -= solve(i, R + 1) - solve(i, L);
} else {
for (int i = k + 1; i < maxD; ++i) ans -= solve(i, R + 1) - solve(i, L);
}
printf("%lld\n", ans);
}
return 0;
}
| CPP |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int B = 172;
const int maxn = 44000;
const int maxm = 20;
unordered_map<bitset<B>, int> mp;
bool vis[maxm][maxn];
bitset<B> a[maxn];
int nxt[maxn][10];
int cnt = 1;
int bit[20];
long long dp[20][10][maxn];
long long solve(long long x, int k) {
if (x == 0) return 1;
int len = 0;
while (x) {
bit[len++] = x % 10;
x /= 10;
}
int u = 1;
long long ret = 0;
for (int i = len - 1; i >= 0; i--) {
for (int j = 0; j < bit[i] + (i == 0); j++) ret += dp[i][k][nxt[u][j]];
u = nxt[u][bit[i]];
}
return ret;
}
int main() {
bitset<B> Bit;
Bit.set(0);
mp[Bit] = 1;
a[1] = Bit;
vis[0][1] = 1;
for (int i = 1; i < maxm; i++)
for (int u = 1; u <= cnt; u++)
if (vis[i - 1][u]) {
for (int j = 0; j < 10; j++) {
if (!nxt[u][j]) {
if (j == 0)
nxt[u][j] = u;
else {
Bit.reset();
for (int k = j; k < B; k++)
if (a[u][k]) Bit[k - j] = 1;
for (int k = 0; k < B - j; k++)
if (a[u][k]) Bit[k + j] = 1;
for (int k = 1; k < j; k++)
if (a[u][k]) Bit[j - k] = 1;
if (mp.count(Bit))
nxt[u][j] = mp[Bit];
else {
nxt[u][j] = mp[Bit] = ++cnt;
a[cnt] = Bit;
}
}
}
vis[i][nxt[u][j]] = 1;
}
}
for (int j = 1; j <= cnt; j++) {
int k = 0;
for (k = 0; k < 10; k++)
if (a[j][k]) break;
for (; k < 10; k++) dp[0][k][j] = 1;
}
for (int j = 1; j < maxm - 1; j++)
for (int k = 1; k <= cnt; k++)
if (vis[maxm - 1 - j][k])
for (int i = 0; i < 10; i++)
for (int l = 0; l < 10; l++) dp[j][l][k] += dp[j - 1][l][nxt[k][i]];
int kase;
scanf("%d", &kase);
while (kase--) {
long long l, r;
int k;
scanf("%I64d%I64d%d", &l, &r, &k);
printf("%I64d\n", solve(r, k) - solve(l - 1, k));
}
return 0;
}
| CPP |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXK = 9;
const int MAXLEN = 19;
const int MAXS = 9 * MAXLEN / 2;
const int XLEN = 5;
int xoff[10];
long long xmask[10];
vector<long long> opt[MAXK + 1][MAXLEN + 1];
vector<long long> ways[MAXK + 1][MAXLEN + 1][10];
long long wayspar[2][MAXLEN + 1];
bitset<MAXS + 1> can[10];
void dfscan(int d, int cnt, int sum, long long x) {
if (d > 9) {
int mx = sum / 2;
while (!can[d - 1].test(mx)) --mx;
int k = sum - 2 * mx;
assert(k <= MAXK);
if (k >= 2) opt[k][0].push_back(x);
} else {
can[d] = can[d - 1];
while (true) {
dfscan(d + 1, cnt, sum, x);
if (cnt >= MAXLEN) break;
++cnt;
sum += d;
x += 1LL << xoff[d];
can[d] |= can[d] << d;
}
}
}
void precalc() {
wayspar[0][0] = 1;
for (int i = (0); i < (MAXLEN); ++i)
wayspar[0][i + 1] = wayspar[0][i] * 5 + wayspar[1][i] * 5,
wayspar[1][i + 1] = wayspar[0][i] * 5 + wayspar[1][i] * 5;
xoff[1] = 0;
for (int i = (2); i <= (9); ++i) xoff[i] = xoff[i - 1] + XLEN;
for (int i = (1); i <= (9); ++i)
xmask[i] = ((long long)((1 << XLEN) - 1)) << xoff[i];
can[0].set(0);
dfscan(1, 0, 0, 0);
for (int k = (2); k <= (MAXK); ++k) {
sort(opt[k][0].begin(), opt[k][0].end());
opt[k][0].erase(unique(opt[k][0].begin(), opt[k][0].end()),
opt[k][0].end());
for (int d = (0); d < (10); ++d)
ways[k][0][d] = vector<long long>(((int)(opt[k][0]).size()), 1);
for (int i = (0); i < (MAXLEN); ++i) {
for (int x = (0); x < (((int)(opt[k][i]).size())); ++x) {
int sum = 0;
for (int d = (1); d <= (9); ++d)
if ((opt[k][i][x] & xmask[d]) != 0)
opt[k][i + 1].push_back(opt[k][i][x] - (1LL << xoff[d])),
sum += (opt[k][i][x] & xmask[d]) >> xoff[d];
if (sum < MAXLEN) opt[k][i + 1].push_back(opt[k][i][x]);
}
sort(opt[k][i + 1].begin(), opt[k][i + 1].end());
opt[k][i + 1].erase(unique(opt[k][i + 1].begin(), opt[k][i + 1].end()),
opt[k][i + 1].end());
for (int d = (0); d < (10); ++d)
ways[k][i + 1][d] = vector<long long>(((int)(opt[k][i + 1]).size()), 0);
for (int x = (0); x < (((int)(opt[k][i]).size())); ++x) {
int sum = 0;
for (int d = (1); d <= (9); ++d)
if ((opt[k][i][x] & xmask[d]) != 0) {
int nx = lower_bound(opt[k][i + 1].begin(), opt[k][i + 1].end(),
opt[k][i][x] - (1LL << xoff[d])) -
opt[k][i + 1].begin();
assert(0 <= nx && nx < ((int)(opt[k][i + 1]).size()) &&
opt[k][i + 1][nx] == opt[k][i][x] - (1LL << xoff[d]));
ways[k][i + 1][d][nx] += ways[k][i][9][x];
sum += (opt[k][i][x] & xmask[d]) >> xoff[d];
}
if (sum < MAXLEN) {
int nx = lower_bound(opt[k][i + 1].begin(), opt[k][i + 1].end(),
opt[k][i][x]) -
opt[k][i + 1].begin();
assert(0 <= nx && nx < ((int)(opt[k][i + 1]).size()) &&
opt[k][i + 1][nx] == opt[k][i][x]);
ways[k][i + 1][0][nx] += ways[k][i][9][x];
}
}
for (int d = (1); d <= (9); ++d)
for (int x = (0); x < (((int)(opt[k][i + 1]).size())); ++x)
ways[k][i + 1][d][x] += ways[k][i + 1][d - 1][x];
}
}
}
char s[MAXLEN + 1];
long long calc(long long lim, int k) {
sprintf(s, "%lld", lim);
int slen = strlen(s);
long long ret = 0, x = 0;
for (int i = (0); i < (slen); ++i) {
int d = s[i] - '0';
if (d == 0) continue;
int idx = lower_bound(opt[k][slen - i].begin(), opt[k][slen - i].end(), x) -
opt[k][slen - i].begin();
if (idx != ((int)(opt[k][slen - i]).size()) && opt[k][slen - i][idx] == x) {
ret += ways[k][slen - i][d - 1][idx];
}
x += 1LL << xoff[d];
}
return ret;
}
long long calcodd(long long lim) {
sprintf(s, "%lld", lim);
int slen = strlen(s);
long long ret = 0;
int par = 0;
for (int i = (0); i < (slen); ++i) {
int d = s[i] - '0';
if (d == 0) continue;
int oddhere = d / 2, evnhere = (d + 1) / 2;
ret += wayspar[par][slen - i - 1] * oddhere +
wayspar[1 - par][slen - i - 1] * evnhere;
if (d % 2 == 1) par ^= 1;
}
return ret;
}
long long solve(long long l, long long r, int kmx) {
long long ret = r - l + 1;
if (kmx == 0) {
ret -= calcodd(r + 1) - calcodd(l);
for (int k = 2; k <= MAXK; k += 2) ret -= calc(r + 1, k) - calc(l, k);
} else {
for (int k = (kmx + 1); k <= (MAXK); ++k)
ret -= calc(r + 1, k) - calc(l, k);
}
return ret;
}
void run() {
int nq;
scanf("%d", &nq);
for (int qi = (0); qi < (nq); ++qi) {
long long l, r;
int k;
scanf("%lld%lld%d", &l, &r, &k);
printf("%lld\n", solve(l, r, k));
}
}
int main() {
precalc();
run();
return 0;
}
| CPP |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int B = 172;
const int maxn = 44000;
const int maxm = 20;
unordered_map<bitset<B>, int> mp;
bool vis[maxm][maxn];
bitset<B> a[maxn];
int nxt[maxn][10];
int cnt;
int bit[20];
long long dp[20][10][maxn];
long long solve(long long cnt, int k) {
if (cnt == 0) return 1;
int m = 0;
while (cnt) bit[m++] = cnt % 10, cnt /= 10;
int u = 1;
long long ret = 0;
for (int i = m - 1; i >= 0; i--) {
for (int j = 0; j < bit[i] + (i == 0); j++) ret += dp[i][k][nxt[u][j]];
u = nxt[u][bit[i]];
}
return ret;
}
int main() {
bitset<B> cur;
cur.set(0);
mp[cur] = 1;
a[1] = cur;
vis[0][1] = 1;
cnt = 1;
for (int i = 1; i < maxm; i++) {
for (int u = 1; u <= cnt; u++)
if (vis[i - 1][u]) {
for (int j = 0; j < 10; j++) {
if (!nxt[u][j]) {
if (j == 0) {
nxt[u][j] = u;
} else {
cur.reset();
for (int k = j; k < B; k++)
if (a[u][k]) cur[k - j] = 1;
for (int k = 0; k < B - j; k++)
if (a[u][k]) cur[k + j] = 1;
for (int k = 1; k < j; k++)
if (a[u][k]) cur[j - k] = 1;
if (mp.count(cur)) {
nxt[u][j] = mp[cur];
} else {
nxt[u][j] = mp[cur] = ++cnt;
a[cnt] = cur;
}
}
}
vis[i][nxt[u][j]] = 1;
}
}
}
for (int j = 1; j <= cnt; j++) {
int k = 0;
for (k = 0; k < 10; k++)
if (a[j][k]) break;
for (; k < 10; k++) dp[0][k][j] = 1;
}
for (int j = 1; j < maxm - 1; j++) {
for (int k = 1; k <= cnt; k++)
if (vis[maxm - 1 - j][k]) {
for (int i = 0; i < 10; i++) {
for (int l = 0; l < 10; l++) dp[j][l][k] += dp[j - 1][l][nxt[k][i]];
}
}
}
int kase;
scanf("%d", &kase);
while (kase--) {
long long l, r;
int k;
scanf("%I64d%I64d%d", &l, &r, &k);
printf("%I64d\n", solve(r, k) - solve(l - 1, k));
}
return 0;
}
| CPP |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int B = 172;
const int maxn = 44000;
const int maxm = 20;
unordered_map<bitset<B>, int> mp;
bool vis[maxm][maxn];
bitset<B> a[maxn];
int nxt[maxn][10];
int cnt = 1;
int bit[20];
long long dp[20][10][maxn];
long long solve(long long x, int k) {
if (x == 0) return 1;
int m = 0;
while (x) bit[m++] = x % 10, x /= 10;
int u = 1;
long long ret = 0;
for (int i = m - 1; i >= 0; i--) {
for (int j = 0; j < bit[i] + (i == 0); j++) ret += dp[i][k][nxt[u][j]];
u = nxt[u][bit[i]];
}
return ret;
}
int main() {
bitset<B> Bit;
Bit.set(0);
mp[Bit] = 1;
a[1] = Bit;
vis[0][1] = 1;
for (int i = 1; i < maxm; i++)
for (int u = 1; u <= cnt; u++)
if (vis[i - 1][u]) {
for (int j = 0; j < 10; j++) {
if (!nxt[u][j]) {
if (j == 0)
nxt[u][j] = u;
else {
Bit.reset();
for (int k = j; k < B; k++)
if (a[u][k]) Bit[k - j] = 1;
for (int k = 0; k < B - j; k++)
if (a[u][k]) Bit[k + j] = 1;
for (int k = 1; k < j; k++)
if (a[u][k]) Bit[j - k] = 1;
if (mp.count(Bit))
nxt[u][j] = mp[Bit];
else {
nxt[u][j] = mp[Bit] = ++cnt;
a[cnt] = Bit;
}
}
}
vis[i][nxt[u][j]] = 1;
}
}
for (int j = 1; j <= cnt; j++) {
int k = 0;
for (k = 0; k < 10; k++)
if (a[j][k]) break;
for (; k < 10; k++) dp[0][k][j] = 1;
}
for (int j = 1; j < maxm - 1; j++)
for (int k = 1; k <= cnt; k++)
if (vis[maxm - 1 - j][k])
for (int i = 0; i < 10; i++)
for (int l = 0; l < 10; l++) dp[j][l][k] += dp[j - 1][l][nxt[k][i]];
int kase;
scanf("%d", &kase);
while (kase--) {
long long l, r;
int k;
scanf("%I64d%I64d%d", &l, &r, &k);
printf("%I64d\n", solve(r, k) - solve(l - 1, k));
}
return 0;
}
| CPP |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int oo = 0x3f3f3f3f;
const long long ooo = 9223372036854775807ll;
const int _cnt = 1000 * 1000 + 7;
const int _p = 1000 * 1000 * 1000 + 7;
const int N = 100005;
const double PI = acos(-1.0);
const double eps = 1e-9;
int o(int x) { return x % _p; }
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
void file_put() {
freopen("filename.in", "r", stdin);
freopen("filename.out", "w", stdout);
}
bitset<165> B[N];
unordered_map<bitset<165>, int> M;
int T, tot = 0, no[N], k, go[N][10], d[20], top;
long long l, r, s[10][20][N], ans;
inline int I(const bitset<165> &x) {
if (M.count(x) == 0) B[M[x] = ++tot] = x;
return M[x];
}
void init() {
I(bitset<165>(1));
for (int i = (1); i <= (tot); ++i)
for (int j = (0); j <= (9); ++j) {
bitset<165> t = B[i] << j | B[i] >> j;
for (int k = (0); k <= (j - 1); ++k)
if (B[i][k]) t[j - k] = 1;
go[i][j] = I(t);
}
for (int i = (1); i <= (tot); ++i)
for (int k = (0); k <= (9); ++k) {
if (B[i][k]) {
no[i] = k;
break;
}
}
for (int i = (1); i <= (tot); ++i)
for (int k = (0); k <= (9); ++k) s[k][0][i] = (k >= no[i]);
for (int k = (0); k <= (9); ++k)
for (int i = (1); i <= (18); ++i)
for (int j = (1); j <= (tot); ++j)
for (int c = (0); c <= (9); ++c) s[k][i][j] += s[k][i - 1][go[j][c]];
}
long long Query(long long x, int k) {
for (top = 0; x; x /= 10) d[++top] = x % 10;
ans = 0;
for (int i = top, now = 1; i; now = go[now][d[i--]])
for (int j = (0); j <= (d[i] - 1); ++j) ans += s[k][i - 1][go[now][j]];
return ans;
}
int main() {
scanf("%d", &T), init();
while (T--) {
scanf("%I64d%I64d%d", &l, &r, &k);
printf("%I64d\n", Query(r + 1, k) - Query(l, k));
}
return 0;
}
| CPP |
924_F. Minimal Subset Difference | We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.
There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.
Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
Output
For each query print a single number β the answer to the query.
Examples
Input
10
1 100 0
1 100 1
1 100 2
1 100 3
1 100 4
1 100 5
1 100 6
1 100 7
1 100 8
1 100 9
Output
9
28
44
58
70
80
88
94
98
100
Input
10
1 1000 0
1 1000 1
1 1000 2
1 1000 3
1 1000 4
1 1000 5
1 1000 6
1 1000 7
1 1000 8
1 1000 9
Output
135
380
573
721
830
906
955
983
996
1000
Note
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.
If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.
100 is k-beautiful if and only if k β₯ 1. | 2 | 12 | #include <bits/stdc++.h>
using namespace std;
const int maxd = 10, maxl = 18, maxs = 81, BLEN = 5, BMSK = 31, maxv = 32175,
maxv2 = 191791;
int htot, cnt[maxv], inc[maxv][maxd + 1], sub[maxv][maxd + 1], ptot,
pos[maxl + 1][maxv];
map<long long, int> Hash;
long long bit[maxd + 1], c[2][maxl + 1];
long long seq[maxv], f[maxv2][maxd + 1][maxd - 1];
bitset<maxs + 1> vis[maxd + 1];
inline int getID(long long msk) {
map<long long, int>::iterator it = Hash.find(msk);
if (it != Hash.end()) return it->second;
seq[htot] = msk;
return Hash[msk] = htot++;
}
void dfs(int dep, int cnt, int sum, long long msk) {
if (dep == maxd) {
for (int i = sum >> 1; i >= 0; --i)
if (vis[dep - 1].test(i)) {
int dif = sum - i - i;
if (dif > 1) {
int idx = getID(msk);
pos[0][idx] = idx;
f[idx][maxd - 1][dif - 1] = 1;
}
break;
}
return;
}
vis[dep] = vis[dep - 1];
dfs(dep + 1, cnt, sum, msk);
while (cnt < maxl) {
vis[dep] |= vis[dep] << dep;
dfs(dep + 1, ++cnt, sum += dep, msk += bit[dep]);
}
}
long long solve(long long lim, int dif) {
if (dif >= maxd - 1) return lim;
char dig[maxl + 3];
int len = sprintf(dig, "%lld", lim), hidx = getID(0), part = 0;
long long ret = lim;
for (int i = 0; i < len; ++i) {
dig[i] -= '0';
if (!dif) {
int odd = dig[i] >> 1, even = (dig[i] + 1) >> 1;
ret -= c[part][len - 1 - i] * odd + c[part ^ 1][len - 1 - i] * even;
part ^= dig[i] & 1;
}
if (hidx >= 0) {
if (len - i > maxl) {
for (int j = 0; j < dig[i]; ++j) {
int hidx2 = inc[hidx][j];
if (hidx2 >= 0) {
int pidx = pos[len - i - 1][hidx2];
if (pidx >= 0) ret -= f[pidx][maxd - 1][dif];
}
}
} else if (dig[i]) {
int pidx = pos[len - i][hidx];
if (pidx >= 0) ret -= f[pidx][dig[i] - 1][dif];
}
hidx = inc[hidx][dig[i]];
}
}
return ret;
}
int main() {
bit[1] = 1;
for (int i = 2; i < maxd; ++i) bit[i] = bit[i - 1] << BLEN;
c[0][0] = 1;
for (int i = 0; i < maxl; ++i) {
int odd = maxd >> 1, even = (maxd + 1) >> 1;
c[0][i + 1] = c[0][i] * even + c[1][i] * odd;
c[1][i + 1] = c[0][i] * odd + c[1][i] * even;
}
memset(inc, -1, sizeof inc);
memset(sub, -1, sizeof sub);
memset(pos, -1, sizeof pos);
vis[0].set(0);
dfs(1, 0, 0, 0);
ptot = htot;
for (int i = 0; i < htot; ++i) {
long long msk = seq[i], tmp = msk;
inc[i][0] = sub[i][0] = i;
for (int j = 1; j < maxd; ++j, tmp >>= BLEN) {
int rem = tmp & BMSK;
if (!rem) continue;
cnt[i] += rem;
int idx = getID(msk - bit[j]);
inc[idx][j] = i;
sub[i][j] = idx;
}
}
for (int i = 0; i <= maxl; ++i) {
for (int hidx = 0; hidx < htot; ++hidx) {
int pidx = pos[i][hidx];
if (pidx < 0) continue;
for (int k = 1; k < maxd; ++k)
for (int d = 1; d < maxd - 1; ++d) f[pidx][k][d] += f[pidx][k - 1][d];
if (i + cnt[hidx] < maxl) {
int pidx2 = pos[i + 1][hidx];
if (pidx2 < 0) pos[i + 1][hidx] = pidx2 = ptot++;
for (int d = 0; d < maxd - 1; ++d)
f[pidx2][0][d] += f[pidx][maxd - 1][d];
}
for (int k = 1; k < maxd; ++k) {
int hidx2 = sub[hidx][k];
if (hidx2 < 0) continue;
int pidx2 = pos[i + 1][hidx2];
if (pidx2 < 0) pos[i + 1][hidx2] = pidx2 = ptot++;
for (int d = 1; d < maxd - 1; ++d)
f[pidx2][k][d] += f[pidx][maxd - 1][d];
}
for (int k = 0; k < maxd; ++k) {
for (int d = 1; d < maxd - 1; d += 2) f[pidx][k][0] += f[pidx][k][d];
for (int d = maxd - 2; d > 1; --d) f[pidx][k][d - 1] += f[pidx][k][d];
}
}
}
int t;
scanf("%d", &t);
for (int Case = 1; Case <= t; ++Case) {
long long L, R;
int k;
scanf("%lld%lld%d", &L, &R, &k);
printf("%lld\n", solve(R + 1, k) - solve(L, k));
}
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int n;
char s[200];
int ucnt, rcnt;
int main() {
scanf("%d", &n);
getchar();
gets(s);
int ans = strlen(s);
for (int i = 0; i < n - 1; i++) {
if ((s[i] == 'U' && s[i + 1] == 'R') || (s[i] == 'R' && s[i + 1] == 'U'))
i++, ans--;
}
cout << ans << endl;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
stack<char> q;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
char a;
cin >> a;
q.push(a);
for (int i = 2; i <= n; i++) {
cin >> a;
if ((q.top()) != 'd' && (q.top()) != a) {
q.pop();
q.push('d');
} else
q.push(a);
}
cout << q.size();
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import java.io.*;
import java.util.StringTokenizer;
public class Solution {
public static void solve(InputReader in, PrintWriter out, DebugWriter debug) throws IOException {
int n = in.nextInt();
String s = in.next();
int ans = 0;
for (int i = 0; i < s.length()-1; i++) {
if ((s.charAt(i) == 'U' && s.charAt(i+1) == 'R') ||
(s.charAt(i) == 'R' && s.charAt(i+1) == 'U')){
ans++;
i++;
}
}
out.println(n-ans);
}
public static final String NAME = "";
public static void main(String[] args) throws IOException{
InputReader in;
PrintWriter out;
DebugWriter debug;
if (args.length > 0 && args[0].equals("file")) {
in = new InputReader(new BufferedReader(new FileReader("input.txt")));
out = new PrintWriter(new FileWriter("output.txt"));
debug = new DebugWriter(true);
int sampleNumber = 1;
do {
String nextSample = "Sample #" + sampleNumber++ + ": ";
out.println(nextSample);
debug.println(nextSample);
solve(in, out, debug);
out.println("");
debug.println("");
} while (in.reader.readLine() != null);
} else {
if (NAME.length() > 0) {
in = new InputReader(new BufferedReader(new FileReader(NAME + ".in")));
out = new PrintWriter(new FileWriter(NAME + ".out"));
} else {
in = new InputReader(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(new OutputStreamWriter(System.out));
}
debug = new DebugWriter(false);
solve(in, out, debug);
}
in.reader.close();
out.close();
}
public static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(BufferedReader reader) {
this.reader = reader;
tokenizer = null;
}
public String next() throws IOException{
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
public int nextInt() throws IOException{
return Integer.parseInt(next());
}
public int[] nextIntArray(int n) throws IOException{
int[] array = new int[n];
for (int i=0; i<n; i++){
array[i] = nextInt();
}
return array;
}
}
public static class DebugWriter {
public boolean enable;
public DebugWriter(boolean enable){
this.enable = enable;
}
public void print(Object o){
if (enable) System.out.print(o);
}
public void println(Object o){
if (enable) System.out.println(o);
}
}
} | JAVA |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n = int(input())
s = str(input())
l = len(s)
prev = '';
for i in s:
if prev=='':
prev = i
continue
if (i=='U' and prev =='R') or (i=='R' and prev =='U'):
l=l-1
prev = ''
else:
prev = i
print(l) | PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, le;
cin >> n;
string s;
cin >> s;
le = s.length();
for (int i = 0; i < s.length(); i++) {
string sub = s.substr(i, 2);
if (sub == "RU" || sub == "UR") {
s.erase(i, 1);
s[i] = 'D';
}
}
cout << s.length() << endl;
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 7;
const int inf = 1e9;
const double eps = 0.0000000001;
int main() {
int n, r = 0, u = 0, ans = 0;
scanf("%d", &n);
string s;
cin >> s;
bool bas = 1;
for (int i = 0; i < n; i++) {
if (s[i] != s[i + 1]) {
ans++;
i++;
} else {
ans++;
}
}
printf("%d\n", ans);
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1000000;
int n, m, q, t[maxn], odp;
char s[maxn];
int main() {
scanf("%d%s", &n, s);
odp = n;
for (int i = 0; i < n - 1; i++)
if (s[i] != s[i + 1]) {
i++;
odp--;
}
printf("%d", odp);
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
int n;
cin >> n;
string s;
cin >> s;
int res = 0;
for (int i = 0; i < n; ++i) {
if (i < n - 1 && s[i] != s[i + 1]) i++;
res++;
}
cout << res << endl;
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, c = 0, C1 = 0;
cin >> n;
char ch[n];
for (int i = 0; i < n; i++) {
cin >> ch[i];
}
for (int i = 0; i < n; i++) {
if ((ch[i] == 'R' && ch[i + 1] == 'U') ||
(ch[i] == 'U' && ch[i + 1] == 'R')) {
C1++;
i++;
} else
c++;
}
cout << c + C1 << endl;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n = int(input())
s = input()
s += ' '
col = 0
i = 0
while i != n:
if (s[i] == 'U' and s[i+1] == 'R') or (s[i] == 'R' and s[i+1] == 'U'):
col += 1
i += 2
else:
i += 1
col += 1
print(col)
| PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | input()
s = list(input())
found = True
while found:
x = []
found = False
i = 0
while i < len(s)-1:
ss = [s[i], s[i+1]]
if 'R' in ss and 'U' in ss:
x.append('D')
i += 2
found = True
else:
x.append(s[i])
i += 1
if i < len(s):
x.append(s[i])
s = x
print(len(s)) | PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n=int(input())
l=list(input())
i=0
N=len(l)
x=0
while i<n-1:
if (l[i]=="U" and l[i+1]=="R") or (l[i]=="R" and l[i+1]=="U"):
x+=1
i+=1
N-=2
i+=1
print(N+x) | PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n=input()
s=input()
ans=len(s)
a=0
i=0
while i<len(s)-1:
if (s[i]=='U' and s[i+1]=='R') or (s[i]=='R' and s[i+1]=='U'):
a+=1
i+=1
i+=1
print(ans-a)
| PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | # import sys;sys.stdin = open("in.txt", "r");sys.stdout = open("out.txt", "w")
n = int(input())
ans = 0
s = input()
i = 0
while(i < n-1):
if((s[i] == s[i+1])):
i += 1
else:
i += 2
ans += 1
print(n - ans) | PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int x, y;
cin >> x;
y = x;
char c = '0', lc;
for (int i = 0; i < x; i++) {
lc = c;
cin >> c;
if ((lc != '0') && (lc != c)) {
y--;
c = '0';
}
}
cout << y << "\n";
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import re
input()
print(len(re.sub(r'UR|RU', 'D', input())))
| PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | def ras(n,a):
x,y=0,0
while(x!=-1 and y!=-1):
x=a.find('RU')
y=a.find('UR')
if(x==-1 and y!=-1):
a=a.replace(a[y]+a[y+1],'D',1)
continue
if(y==-1 and x!=-1):
a=a.replace(a[x]+a[x+1],'D',1)
continue
z=min(x,y)
if(z!=-1):
a=a.replace(a[z]+a[z+1],'D',1)
return(len(a))
n=int(input())
a=input()
r=ras(n,a)
print(r)
| PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import java.util.Scanner;
public class A{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String s = in.next();
int sum = s.length();
for(int i = 1; i < s.length(); i++){
if(s.charAt(i-1) == 'U' && s.charAt(i) == 'R'){
sum--;
i = i+1;
}
else if(s.charAt(i-1) == 'R' && s.charAt(i) == 'U'){
sum--;
i = i+1;
}
}
System.out.println(sum);
}
} | JAVA |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import java.io.*;
import java.util.Arrays;
import java.util.InputMismatchException;
public class TaskA
{
public static void main(String[] args)
{
new TaskA(System.in, System.out);
}
static class Solver implements Runnable
{
int n;
BufferedReader in;
// InputReader in;
PrintWriter out;
void solve() throws IOException
{
int up = 0;
int right = 0;
int n = Integer.parseInt(in.readLine());
char[] s = in.readLine().toCharArray();
int cnt = 0;
for (int i = 0; i < n - 1; i++)
{
if ((s[i] == 'U' && s[i + 1] == 'R') || (s[i] == 'R' && s[i + 1] == 'U'))
{
cnt++;
i++;
}
}
out.println(n - cnt);
}
void debug(Object... o)
{
System.err.println(Arrays.deepToString(o));
}
// uncomment below line to change to BufferedReader
public Solver(BufferedReader in, PrintWriter out)
// public Solver(InputReader in, PrintWriter out)
{
this.in = in;
this.out = out;
}
@Override
public void run()
{
try
{
solve();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
static class CMath
{
static long power(long number, long power)
{
if (number == 1 || number == 0 || power == 0)
return 1;
if (power == 1)
return number;
if (power % 2 == 0)
return power(number * number, power / 2);
else
return power(number * number, power / 2) * number;
}
static long modPower(long number, long power, long mod)
{
if (number == 1 || number == 0 || power == 0)
return 1;
number = mod(number, mod);
if (power == 1)
return number;
long square = mod(number * number, mod);
if (power % 2 == 0)
return modPower(square, power / 2, mod);
else
return mod(modPower(square, power / 2, mod) * number, mod);
}
static long moduloInverse(long number, long mod)
{
return modPower(number, mod - 2, mod);
}
static long mod(long number, long mod)
{
return number - (number / mod) * mod;
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
static long min(long... arr)
{
long min = arr[0];
for (int i = 1; i < arr.length; i++)
min = Math.min(min, arr[i]);
return min;
}
static long max(long... arr)
{
long max = arr[0];
for (int i = 1; i < arr.length; i++)
max = Math.max(max, arr[i]);
return max;
}
static int min(int... arr)
{
int min = arr[0];
for (int i = 1; i < arr.length; i++)
min = Math.min(min, arr[i]);
return min;
}
static int max(int... arr)
{
int max = arr[0];
for (int i = 1; i < arr.length; i++)
max = Math.max(max, arr[i]);
return max;
}
}
public TaskA(InputStream inputStream, OutputStream outputStream)
{
// uncomment below line to change to BufferedReader
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
// InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Thread thread = new Thread(null, new Solver(in, out), "TaskC", 1 << 29);
try
{
thread.start();
thread.join();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
try
{
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
out.flush();
out.close();
}
}
}
| JAVA |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, ans = 0, i;
string s;
cin >> n >> s;
for (i = 0; i < s.size() - 1; i++) {
if (s[i] == 'R' && s[i + 1] == 'U' || s[i] == 'U' && s[i + 1] == 'R')
ans++, i++;
else
ans++;
}
if (i != n) ans++;
cout << ans << endl;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
string s;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
char c;
scanf(" %c", &c);
if (s.empty())
s += c;
else if (s.back() != 'D' && s.back() != c)
s.back() = 'D';
else
s += c;
}
cout << s.size() << endl;
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | f=lambda:map(int,input().split())
n=int(input())
s=input()
i,c=1,0
while i<=n-1:
if s[i-1]+s[i]=='RU' or s[i-1]+s[i]=='UR':
c+=1
i+=2
else:
i+=1
print(n-c) | PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n=int(input())
s=input()
c,i=0,0
while i<n-1:
if s[i]!=s[i+1]:
i+=2
c+=1
else:
i+=1
print(n-c) | PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
bool check(string s) {
if (s == "ABC" || s == "ACB" || s == "BAC" || s == "BCA" || s == "CAB" ||
s == "CBA")
return 1;
return 0;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s;
long long n, cnt = 0;
;
cin >> n;
cin >> s;
for (long long i = 0; i < n - 1; i++) {
if (s[i] != s[i + 1]) {
cnt++;
i++;
}
}
cout << n - cnt << '\n';
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #----Kuzlyaev-Nikita-Codeforces-----
#------------03.04.2020-------------
alph="abcdefghijklmnopqrstuvwxyz"
#-----------------------------------
n=int(input())
a=list(str(input()))
for i in range(n-1):
if a[i]+a[i+1]=="RU":
a[i]="D";a[i+1]=""
continue
if a[i]+a[i+1]=="UR":
a[i]="D";a[i+1]=""
print(a.count("D")+a.count("R")+a.count("U"))
| PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import java.io.*;
import java.util.*;
public class MyClass {
public static void main(String args[]) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
int n = Integer.parseInt(br.readLine());
String str = br.readLine();int count=0;
for(int i=0;i<n;){
if(((i+1)<n)&&((str.charAt(i)=='U')&&(str.charAt(i+1)=='R')||(str.charAt(i)=='R')&&(str.charAt(i+1)=='U'))){
i++;i++;count++;
}
else{count++;i++;}
}
bw.write(""+count+"\n");bw.close();
}
}
| JAVA |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | length_sequence = int(raw_input())
sequence = raw_input()
newSequence = ""
i = 0
while(i < len(sequence)-1):
if(sequence[i] != sequence[i+1]):
sequence = sequence[:i] + "D" + sequence[i+2:]
i += 1
else:
i+=1
print(len(sequence)) | PYTHON |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
(ios::sync_with_stdio(false), cin.tie(NULL));
int n;
cin >> n;
double start = clock();
string seq;
cin >> seq;
int len(0);
for (int i = 0; i < n - 1; i++) {
if (seq[i] != seq[i + 1]) {
len++;
i += 1;
}
}
cout << n - len << "\n";
cerr << "[*] Time: " << (clock() - start) * 1.0 / CLOCKS_PER_SEC
<< std::setprecision(3) << " s"
<< "\n";
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n = int(input())
turns = input().strip()
c = 1
res = len(turns)
while c < (len(turns)):
if turns[c]!= turns[c-1]:
res -=1
c+=1
c+=1
print(res)
| PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n=int(input())
s=input()
sm=''
i=0
while(True):
if(i!=n-1 and (s[i:i+2]=='UR' or s[i:i+2]=='RU')):
sm+='D'
i+=1
else:
sm +=s[i]
if(i==n-1):
break
i+=1
print(len(sm)) | PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 |
input()
i,j=0,1
st=input()
ans=0
while j<len(st):
if st[i]+st[j]=="RU" or st[i]+st[j]=="UR":
i+=2
j+=2
ans+=1
else:
i+=1;j+=1
print(len(st)-ans)
| PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int k = 0, m, n;
string a;
cin >> n;
cin >> a;
for (int i = 0; i < n; i++) {
if (a[i] == 'U' && a[i + 1] == 'R' || a[i] == 'R' && a[i + 1] == 'U') {
k++;
i = i + 1;
}
}
cout << n - k;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int n;
int main() {
cin >> n;
string A;
cin >> A;
for (int i = 0; i < A.size() - 1; i++) {
if (A[i] != A[i + 1]) {
n--;
i++;
}
}
cout << n;
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int N = 102;
int n;
char a[N];
int main() {
cin >> n >> a;
int ans = 0;
for (int i = 0; i < n; ++i) {
if ((a[i] == 'U' && a[i + 1] == 'R') || (a[i] == 'R' && a[i + 1] == 'U')) {
++i;
}
++ans;
}
cout << ans << endl;
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author grolegor
*/
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);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int n = in.nextInt();
String s = in.next();
int ans = 0;
for (int i = 0; i < n; i++) {
char curr = s.charAt(i);
if (i + 1 < n) {
char next = s.charAt(i + 1);
if (curr != next) {
i++;
}
}
ans++;
}
out.println(ans);
}
}
}
| JAVA |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n = int(input())
seq = list(input())
new = ''
i = 0
while i<n:
if i == n-1:
new += seq[i]
i += 1
continue
if seq[i]=='R' and seq[i+1] == 'U' or seq[i]=='U' and seq[i+1]=='R':
new += 'D'
i += 1
else:
new += seq[i]
i += 1
print(len(new)) | PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import java.util.*;
public class dark {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
String s = scan.next();
char[] arr = s.toCharArray();
int answer = 0;
int i =0;
while( i < N) {
if(i <= N-2 && arr[i] == 'U' && arr[i+1] == 'R') {
i=i+2;
}else if(i <= N-2 && arr[i] == 'R' && arr[i+1] == 'U') {
i=i+2;
}else {
i=i+1;
}answer++;
}
System.out.println(answer);
}
}
| JAVA |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int N = 1000;
int n;
char s[N];
int main() {
scanf("%d", &n);
scanf("%s", s + 1);
int ans = 0;
for (int i = 1; i < n; i++) {
if (s[i] != s[i + 1]) {
ans++;
i++;
}
}
printf("%d\n", n - ans);
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #coding: utf-8
entrada1 = int(raw_input())
entrada2 = raw_input()
count = 0
i = 0
anterior = ""
while i < entrada1 :
if entrada2[i] == "U" and anterior == "R":
count += 1
i += 1
if entrada2[i] == "R" and anterior == "U":
count += 1
i += 1
if i < entrada1:
anterior = entrada2[i]
i += 1
print entrada1 - count | PYTHON |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n = int(input())
s = input()
i = 0
c = 0
while i < n - 1:
if s[i] != s[i + 1]:
c += 1
i += 2
else:
i += 1
print(n - c) | PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import re
n = int(input()) ;s = input()
s = re.sub('(RU|UR)', 'D', s)
a = len(s)
print(a) | PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import java.util.*;
import java.io.*;
public class Main
{
public static void main(String... args) throws IOException
{
Scanner sc=new Scanner(System.in);
/*Scanner sc=new Scanner(new FileReader("./input.txt"));
System.setOut(new PrintStream(new File("./output.txt")));*/
int n,i;
n=Integer.parseInt(sc.next());
char a[]=sc.next().toCharArray();
for(i=0;i<n-1;i++){ if(((a[i]=='R')&&(a[i+1]=='U'))||((a[i]=='U')&&(a[i+1]=='R'))) {a[i]='D'; a[i+1]='0';}}
//System.out.println(new String(a));
int ans=0;
for(i=0;i<n;i++) if(a[i]!='0') ans+=1;
System.out.printf("%d\n",ans);
/*int tt,W,H,w,h,x1,x2,y1,y2;
int n,i,j,k;
tt=sc.nextInt();
while((tt--)>0)
{
W=sc.nextInt();H=sc.nextInt();x1=sc.nextInt();y1=sc.nextInt();x2=sc.nextInt();y2=sc.nextInt();w=sc.nextInt();h=sc.nextInt();
System.out.println(W+"\n"+H+"saria"+x1+" "+y1+" "+x2+" "+" "+y2+" "+w+"\n"+h);
} */
}
}
| JAVA |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n = int(input())
s = list(input())
for i in range(n - 1):
if s[i] == 'U' and s[i + 1] == 'R':
s[i] = 'D'
s[i + 1] = ''
if s[i] == 'R' and s[i + 1] == 'U':
s[i] = 'D'
s[i + 1] = ''
print(len(''.join(s)))
| PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n=int(input())
str1=input()
i=0;count=0
while(i<n):
if str1[i:i+2]=="RU" or str1[i:i+2]=="UR":
count+=1
i+=2
else:
i+=1
print(len(str1)-count) | PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | # Diagonal Walking
n = int(raw_input())
word = raw_input()
soma = len(word)
i = 0
j = 1
while i < len(word)-1:
if word[i] != word[j]:
soma -= 1
j += 2
i += 2
else:
j += 1
i += 1
print soma
| PYTHON |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import java.util.*;
// Compiler version JDK 11.0.2
public class Dcoder
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
String s=sc.next();
String h="";
int c=0;
for(int i=0; i<s.length()-1;){
if(s.charAt(i) =='U' && s.charAt(i+1) =='R'){
c++;
i=i+2;
}else if(s.charAt(i) =='R' && s.charAt(i+1) =='U'){
c++;
i=i+2;
}else{
i++;
}
}
System. out. println(n-c);
}
}
| JAVA |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
sc.nextLine();
String str = sc.nextLine();
StringBuilder b = new StringBuilder(str);
int a = 0;
for(int i = 1;i < x;i++)
if(b.charAt(i) == 'R' && b.charAt(i - 1) == 'U' || b.charAt(i) == 'U' && b.charAt(i - 1) == 'R') {
b.setCharAt(i, 'D');
a++;
}
System.out.println(x - a);
}
}
| JAVA |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
int main() {
int n, i, s = 0;
std ::cin >> n;
std ::string m;
std ::cin >> m;
for (i = 0; i < n; ++i) {
if ((m[i] == 'R' && m[i + 1] == 'U') || (m[i] == 'U' && m[i + 1] == 'R')) {
++i;
++s;
}
}
std ::cout << n - s << "\n";
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | ln = int(input())
a = [str(x) for x in input()]
for i, t in enumerate(a):
if i == (ln-1):
break
if t == 'U':
if a[i+1] == 'R':
a[i+1] = 'D'
a[i] = 0
elif t == 'R':
if a[i+1] == 'U':
a[i+1] = 'D'
a[i] = 0
count = 0
for i in a:
if i != 0:
count += 1
print(count)
| PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n = int(input())
s = input()
i = 0
count = 0
while i < n:
if s[i - 1] != s[i]:
count += 1
i += 1
i += 1
print(n - count)
| PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | def shortcut(s):
c,i=0,0
while(i<len(s)-1):
if s[i]=='R':
if s[i+1]=='U':
c+=1
i+=2
else:
i+=1
elif s[i]=='U':
if s[i+1]=='R':
c+=1
i+=2
else:
i+=1
print(len(s)-c)
a=int(input())
s=input()
shortcut(s) | PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
void solve(string s) {
int l = s.length();
for (int i = 0; i < s.length() - 1; i++) {
if (s[i] == 'R' && s[i + 1] == 'U' || s[i] == 'U' && s[i + 1] == 'R') {
l--;
i++;
}
}
cout << l;
}
int n;
string s;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> s;
solve(s);
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.