output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
vector<int> a;
int aa[20];
void search(int x) {
if (x != 0) a.push_back(x);
if (x > 100000000) return;
search(x * 10 + 4);
search(x * 10 + 7);
}
int islucky(int x) {
char tmp[20];
int i, l;
sprintf(tmp, "%d", x);
l = strlen(tmp);
for (i = 0; i < l; i++)
if ((tmp[i] != '4') && (tmp[i] != '7')) return 0;
return 1;
}
int main() {
int st, cnt, n, k, i, j, ans;
int used[20];
long long tmp;
a.clear();
search(0);
sort(a.begin(), a.end());
scanf("%d%d", &n, &k);
tmp = 1;
for (i = 1;; i++) {
tmp *= i;
if (tmp >= k) break;
}
if (n < i) {
printf("-1\n");
return 0;
}
ans = 0;
st = n - i;
cnt = i;
for (i = 0; i < a.size(); i++)
if (a[i] <= st) ans++;
st++;
memset(used, 0, sizeof(used));
for (i = 0; i < cnt; i++) {
tmp /= (cnt - i);
for (j = 0; j < cnt; j++)
if (used[j] == 0) {
if (k <= tmp) {
used[j] = 1;
break;
}
k -= tmp;
}
if ((islucky(st + i) == 1) && (islucky(j + st) == 1)) {
ans++;
}
}
printf("%d\n", ans);
return 0;
}
| ### Prompt
Create a solution in cpp for the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> a;
int aa[20];
void search(int x) {
if (x != 0) a.push_back(x);
if (x > 100000000) return;
search(x * 10 + 4);
search(x * 10 + 7);
}
int islucky(int x) {
char tmp[20];
int i, l;
sprintf(tmp, "%d", x);
l = strlen(tmp);
for (i = 0; i < l; i++)
if ((tmp[i] != '4') && (tmp[i] != '7')) return 0;
return 1;
}
int main() {
int st, cnt, n, k, i, j, ans;
int used[20];
long long tmp;
a.clear();
search(0);
sort(a.begin(), a.end());
scanf("%d%d", &n, &k);
tmp = 1;
for (i = 1;; i++) {
tmp *= i;
if (tmp >= k) break;
}
if (n < i) {
printf("-1\n");
return 0;
}
ans = 0;
st = n - i;
cnt = i;
for (i = 0; i < a.size(); i++)
if (a[i] <= st) ans++;
st++;
memset(used, 0, sizeof(used));
for (i = 0; i < cnt; i++) {
tmp /= (cnt - i);
for (j = 0; j < cnt; j++)
if (used[j] == 0) {
if (k <= tmp) {
used[j] = 1;
break;
}
k -= tmp;
}
if ((islucky(st + i) == 1) && (islucky(j + st) == 1)) {
ans++;
}
}
printf("%d\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
bool lucky(int x) {
while (x) {
if (x % 10 != 4 && x % 10 != 7) break;
x /= 10;
}
if (x == 0)
return true;
else
return false;
}
int fact(int n) {
int x = 1, i;
for (i = 1; i <= n; i++) x *= i;
return x;
}
int a[10000], b[100];
int main() {
int n, k, p = 0, q = 2, r = 2, sum = 0, i, j;
long long x = 1;
scanf("%d %d", &n, &k);
a[0] = 4;
a[1] = 7;
for (i = 0; i < 8; i++) {
for (j = p; j < q; j++) {
a[r++] = a[j] * 10 + 4;
a[r++] = a[j] * 10 + 7;
}
p = q;
q = r;
}
for (i = 1; i <= n; i++) {
x *= i;
if (x >= k) break;
}
if (i > n) {
puts("-1");
return 0;
}
for (j = 0; j < r; j++) {
if (a[j] > n - i) break;
sum++;
}
q = 0;
for (j = n - i + 1; j <= n; j++) b[q++] = j;
for (j = n - i + 1; j < n; j++) {
p = k / fact(n - j);
if (k % fact(n - j) == 0) p--;
if (lucky(j) == 1 && lucky(b[p]) == 1) sum++;
for (i = p; i < q; i++) b[i] = b[i + 1];
k -= p * fact(n - j);
}
if (lucky(n) == 1 && lucky(b[0]) == 1) sum++;
printf("%d\n", sum);
return 0;
}
| ### Prompt
Please create a solution in CPP to the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
bool lucky(int x) {
while (x) {
if (x % 10 != 4 && x % 10 != 7) break;
x /= 10;
}
if (x == 0)
return true;
else
return false;
}
int fact(int n) {
int x = 1, i;
for (i = 1; i <= n; i++) x *= i;
return x;
}
int a[10000], b[100];
int main() {
int n, k, p = 0, q = 2, r = 2, sum = 0, i, j;
long long x = 1;
scanf("%d %d", &n, &k);
a[0] = 4;
a[1] = 7;
for (i = 0; i < 8; i++) {
for (j = p; j < q; j++) {
a[r++] = a[j] * 10 + 4;
a[r++] = a[j] * 10 + 7;
}
p = q;
q = r;
}
for (i = 1; i <= n; i++) {
x *= i;
if (x >= k) break;
}
if (i > n) {
puts("-1");
return 0;
}
for (j = 0; j < r; j++) {
if (a[j] > n - i) break;
sum++;
}
q = 0;
for (j = n - i + 1; j <= n; j++) b[q++] = j;
for (j = n - i + 1; j < n; j++) {
p = k / fact(n - j);
if (k % fact(n - j) == 0) p--;
if (lucky(j) == 1 && lucky(b[p]) == 1) sum++;
for (i = p; i < q; i++) b[i] = b[i + 1];
k -= p * fact(n - j);
}
if (lucky(n) == 1 && lucky(b[0]) == 1) sum++;
printf("%d\n", sum);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T pow2(T a) {
return a * a;
}
template <class T>
inline bool mineq(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
vector<long long> luck;
long long fact[20];
bool islucky(long long n) {
while (n) {
if (n % 10 != 4 && n % 10 != 7) return false;
n /= 10;
}
return true;
}
long long asf(long long n, long long k, long long pref) {
bool used[20] = {};
long long ans = 0;
if (fact[n] < k) return -1;
for (int i = 0; i < n; i++) {
for (int j = 1; j < n + 1; j++)
if (!used[j]) {
if (fact[n - i - 1] < k) {
k -= fact[n - i - 1];
} else {
used[j] = true;
if (islucky(pref + i + 1) && islucky(pref + j)) ans++;
break;
}
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
luck.push_back(4);
luck.push_back(7);
int m = 0;
for (int i = 0; i < 10; i++) {
int n = luck.size();
for (int j = m; j < n; j++) {
luck.push_back(luck[j] * 10 + 4);
luck.push_back(luck[j] * 10 + 7);
}
m = n;
}
sort(luck.begin(), luck.end());
fact[0] = 1;
for (int i = 1; i < 16; i++) fact[i] = i * fact[i - 1];
long long n, k;
cin >> n >> k;
if (n <= 15) {
cout << asf(n, k, 0);
} else {
long long t = asf(15, k, n - 15);
if (t != -1) {
int k = upper_bound(luck.begin(), luck.end(), (n - 15)) - luck.begin();
cout << t + k;
} else
cout << t;
}
}
| ### Prompt
Generate a cpp solution to the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T pow2(T a) {
return a * a;
}
template <class T>
inline bool mineq(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
vector<long long> luck;
long long fact[20];
bool islucky(long long n) {
while (n) {
if (n % 10 != 4 && n % 10 != 7) return false;
n /= 10;
}
return true;
}
long long asf(long long n, long long k, long long pref) {
bool used[20] = {};
long long ans = 0;
if (fact[n] < k) return -1;
for (int i = 0; i < n; i++) {
for (int j = 1; j < n + 1; j++)
if (!used[j]) {
if (fact[n - i - 1] < k) {
k -= fact[n - i - 1];
} else {
used[j] = true;
if (islucky(pref + i + 1) && islucky(pref + j)) ans++;
break;
}
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
luck.push_back(4);
luck.push_back(7);
int m = 0;
for (int i = 0; i < 10; i++) {
int n = luck.size();
for (int j = m; j < n; j++) {
luck.push_back(luck[j] * 10 + 4);
luck.push_back(luck[j] * 10 + 7);
}
m = n;
}
sort(luck.begin(), luck.end());
fact[0] = 1;
for (int i = 1; i < 16; i++) fact[i] = i * fact[i - 1];
long long n, k;
cin >> n >> k;
if (n <= 15) {
cout << asf(n, k, 0);
} else {
long long t = asf(15, k, n - 15);
if (t != -1) {
int k = upper_bound(luck.begin(), luck.end(), (n - 15)) - luck.begin();
cout << t + k;
} else
cout << t;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1E-7;
long long lt[10000], tot, fa[15];
long long n, m;
int b[20];
void work(int x, long long w, int dp) {
if (x == dp) {
lt[tot++] = w;
return;
}
work(x + 1, w * 10 + 4, dp);
work(x + 1, w * 10 + 7, dp);
}
bool lucky(long long u) {
if (u == 0) return false;
while (u > 0) {
if (u % 10 != 4 && u % 10 != 7) return false;
u /= 10;
}
return true;
}
int main() {
tot = 0;
for (int i = 1; i < 11; ++i) work(0, 0, i);
fa[0] = 1;
for (int i = 1; i < 15; ++i) fa[i] = fa[i - 1] * i;
cin >> n >> m;
long long ans = 0;
if (n < 14 && m > fa[n]) {
cout << -1 << endl;
return 0;
}
int l;
--m;
for (l = 14; fa[l] > m; --l)
;
for (int i = 0; lt[i] < n - l; ++i, ++ans)
;
for (int k = l + 1; k > 0; --k) {
int t = m / fa[k - 1];
int cur = 0;
while (b[cur]) ++cur;
for (int i = 0; i < t; ++i) do {
++cur;
} while (b[cur]);
b[cur] = 1;
if (lucky(n - k + 1) && lucky(cur + n - l)) ++ans;
m %= fa[k - 1];
}
cout << ans << endl;
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1E-7;
long long lt[10000], tot, fa[15];
long long n, m;
int b[20];
void work(int x, long long w, int dp) {
if (x == dp) {
lt[tot++] = w;
return;
}
work(x + 1, w * 10 + 4, dp);
work(x + 1, w * 10 + 7, dp);
}
bool lucky(long long u) {
if (u == 0) return false;
while (u > 0) {
if (u % 10 != 4 && u % 10 != 7) return false;
u /= 10;
}
return true;
}
int main() {
tot = 0;
for (int i = 1; i < 11; ++i) work(0, 0, i);
fa[0] = 1;
for (int i = 1; i < 15; ++i) fa[i] = fa[i - 1] * i;
cin >> n >> m;
long long ans = 0;
if (n < 14 && m > fa[n]) {
cout << -1 << endl;
return 0;
}
int l;
--m;
for (l = 14; fa[l] > m; --l)
;
for (int i = 0; lt[i] < n - l; ++i, ++ans)
;
for (int k = l + 1; k > 0; --k) {
int t = m / fa[k - 1];
int cur = 0;
while (b[cur]) ++cur;
for (int i = 0; i < t; ++i) do {
++cur;
} while (b[cur]);
b[cur] = 1;
if (lucky(n - k + 1) && lucky(cur + n - l)) ++ans;
m %= fa[k - 1];
}
cout << ans << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<long long> tail;
vector<long long> used;
long long n, k;
bool isGood(long long x) {
while (x > 0) {
if (x % 10 != 7 && x % 10 != 4) return false;
x /= 10;
}
return true;
}
vector<long long> v;
int main() {
long long i, j;
long long x;
long long to = 4444444444LL;
v.push_back(to);
long long bits;
long long tmp;
for ((bits) = (1); (bits) < (10); ++(bits))
for ((i) = (0); (i) < (1 << bits); ++(i)) {
j = i;
x = 0;
for ((tmp) = (0); (tmp) < (bits); ++(tmp)) {
if (j & 1)
x = x * 10 + 7;
else
x = x * 10 + 4;
j >>= 1;
}
v.push_back((long long)x);
}
sort(v.begin(), v.end());
cin >> n;
cin >> k;
x = 1;
long long fact = 1;
while (fact < k) {
++x;
fact *= x;
}
if (x > n) {
puts("-1");
return 0;
}
--k;
fact /= x;
--x;
tail.push_back(k / fact);
used.push_back(k / fact);
k %= fact;
long long top;
while (x > 0) {
fact /= x;
--x;
top = k / fact;
for (i = 0; i < used.size(); ++i)
if (used[i] <= top) {
++top;
} else
break;
tail.push_back(top);
used.push_back(top);
sort(used.begin(), used.end());
k %= fact;
}
long long ts = tail.size();
for (i = 0; i < tail.size(); ++i) tail[i] = n + tail[i] - tail.size() + 1;
long long ans = 0;
for ((i) = (0); (i) < (v.size()); ++(i))
if (n - ts < v[i]) break;
ans = i;
long long fr = n - ts + 1;
for (i = 0; i < ts; ++i)
if (isGood(i + fr) && isGood(tail[i])) ++ans;
cout << ans << endl;
return 0;
}
| ### Prompt
In CPP, your task is to solve the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<long long> tail;
vector<long long> used;
long long n, k;
bool isGood(long long x) {
while (x > 0) {
if (x % 10 != 7 && x % 10 != 4) return false;
x /= 10;
}
return true;
}
vector<long long> v;
int main() {
long long i, j;
long long x;
long long to = 4444444444LL;
v.push_back(to);
long long bits;
long long tmp;
for ((bits) = (1); (bits) < (10); ++(bits))
for ((i) = (0); (i) < (1 << bits); ++(i)) {
j = i;
x = 0;
for ((tmp) = (0); (tmp) < (bits); ++(tmp)) {
if (j & 1)
x = x * 10 + 7;
else
x = x * 10 + 4;
j >>= 1;
}
v.push_back((long long)x);
}
sort(v.begin(), v.end());
cin >> n;
cin >> k;
x = 1;
long long fact = 1;
while (fact < k) {
++x;
fact *= x;
}
if (x > n) {
puts("-1");
return 0;
}
--k;
fact /= x;
--x;
tail.push_back(k / fact);
used.push_back(k / fact);
k %= fact;
long long top;
while (x > 0) {
fact /= x;
--x;
top = k / fact;
for (i = 0; i < used.size(); ++i)
if (used[i] <= top) {
++top;
} else
break;
tail.push_back(top);
used.push_back(top);
sort(used.begin(), used.end());
k %= fact;
}
long long ts = tail.size();
for (i = 0; i < tail.size(); ++i) tail[i] = n + tail[i] - tail.size() + 1;
long long ans = 0;
for ((i) = (0); (i) < (v.size()); ++(i))
if (n - ts < v[i]) break;
ans = i;
long long fr = n - ts + 1;
for (i = 0; i < ts; ++i)
if (isGood(i + fr) && isGood(tail[i])) ++ans;
cout << ans << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int Len = 2333333;
char buf[Len], *p1 = buf, *p2 = buf, duf[Len], *q1 = duf;
inline char gc();
inline int rd();
inline void pc(char c);
inline void rt(int x);
inline void flush();
int n, k, t, K, ans, f[15], p[15];
vector<int> V;
inline int check(int x) {
while (x)
if (x % 10 != 4 && x % 10 != 7)
return 0;
else
x /= 10;
return 1;
}
inline void Dfs(int x, int d) {
if (x > 9 || d > n) return;
if (x)
if (d < n - t)
++ans;
else if (check(p[n - d]))
++ans;
Dfs(x + 1, d * 10 + 4), Dfs(x + 1, d * 10 + 7);
}
int main() {
n = rd(), K = k = rd(), f[0] = 1;
for (register int i = 1; i <= 12; ++i) f[i] = f[i - 1] * i;
if (n < 13 && k > f[n]) return printf("-1"), 0;
while (k >= f[t] && t < 13) ++t;
--t, --k;
for (register int i = n - t; i <= n; ++i) V.push_back(i);
for (register int i = t; i >= 0; --i) {
int a = k / f[i], b = k % f[i];
k = b, p[i] = V[a], V.erase(V.begin() + a);
}
Dfs(0, 0), rt(ans);
return flush(), 0;
}
inline char gc() {
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, Len, stdin), p1 == p2)
? -1
: *p1++;
}
inline int rd() {
char c;
int f = 1;
while (!isdigit(c = gc()) && c != '-')
;
c == '-' ? f = -1, c = gc() : 0;
int x = c ^ 48;
while (isdigit(c = gc())) x = ((x + (x << 2)) << 1) + (c ^ 48);
return x * f;
}
inline void pc(char c) {
q1 == duf + Len &&fwrite(q1 = duf, 1, Len, stdout), *q1++ = c;
}
inline void rt(int x) {
x < 0 ? pc('-'), x = -x : 0, pc((x >= 10 ? rt(x / 10), x % 10 : x) + 48);
}
inline void flush() { fwrite(duf, 1, q1 - duf, stdout); }
| ### Prompt
Your challenge is to write a cpp solution to the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int Len = 2333333;
char buf[Len], *p1 = buf, *p2 = buf, duf[Len], *q1 = duf;
inline char gc();
inline int rd();
inline void pc(char c);
inline void rt(int x);
inline void flush();
int n, k, t, K, ans, f[15], p[15];
vector<int> V;
inline int check(int x) {
while (x)
if (x % 10 != 4 && x % 10 != 7)
return 0;
else
x /= 10;
return 1;
}
inline void Dfs(int x, int d) {
if (x > 9 || d > n) return;
if (x)
if (d < n - t)
++ans;
else if (check(p[n - d]))
++ans;
Dfs(x + 1, d * 10 + 4), Dfs(x + 1, d * 10 + 7);
}
int main() {
n = rd(), K = k = rd(), f[0] = 1;
for (register int i = 1; i <= 12; ++i) f[i] = f[i - 1] * i;
if (n < 13 && k > f[n]) return printf("-1"), 0;
while (k >= f[t] && t < 13) ++t;
--t, --k;
for (register int i = n - t; i <= n; ++i) V.push_back(i);
for (register int i = t; i >= 0; --i) {
int a = k / f[i], b = k % f[i];
k = b, p[i] = V[a], V.erase(V.begin() + a);
}
Dfs(0, 0), rt(ans);
return flush(), 0;
}
inline char gc() {
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, Len, stdin), p1 == p2)
? -1
: *p1++;
}
inline int rd() {
char c;
int f = 1;
while (!isdigit(c = gc()) && c != '-')
;
c == '-' ? f = -1, c = gc() : 0;
int x = c ^ 48;
while (isdigit(c = gc())) x = ((x + (x << 2)) << 1) + (c ^ 48);
return x * f;
}
inline void pc(char c) {
q1 == duf + Len &&fwrite(q1 = duf, 1, Len, stdout), *q1++ = c;
}
inline void rt(int x) {
x < 0 ? pc('-'), x = -x : 0, pc((x >= 10 ? rt(x / 10), x % 10 : x) + 48);
}
inline void flush() { fwrite(duf, 1, q1 - duf, stdout); }
``` |
#include <bits/stdc++.h>
using namespace std;
void JIZZ() {
cout << "";
exit(0);
}
const long double PI = 3.14159265358979323846264338327950288;
const long double eps = 1e-13;
const long long mod = 1e9 + 7;
vector<long long> luc;
void init() {
for (int dg = 1; dg <= 10; ++dg) {
for (int i = 0; i < (1 << dg); ++i) {
long long x = 0;
for (int j = dg - 1; j >= 0; --j) {
if (i & (1 << j)) {
x = x * 10 + 7;
} else
x = x * 10 + 4;
}
luc.push_back(x);
}
}
}
long long fac[15], a[15], ori[15];
int main() {
init();
ios_base::sync_with_stdio(0);
cin.tie(0);
fac[0] = 1;
for (int i = 1; i <= 13; ++i) {
fac[i] = fac[i - 1] * i;
}
int n, k;
cin >> n >> k;
if (n <= 13 && k > fac[n]) return cout << "-1" << endl, 0;
if (k == 1) {
auto it = upper_bound(luc.begin(), luc.end(), n);
cout << it - luc.begin() << endl;
exit(0);
}
int move = 1;
while (fac[move] < k) ++move;
;
;
int fix = n - move;
int ans = upper_bound(luc.begin(), luc.end(), fix) - luc.begin();
;
;
vector<long long> candi;
for (int i = fix + 1, ptr = 0; i <= n; ++i, ++ptr)
ori[ptr] = i, candi.push_back(i);
;
;
--k;
for (int i = move, ptr = 0; i > 0; --i, ++ptr) {
int it = k / fac[i - 1];
;
;
a[ptr] = candi[it];
candi.erase(candi.begin() + it);
;
;
k %= fac[i - 1];
}
for (int i = 0; i < move; ++i)
if (*lower_bound(luc.begin(), luc.end(), a[i]) == a[i] &&
*lower_bound(luc.begin(), luc.end(), fix + i + 1) == fix + i + 1)
++ans;
cout << ans << endl;
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void JIZZ() {
cout << "";
exit(0);
}
const long double PI = 3.14159265358979323846264338327950288;
const long double eps = 1e-13;
const long long mod = 1e9 + 7;
vector<long long> luc;
void init() {
for (int dg = 1; dg <= 10; ++dg) {
for (int i = 0; i < (1 << dg); ++i) {
long long x = 0;
for (int j = dg - 1; j >= 0; --j) {
if (i & (1 << j)) {
x = x * 10 + 7;
} else
x = x * 10 + 4;
}
luc.push_back(x);
}
}
}
long long fac[15], a[15], ori[15];
int main() {
init();
ios_base::sync_with_stdio(0);
cin.tie(0);
fac[0] = 1;
for (int i = 1; i <= 13; ++i) {
fac[i] = fac[i - 1] * i;
}
int n, k;
cin >> n >> k;
if (n <= 13 && k > fac[n]) return cout << "-1" << endl, 0;
if (k == 1) {
auto it = upper_bound(luc.begin(), luc.end(), n);
cout << it - luc.begin() << endl;
exit(0);
}
int move = 1;
while (fac[move] < k) ++move;
;
;
int fix = n - move;
int ans = upper_bound(luc.begin(), luc.end(), fix) - luc.begin();
;
;
vector<long long> candi;
for (int i = fix + 1, ptr = 0; i <= n; ++i, ++ptr)
ori[ptr] = i, candi.push_back(i);
;
;
--k;
for (int i = move, ptr = 0; i > 0; --i, ++ptr) {
int it = k / fac[i - 1];
;
;
a[ptr] = candi[it];
candi.erase(candi.begin() + it);
;
;
k %= fac[i - 1];
}
for (int i = 0; i < move; ++i)
if (*lower_bound(luc.begin(), luc.end(), a[i]) == a[i] &&
*lower_bound(luc.begin(), luc.end(), fix + i + 1) == fix + i + 1)
++ans;
cout << ans << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1E9 + 7, INF = 2E18 + 5;
const double PI = 2 * acos(0.0);
const long double EPS = 1.0E-14;
long long arrFact[15];
vector<long long> vecInit, vec;
long long n, k, ans;
void make_Permutation() {
vec.push_back(0);
long long ind, per, sz, len;
len = vecInit.size();
for (int i = 0; i < len; i++) {
ind = 0, per = vecInit.size() - 1, sz = vecInit.size();
while (k > arrFact[per]) {
k -= arrFact[per];
ind++;
ind %= sz;
}
vec.push_back(vecInit[ind]);
vecInit.erase(vecInit.begin() + ind);
}
return;
}
bool isLucky(long long num) {
long long val;
while (num > 0) {
val = num % 10;
num /= 10;
if (val != 4 and val != 7) {
return 0;
}
}
return 1;
}
void luckyNumber(long long lim, long long num) {
if (num > lim) return;
ans++;
luckyNumber(lim, num * 10 + 4);
luckyNumber(lim, num * 10 + 7);
return;
}
int main() {
arrFact[0] = arrFact[1] = 1;
for (int i = 2; i <= 14; i++) {
arrFact[i] = i * arrFact[i - 1];
}
ans = 0;
long long ind;
cin >> n >> k;
if (n >= 13) {
luckyNumber(n - 13, 0);
ans--;
for (int i = n - 13 + 1; i <= n; i++) {
vecInit.push_back(i);
}
make_Permutation();
ind = n - 13;
for (int i = 1; i < vec.size(); i++) {
if (isLucky(i + ind) and isLucky(vec[i])) {
ans++;
}
}
cout << ans;
} else {
if (arrFact[n] < k)
cout << "-1\n";
else {
for (int i = 1; i <= n; i++) {
vecInit.push_back(i);
}
make_Permutation();
for (int i = 1; i < vec.size(); i++) {
if (isLucky(i) and isLucky(vec[i])) {
ans++;
}
}
cout << ans;
}
}
return 0;
}
| ### Prompt
Create a solution in cpp for the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1E9 + 7, INF = 2E18 + 5;
const double PI = 2 * acos(0.0);
const long double EPS = 1.0E-14;
long long arrFact[15];
vector<long long> vecInit, vec;
long long n, k, ans;
void make_Permutation() {
vec.push_back(0);
long long ind, per, sz, len;
len = vecInit.size();
for (int i = 0; i < len; i++) {
ind = 0, per = vecInit.size() - 1, sz = vecInit.size();
while (k > arrFact[per]) {
k -= arrFact[per];
ind++;
ind %= sz;
}
vec.push_back(vecInit[ind]);
vecInit.erase(vecInit.begin() + ind);
}
return;
}
bool isLucky(long long num) {
long long val;
while (num > 0) {
val = num % 10;
num /= 10;
if (val != 4 and val != 7) {
return 0;
}
}
return 1;
}
void luckyNumber(long long lim, long long num) {
if (num > lim) return;
ans++;
luckyNumber(lim, num * 10 + 4);
luckyNumber(lim, num * 10 + 7);
return;
}
int main() {
arrFact[0] = arrFact[1] = 1;
for (int i = 2; i <= 14; i++) {
arrFact[i] = i * arrFact[i - 1];
}
ans = 0;
long long ind;
cin >> n >> k;
if (n >= 13) {
luckyNumber(n - 13, 0);
ans--;
for (int i = n - 13 + 1; i <= n; i++) {
vecInit.push_back(i);
}
make_Permutation();
ind = n - 13;
for (int i = 1; i < vec.size(); i++) {
if (isLucky(i + ind) and isLucky(vec[i])) {
ans++;
}
}
cout << ans;
} else {
if (arrFact[n] < k)
cout << "-1\n";
else {
for (int i = 1; i <= n; i++) {
vecInit.push_back(i);
}
make_Permutation();
for (int i = 1; i < vec.size(); i++) {
if (isLucky(i) and isLucky(vec[i])) {
ans++;
}
}
cout << ans;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
inline bool isgood(int a) {
while (a)
if ((a % 10 != 4) && (a % 10 != 7))
return false;
else
a /= 10;
return true;
}
inline int countgood(int r) {
int ans = 0, b[10];
long long a = 0;
for (int i = 0; i < 10; i++) b[i] = 0;
for (;;) {
int i = 0;
while (b[i] == 7) {
b[i] = 4;
i++;
}
b[i] = (b[i] == 0) ? 4 : 7;
for (i = 9, a = 0; i >= 0; i--) a = a * 10 + b[i];
if (a > r) break;
ans++;
}
return ans;
}
int f[14], v[14], t[14];
int main() {
f[0] = 1;
for (int i = 1; i <= 13; i++) f[i] = f[i - 1] * i;
int n, k;
cin >> n >> k;
k--;
if (n <= 13) {
if (f[n] <= k) {
cout << -1;
return 0;
}
for (int i = 0; i < n; i++) v[i] = i + 1;
for (int i = n; i; i--) {
int j = k / f[i - 1];
k %= f[i - 1];
t[n - i + 1] = v[j];
for (; j < i - 1; j++) v[j] = v[j + 1];
}
int ans = 0;
for (int i = 1; i <= n; i++)
if (isgood(i) && isgood(t[i])) ans++;
cout << ans;
return 0;
}
for (int i = 0; i < 13; i++) v[i] = n - 12 + i;
for (int i = 13; i; i--) {
int j = k / f[i - 1];
k %= f[i - 1];
t[14 - i] = v[j];
for (; j < i - 1; j++) v[j] = v[j + 1];
}
int ans = 0;
for (int i = 1; i <= 13; i++)
if (isgood(n - 13 + i) && isgood(t[i])) ans++;
ans += countgood(n - 13);
cout << ans;
return 0;
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline bool isgood(int a) {
while (a)
if ((a % 10 != 4) && (a % 10 != 7))
return false;
else
a /= 10;
return true;
}
inline int countgood(int r) {
int ans = 0, b[10];
long long a = 0;
for (int i = 0; i < 10; i++) b[i] = 0;
for (;;) {
int i = 0;
while (b[i] == 7) {
b[i] = 4;
i++;
}
b[i] = (b[i] == 0) ? 4 : 7;
for (i = 9, a = 0; i >= 0; i--) a = a * 10 + b[i];
if (a > r) break;
ans++;
}
return ans;
}
int f[14], v[14], t[14];
int main() {
f[0] = 1;
for (int i = 1; i <= 13; i++) f[i] = f[i - 1] * i;
int n, k;
cin >> n >> k;
k--;
if (n <= 13) {
if (f[n] <= k) {
cout << -1;
return 0;
}
for (int i = 0; i < n; i++) v[i] = i + 1;
for (int i = n; i; i--) {
int j = k / f[i - 1];
k %= f[i - 1];
t[n - i + 1] = v[j];
for (; j < i - 1; j++) v[j] = v[j + 1];
}
int ans = 0;
for (int i = 1; i <= n; i++)
if (isgood(i) && isgood(t[i])) ans++;
cout << ans;
return 0;
}
for (int i = 0; i < 13; i++) v[i] = n - 12 + i;
for (int i = 13; i; i--) {
int j = k / f[i - 1];
k %= f[i - 1];
t[14 - i] = v[j];
for (; j < i - 1; j++) v[j] = v[j + 1];
}
int ans = 0;
for (int i = 1; i <= 13; i++)
if (isgood(n - 13 + i) && isgood(t[i])) ans++;
ans += countgood(n - 13);
cout << ans;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
set<long long> Luckys, adads;
long long n, k, fact = 1, f = 1, facts[100], ans;
bool mark[100];
vector<int> Permutation;
bool isLucky(long long n) {
while (n) {
if (n % 10 != 4 && n % 10 != 7) return 0;
n /= 10;
}
return 1;
}
void Lucky_Make(long long n, long long MAX) {
if (n > MAX) return;
Luckys.insert(n);
Lucky_Make(n * 10 + 4, MAX);
Lucky_Make(n * 10 + 7, MAX);
}
void Calc_Permutation(int n, int k) {
int ans = 0;
for (int i = 1; i <= n; i++) adads.insert(i);
for (int i = 1; i <= n; i++) {
int now = k / facts[n - i], j;
k %= facts[n - i];
set<long long>::iterator it = adads.begin();
for (j = 0; j < now; j++) it++;
Permutation.push_back(*it);
adads.erase(it);
}
}
int Count(int f) {
int ans = 0;
for (int i = 0; i < Permutation.size(); i++)
if (isLucky(Permutation[i] + n - f) && isLucky(i + 1 + n - f)) ans++;
return ans;
}
int main() {
cin >> n >> k;
facts[0] = 1;
for (; fact < k; f++) {
fact *= f;
facts[f] = fact;
}
f--;
if (f > n) {
cout << -1 << endl;
return 0;
}
Lucky_Make(4, n - f);
Lucky_Make(7, n - f);
ans = Luckys.size();
Calc_Permutation(f, k - 1);
ans += Count(f);
cout << ans << endl;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
set<long long> Luckys, adads;
long long n, k, fact = 1, f = 1, facts[100], ans;
bool mark[100];
vector<int> Permutation;
bool isLucky(long long n) {
while (n) {
if (n % 10 != 4 && n % 10 != 7) return 0;
n /= 10;
}
return 1;
}
void Lucky_Make(long long n, long long MAX) {
if (n > MAX) return;
Luckys.insert(n);
Lucky_Make(n * 10 + 4, MAX);
Lucky_Make(n * 10 + 7, MAX);
}
void Calc_Permutation(int n, int k) {
int ans = 0;
for (int i = 1; i <= n; i++) adads.insert(i);
for (int i = 1; i <= n; i++) {
int now = k / facts[n - i], j;
k %= facts[n - i];
set<long long>::iterator it = adads.begin();
for (j = 0; j < now; j++) it++;
Permutation.push_back(*it);
adads.erase(it);
}
}
int Count(int f) {
int ans = 0;
for (int i = 0; i < Permutation.size(); i++)
if (isLucky(Permutation[i] + n - f) && isLucky(i + 1 + n - f)) ans++;
return ans;
}
int main() {
cin >> n >> k;
facts[0] = 1;
for (; fact < k; f++) {
fact *= f;
facts[f] = fact;
}
f--;
if (f > n) {
cout << -1 << endl;
return 0;
}
Lucky_Make(4, n - f);
Lucky_Make(7, n - f);
ans = Luckys.size();
Calc_Permutation(f, k - 1);
ans += Count(f);
cout << ans << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-6;
const double PI = acos(-1.0);
const int oo = 0x3f3f3f3f;
long long read() {
long long d, f = 1;
char c;
while (!isdigit(c = getchar()))
if (c == '-') f = -1;
d = c ^ '0';
while (isdigit(c = getchar())) d = (d * 10) + (c ^ '0');
return d * f;
}
vector<long long> happy, fac;
void dfs(long long num) {
if (num > int(1e9)) return;
happy.push_back(num);
dfs(num * 10 + 4);
dfs(num * 10 + 7);
}
void init() {
fac.push_back(1);
int i = 1;
while (fac[i - 1] * i <= int(1e9)) fac.push_back(fac[i - 1] * i), ++i;
dfs(4);
dfs(7);
sort(happy.begin(), happy.end());
}
int main() {
init();
int n, k;
while (cin >> n >> k) {
if (n < fac.size() && k > fac[n])
puts("-1");
else {
--k;
int len = upper_bound(fac.begin(), fac.end(), k) - fac.begin();
vector<int> prem(len + 1);
bool flag[20] = {0};
for (int i = 1; i <= len; ++i) {
int rank = -1;
for (int j = 1; j <= len; ++j)
if (!flag[j]) {
++rank;
if (rank == k / fac[len - i]) {
prem[i] = j + n - len;
flag[j] = 1;
k %= fac[len - i];
break;
}
}
}
int cnt1 = 0;
for (int i = 1; i <= len; ++i)
if (binary_search(happy.begin(), happy.end(), n - len + i) &&
!binary_search(happy.begin(), happy.end(), prem[i]))
++cnt1;
int cnt2 = upper_bound(happy.begin(), happy.end(), n) - happy.begin();
cout << cnt2 - cnt1 << endl;
}
}
return 0;
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-6;
const double PI = acos(-1.0);
const int oo = 0x3f3f3f3f;
long long read() {
long long d, f = 1;
char c;
while (!isdigit(c = getchar()))
if (c == '-') f = -1;
d = c ^ '0';
while (isdigit(c = getchar())) d = (d * 10) + (c ^ '0');
return d * f;
}
vector<long long> happy, fac;
void dfs(long long num) {
if (num > int(1e9)) return;
happy.push_back(num);
dfs(num * 10 + 4);
dfs(num * 10 + 7);
}
void init() {
fac.push_back(1);
int i = 1;
while (fac[i - 1] * i <= int(1e9)) fac.push_back(fac[i - 1] * i), ++i;
dfs(4);
dfs(7);
sort(happy.begin(), happy.end());
}
int main() {
init();
int n, k;
while (cin >> n >> k) {
if (n < fac.size() && k > fac[n])
puts("-1");
else {
--k;
int len = upper_bound(fac.begin(), fac.end(), k) - fac.begin();
vector<int> prem(len + 1);
bool flag[20] = {0};
for (int i = 1; i <= len; ++i) {
int rank = -1;
for (int j = 1; j <= len; ++j)
if (!flag[j]) {
++rank;
if (rank == k / fac[len - i]) {
prem[i] = j + n - len;
flag[j] = 1;
k %= fac[len - i];
break;
}
}
}
int cnt1 = 0;
for (int i = 1; i <= len; ++i)
if (binary_search(happy.begin(), happy.end(), n - len + i) &&
!binary_search(happy.begin(), happy.end(), prem[i]))
++cnt1;
int cnt2 = upper_bound(happy.begin(), happy.end(), n) - happy.begin();
cout << cnt2 - cnt1 << endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int dx4[5] = {0, -1, 0, 1, 0};
const int dy4[5] = {0, 0, 1, 0, -1};
const int dx8[9] = {0, -1, 0, 1, 0, 1, 1, -1, -1};
const int dy8[9] = {0, 0, 1, 0, -1, 1, -1, 1, -1};
const int maxn = 4e5 + 3;
const double PI = acos(-1.0);
const long long mod = 1e9 + 7;
long long po(long long a, long long b, long long mod) {
long long res = 1;
a %= mod;
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
long long gcd(long long a, long long b) {
if (a == 0) {
return b;
} else {
return gcd(b % a, a);
}
}
void YES() {
puts("YES");
exit(0);
}
void Yes() {
puts("Yes");
exit(0);
}
void NO() {
puts("NO");
exit(0);
}
void No() {
puts("No");
exit(0);
}
void one() {
puts("-1");
exit(0);
}
long long n, k;
long long fac[20];
bool isluck(long long x) {
while (x) {
int k = x % 10;
if (!(k == 4 || k == 7)) {
return false;
}
x /= 10;
}
return true;
}
int used[20];
vector<int> prem(long long n, long long k) {
vector<int> ans;
k--;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (used[j]) continue;
if (k < fac[n - i]) {
ans.push_back(j);
used[j] = 1;
break;
} else {
k -= fac[n - i];
}
}
}
return ans;
}
vector<long long> g;
void is_ll(long long x) {
if (x <= n) {
g.push_back(x);
is_ll(x * 10 + 4);
is_ll(x * 10 + 7);
}
}
void solve() {
fac[0] = 1;
for (int i = 1; i <= 15; i++) {
fac[i] = fac[i - 1] * i;
}
scanf("%lld", &(n));
scanf("%lld", &(k));
if (n < 13 && k > fac[n]) {
puts("-1");
return;
}
long long N = min(n, 13LL);
vector<int> val = prem(N, k);
is_ll(4);
is_ll(7);
int ans = 0;
int sur = n - N + 1;
for (int i = 0; i < N; i++) {
if (isluck(i + sur) && isluck(val[i] + sur - 1)) ans++;
}
for (int i = 0; i < g.size(); i++) {
if (g[i] < sur) ans++;
}
cout << ans << endl;
}
int main() {
solve();
return 0;
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int dx4[5] = {0, -1, 0, 1, 0};
const int dy4[5] = {0, 0, 1, 0, -1};
const int dx8[9] = {0, -1, 0, 1, 0, 1, 1, -1, -1};
const int dy8[9] = {0, 0, 1, 0, -1, 1, -1, 1, -1};
const int maxn = 4e5 + 3;
const double PI = acos(-1.0);
const long long mod = 1e9 + 7;
long long po(long long a, long long b, long long mod) {
long long res = 1;
a %= mod;
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
long long gcd(long long a, long long b) {
if (a == 0) {
return b;
} else {
return gcd(b % a, a);
}
}
void YES() {
puts("YES");
exit(0);
}
void Yes() {
puts("Yes");
exit(0);
}
void NO() {
puts("NO");
exit(0);
}
void No() {
puts("No");
exit(0);
}
void one() {
puts("-1");
exit(0);
}
long long n, k;
long long fac[20];
bool isluck(long long x) {
while (x) {
int k = x % 10;
if (!(k == 4 || k == 7)) {
return false;
}
x /= 10;
}
return true;
}
int used[20];
vector<int> prem(long long n, long long k) {
vector<int> ans;
k--;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (used[j]) continue;
if (k < fac[n - i]) {
ans.push_back(j);
used[j] = 1;
break;
} else {
k -= fac[n - i];
}
}
}
return ans;
}
vector<long long> g;
void is_ll(long long x) {
if (x <= n) {
g.push_back(x);
is_ll(x * 10 + 4);
is_ll(x * 10 + 7);
}
}
void solve() {
fac[0] = 1;
for (int i = 1; i <= 15; i++) {
fac[i] = fac[i - 1] * i;
}
scanf("%lld", &(n));
scanf("%lld", &(k));
if (n < 13 && k > fac[n]) {
puts("-1");
return;
}
long long N = min(n, 13LL);
vector<int> val = prem(N, k);
is_ll(4);
is_ll(7);
int ans = 0;
int sur = n - N + 1;
for (int i = 0; i < N; i++) {
if (isluck(i + sur) && isluck(val[i] + sur - 1)) ans++;
}
for (int i = 0; i < g.size(); i++) {
if (g[i] < sur) ans++;
}
cout << ans << endl;
}
int main() {
solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long MAX = 1000000000ll;
const long double EPS = 1E-9;
const int INF = (int)1E9;
const long long INF64 = (long long)1E18;
const long double PI = 2 * acos(.0);
long long fact[15];
void factorial(int x) {
fact[0] = 1;
fact[1] = 1;
for (int i = 2; i < (int)(x + 1); i++) fact[i] = i * fact[i - 1];
return;
}
vector<long long> kthPerm(int x, int k) {
bool used[15];
for (int i = 0; i < (int)(x + 1); i++) used[i] = 0;
long long base = 0;
vector<long long> resp;
for (int i = 0; i < (int)(x); i++) {
long long rad = fact[x - i - 1];
for (int j = 1; j < (int)(x + 1); j++) {
if (used[j]) continue;
if (base + rad >= k) {
resp.push_back(j);
used[j] = 1;
break;
} else {
base += rad;
}
}
}
return resp;
}
vector<long long> luck;
void genLucky(long long i) {
if (i >= 1000000000ll) return;
long long k = i * 10ll + 7ll;
long long l = i * 10ll + 4ll;
luck.push_back(k);
luck.push_back(l);
genLucky(k);
genLucky(l);
}
bool isL(int x) {
while (x > 0) {
int y = x % 10;
if (!(y == 4 || y == 7)) return 0;
x /= 10;
}
return 1;
}
int main() {
int n, k;
cin >> n >> k;
factorial(13);
int cont = 0;
if (n < 13) {
if (k > fact[n])
cout << "-1" << endl;
else {
vector<long long> pp = kthPerm(n, k);
for (int i = 0; i < (int)((pp).size()); i++) {
if (isL(pp[i]) && isL(i + 1)) cont++;
}
cout << cont << endl;
}
} else {
vector<long long> pp = kthPerm(13, k);
genLucky(0ll);
int left = n - 13;
for (int i = 0; i < (int)((luck).size()); i++) {
if (luck[i] <= (left)) {
cont++;
}
}
for (int i = 0; i < (int)((pp).size()); i++) {
pp[i] += left;
}
for (int i = 0; i < (int)(13); i++) {
if (isL(pp[i]) && isL(left + i + 1)) cont++;
}
cout << cont << endl;
}
return 0;
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MAX = 1000000000ll;
const long double EPS = 1E-9;
const int INF = (int)1E9;
const long long INF64 = (long long)1E18;
const long double PI = 2 * acos(.0);
long long fact[15];
void factorial(int x) {
fact[0] = 1;
fact[1] = 1;
for (int i = 2; i < (int)(x + 1); i++) fact[i] = i * fact[i - 1];
return;
}
vector<long long> kthPerm(int x, int k) {
bool used[15];
for (int i = 0; i < (int)(x + 1); i++) used[i] = 0;
long long base = 0;
vector<long long> resp;
for (int i = 0; i < (int)(x); i++) {
long long rad = fact[x - i - 1];
for (int j = 1; j < (int)(x + 1); j++) {
if (used[j]) continue;
if (base + rad >= k) {
resp.push_back(j);
used[j] = 1;
break;
} else {
base += rad;
}
}
}
return resp;
}
vector<long long> luck;
void genLucky(long long i) {
if (i >= 1000000000ll) return;
long long k = i * 10ll + 7ll;
long long l = i * 10ll + 4ll;
luck.push_back(k);
luck.push_back(l);
genLucky(k);
genLucky(l);
}
bool isL(int x) {
while (x > 0) {
int y = x % 10;
if (!(y == 4 || y == 7)) return 0;
x /= 10;
}
return 1;
}
int main() {
int n, k;
cin >> n >> k;
factorial(13);
int cont = 0;
if (n < 13) {
if (k > fact[n])
cout << "-1" << endl;
else {
vector<long long> pp = kthPerm(n, k);
for (int i = 0; i < (int)((pp).size()); i++) {
if (isL(pp[i]) && isL(i + 1)) cont++;
}
cout << cont << endl;
}
} else {
vector<long long> pp = kthPerm(13, k);
genLucky(0ll);
int left = n - 13;
for (int i = 0; i < (int)((luck).size()); i++) {
if (luck[i] <= (left)) {
cont++;
}
}
for (int i = 0; i < (int)((pp).size()); i++) {
pp[i] += left;
}
for (int i = 0; i < (int)(13); i++) {
if (isL(pp[i]) && isL(left + i + 1)) cont++;
}
cout << cont << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, k;
int s[20], d[20], t[20], r[20];
int a[2000];
void dfs(long long x) {
if (x > 1000000000) return;
a[++a[0]] = x;
dfs(x * 10 + 4);
dfs(x * 10 + 7);
}
int main() {
cin >> n;
cin >> k;
k--;
a[0] = 0;
dfs(4);
dfs(7);
a[0] = 2000000000;
int x = 1;
s[1] = 1;
for (int i = 1; i < 12; i++) {
x++;
s[i + 1] = (s[i] * x);
}
if (n <= 12 && s[n] <= k) {
cout << -1 << endl;
return 0;
}
for (int i = min(12, n - 1); i >= 1; i--) {
d[min(12, n - 1) - i] = k / s[i];
k = (k % s[i]);
if (i == 1) d[min(12, n - 1)] = k;
}
for (int i = 0; i < min(13, n); i++) t[i] = i;
for (int i = 0; i < min(13, n); i++) {
r[i] = t[d[i]] + (n - min(n, 13) + 1);
swap(t[d[i]], t[min(12, n - 1) - i]);
sort(t, t + (min(12, n - 1) - i));
}
sort(a, a + 1023);
a[1021] = 777777777;
a[1021] = 777777774;
a[1022] = 777777777;
int ans = upper_bound(a, a + 1022, n - 13) - a;
for (int i = 0; i < min(n, 13); i++) {
if (binary_search(a, a + 1023, i + (n - min(12, n - 1)))) {
if (binary_search(a, a + 1023, r[i])) ans++;
}
}
cout << ans << endl;
return 0;
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, k;
int s[20], d[20], t[20], r[20];
int a[2000];
void dfs(long long x) {
if (x > 1000000000) return;
a[++a[0]] = x;
dfs(x * 10 + 4);
dfs(x * 10 + 7);
}
int main() {
cin >> n;
cin >> k;
k--;
a[0] = 0;
dfs(4);
dfs(7);
a[0] = 2000000000;
int x = 1;
s[1] = 1;
for (int i = 1; i < 12; i++) {
x++;
s[i + 1] = (s[i] * x);
}
if (n <= 12 && s[n] <= k) {
cout << -1 << endl;
return 0;
}
for (int i = min(12, n - 1); i >= 1; i--) {
d[min(12, n - 1) - i] = k / s[i];
k = (k % s[i]);
if (i == 1) d[min(12, n - 1)] = k;
}
for (int i = 0; i < min(13, n); i++) t[i] = i;
for (int i = 0; i < min(13, n); i++) {
r[i] = t[d[i]] + (n - min(n, 13) + 1);
swap(t[d[i]], t[min(12, n - 1) - i]);
sort(t, t + (min(12, n - 1) - i));
}
sort(a, a + 1023);
a[1021] = 777777777;
a[1021] = 777777774;
a[1022] = 777777777;
int ans = upper_bound(a, a + 1022, n - 13) - a;
for (int i = 0; i < min(n, 13); i++) {
if (binary_search(a, a + 1023, i + (n - min(12, n - 1)))) {
if (binary_search(a, a + 1023, r[i])) ans++;
}
}
cout << ans << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, k, f[13], p[13];
vector<int> lk;
map<int, bool> bea;
void gen(int x) {
if (x) lk.push_back(x), bea[x] = true;
if (x > int(1e8)) return;
gen(x * 10 + 4);
gen(x * 10 + 7);
}
int main() {
scanf("%d%d", &n, &k);
f[0] = 1;
for (int i = 1; i <= 12; ++i) f[i] = f[i - 1] * i;
if (n <= 12 && k > f[n]) {
printf("-1");
return 0;
}
gen(0);
lk.push_back(int(1e9) + 1);
sort(lk.begin(), lk.end());
int d;
for (int i = 1; i <= n; ++i)
if (f[i] >= k) {
d = i;
break;
}
int ans = 0;
for (int i = 0; i < lk.size(); ++i)
if (lk[i] > n - d) {
ans = i;
break;
}
for (int i = 1; i <= d; ++i) {
for (int j = 1; j <= d; ++j)
if (!p[j]) {
if (k > f[d - i])
k -= f[d - i];
else {
p[j] = 1;
if (bea[n - d + i] && bea[n - d + j]) ++ans;
break;
}
}
}
printf("%d", ans);
return 0;
}
| ### Prompt
Generate a Cpp solution to the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, k, f[13], p[13];
vector<int> lk;
map<int, bool> bea;
void gen(int x) {
if (x) lk.push_back(x), bea[x] = true;
if (x > int(1e8)) return;
gen(x * 10 + 4);
gen(x * 10 + 7);
}
int main() {
scanf("%d%d", &n, &k);
f[0] = 1;
for (int i = 1; i <= 12; ++i) f[i] = f[i - 1] * i;
if (n <= 12 && k > f[n]) {
printf("-1");
return 0;
}
gen(0);
lk.push_back(int(1e9) + 1);
sort(lk.begin(), lk.end());
int d;
for (int i = 1; i <= n; ++i)
if (f[i] >= k) {
d = i;
break;
}
int ans = 0;
for (int i = 0; i < lk.size(); ++i)
if (lk[i] > n - d) {
ans = i;
break;
}
for (int i = 1; i <= d; ++i) {
for (int j = 1; j <= d; ++j)
if (!p[j]) {
if (k > f[d - i])
k -= f[d - i];
else {
p[j] = 1;
if (bea[n - d + i] && bea[n - d + j]) ++ans;
break;
}
}
}
printf("%d", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-6;
const int N = 2000010;
const int INF = 0x3f3f3f3f;
const int mod = 1000000007;
const int dir[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, -1, -1, -1, 1, 1, -1, 1, 1};
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long powmod(long long a, long long b) {
long long res = 1;
a %= mod;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
template <class T>
bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
template <class T>
void read(T& x) {
char c;
bool op = 0;
while (c = getchar(), c < '0' || c > '9')
if (c == '-') op = 1;
x = c - '0';
while (c = getchar(), c >= '0' && c <= '9') x = x * 10 + c - '0';
if (op) x = -x;
}
template <class T>
void write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) write(x / 10);
putchar(x % 10 + 48);
}
long long n, k;
long long ans;
long long fac[N] = {1};
long long res[N];
long long tot = 0;
long long find(long long x) {
sort(res + 1, res + 1 + tot);
long long ret = res[x];
for (int i = x + 1; i <= tot; i++) {
res[i - 1] = res[i];
}
--tot;
return ret;
}
void dfs(long long x, long long lim) {
if (x > lim) return;
if (x != 0) ++ans;
dfs(x * 10 + 4, lim);
dfs(x * 10 + 7, lim);
}
bool ck(long long x) {
while (x) {
if (x % 10 != 4 && x % 10 != 7) return false;
x /= 10;
}
return true;
}
void solve() {
read(n), read(k);
long long p = -1;
for (int i = 1; i <= n; i++) {
fac[i] = fac[i - 1] * i;
res[++tot] = n - i + 1;
if (fac[i] >= k) {
p = i;
break;
}
}
k--;
if (p == -1) {
puts("-1");
return;
}
dfs(0, n - p);
for (long long i = p, val, pos; i >= 1; i--) {
val = find(k / fac[i - 1] + 1);
pos = n - i + 1;
if (ck(val) && ck(pos)) {
++ans;
}
k = k % fac[i - 1];
}
write(ans);
putchar('\n');
}
int main() {
solve();
return 0;
}
| ### Prompt
Develop a solution in CPP to the problem described below:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-6;
const int N = 2000010;
const int INF = 0x3f3f3f3f;
const int mod = 1000000007;
const int dir[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, -1, -1, -1, 1, 1, -1, 1, 1};
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long powmod(long long a, long long b) {
long long res = 1;
a %= mod;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
template <class T>
bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
template <class T>
void read(T& x) {
char c;
bool op = 0;
while (c = getchar(), c < '0' || c > '9')
if (c == '-') op = 1;
x = c - '0';
while (c = getchar(), c >= '0' && c <= '9') x = x * 10 + c - '0';
if (op) x = -x;
}
template <class T>
void write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) write(x / 10);
putchar(x % 10 + 48);
}
long long n, k;
long long ans;
long long fac[N] = {1};
long long res[N];
long long tot = 0;
long long find(long long x) {
sort(res + 1, res + 1 + tot);
long long ret = res[x];
for (int i = x + 1; i <= tot; i++) {
res[i - 1] = res[i];
}
--tot;
return ret;
}
void dfs(long long x, long long lim) {
if (x > lim) return;
if (x != 0) ++ans;
dfs(x * 10 + 4, lim);
dfs(x * 10 + 7, lim);
}
bool ck(long long x) {
while (x) {
if (x % 10 != 4 && x % 10 != 7) return false;
x /= 10;
}
return true;
}
void solve() {
read(n), read(k);
long long p = -1;
for (int i = 1; i <= n; i++) {
fac[i] = fac[i - 1] * i;
res[++tot] = n - i + 1;
if (fac[i] >= k) {
p = i;
break;
}
}
k--;
if (p == -1) {
puts("-1");
return;
}
dfs(0, n - p);
for (long long i = p, val, pos; i >= 1; i--) {
val = find(k / fac[i - 1] + 1);
pos = n - i + 1;
if (ck(val) && ck(pos)) {
++ans;
}
k = k % fac[i - 1];
}
write(ans);
putchar('\n');
}
int main() {
solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int c(long long a, long long b) {
if (a > b) return 0;
return 1 + c(a * 10 + 4, b) + c(a * 10 + 7, b);
}
int is(long long a) {
return a == 0 || (a % 10 == 4 || a % 10 == 7) && is(a / 10);
}
long long f[20];
bool u[20];
int main() {
int n, k;
cin >> n >> k;
k--;
f[0] = 1;
for (int i = 1; i <= 13; i++) f[i] = f[i - 1] * i;
if (n <= 13 && f[n] <= k) {
cout << -1;
return 0;
}
int ans = c(0, n) - 1;
for (int i = 13; i >= 0; i--) {
int shift = k / f[i], c = -1;
while (shift >= 0) {
shift -= !u[++c];
}
u[c] = 1;
int t = c - 13 + n;
if (t > 0) {
if (is(n - i)) ans += is(t) - 1;
}
k %= f[i];
}
cout << ans;
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int c(long long a, long long b) {
if (a > b) return 0;
return 1 + c(a * 10 + 4, b) + c(a * 10 + 7, b);
}
int is(long long a) {
return a == 0 || (a % 10 == 4 || a % 10 == 7) && is(a / 10);
}
long long f[20];
bool u[20];
int main() {
int n, k;
cin >> n >> k;
k--;
f[0] = 1;
for (int i = 1; i <= 13; i++) f[i] = f[i - 1] * i;
if (n <= 13 && f[n] <= k) {
cout << -1;
return 0;
}
int ans = c(0, n) - 1;
for (int i = 13; i >= 0; i--) {
int shift = k / f[i], c = -1;
while (shift >= 0) {
shift -= !u[++c];
}
u[c] = 1;
int t = c - 13 + n;
if (t > 0) {
if (is(n - i)) ans += is(t) - 1;
}
k %= f[i];
}
cout << ans;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long a[10005];
int f[20], nums[20], c[20];
int num;
int g(int x) {
int ret = 0;
for (int i = 1; i <= num; i++)
if (a[i] <= x) ret++;
return ret;
}
bool lucky(int x) {
while (x) {
int c = x % 10;
if (c != 4 && c != 7) return false;
x /= 10;
}
return true;
}
int main() {
int n, k;
scanf("%d%d", &n, &k);
f[0] = 1;
for (int i = 1; i <= 12; i++) f[i] = i * f[i - 1];
f[13] = 2123456789;
if (n <= 12)
if (f[n] < k) {
printf("-1\n");
return 0;
}
k--;
for (int i = 1; i <= 13; i++) nums[i] = n - 13 + i;
for (int i = 13; i >= 1; i--) {
int tmp = k / f[i - 1] + 1;
c[13 - i + 1] = nums[tmp];
for (int j = tmp; j <= 13; j++) nums[j] = nums[j + 1];
k %= f[i - 1];
}
num = 0;
for (int br = 1; br <= 10; br++)
for (int i = 1; i <= (1 << br); i++) {
int tmp = i;
num++;
for (int j = 1; j <= br; j++) {
if (tmp % 2)
a[num] = a[num] * 10LL + 4LL;
else
a[num] = a[num] * 10LL + 7LL;
tmp /= 2;
}
}
sort(a + 1, a + num + 1);
int sol = 0;
for (int i = max(n - 12, 1); i <= n; i++) {
if (lucky(c[13 - (n - i)]) && lucky(i)) sol++;
}
printf("%d\n", g(n - 13) + sol);
return 0;
}
| ### Prompt
Please create a solution in Cpp to the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long a[10005];
int f[20], nums[20], c[20];
int num;
int g(int x) {
int ret = 0;
for (int i = 1; i <= num; i++)
if (a[i] <= x) ret++;
return ret;
}
bool lucky(int x) {
while (x) {
int c = x % 10;
if (c != 4 && c != 7) return false;
x /= 10;
}
return true;
}
int main() {
int n, k;
scanf("%d%d", &n, &k);
f[0] = 1;
for (int i = 1; i <= 12; i++) f[i] = i * f[i - 1];
f[13] = 2123456789;
if (n <= 12)
if (f[n] < k) {
printf("-1\n");
return 0;
}
k--;
for (int i = 1; i <= 13; i++) nums[i] = n - 13 + i;
for (int i = 13; i >= 1; i--) {
int tmp = k / f[i - 1] + 1;
c[13 - i + 1] = nums[tmp];
for (int j = tmp; j <= 13; j++) nums[j] = nums[j + 1];
k %= f[i - 1];
}
num = 0;
for (int br = 1; br <= 10; br++)
for (int i = 1; i <= (1 << br); i++) {
int tmp = i;
num++;
for (int j = 1; j <= br; j++) {
if (tmp % 2)
a[num] = a[num] * 10LL + 4LL;
else
a[num] = a[num] * 10LL + 7LL;
tmp /= 2;
}
}
sort(a + 1, a + num + 1);
int sol = 0;
for (int i = max(n - 12, 1); i <= n; i++) {
if (lucky(c[13 - (n - i)]) && lucky(i)) sol++;
}
printf("%d\n", g(n - 13) + sol);
return 0;
}
``` |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
template <class T>
bool scanff(T &ret) {
char c;
int sgn;
T bit = 0.1;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && c != '.' && (c < '0' || c > '9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
if (c == ' ' || c == '\n') {
ret *= sgn;
return 1;
}
while (c = getchar(), c >= '0' && c <= '9') ret += (c - '0') * bit, bit /= 10;
ret *= sgn;
return 1;
}
using namespace std;
long long f[22];
int n;
long long k;
int a[22];
set<int> st;
set<int>::iterator it;
void geta(int n, long long k) {
k--;
int idx = n - 1;
for (int i = int(1); i <= int(n); i++) st.insert(i);
for (int i = int(1); i <= int(n); i++) {
int cot = 1;
if (idx >= 0)
while (k >= f[idx]) cot++, k -= f[idx];
it = st.begin();
for (int j = int(1); j <= int(cot - 1); j++) it++;
a[i] = *it;
st.erase(*it);
idx--;
}
}
long long b[4444], bn;
int main() {
f[0] = 1;
for (int i = int(1); i <= int(13); i++) f[i] = f[i - 1] * i;
scanff(n);
scanff(k);
for (int j = int(1); j <= int(10); j++)
for (int i = int(0); i <= int(1 << j); i++) {
int x = i;
long long t = 0;
for (int k = int(1); k <= int(j); k++) {
t = t * 10;
if (x & 1)
t += 4;
else
t += 7;
x = x / 2;
}
b[++bn] = t;
}
sort(b + 1, b + 1 + bn);
bn = unique(b + 1, b + 1 + bn) - b - 1;
if (n <= 13) {
if (f[n] < k) {
printf("-1\n");
return 0;
}
geta(n, k);
int ans = 0;
if (a[4] == 4 || a[4] == 7) ans++;
if (a[7] == 7 || a[7] == 4) ans++;
printf("%d\n", ans);
} else {
n = n - 13;
int idx = lower_bound(b + 1, b + 1 + bn, n) - b;
int ans = idx - 1;
if (b[idx] <= n) ans++;
geta(13, k);
for (int i = int(1); i <= int(13); i++) {
int idx = i + n;
int val = a[i] + n;
if (b[lower_bound(b + 1, b + 1 + bn, idx) - b] == idx &&
b[lower_bound(b + 1, b + 1 + bn, val) - b] == val)
ans++;
}
printf("%d\n", ans);
}
return 0;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
template <class T>
bool scanff(T &ret) {
char c;
int sgn;
T bit = 0.1;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && c != '.' && (c < '0' || c > '9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
if (c == ' ' || c == '\n') {
ret *= sgn;
return 1;
}
while (c = getchar(), c >= '0' && c <= '9') ret += (c - '0') * bit, bit /= 10;
ret *= sgn;
return 1;
}
using namespace std;
long long f[22];
int n;
long long k;
int a[22];
set<int> st;
set<int>::iterator it;
void geta(int n, long long k) {
k--;
int idx = n - 1;
for (int i = int(1); i <= int(n); i++) st.insert(i);
for (int i = int(1); i <= int(n); i++) {
int cot = 1;
if (idx >= 0)
while (k >= f[idx]) cot++, k -= f[idx];
it = st.begin();
for (int j = int(1); j <= int(cot - 1); j++) it++;
a[i] = *it;
st.erase(*it);
idx--;
}
}
long long b[4444], bn;
int main() {
f[0] = 1;
for (int i = int(1); i <= int(13); i++) f[i] = f[i - 1] * i;
scanff(n);
scanff(k);
for (int j = int(1); j <= int(10); j++)
for (int i = int(0); i <= int(1 << j); i++) {
int x = i;
long long t = 0;
for (int k = int(1); k <= int(j); k++) {
t = t * 10;
if (x & 1)
t += 4;
else
t += 7;
x = x / 2;
}
b[++bn] = t;
}
sort(b + 1, b + 1 + bn);
bn = unique(b + 1, b + 1 + bn) - b - 1;
if (n <= 13) {
if (f[n] < k) {
printf("-1\n");
return 0;
}
geta(n, k);
int ans = 0;
if (a[4] == 4 || a[4] == 7) ans++;
if (a[7] == 7 || a[7] == 4) ans++;
printf("%d\n", ans);
} else {
n = n - 13;
int idx = lower_bound(b + 1, b + 1 + bn, n) - b;
int ans = idx - 1;
if (b[idx] <= n) ans++;
geta(13, k);
for (int i = int(1); i <= int(13); i++) {
int idx = i + n;
int val = a[i] + n;
if (b[lower_bound(b + 1, b + 1 + bn, idx) - b] == idx &&
b[lower_bound(b + 1, b + 1 + bn, val) - b] == val)
ans++;
}
printf("%d\n", ans);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long p[20];
int n, k, ans;
void pcheck(int num) {
char s[20];
int i;
sprintf(s, "%d", num);
for (i = 0; s[i]; i++)
if (s[i] != '4' && s[i] != '7') return;
ans++;
}
void check(long long num) {
if (num > n) return;
if (n - num > 15)
pcheck(num);
else {
int kk, pp[20], sk = k - 1, i, j;
kk = min(n, 17);
for (i = 1; i <= kk; i++) pp[i] = n - kk + i;
for (i = 1; i <= kk; i++)
if (sk >= p[kk - i]) {
int d = sk / p[kk - i];
int save = pp[i + d];
for (j = i + d; j > i; j--) pp[j] = pp[j - 1];
pp[j] = save;
sk -= d * p[kk - i];
}
pcheck(pp[kk - (n - num)]);
}
}
void solve(long long num, int d) {
if (d > 9) return;
if (num) check(num);
solve(num * 10 + 4, d + 1);
solve(num * 10 + 7, d + 1);
}
int main() {
int i;
cin >> n >> k;
p[1] = p[0] = 1;
for (i = 2; i < 18; i++) p[i] = i * p[i - 1];
if (n < 18 && p[n] < k)
cout << "-1";
else {
ans = 0;
solve(0, 0);
cout << ans;
}
return 0;
}
| ### Prompt
In cpp, your task is to solve the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long p[20];
int n, k, ans;
void pcheck(int num) {
char s[20];
int i;
sprintf(s, "%d", num);
for (i = 0; s[i]; i++)
if (s[i] != '4' && s[i] != '7') return;
ans++;
}
void check(long long num) {
if (num > n) return;
if (n - num > 15)
pcheck(num);
else {
int kk, pp[20], sk = k - 1, i, j;
kk = min(n, 17);
for (i = 1; i <= kk; i++) pp[i] = n - kk + i;
for (i = 1; i <= kk; i++)
if (sk >= p[kk - i]) {
int d = sk / p[kk - i];
int save = pp[i + d];
for (j = i + d; j > i; j--) pp[j] = pp[j - 1];
pp[j] = save;
sk -= d * p[kk - i];
}
pcheck(pp[kk - (n - num)]);
}
}
void solve(long long num, int d) {
if (d > 9) return;
if (num) check(num);
solve(num * 10 + 4, d + 1);
solve(num * 10 + 7, d + 1);
}
int main() {
int i;
cin >> n >> k;
p[1] = p[0] = 1;
for (i = 2; i < 18; i++) p[i] = i * p[i - 1];
if (n < 18 && p[n] < k)
cout << "-1";
else {
ans = 0;
solve(0, 0);
cout << ans;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long N = 14;
long long n, k, ans, fac[N];
long long get(string &s) {
long long res = 0;
for (char c : s) res = 10 * res + (long long)(c - '0');
return res;
}
string get_s(long long x) {
string res;
while (x) {
res += (char)((x % 10) + '0');
x /= 10;
}
reverse(res.begin(), res.end());
return res;
}
string nxt(string s) {
long long indx = 0;
while (indx < s.size() && (s[indx] == '4' || s[indx] == '7')) indx++;
if (indx == (long long)s.size()) return s;
if (s[indx] < '4') {
for (long long i = indx; i < s.size(); i++) s[i] = '4';
return s;
}
if (s[indx] < '7') {
s[indx] = '7';
for (long long i = indx + 1; i < s.size(); i++) s[i] = '4';
return s;
}
for (long long i = indx - 1; ~i; i--) {
if (s[i] == '4') {
s[i] = '7';
for (long long j = i + 1; j < s.size(); j++) s[j] = '4';
return s;
}
}
string res;
for (long long i = 0; i <= (long long)s.size(); i++) res += '4';
return res;
}
bool luk(long long x) {
while (x) {
if (x % 10 != 4 && x % 10 != 7) return 0;
x /= 10;
}
return 1;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
fac[0] = 1;
for (long long i = 1; i < N; i++) fac[i] = fac[i - 1] * i;
cin >> n >> k;
if (n < N && fac[n] < k) return cout << -1, 0;
string now = "1";
while (get(now) <= n) {
now = nxt(now);
long long x = get(now);
if (n - x > 12) ans++;
now = get_s(get(now) + 1);
}
vector<long long> v;
for (long long i = max(1LL, n - 12); i <= n; i++) v.push_back(i);
long long rem = max(1LL, n - 12);
while (v.size() && k) {
for (long long j = 0; j <= v.size(); j++)
if (k > j * fac[(long long)v.size() - 1] &&
k <= (j + 1) * fac[(long long)v.size() - 1]) {
k -= j * fac[(long long)v.size() - 1];
swap(v[0], v[j]);
sort(v.begin() + 1, v.end());
if (luk(rem) && luk(v[0])) ans++;
v.erase(v.begin(), v.begin() + 1);
rem++;
break;
}
}
for (long long i = 0; i < v.size(); i++)
if (luk(rem + i) && luk(v[i])) ans++;
cout << ans;
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long N = 14;
long long n, k, ans, fac[N];
long long get(string &s) {
long long res = 0;
for (char c : s) res = 10 * res + (long long)(c - '0');
return res;
}
string get_s(long long x) {
string res;
while (x) {
res += (char)((x % 10) + '0');
x /= 10;
}
reverse(res.begin(), res.end());
return res;
}
string nxt(string s) {
long long indx = 0;
while (indx < s.size() && (s[indx] == '4' || s[indx] == '7')) indx++;
if (indx == (long long)s.size()) return s;
if (s[indx] < '4') {
for (long long i = indx; i < s.size(); i++) s[i] = '4';
return s;
}
if (s[indx] < '7') {
s[indx] = '7';
for (long long i = indx + 1; i < s.size(); i++) s[i] = '4';
return s;
}
for (long long i = indx - 1; ~i; i--) {
if (s[i] == '4') {
s[i] = '7';
for (long long j = i + 1; j < s.size(); j++) s[j] = '4';
return s;
}
}
string res;
for (long long i = 0; i <= (long long)s.size(); i++) res += '4';
return res;
}
bool luk(long long x) {
while (x) {
if (x % 10 != 4 && x % 10 != 7) return 0;
x /= 10;
}
return 1;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
fac[0] = 1;
for (long long i = 1; i < N; i++) fac[i] = fac[i - 1] * i;
cin >> n >> k;
if (n < N && fac[n] < k) return cout << -1, 0;
string now = "1";
while (get(now) <= n) {
now = nxt(now);
long long x = get(now);
if (n - x > 12) ans++;
now = get_s(get(now) + 1);
}
vector<long long> v;
for (long long i = max(1LL, n - 12); i <= n; i++) v.push_back(i);
long long rem = max(1LL, n - 12);
while (v.size() && k) {
for (long long j = 0; j <= v.size(); j++)
if (k > j * fac[(long long)v.size() - 1] &&
k <= (j + 1) * fac[(long long)v.size() - 1]) {
k -= j * fac[(long long)v.size() - 1];
swap(v[0], v[j]);
sort(v.begin() + 1, v.end());
if (luk(rem) && luk(v[0])) ans++;
v.erase(v.begin(), v.begin() + 1);
rem++;
break;
}
}
for (long long i = 0; i < v.size(); i++)
if (luk(rem + i) && luk(v[i])) ans++;
cout << ans;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long fact[20], n, k, anss;
vector<int> vec, vec2;
long long num(int a) {
int j = 0;
for (int i = 31; i >= 0; i--)
if ((a >> i) & 1) {
j = i;
break;
}
long long ans = 0;
for (int i = j - 1; i >= 0; i--) {
ans *= 10;
if ((a >> i) & 1)
ans += 7;
else
ans += 4;
}
return ans;
}
bool lucky(int a) {
while (a > 0) {
if (a % 10 != 4 && a % 10 != 7) return false;
a /= 10;
}
return true;
}
int main() {
fact[0] = 1;
for (long long i = 1; i < 14; i++) fact[i] = fact[i - 1] * i;
cin >> n >> k;
if (n < 14 && k > fact[n]) {
cout << -1 << endl;
return 0;
}
k--;
int j = 1;
while (fact[j] <= k) j++;
j = n - j + 1;
for (int i = j; i <= n; i++) vec.push_back(i);
int Max = n + 1;
for (int i = j; i <= n; i++)
if (lucky(i)) {
Max = i;
break;
}
for (int i = j; i <= n; i++) {
int num = k / fact[n - i];
if (lucky(vec[num]) && lucky(i)) anss++;
k -= num * fact[n - i];
vec2.clear();
for (int i = 0; i < num; i++) vec2.push_back(vec[i]);
for (int i = num + 1; i < (int)vec.size(); i++) vec2.push_back(vec[i]);
vec.clear();
for (int i = 0; i < (int)vec2.size(); i++) vec.push_back(vec2[i]);
}
int a = 2;
while (num(a) < Max) {
anss++;
a++;
}
cout << anss << endl;
return 0;
}
| ### Prompt
Please create a solution in CPP to the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long fact[20], n, k, anss;
vector<int> vec, vec2;
long long num(int a) {
int j = 0;
for (int i = 31; i >= 0; i--)
if ((a >> i) & 1) {
j = i;
break;
}
long long ans = 0;
for (int i = j - 1; i >= 0; i--) {
ans *= 10;
if ((a >> i) & 1)
ans += 7;
else
ans += 4;
}
return ans;
}
bool lucky(int a) {
while (a > 0) {
if (a % 10 != 4 && a % 10 != 7) return false;
a /= 10;
}
return true;
}
int main() {
fact[0] = 1;
for (long long i = 1; i < 14; i++) fact[i] = fact[i - 1] * i;
cin >> n >> k;
if (n < 14 && k > fact[n]) {
cout << -1 << endl;
return 0;
}
k--;
int j = 1;
while (fact[j] <= k) j++;
j = n - j + 1;
for (int i = j; i <= n; i++) vec.push_back(i);
int Max = n + 1;
for (int i = j; i <= n; i++)
if (lucky(i)) {
Max = i;
break;
}
for (int i = j; i <= n; i++) {
int num = k / fact[n - i];
if (lucky(vec[num]) && lucky(i)) anss++;
k -= num * fact[n - i];
vec2.clear();
for (int i = 0; i < num; i++) vec2.push_back(vec[i]);
for (int i = num + 1; i < (int)vec.size(); i++) vec2.push_back(vec[i]);
vec.clear();
for (int i = 0; i < (int)vec2.size(); i++) vec.push_back(vec2[i]);
}
int a = 2;
while (num(a) < Max) {
anss++;
a++;
}
cout << anss << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, k;
int s[20], d[20], t[20], r[20];
int a[2000];
void dfs(long long x) {
if (x > 1000000000) return;
a[a[2000]++] = x;
dfs(x * 10 + 4);
dfs(x * 10 + 7);
}
int main() {
cin >> n;
cin >> k;
k--;
a[2000] = 0;
dfs(4);
dfs(7);
int x = 1;
s[1] = 1;
for (int i = 1; i < 12; i++) {
x++;
s[i + 1] = (s[i] * x);
}
if (n <= 12 && s[n] <= k) {
cout << -1 << endl;
return 0;
}
for (int i = min(12, n - 1); i >= 1; i--) {
d[min(12, n - 1) - i] = k / s[i];
k = (k % s[i]);
if (i == 1) d[min(12, n - 1)] = k;
}
for (int i = 0; i < min(13, n); i++) t[i] = i;
for (int i = 0; i < min(13, n); i++) {
r[i] = t[d[i]] + (n - min(n, 13) + 1);
swap(t[d[i]], t[min(12, n - 1) - i]);
sort(t, t + (min(12, n - 1) - i));
}
sort(a, a + 1022);
int ans = upper_bound(a, a + 1022, n - 13) - a;
for (int i = 0; i < min(n, 13); i++) {
if (binary_search(a, a + 1023, i + (n - min(12, n - 1)))) {
if (binary_search(a, a + 1023, r[i])) ans++;
}
}
cout << ans << endl;
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, k;
int s[20], d[20], t[20], r[20];
int a[2000];
void dfs(long long x) {
if (x > 1000000000) return;
a[a[2000]++] = x;
dfs(x * 10 + 4);
dfs(x * 10 + 7);
}
int main() {
cin >> n;
cin >> k;
k--;
a[2000] = 0;
dfs(4);
dfs(7);
int x = 1;
s[1] = 1;
for (int i = 1; i < 12; i++) {
x++;
s[i + 1] = (s[i] * x);
}
if (n <= 12 && s[n] <= k) {
cout << -1 << endl;
return 0;
}
for (int i = min(12, n - 1); i >= 1; i--) {
d[min(12, n - 1) - i] = k / s[i];
k = (k % s[i]);
if (i == 1) d[min(12, n - 1)] = k;
}
for (int i = 0; i < min(13, n); i++) t[i] = i;
for (int i = 0; i < min(13, n); i++) {
r[i] = t[d[i]] + (n - min(n, 13) + 1);
swap(t[d[i]], t[min(12, n - 1) - i]);
sort(t, t + (min(12, n - 1) - i));
}
sort(a, a + 1022);
int ans = upper_bound(a, a + 1022, n - 13) - a;
for (int i = 0; i < min(n, 13); i++) {
if (binary_search(a, a + 1023, i + (n - min(12, n - 1)))) {
if (binary_search(a, a + 1023, r[i])) ans++;
}
}
cout << ans << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 210;
const int M = 200000;
const int K = 510;
const long long LIT = 1000000000LL;
const int INF = 1 << 30;
long long n, k;
vector<long long> num;
void dfs(long long u) {
if (u) num.push_back(u);
if (u > LIT) return;
dfs(u * 10 + 4);
dfs(u * 10 + 7);
}
void init() {
dfs(0);
sort(num.begin(), num.end());
}
bool init2() {
long long tmp = 1;
for (long long i = 1; i <= n; i++) {
tmp *= i;
if (tmp >= k) return 1;
}
cout << -1 << endl;
return 0;
}
bool valid(int x) {
for (int i = 0; i < num.size(); i++)
if (num[i] == x) return 1;
return 0;
}
vector<long long> cal(vector<long long> v, long long m) {
int len = v.size();
vector<long long> ret = vector<long long>(len);
for (int i = len - 1; i >= 0; i--) {
ret[i] = m % (len - i);
m /= len - i;
}
for (int i = len - 1; i >= 0; i--) {
for (int j = i - 1; j >= 0; j--) {
if (ret[j] > ret[i]) continue;
ret[i]++;
}
}
for (int i = 0; i < len; i++) ret[i] = v[ret[i]];
return ret;
}
void solve() {
vector<long long> v;
long long bnd = max(n - 15, 1LL);
for (long long i = bnd; i <= n; i++) v.push_back(i);
v = cal(v, --k);
int ret = 0;
for (int i = 0; i < num.size(); i++) {
if (num[i] > bnd) break;
ret++;
}
for (int i = 0; i < v.size(); i++) {
long long p = bnd + i;
long long m = v[i];
if (valid(p) && valid(m)) ret++;
}
cout << ret << endl;
}
int main() {
init();
while (cin >> n >> k) {
if (!init2()) continue;
solve();
}
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 210;
const int M = 200000;
const int K = 510;
const long long LIT = 1000000000LL;
const int INF = 1 << 30;
long long n, k;
vector<long long> num;
void dfs(long long u) {
if (u) num.push_back(u);
if (u > LIT) return;
dfs(u * 10 + 4);
dfs(u * 10 + 7);
}
void init() {
dfs(0);
sort(num.begin(), num.end());
}
bool init2() {
long long tmp = 1;
for (long long i = 1; i <= n; i++) {
tmp *= i;
if (tmp >= k) return 1;
}
cout << -1 << endl;
return 0;
}
bool valid(int x) {
for (int i = 0; i < num.size(); i++)
if (num[i] == x) return 1;
return 0;
}
vector<long long> cal(vector<long long> v, long long m) {
int len = v.size();
vector<long long> ret = vector<long long>(len);
for (int i = len - 1; i >= 0; i--) {
ret[i] = m % (len - i);
m /= len - i;
}
for (int i = len - 1; i >= 0; i--) {
for (int j = i - 1; j >= 0; j--) {
if (ret[j] > ret[i]) continue;
ret[i]++;
}
}
for (int i = 0; i < len; i++) ret[i] = v[ret[i]];
return ret;
}
void solve() {
vector<long long> v;
long long bnd = max(n - 15, 1LL);
for (long long i = bnd; i <= n; i++) v.push_back(i);
v = cal(v, --k);
int ret = 0;
for (int i = 0; i < num.size(); i++) {
if (num[i] > bnd) break;
ret++;
}
for (int i = 0; i < v.size(); i++) {
long long p = bnd + i;
long long m = v[i];
if (valid(p) && valid(m)) ret++;
}
cout << ret << endl;
}
int main() {
init();
while (cin >> n >> k) {
if (!init2()) continue;
solve();
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const long long inf = 5e18;
vector<string> num_string({"4", "7"});
map<long long, int> mp;
vector<long long> fact;
bool check(long long mid, long long n, long long k) {
if (n - mid >= fact.size()) {
return false;
} else {
return fact[n - mid] <= k;
}
}
int main() {
fact.push_back(1);
while (fact.back() < 6e12) {
fact.push_back(fact.back() * (fact.size()));
}
long long fs = fact.size();
long long n, k;
cin >> n >> k;
if (n < fact.size() and fact[n] < k) {
cout << -1 << endl;
return 0;
}
k--;
while (num_string.back().size() < 10) {
vector<string> next_num = num_string;
for (string s : num_string) {
if (s.size() == num_string.back().size()) {
s.push_back('4');
next_num.push_back(s);
s.back() = '7';
next_num.push_back(s);
}
}
num_string = next_num;
}
for (string s : num_string) {
mp[(stoll(s))] = 1;
}
long long ans = 0;
for (auto& x : mp) {
if (x.first <= n) {
ans++;
}
}
int i = max(1ll, n - fs + 1);
vector<long long> remainingElements;
for (int j = i; j <= n; j++) {
remainingElements.push_back(j);
}
while (i <= n) {
long long rem = n - i;
if (fact[rem] <= k) {
long long div = k / fact[rem];
k -= div * fact[rem];
if (mp[i] and !mp[remainingElements[div]]) {
ans--;
}
remainingElements.erase(remainingElements.begin() + div);
} else {
if (mp[i] and !mp[remainingElements[0]]) {
ans--;
}
remainingElements.erase(remainingElements.begin());
}
i++;
}
cout << ans << endl;
}
| ### Prompt
Create a solution in cpp for the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const long long inf = 5e18;
vector<string> num_string({"4", "7"});
map<long long, int> mp;
vector<long long> fact;
bool check(long long mid, long long n, long long k) {
if (n - mid >= fact.size()) {
return false;
} else {
return fact[n - mid] <= k;
}
}
int main() {
fact.push_back(1);
while (fact.back() < 6e12) {
fact.push_back(fact.back() * (fact.size()));
}
long long fs = fact.size();
long long n, k;
cin >> n >> k;
if (n < fact.size() and fact[n] < k) {
cout << -1 << endl;
return 0;
}
k--;
while (num_string.back().size() < 10) {
vector<string> next_num = num_string;
for (string s : num_string) {
if (s.size() == num_string.back().size()) {
s.push_back('4');
next_num.push_back(s);
s.back() = '7';
next_num.push_back(s);
}
}
num_string = next_num;
}
for (string s : num_string) {
mp[(stoll(s))] = 1;
}
long long ans = 0;
for (auto& x : mp) {
if (x.first <= n) {
ans++;
}
}
int i = max(1ll, n - fs + 1);
vector<long long> remainingElements;
for (int j = i; j <= n; j++) {
remainingElements.push_back(j);
}
while (i <= n) {
long long rem = n - i;
if (fact[rem] <= k) {
long long div = k / fact[rem];
k -= div * fact[rem];
if (mp[i] and !mp[remainingElements[div]]) {
ans--;
}
remainingElements.erase(remainingElements.begin() + div);
} else {
if (mp[i] and !mp[remainingElements[0]]) {
ans--;
}
remainingElements.erase(remainingElements.begin());
}
i++;
}
cout << ans << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long int fact(long long int n) {
if (n == 0) return 1;
return n * fact(n - 1);
}
vector<int> permutacion(int n, int k) {
if (n == 1) return vector<int>(1, 1);
for (int val = 1; val <= n; val++) {
if (val * fact(n - 1) >= k) {
vector<int> v = permutacion(n - 1, k - (val - 1) * fact(n - 1));
for (int i = 0; i < int(v.size()); i++)
if (v[i] >= val) v[i]++;
vector<int> nextv(1, val);
for (int i = 0; i < int(v.size()); i++) nextv.push_back(v[i]);
return nextv;
}
}
return vector<int>(1, 1);
}
void escriu(vector<int> v) {
for (int i = 0; i < int(v.size()); i++) cout << v[i] << ",";
cout << endl;
}
int main() {
vector<long long int> number;
for (int numdig = 1; numdig <= 10; numdig++) {
for (long long int c = 0; c < (1LL << numdig); c++) {
long long int num = 0;
long long int val = c;
for (int i = 0; i < numdig; i++) {
int dig = 4;
if (val % 2 == 1) dig = 7;
val /= 2;
num = 10 * num + dig;
}
number.push_back(num);
}
}
sort(number.begin(), number.end());
set<long long int> s;
for (int i = 0; i < int(number.size()); i++) s.insert(number[i]);
long long int n, k;
cin >> n >> k;
vector<int> p = permutacion(15, k);
if (n < 15 and fact(n) < k) {
cout << -1 << endl;
} else {
long long int sol = 0;
for (int i = 0; i < int(number.size()); i++) {
long long int index = number[i];
if (index < n - 15 + 1) {
sol++;
} else if (index <= n) {
long long int desp = n - 15;
if (s.find(p[index - (n - 15 + 1)] + desp) != s.end()) {
sol++;
}
}
}
cout << sol << endl;
}
}
| ### Prompt
In cpp, your task is to solve the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long int fact(long long int n) {
if (n == 0) return 1;
return n * fact(n - 1);
}
vector<int> permutacion(int n, int k) {
if (n == 1) return vector<int>(1, 1);
for (int val = 1; val <= n; val++) {
if (val * fact(n - 1) >= k) {
vector<int> v = permutacion(n - 1, k - (val - 1) * fact(n - 1));
for (int i = 0; i < int(v.size()); i++)
if (v[i] >= val) v[i]++;
vector<int> nextv(1, val);
for (int i = 0; i < int(v.size()); i++) nextv.push_back(v[i]);
return nextv;
}
}
return vector<int>(1, 1);
}
void escriu(vector<int> v) {
for (int i = 0; i < int(v.size()); i++) cout << v[i] << ",";
cout << endl;
}
int main() {
vector<long long int> number;
for (int numdig = 1; numdig <= 10; numdig++) {
for (long long int c = 0; c < (1LL << numdig); c++) {
long long int num = 0;
long long int val = c;
for (int i = 0; i < numdig; i++) {
int dig = 4;
if (val % 2 == 1) dig = 7;
val /= 2;
num = 10 * num + dig;
}
number.push_back(num);
}
}
sort(number.begin(), number.end());
set<long long int> s;
for (int i = 0; i < int(number.size()); i++) s.insert(number[i]);
long long int n, k;
cin >> n >> k;
vector<int> p = permutacion(15, k);
if (n < 15 and fact(n) < k) {
cout << -1 << endl;
} else {
long long int sol = 0;
for (int i = 0; i < int(number.size()); i++) {
long long int index = number[i];
if (index < n - 15 + 1) {
sol++;
} else if (index <= n) {
long long int desp = n - 15;
if (s.find(p[index - (n - 15 + 1)] + desp) != s.end()) {
sol++;
}
}
}
cout << sol << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10000 + 1000;
const int inf = 1e9;
int per[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
bool is_lucky(int x) {
while (x) {
if (x % 10 != 4 && x % 10 != 7) return 0;
x /= 10;
}
return 1;
}
long long makelucky(long long x, int l) {
long long lucky = 0;
while (l--) {
if (x & 1)
lucky = lucky * 10 + 7;
else
lucky = lucky * 10 + 4;
x /= 2;
}
return lucky;
}
long long solve(int n) {
set<long long> st;
for (long long l = 1; l <= 10; l++)
for (long long i = 0; i < ((long long)1 << l); i++)
st.insert(makelucky(i, l));
long long res = 0;
while (!st.empty()) {
long long lucky = *st.begin();
st.erase(*st.begin());
res += (lucky <= n - 13);
}
return res;
}
long long fact(long long x) {
long long res = 1;
for (int i = 1; i <= x; i++) res *= i;
return res;
}
void kthper(int k, int l, int r) {
if (r - l == 0) return;
long long f = fact(r - l), t = 0;
while ((t + 1) * f <= k) t++;
if (t) {
int b = per[l];
per[l] = per[l + t];
for (int i = l + t; i >= l + 2; i--) per[i] = per[i - 1];
per[l + 1] = b;
}
kthper(k - t * f, l + 1, r);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, k;
cin >> n >> k;
if (n <= 14) {
long long res = 1;
for (int i = 1; i <= n; i++) res *= i;
if (k > res) return cout << -1 << endl, 0;
}
int r = min(n, (long long)13);
long long ans = solve(n);
kthper(k - 1, 1, r);
for (int i = 1; i <= r; i++)
ans += (is_lucky(n - r + i) && is_lucky(per[i] + n - r));
cout << ans << endl;
return 0;
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10000 + 1000;
const int inf = 1e9;
int per[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
bool is_lucky(int x) {
while (x) {
if (x % 10 != 4 && x % 10 != 7) return 0;
x /= 10;
}
return 1;
}
long long makelucky(long long x, int l) {
long long lucky = 0;
while (l--) {
if (x & 1)
lucky = lucky * 10 + 7;
else
lucky = lucky * 10 + 4;
x /= 2;
}
return lucky;
}
long long solve(int n) {
set<long long> st;
for (long long l = 1; l <= 10; l++)
for (long long i = 0; i < ((long long)1 << l); i++)
st.insert(makelucky(i, l));
long long res = 0;
while (!st.empty()) {
long long lucky = *st.begin();
st.erase(*st.begin());
res += (lucky <= n - 13);
}
return res;
}
long long fact(long long x) {
long long res = 1;
for (int i = 1; i <= x; i++) res *= i;
return res;
}
void kthper(int k, int l, int r) {
if (r - l == 0) return;
long long f = fact(r - l), t = 0;
while ((t + 1) * f <= k) t++;
if (t) {
int b = per[l];
per[l] = per[l + t];
for (int i = l + t; i >= l + 2; i--) per[i] = per[i - 1];
per[l + 1] = b;
}
kthper(k - t * f, l + 1, r);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, k;
cin >> n >> k;
if (n <= 14) {
long long res = 1;
for (int i = 1; i <= n; i++) res *= i;
if (k > res) return cout << -1 << endl, 0;
}
int r = min(n, (long long)13);
long long ans = solve(n);
kthper(k - 1, 1, r);
for (int i = 1; i <= r; i++)
ans += (is_lucky(n - r + i) && is_lucky(per[i] + n - r));
cout << ans << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int c = 15;
int n, k;
int r;
long long f[c];
vector<int> v;
int ans;
void go(long long i) {
if (i > (long long)n) return;
if (i) {
if (i <= n - r)
ans++;
else
v.push_back(i);
}
go(i * 10 + 4);
go(i * 10 + 7);
}
bool b[c];
bool q[c];
int main() {
int i;
f[0] = 1;
for (i = 1; i < c; ++i) f[i] = f[i - 1] * i;
scanf("%d%d", &n, &k);
if (n < c && f[n] < k) {
printf("-1\n");
return 0;
}
i = 1;
while (f[i] < k) ++i;
r = i;
go(0);
sort(v.begin(), v.end());
for (i = 0; i < v.size(); ++i) b[v[i] - (n - r)] = 1;
k--;
i = 1;
int j;
while (i <= r) {
int d = (k / f[r - i]) + 1;
k %= f[r - i];
j = 1;
while (d) {
if (!q[j]) --d;
if (d) ++j;
}
q[j] = 1;
if (b[i] && b[j]) ++ans;
++i;
}
printf("%d\n", ans);
return 0;
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int c = 15;
int n, k;
int r;
long long f[c];
vector<int> v;
int ans;
void go(long long i) {
if (i > (long long)n) return;
if (i) {
if (i <= n - r)
ans++;
else
v.push_back(i);
}
go(i * 10 + 4);
go(i * 10 + 7);
}
bool b[c];
bool q[c];
int main() {
int i;
f[0] = 1;
for (i = 1; i < c; ++i) f[i] = f[i - 1] * i;
scanf("%d%d", &n, &k);
if (n < c && f[n] < k) {
printf("-1\n");
return 0;
}
i = 1;
while (f[i] < k) ++i;
r = i;
go(0);
sort(v.begin(), v.end());
for (i = 0; i < v.size(); ++i) b[v[i] - (n - r)] = 1;
k--;
i = 1;
int j;
while (i <= r) {
int d = (k / f[r - i]) + 1;
k %= f[r - i];
j = 1;
while (d) {
if (!q[j]) --d;
if (d) ++j;
}
q[j] = 1;
if (b[i] && b[j]) ++ans;
++i;
}
printf("%d\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
bool islucky(long long n) {
while (n) {
if (n % 10 == 4 || n % 10 == 7) {
} else
return 0;
n = n / 10;
}
return 1;
}
long long fact(long long k) {
long long n = 1;
while (k > 1) {
n = n * k;
k--;
}
return n;
}
vector<long long> factoradic_number(long long k) {
vector<long long> fac;
for (int i = 0; i <= 13; i++) {
fac.push_back((fact(i)));
}
long long kk = k, coef = 0;
;
vector<long long> factoradic;
int rp = 1;
while (kk >= fac[rp]) rp++;
while (kk) {
rp--;
coef = kk / fac[rp];
factoradic.push_back((coef));
kk = kk - fac[rp] * coef;
if (kk == 0) {
while (rp != 1) {
factoradic.push_back((0));
rp--;
}
}
}
return factoradic;
}
vector<long long> kthpermutation(vector<long long> v, long long k) {
vector<long long> factoradic;
vector<long long> permutation;
factoradic = factoradic_number(k);
while (factoradic.size() < v.size() - 1)
factoradic.insert(factoradic.begin(), 0);
int sz = v.size();
while (permutation.size() != sz - 1) {
permutation.push_back((v[factoradic[0]]));
v.erase(v.begin() + factoradic[0], v.begin() + factoradic[0] + 1);
factoradic.erase(factoradic.begin(), factoradic.begin() + 1);
}
permutation.push_back((v[0]));
return permutation;
}
int main() {
long long n, k;
cin >> n >> k;
vector<long long> fac;
for (int i = 0; i <= 13; i++) {
fac.push_back((fact(i)));
}
if (n <= 13) {
if (fac[n] < k) {
cout << "-1";
return 0;
}
}
k--;
vector<long long> num;
int sz = 2;
long long r, l;
num.push_back((4));
num.push_back((7));
int noe = 0;
while (num[num.size() - 1] != 777777777) {
sz = num.size();
for (int i = (sz - 1) / 2; i < sz; i++) {
long long p = num[i];
long long q = p * 10 + 4;
num.push_back((q));
q = p * 10 + 7;
num.push_back((q));
}
sz = num.size();
}
bool flag = 0;
long long nn = n;
if (n > 13) {
flag = 1;
n = n - 13;
}
int i = 0;
vector<long long> pq;
long long lll = 1;
if (flag) {
while (n >= num[i] && flag && i < num.size()) {
noe++;
i++;
}
lll = n + 1;
while (n++ < nn) {
pq.push_back((n));
}
n = nn;
} else {
nn = 1;
lll = 1;
while (n >= nn) {
pq.push_back((nn));
nn++;
}
}
vector<long long> pq1 = kthpermutation(pq, k);
int ii = 0;
for (; lll <= n; lll++) {
if (islucky(lll) && islucky(pq1[ii])) noe++;
ii++;
}
cout << noe;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
bool islucky(long long n) {
while (n) {
if (n % 10 == 4 || n % 10 == 7) {
} else
return 0;
n = n / 10;
}
return 1;
}
long long fact(long long k) {
long long n = 1;
while (k > 1) {
n = n * k;
k--;
}
return n;
}
vector<long long> factoradic_number(long long k) {
vector<long long> fac;
for (int i = 0; i <= 13; i++) {
fac.push_back((fact(i)));
}
long long kk = k, coef = 0;
;
vector<long long> factoradic;
int rp = 1;
while (kk >= fac[rp]) rp++;
while (kk) {
rp--;
coef = kk / fac[rp];
factoradic.push_back((coef));
kk = kk - fac[rp] * coef;
if (kk == 0) {
while (rp != 1) {
factoradic.push_back((0));
rp--;
}
}
}
return factoradic;
}
vector<long long> kthpermutation(vector<long long> v, long long k) {
vector<long long> factoradic;
vector<long long> permutation;
factoradic = factoradic_number(k);
while (factoradic.size() < v.size() - 1)
factoradic.insert(factoradic.begin(), 0);
int sz = v.size();
while (permutation.size() != sz - 1) {
permutation.push_back((v[factoradic[0]]));
v.erase(v.begin() + factoradic[0], v.begin() + factoradic[0] + 1);
factoradic.erase(factoradic.begin(), factoradic.begin() + 1);
}
permutation.push_back((v[0]));
return permutation;
}
int main() {
long long n, k;
cin >> n >> k;
vector<long long> fac;
for (int i = 0; i <= 13; i++) {
fac.push_back((fact(i)));
}
if (n <= 13) {
if (fac[n] < k) {
cout << "-1";
return 0;
}
}
k--;
vector<long long> num;
int sz = 2;
long long r, l;
num.push_back((4));
num.push_back((7));
int noe = 0;
while (num[num.size() - 1] != 777777777) {
sz = num.size();
for (int i = (sz - 1) / 2; i < sz; i++) {
long long p = num[i];
long long q = p * 10 + 4;
num.push_back((q));
q = p * 10 + 7;
num.push_back((q));
}
sz = num.size();
}
bool flag = 0;
long long nn = n;
if (n > 13) {
flag = 1;
n = n - 13;
}
int i = 0;
vector<long long> pq;
long long lll = 1;
if (flag) {
while (n >= num[i] && flag && i < num.size()) {
noe++;
i++;
}
lll = n + 1;
while (n++ < nn) {
pq.push_back((n));
}
n = nn;
} else {
nn = 1;
lll = 1;
while (n >= nn) {
pq.push_back((nn));
nn++;
}
}
vector<long long> pq1 = kthpermutation(pq, k);
int ii = 0;
for (; lll <= n; lll++) {
if (islucky(lll) && islucky(pq1[ii])) noe++;
ii++;
}
cout << noe;
}
``` |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:640000000")
using namespace std;
bool lucky(int x) {
for (; x; x /= 10)
if (x % 10 != 4 && x % 10 != 7) return false;
return true;
}
unsigned long long luck[] = {
4, 7, 44, 47, 74, 77, 444,
447, 474, 477, 744, 747, 774, 777,
4444, 4447, 4474, 4477, 4744, 4747, 4774,
4777, 7444, 7447, 7474, 7477, 7744, 7747,
7774, 7777, 44444, 44447, 44474, 44477, 44744,
44747, 44774, 44777, 47444, 47447, 47474, 47477,
47744, 47747, 47774, 47777, 74444, 74447, 74474,
74477, 74744, 74747, 74774, 74777, 77444, 77447,
77474, 77477, 77744, 77747, 77774, 77777, 444444,
444447, 444474, 444477, 444744, 444747, 444774, 444777,
447444, 447447, 447474, 447477, 447744, 447747, 447774,
447777, 474444, 474447, 474474, 474477, 474744, 474747,
474774, 474777, 477444, 477447, 477474, 477477, 477744,
477747, 477774, 477777, 744444, 744447, 744474, 744477,
744744, 744747, 744774, 744777, 747444, 747447, 747474,
747477, 747744, 747747, 747774, 747777, 774444, 774447,
774474, 774477, 774744, 774747, 774774, 774777, 777444,
777447, 777474, 777477, 777744, 777747, 777774, 777777,
4444444, 4444447, 4444474, 4444477, 4444744, 4444747, 4444774,
4444777, 4447444, 4447447, 4447474, 4447477, 4447744, 4447747,
4447774, 4447777, 4474444, 4474447, 4474474, 4474477, 4474744,
4474747, 4474774, 4474777, 4477444, 4477447, 4477474, 4477477,
4477744, 4477747, 4477774, 4477777, 4744444, 4744447, 4744474,
4744477, 4744744, 4744747, 4744774, 4744777, 4747444, 4747447,
4747474, 4747477, 4747744, 4747747, 4747774, 4747777, 4774444,
4774447, 4774474, 4774477, 4774744, 4774747, 4774774, 4774777,
4777444, 4777447, 4777474, 4777477, 4777744, 4777747, 4777774,
4777777, 7444444, 7444447, 7444474, 7444477, 7444744, 7444747,
7444774, 7444777, 7447444, 7447447, 7447474, 7447477, 7447744,
7447747, 7447774, 7447777, 7474444, 7474447, 7474474, 7474477,
7474744, 7474747, 7474774, 7474777, 7477444, 7477447, 7477474,
7477477, 7477744, 7477747, 7477774, 7477777, 7744444, 7744447,
7744474, 7744477, 7744744, 7744747, 7744774, 7744777, 7747444,
7747447, 7747474, 7747477, 7747744, 7747747, 7747774, 7747777,
7774444, 7774447, 7774474, 7774477, 7774744, 7774747, 7774774,
7774777, 7777444, 7777447, 7777474, 7777477, 7777744, 7777747,
7777774, 7777777, 44444444, 44444447, 44444474, 44444477, 44444744,
44444747, 44444774, 44444777, 44447444, 44447447, 44447474, 44447477,
44447744, 44447747, 44447774, 44447777, 44474444, 44474447, 44474474,
44474477, 44474744, 44474747, 44474774, 44474777, 44477444, 44477447,
44477474, 44477477, 44477744, 44477747, 44477774, 44477777, 44744444,
44744447, 44744474, 44744477, 44744744, 44744747, 44744774, 44744777,
44747444, 44747447, 44747474, 44747477, 44747744, 44747747, 44747774,
44747777, 44774444, 44774447, 44774474, 44774477, 44774744, 44774747,
44774774, 44774777, 44777444, 44777447, 44777474, 44777477, 44777744,
44777747, 44777774, 44777777, 47444444, 47444447, 47444474, 47444477,
47444744, 47444747, 47444774, 47444777, 47447444, 47447447, 47447474,
47447477, 47447744, 47447747, 47447774, 47447777, 47474444, 47474447,
47474474, 47474477, 47474744, 47474747, 47474774, 47474777, 47477444,
47477447, 47477474, 47477477, 47477744, 47477747, 47477774, 47477777,
47744444, 47744447, 47744474, 47744477, 47744744, 47744747, 47744774,
47744777, 47747444, 47747447, 47747474, 47747477, 47747744, 47747747,
47747774, 47747777, 47774444, 47774447, 47774474, 47774477, 47774744,
47774747, 47774774, 47774777, 47777444, 47777447, 47777474, 47777477,
47777744, 47777747, 47777774, 47777777, 74444444, 74444447, 74444474,
74444477, 74444744, 74444747, 74444774, 74444777, 74447444, 74447447,
74447474, 74447477, 74447744, 74447747, 74447774, 74447777, 74474444,
74474447, 74474474, 74474477, 74474744, 74474747, 74474774, 74474777,
74477444, 74477447, 74477474, 74477477, 74477744, 74477747, 74477774,
74477777, 74744444, 74744447, 74744474, 74744477, 74744744, 74744747,
74744774, 74744777, 74747444, 74747447, 74747474, 74747477, 74747744,
74747747, 74747774, 74747777, 74774444, 74774447, 74774474, 74774477,
74774744, 74774747, 74774774, 74774777, 74777444, 74777447, 74777474,
74777477, 74777744, 74777747, 74777774, 74777777, 77444444, 77444447,
77444474, 77444477, 77444744, 77444747, 77444774, 77444777, 77447444,
77447447, 77447474, 77447477, 77447744, 77447747, 77447774, 77447777,
77474444, 77474447, 77474474, 77474477, 77474744, 77474747, 77474774,
77474777, 77477444, 77477447, 77477474, 77477477, 77477744, 77477747,
77477774, 77477777, 77744444, 77744447, 77744474, 77744477, 77744744,
77744747, 77744774, 77744777, 77747444, 77747447, 77747474, 77747477,
77747744, 77747747, 77747774, 77747777, 77774444, 77774447, 77774474,
77774477, 77774744, 77774747, 77774774, 77774777, 77777444, 77777447,
77777474, 77777477, 77777744, 77777747, 77777774, 77777777, 444444444,
444444447, 444444474, 444444477, 444444744, 444444747, 444444774, 444444777,
444447444, 444447447, 444447474, 444447477, 444447744, 444447747, 444447774,
444447777, 444474444, 444474447, 444474474, 444474477, 444474744, 444474747,
444474774, 444474777, 444477444, 444477447, 444477474, 444477477, 444477744,
444477747, 444477774, 444477777, 444744444, 444744447, 444744474, 444744477,
444744744, 444744747, 444744774, 444744777, 444747444, 444747447, 444747474,
444747477, 444747744, 444747747, 444747774, 444747777, 444774444, 444774447,
444774474, 444774477, 444774744, 444774747, 444774774, 444774777, 444777444,
444777447, 444777474, 444777477, 444777744, 444777747, 444777774, 444777777,
447444444, 447444447, 447444474, 447444477, 447444744, 447444747, 447444774,
447444777, 447447444, 447447447, 447447474, 447447477, 447447744, 447447747,
447447774, 447447777, 447474444, 447474447, 447474474, 447474477, 447474744,
447474747, 447474774, 447474777, 447477444, 447477447, 447477474, 447477477,
447477744, 447477747, 447477774, 447477777, 447744444, 447744447, 447744474,
447744477, 447744744, 447744747, 447744774, 447744777, 447747444, 447747447,
447747474, 447747477, 447747744, 447747747, 447747774, 447747777, 447774444,
447774447, 447774474, 447774477, 447774744, 447774747, 447774774, 447774777,
447777444, 447777447, 447777474, 447777477, 447777744, 447777747, 447777774,
447777777, 474444444, 474444447, 474444474, 474444477, 474444744, 474444747,
474444774, 474444777, 474447444, 474447447, 474447474, 474447477, 474447744,
474447747, 474447774, 474447777, 474474444, 474474447, 474474474, 474474477,
474474744, 474474747, 474474774, 474474777, 474477444, 474477447, 474477474,
474477477, 474477744, 474477747, 474477774, 474477777, 474744444, 474744447,
474744474, 474744477, 474744744, 474744747, 474744774, 474744777, 474747444,
474747447, 474747474, 474747477, 474747744, 474747747, 474747774, 474747777,
474774444, 474774447, 474774474, 474774477, 474774744, 474774747, 474774774,
474774777, 474777444, 474777447, 474777474, 474777477, 474777744, 474777747,
474777774, 474777777, 477444444, 477444447, 477444474, 477444477, 477444744,
477444747, 477444774, 477444777, 477447444, 477447447, 477447474, 477447477,
477447744, 477447747, 477447774, 477447777, 477474444, 477474447, 477474474,
477474477, 477474744, 477474747, 477474774, 477474777, 477477444, 477477447,
477477474, 477477477, 477477744, 477477747, 477477774, 477477777, 477744444,
477744447, 477744474, 477744477, 477744744, 477744747, 477744774, 477744777,
477747444, 477747447, 477747474, 477747477, 477747744, 477747747, 477747774,
477747777, 477774444, 477774447, 477774474, 477774477, 477774744, 477774747,
477774774, 477774777, 477777444, 477777447, 477777474, 477777477, 477777744,
477777747, 477777774, 477777777, 744444444, 744444447, 744444474, 744444477,
744444744, 744444747, 744444774, 744444777, 744447444, 744447447, 744447474,
744447477, 744447744, 744447747, 744447774, 744447777, 744474444, 744474447,
744474474, 744474477, 744474744, 744474747, 744474774, 744474777, 744477444,
744477447, 744477474, 744477477, 744477744, 744477747, 744477774, 744477777,
744744444, 744744447, 744744474, 744744477, 744744744, 744744747, 744744774,
744744777, 744747444, 744747447, 744747474, 744747477, 744747744, 744747747,
744747774, 744747777, 744774444, 744774447, 744774474, 744774477, 744774744,
744774747, 744774774, 744774777, 744777444, 744777447, 744777474, 744777477,
744777744, 744777747, 744777774, 744777777, 747444444, 747444447, 747444474,
747444477, 747444744, 747444747, 747444774, 747444777, 747447444, 747447447,
747447474, 747447477, 747447744, 747447747, 747447774, 747447777, 747474444,
747474447, 747474474, 747474477, 747474744, 747474747, 747474774, 747474777,
747477444, 747477447, 747477474, 747477477, 747477744, 747477747, 747477774,
747477777, 747744444, 747744447, 747744474, 747744477, 747744744, 747744747,
747744774, 747744777, 747747444, 747747447, 747747474, 747747477, 747747744,
747747747, 747747774, 747747777, 747774444, 747774447, 747774474, 747774477,
747774744, 747774747, 747774774, 747774777, 747777444, 747777447, 747777474,
747777477, 747777744, 747777747, 747777774, 747777777, 774444444, 774444447,
774444474, 774444477, 774444744, 774444747, 774444774, 774444777, 774447444,
774447447, 774447474, 774447477, 774447744, 774447747, 774447774, 774447777,
774474444, 774474447, 774474474, 774474477, 774474744, 774474747, 774474774,
774474777, 774477444, 774477447, 774477474, 774477477, 774477744, 774477747,
774477774, 774477777, 774744444, 774744447, 774744474, 774744477, 774744744,
774744747, 774744774, 774744777, 774747444, 774747447, 774747474, 774747477,
774747744, 774747747, 774747774, 774747777, 774774444, 774774447, 774774474,
774774477, 774774744, 774774747, 774774774, 774774777, 774777444, 774777447,
774777474, 774777477, 774777744, 774777747, 774777774, 774777777, 777444444,
777444447, 777444474, 777444477, 777444744, 777444747, 777444774, 777444777,
777447444, 777447447, 777447474, 777447477, 777447744, 777447747, 777447774,
777447777, 777474444, 777474447, 777474474, 777474477, 777474744, 777474747,
777474774, 777474777, 777477444, 777477447, 777477474, 777477477, 777477744,
777477747, 777477774, 777477777, 777744444, 777744447, 777744474, 777744477,
777744744, 777744747, 777744774, 777744777, 777747444, 777747447, 777747474,
777747477, 777747744, 777747747, 777747774, 777747777, 777774444, 777774447,
777774474, 777774477, 777774744, 777774747, 777774774, 777774777, 777777444,
777777447, 777777474, 777777477, 777777744, 777777747, 777777774, 777777777,
4444444444};
long long fact[] = {1, 1, 2, 6,
24, 120, 720, 5040,
40320, 362880, 3628800, 39916800,
479001600, 6227020800, 87178291200, 1307674368000};
void input(vector<int> &M, int n) {
M.resize(n + 1);
for (int i = 1; i <= n; i++) scanf("%d", &M[i]);
}
long long n, k;
int main() {
cin >> n >> k;
if (n <= 15 && k > fact[n])
printf("-1\n");
else {
long long st = n - 15;
if (st <= 0) st = 1;
vector<int> perm;
set<long long> was;
for (long long i = st; i <= n; i++) was.insert(i);
int j = n - st + 1;
for (long long i = st; i <= n; i++, j--) {
set<long long>::iterator it = was.begin();
while (k > fact[j - 1]) it++, k -= fact[j - 1];
perm.push_back(*it);
was.erase(it);
}
int ans = 0;
for (int i = 0; i < perm.size(); i++)
if (lucky(st + i) && lucky(perm[i])) ans++;
int q = 0;
while (luck[q] < st) q++, ans++;
printf("%d\n", ans);
}
return 0;
}
| ### Prompt
In CPP, your task is to solve the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:640000000")
using namespace std;
bool lucky(int x) {
for (; x; x /= 10)
if (x % 10 != 4 && x % 10 != 7) return false;
return true;
}
unsigned long long luck[] = {
4, 7, 44, 47, 74, 77, 444,
447, 474, 477, 744, 747, 774, 777,
4444, 4447, 4474, 4477, 4744, 4747, 4774,
4777, 7444, 7447, 7474, 7477, 7744, 7747,
7774, 7777, 44444, 44447, 44474, 44477, 44744,
44747, 44774, 44777, 47444, 47447, 47474, 47477,
47744, 47747, 47774, 47777, 74444, 74447, 74474,
74477, 74744, 74747, 74774, 74777, 77444, 77447,
77474, 77477, 77744, 77747, 77774, 77777, 444444,
444447, 444474, 444477, 444744, 444747, 444774, 444777,
447444, 447447, 447474, 447477, 447744, 447747, 447774,
447777, 474444, 474447, 474474, 474477, 474744, 474747,
474774, 474777, 477444, 477447, 477474, 477477, 477744,
477747, 477774, 477777, 744444, 744447, 744474, 744477,
744744, 744747, 744774, 744777, 747444, 747447, 747474,
747477, 747744, 747747, 747774, 747777, 774444, 774447,
774474, 774477, 774744, 774747, 774774, 774777, 777444,
777447, 777474, 777477, 777744, 777747, 777774, 777777,
4444444, 4444447, 4444474, 4444477, 4444744, 4444747, 4444774,
4444777, 4447444, 4447447, 4447474, 4447477, 4447744, 4447747,
4447774, 4447777, 4474444, 4474447, 4474474, 4474477, 4474744,
4474747, 4474774, 4474777, 4477444, 4477447, 4477474, 4477477,
4477744, 4477747, 4477774, 4477777, 4744444, 4744447, 4744474,
4744477, 4744744, 4744747, 4744774, 4744777, 4747444, 4747447,
4747474, 4747477, 4747744, 4747747, 4747774, 4747777, 4774444,
4774447, 4774474, 4774477, 4774744, 4774747, 4774774, 4774777,
4777444, 4777447, 4777474, 4777477, 4777744, 4777747, 4777774,
4777777, 7444444, 7444447, 7444474, 7444477, 7444744, 7444747,
7444774, 7444777, 7447444, 7447447, 7447474, 7447477, 7447744,
7447747, 7447774, 7447777, 7474444, 7474447, 7474474, 7474477,
7474744, 7474747, 7474774, 7474777, 7477444, 7477447, 7477474,
7477477, 7477744, 7477747, 7477774, 7477777, 7744444, 7744447,
7744474, 7744477, 7744744, 7744747, 7744774, 7744777, 7747444,
7747447, 7747474, 7747477, 7747744, 7747747, 7747774, 7747777,
7774444, 7774447, 7774474, 7774477, 7774744, 7774747, 7774774,
7774777, 7777444, 7777447, 7777474, 7777477, 7777744, 7777747,
7777774, 7777777, 44444444, 44444447, 44444474, 44444477, 44444744,
44444747, 44444774, 44444777, 44447444, 44447447, 44447474, 44447477,
44447744, 44447747, 44447774, 44447777, 44474444, 44474447, 44474474,
44474477, 44474744, 44474747, 44474774, 44474777, 44477444, 44477447,
44477474, 44477477, 44477744, 44477747, 44477774, 44477777, 44744444,
44744447, 44744474, 44744477, 44744744, 44744747, 44744774, 44744777,
44747444, 44747447, 44747474, 44747477, 44747744, 44747747, 44747774,
44747777, 44774444, 44774447, 44774474, 44774477, 44774744, 44774747,
44774774, 44774777, 44777444, 44777447, 44777474, 44777477, 44777744,
44777747, 44777774, 44777777, 47444444, 47444447, 47444474, 47444477,
47444744, 47444747, 47444774, 47444777, 47447444, 47447447, 47447474,
47447477, 47447744, 47447747, 47447774, 47447777, 47474444, 47474447,
47474474, 47474477, 47474744, 47474747, 47474774, 47474777, 47477444,
47477447, 47477474, 47477477, 47477744, 47477747, 47477774, 47477777,
47744444, 47744447, 47744474, 47744477, 47744744, 47744747, 47744774,
47744777, 47747444, 47747447, 47747474, 47747477, 47747744, 47747747,
47747774, 47747777, 47774444, 47774447, 47774474, 47774477, 47774744,
47774747, 47774774, 47774777, 47777444, 47777447, 47777474, 47777477,
47777744, 47777747, 47777774, 47777777, 74444444, 74444447, 74444474,
74444477, 74444744, 74444747, 74444774, 74444777, 74447444, 74447447,
74447474, 74447477, 74447744, 74447747, 74447774, 74447777, 74474444,
74474447, 74474474, 74474477, 74474744, 74474747, 74474774, 74474777,
74477444, 74477447, 74477474, 74477477, 74477744, 74477747, 74477774,
74477777, 74744444, 74744447, 74744474, 74744477, 74744744, 74744747,
74744774, 74744777, 74747444, 74747447, 74747474, 74747477, 74747744,
74747747, 74747774, 74747777, 74774444, 74774447, 74774474, 74774477,
74774744, 74774747, 74774774, 74774777, 74777444, 74777447, 74777474,
74777477, 74777744, 74777747, 74777774, 74777777, 77444444, 77444447,
77444474, 77444477, 77444744, 77444747, 77444774, 77444777, 77447444,
77447447, 77447474, 77447477, 77447744, 77447747, 77447774, 77447777,
77474444, 77474447, 77474474, 77474477, 77474744, 77474747, 77474774,
77474777, 77477444, 77477447, 77477474, 77477477, 77477744, 77477747,
77477774, 77477777, 77744444, 77744447, 77744474, 77744477, 77744744,
77744747, 77744774, 77744777, 77747444, 77747447, 77747474, 77747477,
77747744, 77747747, 77747774, 77747777, 77774444, 77774447, 77774474,
77774477, 77774744, 77774747, 77774774, 77774777, 77777444, 77777447,
77777474, 77777477, 77777744, 77777747, 77777774, 77777777, 444444444,
444444447, 444444474, 444444477, 444444744, 444444747, 444444774, 444444777,
444447444, 444447447, 444447474, 444447477, 444447744, 444447747, 444447774,
444447777, 444474444, 444474447, 444474474, 444474477, 444474744, 444474747,
444474774, 444474777, 444477444, 444477447, 444477474, 444477477, 444477744,
444477747, 444477774, 444477777, 444744444, 444744447, 444744474, 444744477,
444744744, 444744747, 444744774, 444744777, 444747444, 444747447, 444747474,
444747477, 444747744, 444747747, 444747774, 444747777, 444774444, 444774447,
444774474, 444774477, 444774744, 444774747, 444774774, 444774777, 444777444,
444777447, 444777474, 444777477, 444777744, 444777747, 444777774, 444777777,
447444444, 447444447, 447444474, 447444477, 447444744, 447444747, 447444774,
447444777, 447447444, 447447447, 447447474, 447447477, 447447744, 447447747,
447447774, 447447777, 447474444, 447474447, 447474474, 447474477, 447474744,
447474747, 447474774, 447474777, 447477444, 447477447, 447477474, 447477477,
447477744, 447477747, 447477774, 447477777, 447744444, 447744447, 447744474,
447744477, 447744744, 447744747, 447744774, 447744777, 447747444, 447747447,
447747474, 447747477, 447747744, 447747747, 447747774, 447747777, 447774444,
447774447, 447774474, 447774477, 447774744, 447774747, 447774774, 447774777,
447777444, 447777447, 447777474, 447777477, 447777744, 447777747, 447777774,
447777777, 474444444, 474444447, 474444474, 474444477, 474444744, 474444747,
474444774, 474444777, 474447444, 474447447, 474447474, 474447477, 474447744,
474447747, 474447774, 474447777, 474474444, 474474447, 474474474, 474474477,
474474744, 474474747, 474474774, 474474777, 474477444, 474477447, 474477474,
474477477, 474477744, 474477747, 474477774, 474477777, 474744444, 474744447,
474744474, 474744477, 474744744, 474744747, 474744774, 474744777, 474747444,
474747447, 474747474, 474747477, 474747744, 474747747, 474747774, 474747777,
474774444, 474774447, 474774474, 474774477, 474774744, 474774747, 474774774,
474774777, 474777444, 474777447, 474777474, 474777477, 474777744, 474777747,
474777774, 474777777, 477444444, 477444447, 477444474, 477444477, 477444744,
477444747, 477444774, 477444777, 477447444, 477447447, 477447474, 477447477,
477447744, 477447747, 477447774, 477447777, 477474444, 477474447, 477474474,
477474477, 477474744, 477474747, 477474774, 477474777, 477477444, 477477447,
477477474, 477477477, 477477744, 477477747, 477477774, 477477777, 477744444,
477744447, 477744474, 477744477, 477744744, 477744747, 477744774, 477744777,
477747444, 477747447, 477747474, 477747477, 477747744, 477747747, 477747774,
477747777, 477774444, 477774447, 477774474, 477774477, 477774744, 477774747,
477774774, 477774777, 477777444, 477777447, 477777474, 477777477, 477777744,
477777747, 477777774, 477777777, 744444444, 744444447, 744444474, 744444477,
744444744, 744444747, 744444774, 744444777, 744447444, 744447447, 744447474,
744447477, 744447744, 744447747, 744447774, 744447777, 744474444, 744474447,
744474474, 744474477, 744474744, 744474747, 744474774, 744474777, 744477444,
744477447, 744477474, 744477477, 744477744, 744477747, 744477774, 744477777,
744744444, 744744447, 744744474, 744744477, 744744744, 744744747, 744744774,
744744777, 744747444, 744747447, 744747474, 744747477, 744747744, 744747747,
744747774, 744747777, 744774444, 744774447, 744774474, 744774477, 744774744,
744774747, 744774774, 744774777, 744777444, 744777447, 744777474, 744777477,
744777744, 744777747, 744777774, 744777777, 747444444, 747444447, 747444474,
747444477, 747444744, 747444747, 747444774, 747444777, 747447444, 747447447,
747447474, 747447477, 747447744, 747447747, 747447774, 747447777, 747474444,
747474447, 747474474, 747474477, 747474744, 747474747, 747474774, 747474777,
747477444, 747477447, 747477474, 747477477, 747477744, 747477747, 747477774,
747477777, 747744444, 747744447, 747744474, 747744477, 747744744, 747744747,
747744774, 747744777, 747747444, 747747447, 747747474, 747747477, 747747744,
747747747, 747747774, 747747777, 747774444, 747774447, 747774474, 747774477,
747774744, 747774747, 747774774, 747774777, 747777444, 747777447, 747777474,
747777477, 747777744, 747777747, 747777774, 747777777, 774444444, 774444447,
774444474, 774444477, 774444744, 774444747, 774444774, 774444777, 774447444,
774447447, 774447474, 774447477, 774447744, 774447747, 774447774, 774447777,
774474444, 774474447, 774474474, 774474477, 774474744, 774474747, 774474774,
774474777, 774477444, 774477447, 774477474, 774477477, 774477744, 774477747,
774477774, 774477777, 774744444, 774744447, 774744474, 774744477, 774744744,
774744747, 774744774, 774744777, 774747444, 774747447, 774747474, 774747477,
774747744, 774747747, 774747774, 774747777, 774774444, 774774447, 774774474,
774774477, 774774744, 774774747, 774774774, 774774777, 774777444, 774777447,
774777474, 774777477, 774777744, 774777747, 774777774, 774777777, 777444444,
777444447, 777444474, 777444477, 777444744, 777444747, 777444774, 777444777,
777447444, 777447447, 777447474, 777447477, 777447744, 777447747, 777447774,
777447777, 777474444, 777474447, 777474474, 777474477, 777474744, 777474747,
777474774, 777474777, 777477444, 777477447, 777477474, 777477477, 777477744,
777477747, 777477774, 777477777, 777744444, 777744447, 777744474, 777744477,
777744744, 777744747, 777744774, 777744777, 777747444, 777747447, 777747474,
777747477, 777747744, 777747747, 777747774, 777747777, 777774444, 777774447,
777774474, 777774477, 777774744, 777774747, 777774774, 777774777, 777777444,
777777447, 777777474, 777777477, 777777744, 777777747, 777777774, 777777777,
4444444444};
long long fact[] = {1, 1, 2, 6,
24, 120, 720, 5040,
40320, 362880, 3628800, 39916800,
479001600, 6227020800, 87178291200, 1307674368000};
void input(vector<int> &M, int n) {
M.resize(n + 1);
for (int i = 1; i <= n; i++) scanf("%d", &M[i]);
}
long long n, k;
int main() {
cin >> n >> k;
if (n <= 15 && k > fact[n])
printf("-1\n");
else {
long long st = n - 15;
if (st <= 0) st = 1;
vector<int> perm;
set<long long> was;
for (long long i = st; i <= n; i++) was.insert(i);
int j = n - st + 1;
for (long long i = st; i <= n; i++, j--) {
set<long long>::iterator it = was.begin();
while (k > fact[j - 1]) it++, k -= fact[j - 1];
perm.push_back(*it);
was.erase(it);
}
int ans = 0;
for (int i = 0; i < perm.size(); i++)
if (lucky(st + i) && lucky(perm[i])) ans++;
int q = 0;
while (luck[q] < st) q++, ans++;
printf("%d\n", ans);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, k;
int n1;
vector<long long> v;
int c[20];
int a[20];
bool viz[20];
int f[20];
inline void get(int i) {
int cif = 0;
long long zece = 1;
long long r = 0;
for (zece = 1; i > 0; i >>= 1, zece *= 10LL) {
++cif;
if ((i & 1) != 0)
r += 7LL * zece;
else
r += 4LL * zece;
}
if (r != 0LL) v.push_back(r);
for (++cif; cif < 11; ++cif, zece *= 10LL) {
r += 4LL * zece;
v.push_back(r);
}
}
void back(int k1) {
if (k1 == n1 + 1) return;
for (int i = 1; i <= n1; ++i) {
if (viz[i]) continue;
if (k > f[n1 - k1])
k -= f[n1 - k1];
else {
viz[i] = true;
a[k1] = i;
back(k1 + 1);
return;
}
}
printf("-1\n");
exit(0);
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 0; i < 1024; ++i) {
get(i);
}
sort(v.begin(), v.end());
int start = max(1, n - 12);
f[0] = f[1] = 1;
for (int i = 2; i < 13; ++i) f[i] = f[i - 1] * i;
n1 = n - start + 1;
back(1);
int rez = 0;
long long start1 = (long long)start;
for (size_t i = 0, lim = v.size(); i < lim && v[i] < start1; ++i) {
++rez;
}
int x = start;
for (int i = 1; i <= n1; ++i, ++x) {
c[i] = x;
}
for (int i = 1; i <= n1; ++i) {
if (find(v.begin(), v.end(), (long long)(i - 1) + start1) != v.end() &&
find(v.begin(), v.end(), (long long)c[a[i]]) != v.end())
++rez;
}
printf("%d\n", rez);
return 0;
}
| ### Prompt
Develop a solution in CPP to the problem described below:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, k;
int n1;
vector<long long> v;
int c[20];
int a[20];
bool viz[20];
int f[20];
inline void get(int i) {
int cif = 0;
long long zece = 1;
long long r = 0;
for (zece = 1; i > 0; i >>= 1, zece *= 10LL) {
++cif;
if ((i & 1) != 0)
r += 7LL * zece;
else
r += 4LL * zece;
}
if (r != 0LL) v.push_back(r);
for (++cif; cif < 11; ++cif, zece *= 10LL) {
r += 4LL * zece;
v.push_back(r);
}
}
void back(int k1) {
if (k1 == n1 + 1) return;
for (int i = 1; i <= n1; ++i) {
if (viz[i]) continue;
if (k > f[n1 - k1])
k -= f[n1 - k1];
else {
viz[i] = true;
a[k1] = i;
back(k1 + 1);
return;
}
}
printf("-1\n");
exit(0);
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 0; i < 1024; ++i) {
get(i);
}
sort(v.begin(), v.end());
int start = max(1, n - 12);
f[0] = f[1] = 1;
for (int i = 2; i < 13; ++i) f[i] = f[i - 1] * i;
n1 = n - start + 1;
back(1);
int rez = 0;
long long start1 = (long long)start;
for (size_t i = 0, lim = v.size(); i < lim && v[i] < start1; ++i) {
++rez;
}
int x = start;
for (int i = 1; i <= n1; ++i, ++x) {
c[i] = x;
}
for (int i = 1; i <= n1; ++i) {
if (find(v.begin(), v.end(), (long long)(i - 1) + start1) != v.end() &&
find(v.begin(), v.end(), (long long)c[a[i]]) != v.end())
++rez;
}
printf("%d\n", rez);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long n, k;
long long fact[20];
inline long long XF(int x) { return x > 13 ? 10000000000LL : fact[x]; }
int L, R;
map<int, int> pv;
vector<int> avb;
int lucky(int w) {
while (w) {
int T = w % 10;
if (T != 4 && T != 7) return 0;
w /= 10;
}
return 1;
}
int main() {
cin >> n >> k;
fact[0] = 1;
for (int i = 1; i <= 13; ++i) fact[i] = i * fact[i - 1];
if (XF(n) < k) {
cout << "-1\n";
return 0;
}
R = n;
L = R - min(n, 13LL) + 1;
for (int i = L; i <= R; ++i) avb.push_back(i);
for (int i = L; i <= R; ++i) {
int j = 0;
for (j = 0;; ++j)
if (XF(R - i) < k)
k -= XF(R - i);
else
break;
pv[i] = avb[j];
avb.erase(avb.begin() + j);
}
int ans = 0;
for (int len = 1; len <= 9; ++len)
for (int msk = 0; msk < (1 << len); ++msk) {
long long T = 0;
for (int i = 0; i < len; ++i) T = T * 10 + ((msk & (1 << i)) ? 4 : 7);
if (T <= n) {
if (!pv.count(T))
ans += lucky(T);
else
ans += lucky(pv[T]);
}
}
cout << ans << '\n';
return 0;
}
| ### Prompt
Your task is to create a cpp solution to the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long n, k;
long long fact[20];
inline long long XF(int x) { return x > 13 ? 10000000000LL : fact[x]; }
int L, R;
map<int, int> pv;
vector<int> avb;
int lucky(int w) {
while (w) {
int T = w % 10;
if (T != 4 && T != 7) return 0;
w /= 10;
}
return 1;
}
int main() {
cin >> n >> k;
fact[0] = 1;
for (int i = 1; i <= 13; ++i) fact[i] = i * fact[i - 1];
if (XF(n) < k) {
cout << "-1\n";
return 0;
}
R = n;
L = R - min(n, 13LL) + 1;
for (int i = L; i <= R; ++i) avb.push_back(i);
for (int i = L; i <= R; ++i) {
int j = 0;
for (j = 0;; ++j)
if (XF(R - i) < k)
k -= XF(R - i);
else
break;
pv[i] = avb[j];
avb.erase(avb.begin() + j);
}
int ans = 0;
for (int len = 1; len <= 9; ++len)
for (int msk = 0; msk < (1 << len); ++msk) {
long long T = 0;
for (int i = 0; i < len; ++i) T = T * 10 + ((msk & (1 << i)) ? 4 : 7);
if (T <= n) {
if (!pv.count(T))
ans += lucky(T);
else
ans += lucky(pv[T]);
}
}
cout << ans << '\n';
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long n, k, m;
long long ed;
long long f[20], cur[20], a[20], vis[20];
long long ans = 0;
void solve(long long now) {
if (now > m) return;
ans++;
solve(now * 10 + 4);
solve(now * 10 + 7);
}
bool legal(long long x) {
while (x > 0) {
long long tmp = x % 10;
if (tmp != 4 && tmp != 7) return false;
x /= 10;
}
return true;
}
int main() {
cin >> n >> k;
ed = 1;
f[ed] = 1;
f[0] = 1;
while (f[ed] < k) {
f[ed + 1] = f[ed] * (ed + 1);
++ed;
}
m = n - ed;
if (m < 0) {
cout << -1 << endl;
return 0;
}
memset(vis, false, sizeof(vis));
for (int i = 1; i <= ed; i++) {
cur[i] = (k + f[ed - i] - 1) / f[ed - i];
int cnt = 0;
for (int j = 1;; j++) {
if (!vis[j]) cnt++;
if (cnt == cur[i]) {
a[i] = j;
vis[j] = true;
break;
}
}
k -= f[ed - i] * (cur[i] - 1);
}
solve(4);
solve(7);
for (int i = 1; i <= ed; i++) {
long long num = a[i] + m;
long long pos = i + m;
if (legal(num) && legal(pos)) ans++;
}
cout << ans << endl;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long n, k, m;
long long ed;
long long f[20], cur[20], a[20], vis[20];
long long ans = 0;
void solve(long long now) {
if (now > m) return;
ans++;
solve(now * 10 + 4);
solve(now * 10 + 7);
}
bool legal(long long x) {
while (x > 0) {
long long tmp = x % 10;
if (tmp != 4 && tmp != 7) return false;
x /= 10;
}
return true;
}
int main() {
cin >> n >> k;
ed = 1;
f[ed] = 1;
f[0] = 1;
while (f[ed] < k) {
f[ed + 1] = f[ed] * (ed + 1);
++ed;
}
m = n - ed;
if (m < 0) {
cout << -1 << endl;
return 0;
}
memset(vis, false, sizeof(vis));
for (int i = 1; i <= ed; i++) {
cur[i] = (k + f[ed - i] - 1) / f[ed - i];
int cnt = 0;
for (int j = 1;; j++) {
if (!vis[j]) cnt++;
if (cnt == cur[i]) {
a[i] = j;
vis[j] = true;
break;
}
}
k -= f[ed - i] * (cur[i] - 1);
}
solve(4);
solve(7);
for (int i = 1; i <= ed; i++) {
long long num = a[i] + m;
long long pos = i + m;
if (legal(num) && legal(pos)) ans++;
}
cout << ans << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long LINF = ~(((long long)0x1) << 63) / 2;
const int INF = 0X3F3F3F3F;
const double eps = 1e-7;
const long long limit = 1000000000LL;
vector<long long> c;
void dfs(long long now) {
if (now > limit) return;
if (now != 0) c.push_back(now);
dfs(now * 10 + 4);
dfs(now * 10 + 7);
}
int loc[15], lim;
void solve(int pos, int k) {
int now = 1, i, j, ans = 0, cnt = 0;
if (pos == lim + 1) return;
for (i = 1; i <= pos - 1; i++) now *= i;
for (i = 1; i <= lim; i++)
if (!loc[i]) {
ans += now;
cnt++;
if (ans >= k) {
loc[i] = lim - pos + 1;
solve(pos - 1, k - now * (cnt - 1));
break;
}
}
}
bool isOk(int d) {
bool flag = true;
while (d) {
int c = d % 10;
if (c != 4 && c != 7) flag = false;
d /= 10;
}
return flag;
}
int main() {
int n, k, i, j;
cin >> n >> k;
long long ans = 1;
bool ok = false;
dfs(0);
sort(c.begin(), c.end());
for (i = 1; i <= n; i++) {
ans *= i;
if (ans >= k) {
ok = true;
lim = i;
break;
}
}
if (!ok) {
cout << "-1" << endl;
} else {
solve(lim, k);
int ans = 0;
for (i = 0; i < c.size() && c[i] <= n; i++)
if (c[i] < n - lim)
ans++;
else {
if (isOk(loc[(int)(c[i] - (n - lim))] + n - lim)) ans++;
}
cout << ans << endl;
}
return 0;
}
| ### Prompt
Please formulate a CPP solution to the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long LINF = ~(((long long)0x1) << 63) / 2;
const int INF = 0X3F3F3F3F;
const double eps = 1e-7;
const long long limit = 1000000000LL;
vector<long long> c;
void dfs(long long now) {
if (now > limit) return;
if (now != 0) c.push_back(now);
dfs(now * 10 + 4);
dfs(now * 10 + 7);
}
int loc[15], lim;
void solve(int pos, int k) {
int now = 1, i, j, ans = 0, cnt = 0;
if (pos == lim + 1) return;
for (i = 1; i <= pos - 1; i++) now *= i;
for (i = 1; i <= lim; i++)
if (!loc[i]) {
ans += now;
cnt++;
if (ans >= k) {
loc[i] = lim - pos + 1;
solve(pos - 1, k - now * (cnt - 1));
break;
}
}
}
bool isOk(int d) {
bool flag = true;
while (d) {
int c = d % 10;
if (c != 4 && c != 7) flag = false;
d /= 10;
}
return flag;
}
int main() {
int n, k, i, j;
cin >> n >> k;
long long ans = 1;
bool ok = false;
dfs(0);
sort(c.begin(), c.end());
for (i = 1; i <= n; i++) {
ans *= i;
if (ans >= k) {
ok = true;
lim = i;
break;
}
}
if (!ok) {
cout << "-1" << endl;
} else {
solve(lim, k);
int ans = 0;
for (i = 0; i < c.size() && c[i] <= n; i++)
if (c[i] < n - lim)
ans++;
else {
if (isOk(loc[(int)(c[i] - (n - lim))] + n - lim)) ans++;
}
cout << ans << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void RunProgram();
int main() { RunProgram(); }
long long fact[14], k;
int n, m, d[20];
bool avail[20];
void MakeD(int n) {
memset(avail, true, sizeof(avail));
for (int i = 1; i <= n; i++) {
int j;
for (j = 1; j <= n; j++)
if (avail[j])
if (k > fact[n - i])
k -= fact[n - i];
else
break;
j = min(j, n);
d[i] = j;
avail[j] = false;
}
}
bool isLucky(long long x) {
do {
int t = x % 10;
x /= 10;
if (t != 4 && t != 7) return false;
} while (x > 0);
return true;
}
int SolveSmall(int n) {
if (fact[n] < k) return -1;
MakeD(n);
int res = 0;
if (4 <= n) res += isLucky(d[4]);
if (7 <= n) res += isLucky(d[7]);
return res;
}
int Count(long long LIM) {
vector<long long> a;
a.push_back(0);
for (int i = 0; i < (int)a.size(); i++) {
long long v = a[i] * 10 + 4;
if (v <= LIM) a.push_back(v);
v = a[i] * 10 + 7;
if (v <= LIM) a.push_back(v);
}
return (int)a.size() - 1;
}
int SolveBig(int n) {
MakeD(14);
int res = Count(n - 14), delta = n - 14;
for (int i = 1; i <= 14; i++)
res += isLucky(d[i] + delta) && isLucky(i + delta);
return res;
}
void RunProgram() {
fact[0] = 1;
for (int i = 1; i < 14; i++) fact[i] = fact[i - 1] * i;
cin >> n >> k;
if (n < 14)
cout << SolveSmall(n);
else
cout << SolveBig(n);
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void RunProgram();
int main() { RunProgram(); }
long long fact[14], k;
int n, m, d[20];
bool avail[20];
void MakeD(int n) {
memset(avail, true, sizeof(avail));
for (int i = 1; i <= n; i++) {
int j;
for (j = 1; j <= n; j++)
if (avail[j])
if (k > fact[n - i])
k -= fact[n - i];
else
break;
j = min(j, n);
d[i] = j;
avail[j] = false;
}
}
bool isLucky(long long x) {
do {
int t = x % 10;
x /= 10;
if (t != 4 && t != 7) return false;
} while (x > 0);
return true;
}
int SolveSmall(int n) {
if (fact[n] < k) return -1;
MakeD(n);
int res = 0;
if (4 <= n) res += isLucky(d[4]);
if (7 <= n) res += isLucky(d[7]);
return res;
}
int Count(long long LIM) {
vector<long long> a;
a.push_back(0);
for (int i = 0; i < (int)a.size(); i++) {
long long v = a[i] * 10 + 4;
if (v <= LIM) a.push_back(v);
v = a[i] * 10 + 7;
if (v <= LIM) a.push_back(v);
}
return (int)a.size() - 1;
}
int SolveBig(int n) {
MakeD(14);
int res = Count(n - 14), delta = n - 14;
for (int i = 1; i <= 14; i++)
res += isLucky(d[i] + delta) && isLucky(i + delta);
return res;
}
void RunProgram() {
fact[0] = 1;
for (int i = 1; i < 14; i++) fact[i] = fact[i - 1] * i;
cin >> n >> k;
if (n < 14)
cout << SolveSmall(n);
else
cout << SolveBig(n);
}
``` |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int intmax = 0x3f3f3f3f;
const long long lldmax = 0x3f3f3f3f3f3f3f3fll;
double eps = 1e-8;
template <class T>
inline void checkmin(T &a, T b) {
if (b < a) a = b;
}
template <class T>
inline void checkmax(T &a, T b) {
if (b > a) a = b;
}
template <class T>
inline T sqr(T x) {
return x * x;
}
template <class T>
inline T lowbit(T n) {
return (n ^ (n - 1)) & n;
}
template <class T>
inline int countbit(T n) {
return (n == 0) ? 0 : (1 + countbit(n & (n - 1)));
}
template <class T>
inline T checkmod(T n, T m) {
return (n % m + m) % m;
}
inline int rand(int a, int b) {
if (a >= b) return a;
return rand() % (b - a) + a;
}
template <class T>
inline T lcm(T a, T b) {
if (a < 0) return lcm(-a, b);
if (b < 0) return lcm(a, -b);
return a * (b / gcd(a, b));
}
template <class T>
inline T gcd(T a, T b) {
if (a < 0) return gcd(-a, b);
if (b < 0) return gcd(a, -b);
return (b == 0) ? a : gcd(b, a % b);
}
template <class T>
inline T euclid(T a, T b, T &x, T &y) {
if (a < 0) {
T d = euclid(-a, b, x, y);
x = -x;
return d;
}
if (b < 0) {
T d = euclid(a, -b, x, y);
y = -y;
return d;
}
if (b == 0) {
x = 1;
y = 0;
return a;
} else {
T d = euclid(b, a % b, x, y);
T t = x;
x = y;
y = t - (a / b) * y;
return d;
}
}
template <class T>
inline vector<pair<T, int> > factorize(T n) {
vector<pair<T, int> > R;
for (T i = 2; n > 1;) {
if (n % i == 0) {
int C = 0;
for (; n % i == 0; C++, n /= i)
;
R.push_back(make_pair(i, C));
}
i++;
if (i > n / i) i = n;
}
if (n > 1) R.push_back(make_pair(n, 1));
return R;
}
template <class T>
inline bool isPrimeNumber(T n) {
if (n <= 1) return false;
for (T i = 2; i * i <= n; i++)
if (n % i == 0) return false;
return true;
}
template <class T>
inline T eularFunction(T n) {
vector<pair<T, int> > R = factorize(n);
T r = n;
for (int i = 0; i < R.size(); i++) r = r / R[i].first * (R[i].first - 1);
return r;
}
template <class T>
inline int dblcmp(T a, const T b) {
a -= b;
return a > eps ? 1 : (a < -eps ? -1 : 0);
}
template <class T>
inline int sgn(T a) {
return a > eps ? 1 : (a < -eps ? -1 : 0);
}
const int N = 13;
long long tmp[N];
set<int> st;
int n, k;
void dfs(long long a) {
if (a > n) return;
st.insert(a);
dfs(a * 10 + 4);
dfs(a * 10 + 7);
}
bool judge() {
long long ans = 1;
for (int i = 1; i <= n; ++i) {
if (ans > k) break;
ans *= i;
}
return ans >= k;
}
void gao(int a[], int n, int k) {
if (n == 1) return;
int p = k / tmp[n - 1];
int t = a[p];
for (int i = p; i > 0; --i) a[i] = a[i - 1];
a[0] = t;
gao(a + 1, n - 1, k % tmp[n - 1]);
}
int main() {
tmp[0] = 1;
for (int i = 1; i < N; ++i) tmp[i] = tmp[i - 1] * i;
cin >> n >> k;
dfs(4);
dfs(7);
if (!judge()) {
puts("-1");
} else {
int ans = 0;
int m = max(0, n - N);
for (set<int>::iterator it = st.begin(); it != st.end(); ++it) {
if (*it <= m) {
ans++;
}
}
int a[20];
for (int i = 0; i < n - m; ++i) a[i] = i + m + 1;
gao(a, n - m, k - 1);
for (int i = 0; i < n - m; ++i) {
if (st.find(m + 1 + i) != st.end() && st.find(a[i]) != st.end()) ans++;
}
cout << ans << endl;
}
return 0;
}
| ### Prompt
Your challenge is to write a cpp solution to the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int intmax = 0x3f3f3f3f;
const long long lldmax = 0x3f3f3f3f3f3f3f3fll;
double eps = 1e-8;
template <class T>
inline void checkmin(T &a, T b) {
if (b < a) a = b;
}
template <class T>
inline void checkmax(T &a, T b) {
if (b > a) a = b;
}
template <class T>
inline T sqr(T x) {
return x * x;
}
template <class T>
inline T lowbit(T n) {
return (n ^ (n - 1)) & n;
}
template <class T>
inline int countbit(T n) {
return (n == 0) ? 0 : (1 + countbit(n & (n - 1)));
}
template <class T>
inline T checkmod(T n, T m) {
return (n % m + m) % m;
}
inline int rand(int a, int b) {
if (a >= b) return a;
return rand() % (b - a) + a;
}
template <class T>
inline T lcm(T a, T b) {
if (a < 0) return lcm(-a, b);
if (b < 0) return lcm(a, -b);
return a * (b / gcd(a, b));
}
template <class T>
inline T gcd(T a, T b) {
if (a < 0) return gcd(-a, b);
if (b < 0) return gcd(a, -b);
return (b == 0) ? a : gcd(b, a % b);
}
template <class T>
inline T euclid(T a, T b, T &x, T &y) {
if (a < 0) {
T d = euclid(-a, b, x, y);
x = -x;
return d;
}
if (b < 0) {
T d = euclid(a, -b, x, y);
y = -y;
return d;
}
if (b == 0) {
x = 1;
y = 0;
return a;
} else {
T d = euclid(b, a % b, x, y);
T t = x;
x = y;
y = t - (a / b) * y;
return d;
}
}
template <class T>
inline vector<pair<T, int> > factorize(T n) {
vector<pair<T, int> > R;
for (T i = 2; n > 1;) {
if (n % i == 0) {
int C = 0;
for (; n % i == 0; C++, n /= i)
;
R.push_back(make_pair(i, C));
}
i++;
if (i > n / i) i = n;
}
if (n > 1) R.push_back(make_pair(n, 1));
return R;
}
template <class T>
inline bool isPrimeNumber(T n) {
if (n <= 1) return false;
for (T i = 2; i * i <= n; i++)
if (n % i == 0) return false;
return true;
}
template <class T>
inline T eularFunction(T n) {
vector<pair<T, int> > R = factorize(n);
T r = n;
for (int i = 0; i < R.size(); i++) r = r / R[i].first * (R[i].first - 1);
return r;
}
template <class T>
inline int dblcmp(T a, const T b) {
a -= b;
return a > eps ? 1 : (a < -eps ? -1 : 0);
}
template <class T>
inline int sgn(T a) {
return a > eps ? 1 : (a < -eps ? -1 : 0);
}
const int N = 13;
long long tmp[N];
set<int> st;
int n, k;
void dfs(long long a) {
if (a > n) return;
st.insert(a);
dfs(a * 10 + 4);
dfs(a * 10 + 7);
}
bool judge() {
long long ans = 1;
for (int i = 1; i <= n; ++i) {
if (ans > k) break;
ans *= i;
}
return ans >= k;
}
void gao(int a[], int n, int k) {
if (n == 1) return;
int p = k / tmp[n - 1];
int t = a[p];
for (int i = p; i > 0; --i) a[i] = a[i - 1];
a[0] = t;
gao(a + 1, n - 1, k % tmp[n - 1]);
}
int main() {
tmp[0] = 1;
for (int i = 1; i < N; ++i) tmp[i] = tmp[i - 1] * i;
cin >> n >> k;
dfs(4);
dfs(7);
if (!judge()) {
puts("-1");
} else {
int ans = 0;
int m = max(0, n - N);
for (set<int>::iterator it = st.begin(); it != st.end(); ++it) {
if (*it <= m) {
ans++;
}
}
int a[20];
for (int i = 0; i < n - m; ++i) a[i] = i + m + 1;
gao(a, n - m, k - 1);
for (int i = 0; i < n - m; ++i) {
if (st.find(m + 1 + i) != st.end() && st.find(a[i]) != st.end()) ans++;
}
cout << ans << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long fac[20], n, k;
long long answer;
void rec(long long x, long long lim) {
if (x > lim) return;
answer++;
rec(10 * x + 4, lim);
rec(10 * x + 7, lim);
}
long long calc(long long lim) {
rec(4, lim);
rec(7, lim);
}
long long ohMyLuck(long long x) {
while (x > 0) {
if (x % 10 == 7 or x % 10 == 4) {
x /= 10;
} else {
return 0;
}
}
return 1;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
cin >> n >> k;
fac[0] = 1;
for (int i = 1; i <= 15; i++) fac[i] = (fac[i - 1] * i);
if (n <= 13 and fac[n] < k) {
cout << -1 << endl;
return 0;
}
k--;
int mis = -1;
for (int i = 1; i <= 15; i++)
if (k >= fac[i]) mis = i;
calc(n - (mis + 1));
vector<int> vec, my;
for (int i = n - mis; i <= n; i++) vec.push_back(i);
for (int i = vec.size() - 1; i >= 0; i--) {
int d = k / fac[i];
k = k % fac[i];
my.push_back(vec[d]);
vec.erase(vec.begin() + d);
}
for (int i = 0; i < my.size(); i++) {
if (ohMyLuck(my[i]) and ohMyLuck(n - mis + i)) {
answer++;
}
}
cout << answer << endl;
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long fac[20], n, k;
long long answer;
void rec(long long x, long long lim) {
if (x > lim) return;
answer++;
rec(10 * x + 4, lim);
rec(10 * x + 7, lim);
}
long long calc(long long lim) {
rec(4, lim);
rec(7, lim);
}
long long ohMyLuck(long long x) {
while (x > 0) {
if (x % 10 == 7 or x % 10 == 4) {
x /= 10;
} else {
return 0;
}
}
return 1;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
cin >> n >> k;
fac[0] = 1;
for (int i = 1; i <= 15; i++) fac[i] = (fac[i - 1] * i);
if (n <= 13 and fac[n] < k) {
cout << -1 << endl;
return 0;
}
k--;
int mis = -1;
for (int i = 1; i <= 15; i++)
if (k >= fac[i]) mis = i;
calc(n - (mis + 1));
vector<int> vec, my;
for (int i = n - mis; i <= n; i++) vec.push_back(i);
for (int i = vec.size() - 1; i >= 0; i--) {
int d = k / fac[i];
k = k % fac[i];
my.push_back(vec[d]);
vec.erase(vec.begin() + d);
}
for (int i = 0; i < my.size(); i++) {
if (ohMyLuck(my[i]) and ohMyLuck(n - mis + i)) {
answer++;
}
}
cout << answer << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 10;
const int MAXN = 1e4 + 10;
const int MOD = 1e9 + 7;
const int inf = 1e9;
const double pi = acos(-1.0);
const double eps = 1e-6;
int dx[] = {0, -1, 0, 1};
int dy[] = {1, 0, -1, 0};
long long F[40];
int calc(int l, long long n) {
if (n >= l) return 0;
int res = 0;
res += calc(l, n * 10 + 4);
res += calc(l, n * 10 + 7);
return res + 1;
}
bool check(int x) {
bool ok = true;
while (x) {
int t = x % 10;
if (t != 4 && t != 7) ok = false;
x /= 10;
}
return ok;
}
int n, k;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
F[0] = 1LL;
int pos = -1;
for (int i = 1; i <= n; i++) {
F[i] = F[i - 1] * (long long)i;
if (F[i] >= (long long)k) {
pos = i;
break;
}
}
if (pos == -1) {
cout << -1;
return 0;
}
int s = n - pos + 1;
int res = calc(s, 4) + calc(s, 7);
vector<int> V;
for (int i = s; i <= n; i++) V.push_back(i);
for (int i = s; i <= n; i++) {
int j = n - i;
int z, t;
if (F[j] <= k) {
z = (k - 1) / F[j];
t = V[z];
k = (k - 1) % F[j] + 1;
} else {
z = 0;
t = V[0];
}
V.erase(V.begin() + z);
if (check(t) && check(i)) res++;
}
cout << res;
return 0;
}
| ### Prompt
In CPP, your task is to solve the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 10;
const int MAXN = 1e4 + 10;
const int MOD = 1e9 + 7;
const int inf = 1e9;
const double pi = acos(-1.0);
const double eps = 1e-6;
int dx[] = {0, -1, 0, 1};
int dy[] = {1, 0, -1, 0};
long long F[40];
int calc(int l, long long n) {
if (n >= l) return 0;
int res = 0;
res += calc(l, n * 10 + 4);
res += calc(l, n * 10 + 7);
return res + 1;
}
bool check(int x) {
bool ok = true;
while (x) {
int t = x % 10;
if (t != 4 && t != 7) ok = false;
x /= 10;
}
return ok;
}
int n, k;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
F[0] = 1LL;
int pos = -1;
for (int i = 1; i <= n; i++) {
F[i] = F[i - 1] * (long long)i;
if (F[i] >= (long long)k) {
pos = i;
break;
}
}
if (pos == -1) {
cout << -1;
return 0;
}
int s = n - pos + 1;
int res = calc(s, 4) + calc(s, 7);
vector<int> V;
for (int i = s; i <= n; i++) V.push_back(i);
for (int i = s; i <= n; i++) {
int j = n - i;
int z, t;
if (F[j] <= k) {
z = (k - 1) / F[j];
t = V[z];
k = (k - 1) % F[j] + 1;
} else {
z = 0;
t = V[0];
}
V.erase(V.begin() + z);
if (check(t) && check(i)) res++;
}
cout << res;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
set<long long> S;
void go(const long long &x, const int &M, const long long &U) {
if (x > U) return;
if (x > M) S.insert(x);
go(10 * x + 4, M, U), go(10 * x + 7, M, U);
}
bool ok(long long x) {
for (; x; x /= 10) {
int a = x % 10;
if (a != 4 && a != 7) return false;
}
return true;
}
int main() {
long long N, K;
cin >> N >> K;
long long F = -1;
if (N < 14) {
F = 1;
for (long long i = 1; i <= N; ++i) F *= i;
}
int M = 1;
for (long long f = 1; f * M < K; f *= M, M++)
;
if (M > (int)N) {
cout << -1 << endl;
return 0;
}
M = min((int)N, 20);
go(0, 0, N - M);
vector<long long> a(M);
for (int _n(M), i(0); i < _n; i++) a[i] = N - M + i + 1;
if (K < F || F == -1) {
for (long long k = K - 1; k > 0;) {
long long f = 1;
int i = 1;
while (f * (i + 1) <= k) f *= ++i;
int j = k / f;
swap(a[M - i - 1], a[M - i + j - 1]);
sort(&a[0] + M - i, &a[0] + M);
k -= f * j;
}
} else if (K == F) {
reverse((a).begin(), (a).end());
}
int ans = 0;
for (int _n(M), i(0); i < _n; i++)
if (ok(N - M + i + 1) && ok(a[i])) ans++;
ans += S.size();
cout << ans << endl;
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
set<long long> S;
void go(const long long &x, const int &M, const long long &U) {
if (x > U) return;
if (x > M) S.insert(x);
go(10 * x + 4, M, U), go(10 * x + 7, M, U);
}
bool ok(long long x) {
for (; x; x /= 10) {
int a = x % 10;
if (a != 4 && a != 7) return false;
}
return true;
}
int main() {
long long N, K;
cin >> N >> K;
long long F = -1;
if (N < 14) {
F = 1;
for (long long i = 1; i <= N; ++i) F *= i;
}
int M = 1;
for (long long f = 1; f * M < K; f *= M, M++)
;
if (M > (int)N) {
cout << -1 << endl;
return 0;
}
M = min((int)N, 20);
go(0, 0, N - M);
vector<long long> a(M);
for (int _n(M), i(0); i < _n; i++) a[i] = N - M + i + 1;
if (K < F || F == -1) {
for (long long k = K - 1; k > 0;) {
long long f = 1;
int i = 1;
while (f * (i + 1) <= k) f *= ++i;
int j = k / f;
swap(a[M - i - 1], a[M - i + j - 1]);
sort(&a[0] + M - i, &a[0] + M);
k -= f * j;
}
} else if (K == F) {
reverse((a).begin(), (a).end());
}
int ans = 0;
for (int _n(M), i(0); i < _n; i++)
if (ok(N - M + i + 1) && ok(a[i])) ans++;
ans += S.size();
cout << ans << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100099;
int n, k;
vector<long long> lucky;
void gen(long long num) {
lucky.push_back(num);
if (num > n) return;
gen(num * 10 + 4);
gen(num * 10 + 7);
}
vector<int> d;
long long precalc[20];
bool used[20];
void findk(int n, int k, int len) {
if (len < 0) return;
long long sum = 0;
for (int dig = 1; dig <= n; dig++) {
if (!used[dig]) {
if (sum + precalc[len] >= k) {
used[dig] = 1;
d.push_back(dig);
break;
} else
sum += precalc[len];
}
}
findk(n, k - sum, len - 1);
}
bool check(int n, int k, int pos) {
for (int i = 0, _n = (20); i < _n; i++) used[i] = 0;
d.clear();
findk(n, k, n - 1);
if (d[pos - 1] == pos) return 1;
return 0;
}
bool good(long long t) {
while (t) {
if (t % 10 != 7 && t % 10 != 4) return 0;
t /= 10;
}
return 1;
}
int main() {
scanf("%d%d", &n, &k);
gen(4);
gen(7);
long long fact = 1;
precalc[0] = 1;
precalc[1] = 1;
for (int i = 2; i < 20; i++) precalc[i] = precalc[i - 1] * i;
int p = 0;
for (int i = 1; i <= n + 1; i++) {
fact *= i;
p++;
if (fact >= k) break;
}
if (p > n) {
printf("-1\n");
return 0;
}
int last = n - p;
int ans = 0;
sort(lucky.begin(), lucky.end());
for (int i = 0; i < lucky.size(); i++) {
if (lucky[i] <= last) ans++;
if (lucky[i] > n) break;
if (lucky[i] > last) {
for (int i = 0, _n = (20); i < _n; i++) used[i] = 0;
d.clear();
findk(p, k, p - 1);
for (int i = (n - p + 1), _b = (n); i <= _b; i++)
if (good(i) && good(d[i - last - 1] + last)) ans++;
break;
}
}
printf("%d\n", ans);
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100099;
int n, k;
vector<long long> lucky;
void gen(long long num) {
lucky.push_back(num);
if (num > n) return;
gen(num * 10 + 4);
gen(num * 10 + 7);
}
vector<int> d;
long long precalc[20];
bool used[20];
void findk(int n, int k, int len) {
if (len < 0) return;
long long sum = 0;
for (int dig = 1; dig <= n; dig++) {
if (!used[dig]) {
if (sum + precalc[len] >= k) {
used[dig] = 1;
d.push_back(dig);
break;
} else
sum += precalc[len];
}
}
findk(n, k - sum, len - 1);
}
bool check(int n, int k, int pos) {
for (int i = 0, _n = (20); i < _n; i++) used[i] = 0;
d.clear();
findk(n, k, n - 1);
if (d[pos - 1] == pos) return 1;
return 0;
}
bool good(long long t) {
while (t) {
if (t % 10 != 7 && t % 10 != 4) return 0;
t /= 10;
}
return 1;
}
int main() {
scanf("%d%d", &n, &k);
gen(4);
gen(7);
long long fact = 1;
precalc[0] = 1;
precalc[1] = 1;
for (int i = 2; i < 20; i++) precalc[i] = precalc[i - 1] * i;
int p = 0;
for (int i = 1; i <= n + 1; i++) {
fact *= i;
p++;
if (fact >= k) break;
}
if (p > n) {
printf("-1\n");
return 0;
}
int last = n - p;
int ans = 0;
sort(lucky.begin(), lucky.end());
for (int i = 0; i < lucky.size(); i++) {
if (lucky[i] <= last) ans++;
if (lucky[i] > n) break;
if (lucky[i] > last) {
for (int i = 0, _n = (20); i < _n; i++) used[i] = 0;
d.clear();
findk(p, k, p - 1);
for (int i = (n - p + 1), _b = (n); i <= _b; i++)
if (good(i) && good(d[i - last - 1] + last)) ans++;
break;
}
}
printf("%d\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long i, j, k, n, m, t, f, x, y, cnt;
long long nl, tmp, gcnt, ans;
long long luckyNum[20007], fct[16], v[16], ad[16];
queue<long long> qa, qb;
set<long long> st;
long long findFct(long long n) {
i = 0;
for (i = 1; i < 16; i++) {
if (fct[i] > n) {
return i;
}
}
}
void setV(long long n) {
long long i;
n++;
for (i = 1; i < 16; i++) {
if (!v[i]) {
n--;
}
if (n == 0) {
v[i] = true;
gcnt++;
ad[gcnt] = i + nl;
break;
}
}
}
int main() {
memset(luckyNum, 0, sizeof(luckyNum));
cnt = 0;
qa.push(4);
luckyNum[cnt++] = 4;
st.insert(4);
qa.push(7);
luckyNum[cnt++] = 7;
st.insert(7);
for (i = 1; i < 10; i++) {
while (!qa.empty()) {
x = qa.front();
qa.pop();
qb.push(x * 10 + 4);
luckyNum[cnt++] = x * 10 + 4;
st.insert(x * 10 + 4);
qb.push(x * 10 + 7);
luckyNum[cnt++] = x * 10 + 7;
st.insert(x * 10 + 7);
}
while (!qb.empty()) {
qa.push(qb.front());
qb.pop();
}
}
fct[0] = 1;
for (i = 1; i < 16; i++) {
fct[i] = fct[i - 1] * i;
}
while (cin >> n >> k) {
memset(v, 0, sizeof(v));
memset(ad, 0, sizeof(ad));
gcnt = 0;
ans = 0;
k--;
m = findFct(k);
nl = n - m;
if (nl < 0) {
cout << "-1" << endl;
continue;
}
i = 0;
while (luckyNum[i] <= nl) {
ans++;
i++;
}
tmp = m;
m--;
while (k > 0) {
t = k / fct[m];
k %= fct[m];
m--;
setV(t);
}
for (i = 1; i <= tmp; i++) {
if (!v[i]) {
gcnt++;
ad[gcnt] = i + nl;
}
}
for (i = 1; i <= gcnt; i++) {
if (st.find(ad[i]) != st.end() && st.find(i + nl) != st.end()) {
ans++;
}
}
cout << ans << endl;
}
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long i, j, k, n, m, t, f, x, y, cnt;
long long nl, tmp, gcnt, ans;
long long luckyNum[20007], fct[16], v[16], ad[16];
queue<long long> qa, qb;
set<long long> st;
long long findFct(long long n) {
i = 0;
for (i = 1; i < 16; i++) {
if (fct[i] > n) {
return i;
}
}
}
void setV(long long n) {
long long i;
n++;
for (i = 1; i < 16; i++) {
if (!v[i]) {
n--;
}
if (n == 0) {
v[i] = true;
gcnt++;
ad[gcnt] = i + nl;
break;
}
}
}
int main() {
memset(luckyNum, 0, sizeof(luckyNum));
cnt = 0;
qa.push(4);
luckyNum[cnt++] = 4;
st.insert(4);
qa.push(7);
luckyNum[cnt++] = 7;
st.insert(7);
for (i = 1; i < 10; i++) {
while (!qa.empty()) {
x = qa.front();
qa.pop();
qb.push(x * 10 + 4);
luckyNum[cnt++] = x * 10 + 4;
st.insert(x * 10 + 4);
qb.push(x * 10 + 7);
luckyNum[cnt++] = x * 10 + 7;
st.insert(x * 10 + 7);
}
while (!qb.empty()) {
qa.push(qb.front());
qb.pop();
}
}
fct[0] = 1;
for (i = 1; i < 16; i++) {
fct[i] = fct[i - 1] * i;
}
while (cin >> n >> k) {
memset(v, 0, sizeof(v));
memset(ad, 0, sizeof(ad));
gcnt = 0;
ans = 0;
k--;
m = findFct(k);
nl = n - m;
if (nl < 0) {
cout << "-1" << endl;
continue;
}
i = 0;
while (luckyNum[i] <= nl) {
ans++;
i++;
}
tmp = m;
m--;
while (k > 0) {
t = k / fct[m];
k %= fct[m];
m--;
setV(t);
}
for (i = 1; i <= tmp; i++) {
if (!v[i]) {
gcnt++;
ad[gcnt] = i + nl;
}
}
for (i = 1; i <= gcnt; i++) {
if (st.find(ad[i]) != st.end() && st.find(i + nl) != st.end()) {
ans++;
}
}
cout << ans << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
void gen(vector<int> &nums, long long int x) {
if (x > 1000000000) return;
if (x > 0) nums.push_back(x);
gen(nums, 10 * x + 4);
gen(nums, 10 * x + 7);
}
bool isLucky(int x) {
while (x > 0) {
if (x % 10 != 4 && x % 10 != 7) return false;
x /= 10;
}
return true;
}
int main() {
int n, k;
long long int fac[16];
bool used[16];
int perm[16];
cin >> n >> k;
memset(perm, 0, sizeof(perm));
memset(used, 0, sizeof(used));
fac[0] = 1;
for (int i = 1; i < 16; i++) fac[i] = i * fac[i - 1];
int m = min(15, n);
if (fac[m] < k) {
cout << -1 << endl;
return 0;
}
for (int i = 0; i < m; i++) {
int p = 1;
while (k > fac[m - 1 - i]) {
k -= fac[m - 1 - i];
++p;
}
for (int j = 0; j < m; j++) {
if (!used[j]) --p;
if (!p) {
perm[i] = n - m + j + 1;
used[j] = true;
break;
}
}
}
vector<int> lucky;
gen(lucky, 0);
int ret = 0;
for (int i = 0; i < (int)lucky.size(); i++) {
int num = lucky[i];
if (num > n) continue;
if (n > 15) {
if (num <= n - 15)
++ret;
else {
int x = 14 - (n - num);
if (isLucky(perm[x])) ++ret;
}
} else {
if (isLucky(perm[num - 1])) ++ret;
}
}
cout << ret << endl;
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
void gen(vector<int> &nums, long long int x) {
if (x > 1000000000) return;
if (x > 0) nums.push_back(x);
gen(nums, 10 * x + 4);
gen(nums, 10 * x + 7);
}
bool isLucky(int x) {
while (x > 0) {
if (x % 10 != 4 && x % 10 != 7) return false;
x /= 10;
}
return true;
}
int main() {
int n, k;
long long int fac[16];
bool used[16];
int perm[16];
cin >> n >> k;
memset(perm, 0, sizeof(perm));
memset(used, 0, sizeof(used));
fac[0] = 1;
for (int i = 1; i < 16; i++) fac[i] = i * fac[i - 1];
int m = min(15, n);
if (fac[m] < k) {
cout << -1 << endl;
return 0;
}
for (int i = 0; i < m; i++) {
int p = 1;
while (k > fac[m - 1 - i]) {
k -= fac[m - 1 - i];
++p;
}
for (int j = 0; j < m; j++) {
if (!used[j]) --p;
if (!p) {
perm[i] = n - m + j + 1;
used[j] = true;
break;
}
}
}
vector<int> lucky;
gen(lucky, 0);
int ret = 0;
for (int i = 0; i < (int)lucky.size(); i++) {
int num = lucky[i];
if (num > n) continue;
if (n > 15) {
if (num <= n - 15)
++ret;
else {
int x = 14 - (n - num);
if (isLucky(perm[x])) ++ret;
}
} else {
if (isLucky(perm[num - 1])) ++ret;
}
}
cout << ret << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = -1;
long long n, K, pot[20], fact[20], ans;
set<long long> luckySet;
bool used[20];
long long arr[20];
void kth(int pos, int size, long long k) {
if (pos == size + 1) {
return;
}
long long p = 1;
while (true) {
assert(p <= 20);
if (used[p]) {
p++;
continue;
}
if (k > fact[size - pos]) {
k -= fact[size - pos];
p++;
continue;
}
break;
}
arr[pos - 1] = p;
used[p] = true;
kth(pos + 1, size, k);
}
bool isLucky(long long v, const vector<long long> &lucky) {
for (int i = (0); i < (int)(lucky.size()); i++) {
if (lucky[i] == v) return true;
}
return false;
}
int main() {
scanf("%lld %lld", &n, &K);
pot[0] = 1;
for (int i = (1); i < (int)(18); i++) {
pot[i] = pot[i - 1] * 10LL;
}
fact[0] = 1;
for (int i = (1); i < (int)(20); i++) {
fact[i] = fact[i - 1] * i;
}
if (n <= 15 && fact[n] < K) {
printf("-1\n");
return 0;
}
for (int bm = (0); bm < (int)(1024); bm++) {
for (int j = (1); j < (int)(11); j++) {
long long l = 0;
for (int k = (0); k < (int)(j); k++) {
l += (bm & (1 << k)) ? 7 * pot[k] : 4 * pot[k];
}
luckySet.insert(l);
}
}
vector<long long> lucky(luckySet.begin(), luckySet.end());
for (int i = (0); i < (int)(lucky.size()); i++) {
if (lucky[i] <= n - 15) {
ans++;
}
}
int sz = min(n, 15LL);
kth(1, sz, K);
for (int i = (0); i < (int)(sz); i++) {
arr[i] += n - sz;
if (isLucky(n - sz + 1 + i, lucky) && isLucky(arr[i], lucky)) {
ans++;
}
}
printf("%lld\n", ans);
return 0;
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = -1;
long long n, K, pot[20], fact[20], ans;
set<long long> luckySet;
bool used[20];
long long arr[20];
void kth(int pos, int size, long long k) {
if (pos == size + 1) {
return;
}
long long p = 1;
while (true) {
assert(p <= 20);
if (used[p]) {
p++;
continue;
}
if (k > fact[size - pos]) {
k -= fact[size - pos];
p++;
continue;
}
break;
}
arr[pos - 1] = p;
used[p] = true;
kth(pos + 1, size, k);
}
bool isLucky(long long v, const vector<long long> &lucky) {
for (int i = (0); i < (int)(lucky.size()); i++) {
if (lucky[i] == v) return true;
}
return false;
}
int main() {
scanf("%lld %lld", &n, &K);
pot[0] = 1;
for (int i = (1); i < (int)(18); i++) {
pot[i] = pot[i - 1] * 10LL;
}
fact[0] = 1;
for (int i = (1); i < (int)(20); i++) {
fact[i] = fact[i - 1] * i;
}
if (n <= 15 && fact[n] < K) {
printf("-1\n");
return 0;
}
for (int bm = (0); bm < (int)(1024); bm++) {
for (int j = (1); j < (int)(11); j++) {
long long l = 0;
for (int k = (0); k < (int)(j); k++) {
l += (bm & (1 << k)) ? 7 * pot[k] : 4 * pot[k];
}
luckySet.insert(l);
}
}
vector<long long> lucky(luckySet.begin(), luckySet.end());
for (int i = (0); i < (int)(lucky.size()); i++) {
if (lucky[i] <= n - 15) {
ans++;
}
}
int sz = min(n, 15LL);
kth(1, sz, K);
for (int i = (0); i < (int)(sz); i++) {
arr[i] += n - sz;
if (isLucky(n - sz + 1 + i, lucky) && isLucky(arr[i], lucky)) {
ans++;
}
}
printf("%lld\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long gao(int x, int c) {
long long ret = 0, g = 1;
for (; c--; g *= 10, x >>= 1) {
ret += g * ((x & 1) ? 7 : 4);
}
return ret;
}
vector<long long> v;
long long fct[16];
int c[16];
bool p[16];
bool lk(int x) {
while (x) {
if (x % 10 != 4 && x % 10 != 7) return false;
x /= 10;
}
return true;
}
int main() {
int i, j;
fct[0] = 1;
for (i = 1; i <= (15); ++i) fct[i] = fct[i - 1] * i;
for (i = 1; i < 12; ++i)
for (j = 0; j < 1 << i; ++j) v.push_back(gao(j, i));
sort(v.begin(), v.end());
long long n, k;
cin >> n >> k;
if (n <= 15 && k > fct[n]) {
puts("-1");
return 0;
}
int u;
long long h;
for (u = 0, h = 1; h < k; h *= (++u))
;
int ans = (n - u >= 4)
? (upper_bound(v.begin(), v.end(), n - u + 1) - v.begin())
: 0;
k--;
for (i = 0; i < u; ++i) {
for (j = 0, h = k / fct[u - i - 1] + 1;; ++j) {
if (!p[j]) --h;
if (h == 0) {
k %= fct[u - i - 1];
p[j] = true;
break;
}
}
c[i] = j;
}
for (int i = 0; i < u; ++i) {
if (lk(n - u + c[i] + 1) && lk(n - u + i + 1)) ++ans;
}
printf("%d\n", ans);
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long gao(int x, int c) {
long long ret = 0, g = 1;
for (; c--; g *= 10, x >>= 1) {
ret += g * ((x & 1) ? 7 : 4);
}
return ret;
}
vector<long long> v;
long long fct[16];
int c[16];
bool p[16];
bool lk(int x) {
while (x) {
if (x % 10 != 4 && x % 10 != 7) return false;
x /= 10;
}
return true;
}
int main() {
int i, j;
fct[0] = 1;
for (i = 1; i <= (15); ++i) fct[i] = fct[i - 1] * i;
for (i = 1; i < 12; ++i)
for (j = 0; j < 1 << i; ++j) v.push_back(gao(j, i));
sort(v.begin(), v.end());
long long n, k;
cin >> n >> k;
if (n <= 15 && k > fct[n]) {
puts("-1");
return 0;
}
int u;
long long h;
for (u = 0, h = 1; h < k; h *= (++u))
;
int ans = (n - u >= 4)
? (upper_bound(v.begin(), v.end(), n - u + 1) - v.begin())
: 0;
k--;
for (i = 0; i < u; ++i) {
for (j = 0, h = k / fct[u - i - 1] + 1;; ++j) {
if (!p[j]) --h;
if (h == 0) {
k %= fct[u - i - 1];
p[j] = true;
break;
}
}
c[i] = j;
}
for (int i = 0; i < u; ++i) {
if (lk(n - u + c[i] + 1) && lk(n - u + i + 1)) ++ans;
}
printf("%d\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
set<long long> S;
void go(const long long &x, const int &M, const long long &U) {
if (x > U) return;
if (x > M) S.insert(x);
go(10 * x + 4, M, U), go(10 * x + 7, M, U);
}
bool ok(long long x) {
for (; x; x /= 10) {
int a = x % 10;
if (a != 4 && a != 7) return false;
}
return true;
}
int main() {
long long N, K;
cin >> N >> K;
long long F = -1;
int M = 1;
for (long long f = 1; f * M < K; f *= M, M++)
;
if (M > (int)N) {
cout << -1 << endl;
return 0;
}
M = min((int)N, 20);
go(0, 0, N - M);
vector<long long> a(M);
for (int _n(M), i(0); i < _n; i++) a[i] = N - M + i + 1;
if (K < F || F == -1) {
for (long long k = K - 1; k > 0;) {
long long f = 1;
int i = 1;
while (f * (i + 1) <= k) f *= ++i;
int j = k / f;
swap(a[M - i - 1], a[M - i + j - 1]);
sort(&a[0] + M - i, &a[0] + M);
k -= f * j;
}
} else if (K == F) {
reverse((a).begin(), (a).end());
}
int ans = 0;
for (int _n(M), i(0); i < _n; i++)
if (ok(N - M + i + 1) && ok(a[i])) ans++;
ans += S.size();
cout << ans << endl;
return 0;
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
set<long long> S;
void go(const long long &x, const int &M, const long long &U) {
if (x > U) return;
if (x > M) S.insert(x);
go(10 * x + 4, M, U), go(10 * x + 7, M, U);
}
bool ok(long long x) {
for (; x; x /= 10) {
int a = x % 10;
if (a != 4 && a != 7) return false;
}
return true;
}
int main() {
long long N, K;
cin >> N >> K;
long long F = -1;
int M = 1;
for (long long f = 1; f * M < K; f *= M, M++)
;
if (M > (int)N) {
cout << -1 << endl;
return 0;
}
M = min((int)N, 20);
go(0, 0, N - M);
vector<long long> a(M);
for (int _n(M), i(0); i < _n; i++) a[i] = N - M + i + 1;
if (K < F || F == -1) {
for (long long k = K - 1; k > 0;) {
long long f = 1;
int i = 1;
while (f * (i + 1) <= k) f *= ++i;
int j = k / f;
swap(a[M - i - 1], a[M - i + j - 1]);
sort(&a[0] + M - i, &a[0] + M);
k -= f * j;
}
} else if (K == F) {
reverse((a).begin(), (a).end());
}
int ans = 0;
for (int _n(M), i(0); i < _n; i++)
if (ok(N - M + i + 1) && ok(a[i])) ans++;
ans += S.size();
cout << ans << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int inf = 1000000005;
long long int llinf = 2000000000000000005LL;
long long int mod = 1000000007;
long long int mod9 = 1000000009;
double pi = 3.1415926535897;
double eps = 1e-15;
int dx[] = {1, -1, 0, 0, 1, -1, 1, -1}, dy[] = {0, 0, 1, -1, 1, 1, -1, -1};
vector<bool> isprime;
vector<int> primes;
void seive(int n, bool wantlist = true) {
isprime.resize(n + 1, true);
isprime[0] = isprime[1] = false;
int sq = sqrt(n + 1);
for (int i = 2; i < sq + 1; i++) {
if (isprime[i]) {
for (int j = i * i; j <= n; j += i) isprime[j] = false;
}
}
for (int i = 2; wantlist && i <= n; i++) {
if (isprime[i]) primes.push_back(i);
}
}
template <class T>
inline T gcd(T a, T b) {
while (b > 0) {
a %= b;
swap(a, b);
}
return a;
}
template <class T>
inline T lcm(T a, T b) {
return a * b / gcd(a, b);
}
template <class T>
inline vector<T> operator+(vector<T>& a, vector<T>& b) {
assert(a.size() == b.size());
int n = a.size();
vector<T> c(n);
for (int i = 0; i < n; i++) c[i] = a[i] + b[i];
return c;
}
int fastMax(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y; }
inline long long int bexp(long long int x, long long int n,
long long int m = mod) {
long long int res = 1;
x %= m;
while (n) {
if (n & 1) res = res * x % m;
x = x * x % m;
n >>= 1;
}
return res;
}
inline bool ispalin(string& str) {
int n = str.length();
for (int i = 0; i < n / 2; i++)
if (str[i] != str[n - i - 1]) return false;
return true;
}
bool lucky(int n) {
bool ans = true;
while (n > 0) {
ans = ans && (n % 10 == 4 || n % 10 == 7);
n /= 10;
}
return ans;
}
int main() {
int n, k;
cin >> n >> k;
k--;
if (n <= 14) {
long long int n1 = 1;
for (int i = 1; i < n + 1; i++) n1 *= i;
if (n1 < k + 1) {
cout << -1 << endl;
return 0;
}
}
vector<int> rep;
for (int i = 1; i < 50 && k > 0; i++) {
rep.push_back(k % i);
k /= i;
}
reverse(rep.begin(), rep.end());
vector<int> a(rep.size());
for (int i = 0; i < rep.size(); i++) a[i] = i + 1 + (n - rep.size());
int cnt = 0;
for (int i = 0; i < rep.size(); i++) {
int pos = n - rep.size() + i + 1;
if (lucky(pos) && lucky(a[rep[i]])) cnt++;
a.erase(a.begin() + rep[i]);
}
vector<long long int> luc;
for (int x = 2; x < 2048; x++) {
long long int num = 0, msk = x, mul = 1;
while (msk > 1) {
if (msk & 1)
num += 7L * mul;
else
num += 4L * mul;
msk >>= 1;
mul *= 10L;
}
luc.push_back(num);
}
int i = 0;
while (luc[i++] <= n - rep.size()) cnt++;
cout << cnt << endl;
}
| ### Prompt
Create a solution in CPP for the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int inf = 1000000005;
long long int llinf = 2000000000000000005LL;
long long int mod = 1000000007;
long long int mod9 = 1000000009;
double pi = 3.1415926535897;
double eps = 1e-15;
int dx[] = {1, -1, 0, 0, 1, -1, 1, -1}, dy[] = {0, 0, 1, -1, 1, 1, -1, -1};
vector<bool> isprime;
vector<int> primes;
void seive(int n, bool wantlist = true) {
isprime.resize(n + 1, true);
isprime[0] = isprime[1] = false;
int sq = sqrt(n + 1);
for (int i = 2; i < sq + 1; i++) {
if (isprime[i]) {
for (int j = i * i; j <= n; j += i) isprime[j] = false;
}
}
for (int i = 2; wantlist && i <= n; i++) {
if (isprime[i]) primes.push_back(i);
}
}
template <class T>
inline T gcd(T a, T b) {
while (b > 0) {
a %= b;
swap(a, b);
}
return a;
}
template <class T>
inline T lcm(T a, T b) {
return a * b / gcd(a, b);
}
template <class T>
inline vector<T> operator+(vector<T>& a, vector<T>& b) {
assert(a.size() == b.size());
int n = a.size();
vector<T> c(n);
for (int i = 0; i < n; i++) c[i] = a[i] + b[i];
return c;
}
int fastMax(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y; }
inline long long int bexp(long long int x, long long int n,
long long int m = mod) {
long long int res = 1;
x %= m;
while (n) {
if (n & 1) res = res * x % m;
x = x * x % m;
n >>= 1;
}
return res;
}
inline bool ispalin(string& str) {
int n = str.length();
for (int i = 0; i < n / 2; i++)
if (str[i] != str[n - i - 1]) return false;
return true;
}
bool lucky(int n) {
bool ans = true;
while (n > 0) {
ans = ans && (n % 10 == 4 || n % 10 == 7);
n /= 10;
}
return ans;
}
int main() {
int n, k;
cin >> n >> k;
k--;
if (n <= 14) {
long long int n1 = 1;
for (int i = 1; i < n + 1; i++) n1 *= i;
if (n1 < k + 1) {
cout << -1 << endl;
return 0;
}
}
vector<int> rep;
for (int i = 1; i < 50 && k > 0; i++) {
rep.push_back(k % i);
k /= i;
}
reverse(rep.begin(), rep.end());
vector<int> a(rep.size());
for (int i = 0; i < rep.size(); i++) a[i] = i + 1 + (n - rep.size());
int cnt = 0;
for (int i = 0; i < rep.size(); i++) {
int pos = n - rep.size() + i + 1;
if (lucky(pos) && lucky(a[rep[i]])) cnt++;
a.erase(a.begin() + rep[i]);
}
vector<long long int> luc;
for (int x = 2; x < 2048; x++) {
long long int num = 0, msk = x, mul = 1;
while (msk > 1) {
if (msk & 1)
num += 7L * mul;
else
num += 4L * mul;
msk >>= 1;
mul *= 10L;
}
luc.push_back(num);
}
int i = 0;
while (luc[i++] <= n - rep.size()) cnt++;
cout << cnt << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int N, K;
vector<int> lucky;
void gen(long long N, long long v) {
if (v > N) {
return;
}
if (v != 0) {
lucky.push_back((int)v);
}
gen(N, 10 * v + 4);
gen(N, 10 * v + 7);
}
vector<int> fact;
vector<int> a;
set<int> s;
void gen2(int K, int index) {
if (index < 0) return;
int v = K / fact[index];
set<int>::iterator it = s.begin();
advance(it, v);
a.push_back(*it);
s.erase(it);
gen2(K - v * fact[index], index - 1);
}
int main() {
scanf("%d%d", &N, &K);
K--;
fact.push_back(1);
for (int i = 1;; i++) {
long long v = (long long)fact.back() * i;
if (v > (long long)K) {
break;
}
fact.push_back((int)v);
}
for (int i = 0; i < (int)((fact).size()); i++) {
s.insert(i);
}
gen2(K, (int)((fact).size()) - 1);
if (N < (int)((a).size())) {
puts("-1");
return 0;
}
for (int i = 0; i < (int)((a).size()); i++) {
a[i] += (1 + N - (int)((a).size()));
}
gen(N, 0);
sort(lucky.begin(), lucky.end());
int cnt = 0;
for (int i = 0; i < (int)((lucky).size()); i++) {
int index = lucky[i];
if (index > N - (int)((a).size())) {
index = a[index - (N - (int)((a).size())) - 1];
}
if (binary_search(lucky.begin(), lucky.end(), index)) {
cnt++;
}
}
printf("%d\n", cnt);
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int N, K;
vector<int> lucky;
void gen(long long N, long long v) {
if (v > N) {
return;
}
if (v != 0) {
lucky.push_back((int)v);
}
gen(N, 10 * v + 4);
gen(N, 10 * v + 7);
}
vector<int> fact;
vector<int> a;
set<int> s;
void gen2(int K, int index) {
if (index < 0) return;
int v = K / fact[index];
set<int>::iterator it = s.begin();
advance(it, v);
a.push_back(*it);
s.erase(it);
gen2(K - v * fact[index], index - 1);
}
int main() {
scanf("%d%d", &N, &K);
K--;
fact.push_back(1);
for (int i = 1;; i++) {
long long v = (long long)fact.back() * i;
if (v > (long long)K) {
break;
}
fact.push_back((int)v);
}
for (int i = 0; i < (int)((fact).size()); i++) {
s.insert(i);
}
gen2(K, (int)((fact).size()) - 1);
if (N < (int)((a).size())) {
puts("-1");
return 0;
}
for (int i = 0; i < (int)((a).size()); i++) {
a[i] += (1 + N - (int)((a).size()));
}
gen(N, 0);
sort(lucky.begin(), lucky.end());
int cnt = 0;
for (int i = 0; i < (int)((lucky).size()); i++) {
int index = lucky[i];
if (index > N - (int)((a).size())) {
index = a[index - (N - (int)((a).size())) - 1];
}
if (binary_search(lucky.begin(), lucky.end(), index)) {
cnt++;
}
}
printf("%d\n", cnt);
return 0;
}
``` |
#include <bits/stdc++.h>
int mul[14];
int Lucky[1025], tot;
void pre() {
mul[0] = 1;
for (int i = 1; i < 14; i++) mul[i] = mul[i - 1] * i;
for (int bit = 1; bit < 10; bit++) {
for (int i = 0; i < 1 << bit; i++) {
int num = 0;
for (int j = bit - 1; j >= 0; j--) {
if (i >> j & 1)
num = num * 10 + 7;
else
num = num * 10 + 4;
}
Lucky[tot++] = num;
}
}
}
int Cantor(int *num, int ans, int n) {
int flag[14] = {0};
ans--;
if (n > 13) n = 13;
for (int i = 1; i <= n; i++) {
int k = ans / mul[n - i], j;
ans %= mul[n - i];
for (j = 1; j <= n; j++) {
if (!flag[j]) k--;
if (k < 0) break;
}
if (k >= 0) return -1;
num[i] = j;
flag[j] = 1;
}
return 0;
}
bool jg(int x) {
while (x) {
if (x % 10 != 4 && x % 10 != 7) return false;
x /= 10;
}
return true;
}
int main() {
pre();
int N, K;
scanf("%d%d", &N, &K);
int num[14];
int ans = Cantor(num, K, N);
if (ans == -1) return puts("-1"), 0;
if (N <= 13) {
for (int i = 1; i <= N; i++)
if (jg(num[i]) && jg(i)) ans++;
} else {
int Base = N - 13;
for (int i = N - 12, j = 1; i <= N; i++, j++) {
if (jg(i) && jg(num[j] + Base)) ans++;
}
int k = std::lower_bound(Lucky, Lucky + tot, N - 13) - Lucky;
ans += k;
if (jg(N - 13)) ans++;
}
printf("%d\n", ans);
return 0;
}
| ### Prompt
Please create a solution in Cpp to the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
int mul[14];
int Lucky[1025], tot;
void pre() {
mul[0] = 1;
for (int i = 1; i < 14; i++) mul[i] = mul[i - 1] * i;
for (int bit = 1; bit < 10; bit++) {
for (int i = 0; i < 1 << bit; i++) {
int num = 0;
for (int j = bit - 1; j >= 0; j--) {
if (i >> j & 1)
num = num * 10 + 7;
else
num = num * 10 + 4;
}
Lucky[tot++] = num;
}
}
}
int Cantor(int *num, int ans, int n) {
int flag[14] = {0};
ans--;
if (n > 13) n = 13;
for (int i = 1; i <= n; i++) {
int k = ans / mul[n - i], j;
ans %= mul[n - i];
for (j = 1; j <= n; j++) {
if (!flag[j]) k--;
if (k < 0) break;
}
if (k >= 0) return -1;
num[i] = j;
flag[j] = 1;
}
return 0;
}
bool jg(int x) {
while (x) {
if (x % 10 != 4 && x % 10 != 7) return false;
x /= 10;
}
return true;
}
int main() {
pre();
int N, K;
scanf("%d%d", &N, &K);
int num[14];
int ans = Cantor(num, K, N);
if (ans == -1) return puts("-1"), 0;
if (N <= 13) {
for (int i = 1; i <= N; i++)
if (jg(num[i]) && jg(i)) ans++;
} else {
int Base = N - 13;
for (int i = N - 12, j = 1; i <= N; i++, j++) {
if (jg(i) && jg(num[j] + Base)) ans++;
}
int k = std::lower_bound(Lucky, Lucky + tot, N - 13) - Lucky;
ans += k;
if (jg(N - 13)) ans++;
}
printf("%d\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, k, c;
long long factN = 1;
int a[20], tk[20];
int lck[100000], reach = 0;
void DFS(long long x) {
if (x > 1000000000) {
return;
}
lck[reach++] = x;
DFS(x * 10 + 4);
DFS(x * 10 + 7);
}
bool lucky(long long x) {
do {
int cur = x % 10;
if (cur != 4 && cur != 7) {
return 0;
}
x /= 10;
} while (x != 0);
return 1;
}
int main() {
cin >> n >> k;
int cur = 2;
while (cur <= n && factN <= k) {
factN *= cur;
cur++;
}
if (factN < k) {
cout << "-1\n";
return 0;
}
c = min(13, n);
cur = 2;
factN = 1;
for (cur = 2; cur <= c; cur++) {
factN *= cur;
}
for (int i = 0; i < c; i++) {
factN /= (c - i);
int wht = (k - 1) / factN + 1;
int j = 1;
for (int hmany = 0; j <= c && hmany < wht; j++) {
if (tk[j] == 0) {
hmany++;
}
}
j--;
a[i] = j;
tk[j] = 1;
k -= (wht - 1) * factN;
}
long long ans = 0;
for (int i = 0; i < c; i++) {
int ss = a[i] + max(n - 13, 0);
if (lucky(ss)) {
if (lucky(i + 1 + max(n - 13, 0))) {
ans++;
}
}
}
if (n > 12) {
DFS(4);
DFS(7);
sort(lck, lck + reach);
ans += (upper_bound(lck, lck + reach, n - 13) - lck);
}
cout << ans << '\n';
return 0;
}
| ### Prompt
Please create a solution in Cpp to the following problem:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
Input
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
Output
If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
Examples
Input
7 4
Output
1
Input
4 7
Output
1
Note
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, k, c;
long long factN = 1;
int a[20], tk[20];
int lck[100000], reach = 0;
void DFS(long long x) {
if (x > 1000000000) {
return;
}
lck[reach++] = x;
DFS(x * 10 + 4);
DFS(x * 10 + 7);
}
bool lucky(long long x) {
do {
int cur = x % 10;
if (cur != 4 && cur != 7) {
return 0;
}
x /= 10;
} while (x != 0);
return 1;
}
int main() {
cin >> n >> k;
int cur = 2;
while (cur <= n && factN <= k) {
factN *= cur;
cur++;
}
if (factN < k) {
cout << "-1\n";
return 0;
}
c = min(13, n);
cur = 2;
factN = 1;
for (cur = 2; cur <= c; cur++) {
factN *= cur;
}
for (int i = 0; i < c; i++) {
factN /= (c - i);
int wht = (k - 1) / factN + 1;
int j = 1;
for (int hmany = 0; j <= c && hmany < wht; j++) {
if (tk[j] == 0) {
hmany++;
}
}
j--;
a[i] = j;
tk[j] = 1;
k -= (wht - 1) * factN;
}
long long ans = 0;
for (int i = 0; i < c; i++) {
int ss = a[i] + max(n - 13, 0);
if (lucky(ss)) {
if (lucky(i + 1 + max(n - 13, 0))) {
ans++;
}
}
}
if (n > 12) {
DFS(4);
DFS(7);
sort(lck, lck + reach);
ans += (upper_bound(lck, lck + reach, n - 13) - lck);
}
cout << ans << '\n';
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int cas, n;
pair<long long, long long> a[maxn];
long long k;
bool chk(long long mid) {
long long tmp = 0;
int pos = n / 2 + 1;
for (int i = 1; i <= n; i++) {
if (a[i].second >= mid && pos) {
tmp += max(mid, a[i].first);
pos--;
} else
tmp += a[i].first;
}
return tmp <= k && !pos;
}
int main() {
cin >> cas;
while (cas--) {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i].first >> a[i].second;
}
sort(a + 1, a + 1 + n, greater<pair<long long, long long> >{});
long long l = 0, r = 1e9 + 1;
while (l < r) {
long long mid = l + r + 1 >> 1;
if (chk(mid))
l = mid;
else
r = mid - 1;
}
cout << l << endl;
}
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int cas, n;
pair<long long, long long> a[maxn];
long long k;
bool chk(long long mid) {
long long tmp = 0;
int pos = n / 2 + 1;
for (int i = 1; i <= n; i++) {
if (a[i].second >= mid && pos) {
tmp += max(mid, a[i].first);
pos--;
} else
tmp += a[i].first;
}
return tmp <= k && !pos;
}
int main() {
cin >> cas;
while (cas--) {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i].first >> a[i].second;
}
sort(a + 1, a + 1 + n, greater<pair<long long, long long> >{});
long long l = 0, r = 1e9 + 1;
while (l < r) {
long long mid = l + r + 1 >> 1;
if (chk(mid))
l = mid;
else
r = mid - 1;
}
cout << l << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
const long long inf = (long long)1e18 + 7;
const long long Mod = (long long)998244353;
using namespace std;
long long modexpo(long long a, long long b) {
long long ret = 1;
while (b) {
if (b & 1) {
ret = (ret * a) % Mod;
}
a = (a * a) % Mod;
b >>= 1;
}
return ret;
}
bool solve(vector<pair<long long, long long> > v, long long med, long long s,
long long n) {
long long cnt = 0, i;
for (i = 0; i < n; i++) {
if (v[i].first >= med) {
cnt++;
if (cnt > n / 2) return true;
} else if (v[i].first < med && v[i].second >= med) {
s -= (med - v[i].first);
if (s < 0) return false;
cnt++;
if (cnt > n / 2) return true;
}
}
return false;
}
bool cmp(pair<long long, long long> a, pair<long long, long long> b) {
if (a.first == b.first) {
return a.second > b.second;
}
return a.first > b.first;
}
int main() {
long long t;
cin >> t;
while (t--) {
long long s, i, n, ans = inf, mini = inf, maxi = 0;
cin >> n >> s;
long long arr[n + 2], l[n + 2], r[n + 2];
vector<pair<long long, long long> > v;
for (i = 0; i < n; i++) {
cin >> l[i] >> r[i];
v.push_back({l[i], r[i]});
s -= l[i];
mini = min(mini, l[i]);
maxi = max(maxi, r[i]);
}
sort(v.begin(), v.end(), cmp);
long long lo = mini, hi = maxi;
while (lo <= hi) {
long long mid = (lo + hi) / 2;
if (solve(v, mid, s, n)) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
cout << ans << endl;
}
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
const long long inf = (long long)1e18 + 7;
const long long Mod = (long long)998244353;
using namespace std;
long long modexpo(long long a, long long b) {
long long ret = 1;
while (b) {
if (b & 1) {
ret = (ret * a) % Mod;
}
a = (a * a) % Mod;
b >>= 1;
}
return ret;
}
bool solve(vector<pair<long long, long long> > v, long long med, long long s,
long long n) {
long long cnt = 0, i;
for (i = 0; i < n; i++) {
if (v[i].first >= med) {
cnt++;
if (cnt > n / 2) return true;
} else if (v[i].first < med && v[i].second >= med) {
s -= (med - v[i].first);
if (s < 0) return false;
cnt++;
if (cnt > n / 2) return true;
}
}
return false;
}
bool cmp(pair<long long, long long> a, pair<long long, long long> b) {
if (a.first == b.first) {
return a.second > b.second;
}
return a.first > b.first;
}
int main() {
long long t;
cin >> t;
while (t--) {
long long s, i, n, ans = inf, mini = inf, maxi = 0;
cin >> n >> s;
long long arr[n + 2], l[n + 2], r[n + 2];
vector<pair<long long, long long> > v;
for (i = 0; i < n; i++) {
cin >> l[i] >> r[i];
v.push_back({l[i], r[i]});
s -= l[i];
mini = min(mini, l[i]);
maxi = max(maxi, r[i]);
}
sort(v.begin(), v.end(), cmp);
long long lo = mini, hi = maxi;
while (lo <= hi) {
long long mid = (lo + hi) / 2;
if (solve(v, mid, s, n)) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
cout << ans << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long int inf = 9e18;
const long double pi = 2 * acos(0.0);
long long int tes, n, s;
vector<pair<long long int, long long int>> arr;
long long int power(long long int x, long long int y) {
long long int res = 1ll;
while (y > 0) {
if (y & 1) res = res * x;
y >>= 1;
x = x * x;
}
return res;
}
bool calc(long long int med) {
long long int sum = 0, tar = (n + 1) / 2;
vector<long long int> temp(n, -1), temp2;
;
for (int i = 0; i < n; i++) {
if (med > arr[i].first && med <= arr[i].second)
temp[i] = 1;
else if (arr[i].second < med)
temp[i] = 2;
else
temp[i] = 3;
};
for (int i = 0; i < n; i++) {
if (temp[i] == 2)
sum += arr[i].first;
else if (temp[i] == 3)
sum += arr[i].first, tar--;
else
temp2.push_back(arr[i].first);
}
long long int n1 = (int)((temp2).size());
sort(temp2.begin(), temp2.end());
;
for (int i = n1 - 1; i > -1; i--) {
if (tar > 0)
sum += med, tar--;
else
sum += temp2[i];
}
if (tar > 0 || sum > s)
return false;
else
return true;
}
void solve() {
arr.clear();
cin >> n >> s;
arr.resize(n);
;
for (int i = 0; i < n; i++) cin >> arr[i].first >> arr[i].second;
long long int l = 0, r = s + 1;
while (r > l + 1) {
long long int mid = (l + r) / 2;
if (calc(mid))
l = mid;
else
r = mid;
}
cout << l << "\n";
}
int32_t main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
cin >> tes;
while (tes--) {
solve();
}
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long int inf = 9e18;
const long double pi = 2 * acos(0.0);
long long int tes, n, s;
vector<pair<long long int, long long int>> arr;
long long int power(long long int x, long long int y) {
long long int res = 1ll;
while (y > 0) {
if (y & 1) res = res * x;
y >>= 1;
x = x * x;
}
return res;
}
bool calc(long long int med) {
long long int sum = 0, tar = (n + 1) / 2;
vector<long long int> temp(n, -1), temp2;
;
for (int i = 0; i < n; i++) {
if (med > arr[i].first && med <= arr[i].second)
temp[i] = 1;
else if (arr[i].second < med)
temp[i] = 2;
else
temp[i] = 3;
};
for (int i = 0; i < n; i++) {
if (temp[i] == 2)
sum += arr[i].first;
else if (temp[i] == 3)
sum += arr[i].first, tar--;
else
temp2.push_back(arr[i].first);
}
long long int n1 = (int)((temp2).size());
sort(temp2.begin(), temp2.end());
;
for (int i = n1 - 1; i > -1; i--) {
if (tar > 0)
sum += med, tar--;
else
sum += temp2[i];
}
if (tar > 0 || sum > s)
return false;
else
return true;
}
void solve() {
arr.clear();
cin >> n >> s;
arr.resize(n);
;
for (int i = 0; i < n; i++) cin >> arr[i].first >> arr[i].second;
long long int l = 0, r = s + 1;
while (r > l + 1) {
long long int mid = (l + r) / 2;
if (calc(mid))
l = mid;
else
r = mid;
}
cout << l << "\n";
}
int32_t main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
cin >> tes;
while (tes--) {
solve();
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
struct node {
int l, r;
} a[N];
bool operator<(const node& a, const node& b) { return a.l > b.l; }
long long n, s;
bool check(long long x) {
long long i = 0, k = 0, sum = 0;
while (k < n / 2 + 1) {
i++;
if (i > n) break;
if (a[i].r < x)
continue;
else {
k++;
sum += max(0ll, x - a[i].l);
}
}
if (k != n / 2 + 1)
return 0;
else
return sum <= s;
}
int main() {
int T;
cin >> T;
while (T--) {
cin >> n >> s;
for (int i = 1; i <= n; i++) {
cin >> a[i].l >> a[i].r;
s -= a[i].l;
}
sort(a + 1, a + n + 1);
long long l = 1, r = 1e9;
while (l <= r) {
long long mid = (l + r) / 2;
if (check(mid))
l = mid + 1;
else
r = mid - 1;
}
cout << r << endl;
}
}
| ### Prompt
Generate a Cpp solution to the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
struct node {
int l, r;
} a[N];
bool operator<(const node& a, const node& b) { return a.l > b.l; }
long long n, s;
bool check(long long x) {
long long i = 0, k = 0, sum = 0;
while (k < n / 2 + 1) {
i++;
if (i > n) break;
if (a[i].r < x)
continue;
else {
k++;
sum += max(0ll, x - a[i].l);
}
}
if (k != n / 2 + 1)
return 0;
else
return sum <= s;
}
int main() {
int T;
cin >> T;
while (T--) {
cin >> n >> s;
for (int i = 1; i <= n; i++) {
cin >> a[i].l >> a[i].r;
s -= a[i].l;
}
sort(a + 1, a + n + 1);
long long l = 1, r = 1e9;
while (l <= r) {
long long mid = (l + r) / 2;
if (check(mid))
l = mid + 1;
else
r = mid - 1;
}
cout << r << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <class T>
int read(T& x) {
int c = getchar(), f = (x = 0);
while (~c && (c < 48 || c > 57)) {
if (c == '-') {
f = 1;
}
c = getchar();
}
if (!~c) {
return 0;
}
while (c > 47 && c < 58) {
x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
}
if (f) {
x = -x;
}
return 1;
}
template <class T, class U>
inline int read(T& a, U& b) {
return read(a) && read(b);
}
template <class T, class U, class V>
inline int read(T& a, U& b, V& c) {
return read(a) && read(b) && read(c);
}
inline int read(double& _, double& __) { return scanf("%lf%lf", &_, &__); }
inline int read(char* _) { return scanf("%s", _); }
inline int read(double& _) { return scanf("%lf", &_); }
inline void print(long long _) { printf("%lld ", _); }
inline void print(int _) { printf("%d ", _); }
inline void println(long long _) { printf("%lld\n", _); }
inline void println(int _) { printf("%d\n", _); }
template <class T>
inline void cmax(T& _, T __) {
if (_ < __) _ = __;
}
template <class T>
inline void cmin(T& _, T __) {
if (_ > __) _ = __;
}
const int maxn = 2e5 + 10, INF = 0x3f3f3f3f;
const long long LINF = (long long)INF << 32 | INF;
long long mid;
struct Node {
long long L, R;
Node(long long a = 0, long long b = 0) : L(a), R(b) {}
bool operator<(const Node& T) const { return L > T.L; }
} a[maxn];
long long T, n, tot, L = 0, R = INF, x;
bool Judge() {
int cnt = 0;
long long tmp = tot;
for (int i = 1; i <= (n); i++) {
if (a[i].L >= mid) {
++cnt;
continue;
}
if (a[i].R < mid) continue;
if (tmp < mid - a[i].L) break;
tmp -= mid - a[i].L;
if (++cnt >= x) return true;
}
return cnt >= x;
}
int main() {
int T;
read(T);
while (T--) {
read(n, tot);
L = 0, R = tot, x = (n + 1) / 2;
for (int i = 1; i <= (n); i++) {
read(a[i].L, a[i].R);
tot -= a[i].L;
}
sort(a + 1, a + n + 1);
while (L < R) {
mid = L + (R - L + 1) / 2;
if (Judge())
L = mid;
else
R = mid - 1;
}
printf("%lld\n", L);
}
return 0;
}
| ### Prompt
Please create a solution in cpp to the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T>
int read(T& x) {
int c = getchar(), f = (x = 0);
while (~c && (c < 48 || c > 57)) {
if (c == '-') {
f = 1;
}
c = getchar();
}
if (!~c) {
return 0;
}
while (c > 47 && c < 58) {
x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
}
if (f) {
x = -x;
}
return 1;
}
template <class T, class U>
inline int read(T& a, U& b) {
return read(a) && read(b);
}
template <class T, class U, class V>
inline int read(T& a, U& b, V& c) {
return read(a) && read(b) && read(c);
}
inline int read(double& _, double& __) { return scanf("%lf%lf", &_, &__); }
inline int read(char* _) { return scanf("%s", _); }
inline int read(double& _) { return scanf("%lf", &_); }
inline void print(long long _) { printf("%lld ", _); }
inline void print(int _) { printf("%d ", _); }
inline void println(long long _) { printf("%lld\n", _); }
inline void println(int _) { printf("%d\n", _); }
template <class T>
inline void cmax(T& _, T __) {
if (_ < __) _ = __;
}
template <class T>
inline void cmin(T& _, T __) {
if (_ > __) _ = __;
}
const int maxn = 2e5 + 10, INF = 0x3f3f3f3f;
const long long LINF = (long long)INF << 32 | INF;
long long mid;
struct Node {
long long L, R;
Node(long long a = 0, long long b = 0) : L(a), R(b) {}
bool operator<(const Node& T) const { return L > T.L; }
} a[maxn];
long long T, n, tot, L = 0, R = INF, x;
bool Judge() {
int cnt = 0;
long long tmp = tot;
for (int i = 1; i <= (n); i++) {
if (a[i].L >= mid) {
++cnt;
continue;
}
if (a[i].R < mid) continue;
if (tmp < mid - a[i].L) break;
tmp -= mid - a[i].L;
if (++cnt >= x) return true;
}
return cnt >= x;
}
int main() {
int T;
read(T);
while (T--) {
read(n, tot);
L = 0, R = tot, x = (n + 1) / 2;
for (int i = 1; i <= (n); i++) {
read(a[i].L, a[i].R);
tot -= a[i].L;
}
sort(a + 1, a + n + 1);
while (L < R) {
mid = L + (R - L + 1) / 2;
if (Judge())
L = mid;
else
R = mid - 1;
}
printf("%lld\n", L);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long t, n, s, l, r, mid, sum, ans, answer;
pair<long long, long long> p[200005];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> t;
for (int rr = 0; rr < t; rr++) {
cin >> n >> s;
for (int i = 0; i < n; i++) {
cin >> p[i].first >> p[i].second;
}
sort(p, p + n);
l = 1;
r = s;
while (l <= r) {
mid = (l + r) >> 1;
sum = (n + 1) >> 1;
ans = 0;
for (int i = n - 1; i >= 0; i--) {
if (p[i].first <= mid && p[i].second >= mid && sum > 0) {
sum--;
ans += mid;
} else if (p[i].first > mid) {
sum--;
ans += p[i].first;
} else {
ans += p[i].first;
}
}
if (ans > s || sum > 0) {
r = mid - 1;
} else {
l = mid + 1;
answer = mid;
}
}
cout << answer << '\n';
}
}
| ### Prompt
Please formulate a CPP solution to the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long t, n, s, l, r, mid, sum, ans, answer;
pair<long long, long long> p[200005];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> t;
for (int rr = 0; rr < t; rr++) {
cin >> n >> s;
for (int i = 0; i < n; i++) {
cin >> p[i].first >> p[i].second;
}
sort(p, p + n);
l = 1;
r = s;
while (l <= r) {
mid = (l + r) >> 1;
sum = (n + 1) >> 1;
ans = 0;
for (int i = n - 1; i >= 0; i--) {
if (p[i].first <= mid && p[i].second >= mid && sum > 0) {
sum--;
ans += mid;
} else if (p[i].first > mid) {
sum--;
ans += p[i].first;
} else {
ans += p[i].first;
}
}
if (ans > s || sum > 0) {
r = mid - 1;
} else {
l = mid + 1;
answer = mid;
}
}
cout << answer << '\n';
}
}
``` |
#include <bits/stdc++.h>
const long long int inf = 9e18;
const long double pi = 2 * acos(0.0);
using namespace std;
long long int power(long long int a, long long int n) {
if (n == 0) {
return 1;
}
long long int p = power(a, n / 2) % 1000000007;
p = (p * p) % 1000000007;
if (n % 2 == 1) {
p = (p * a) % 1000000007;
}
return p;
}
long long int gcd(long long int a, long long int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
const int N = 1e6;
vector<long long int> sieve(N, 0);
void si() {
sieve[1] = 1;
for (int i = 2; i < N; i++) {
if (sieve[i] == 0) {
for (int j = i; j < N; j += i) {
sieve[j] = i;
}
}
}
}
long long int isprime(long long int n) {
for (int i = 2; i < n; i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
long long int n, s;
int solve(vector<pair<long long int, long long int> > &a, long long int val,
long long int s) {
vector<pair<long long int, long long int> > v;
int need = (n - 1) / 2;
long long int tot = 0;
int left = 0, right = 0;
for (int i = 0; i < n; i++) {
if (a[i].first < val && a[i].second < val) {
left += 1;
tot += a[i].first;
} else if (a[i].first > val && a[i].second > val) {
right += 1;
tot += a[i].first;
} else {
v.push_back(make_pair(a[i].first, a[i].second));
}
}
sort(v.begin(), v.end());
if (left > need) {
return -1;
}
if (right > need) {
return -2;
}
int need1 = need - left;
for (int i = 0; i < need1; i++) {
tot += v[i].first;
}
int need2 = need - right + 1;
for (int i = 0; i < need2; i++) {
tot += val;
}
if (tot > s) {
return -2;
}
return 1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
int t;
cin >> t;
while (t--) {
cin >> n >> s;
vector<long long int> L(n);
vector<long long int> R(n);
vector<pair<long long int, long long int> > a(n);
for (int i = 0; i < n; i++) {
cin >> L[i] >> R[i];
a[i] = make_pair(L[i], R[i]);
}
sort(L.begin(), L.end());
sort(R.begin(), R.end());
long long int l = L[n / 2];
long long int r = R[n / 2];
while (l <= r) {
long long int mid = (l + r) / 2;
if (solve(a, mid, s) == -1) {
l = mid + 1;
} else if (solve(a, mid, s) == -2) {
r = mid - 1;
} else {
if (solve(a, mid + 1, s) == -1 || solve(a, mid + 1, s) == -2) {
cout << mid << "\n";
break;
} else {
l = mid + 1;
}
}
}
}
return 0;
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
const long long int inf = 9e18;
const long double pi = 2 * acos(0.0);
using namespace std;
long long int power(long long int a, long long int n) {
if (n == 0) {
return 1;
}
long long int p = power(a, n / 2) % 1000000007;
p = (p * p) % 1000000007;
if (n % 2 == 1) {
p = (p * a) % 1000000007;
}
return p;
}
long long int gcd(long long int a, long long int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
const int N = 1e6;
vector<long long int> sieve(N, 0);
void si() {
sieve[1] = 1;
for (int i = 2; i < N; i++) {
if (sieve[i] == 0) {
for (int j = i; j < N; j += i) {
sieve[j] = i;
}
}
}
}
long long int isprime(long long int n) {
for (int i = 2; i < n; i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
long long int n, s;
int solve(vector<pair<long long int, long long int> > &a, long long int val,
long long int s) {
vector<pair<long long int, long long int> > v;
int need = (n - 1) / 2;
long long int tot = 0;
int left = 0, right = 0;
for (int i = 0; i < n; i++) {
if (a[i].first < val && a[i].second < val) {
left += 1;
tot += a[i].first;
} else if (a[i].first > val && a[i].second > val) {
right += 1;
tot += a[i].first;
} else {
v.push_back(make_pair(a[i].first, a[i].second));
}
}
sort(v.begin(), v.end());
if (left > need) {
return -1;
}
if (right > need) {
return -2;
}
int need1 = need - left;
for (int i = 0; i < need1; i++) {
tot += v[i].first;
}
int need2 = need - right + 1;
for (int i = 0; i < need2; i++) {
tot += val;
}
if (tot > s) {
return -2;
}
return 1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
int t;
cin >> t;
while (t--) {
cin >> n >> s;
vector<long long int> L(n);
vector<long long int> R(n);
vector<pair<long long int, long long int> > a(n);
for (int i = 0; i < n; i++) {
cin >> L[i] >> R[i];
a[i] = make_pair(L[i], R[i]);
}
sort(L.begin(), L.end());
sort(R.begin(), R.end());
long long int l = L[n / 2];
long long int r = R[n / 2];
while (l <= r) {
long long int mid = (l + r) / 2;
if (solve(a, mid, s) == -1) {
l = mid + 1;
} else if (solve(a, mid, s) == -2) {
r = mid - 1;
} else {
if (solve(a, mid + 1, s) == -1 || solve(a, mid + 1, s) == -2) {
cout << mid << "\n";
break;
} else {
l = mid + 1;
}
}
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline long long rand(long long x, long long y) {
return (rng() % (y + 1 - x)) + x;
}
string to_string(char c) {
string second(1, c);
return second;
}
template <typename T>
inline T gcd(T a, T b) {
return a == 0 ? b : gcd(b % a, a);
}
long long int t, n, second;
pair<long long int, long long int> A[(200006)];
vector<long long int> v;
bool bstar(long long int x) {
v.clear();
for (long long int i = (n - 1); i >= (long long int)(0); --i)
if (A[i].second >= x && v.size() < (n / 2 + 1)) {
v.push_back(A[i].first);
}
if (v.size() < (n / 2 + 1)) return 0;
long long int ans = 0;
for (auto i : v) ans += max(0ll, x - i);
return ans <= second;
}
void solve() {
cin >> n >> second;
for (long long int i = (0); i <= (long long int)(n - 1); ++i) {
long long int a, b;
cin >> a >> b;
second -= a;
A[i] = pair<long long int, long long int>(a, b);
}
sort(A, A + n);
long long int st = 0, en = 2e14 + 10, mid = 0;
while (en - st > 1) {
mid = (st + en) >> 1;
if (bstar(mid))
st = mid;
else
en = mid;
}
cout << st << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> t;
while (t--) {
solve();
}
}
| ### Prompt
Create a solution in CPP for the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline long long rand(long long x, long long y) {
return (rng() % (y + 1 - x)) + x;
}
string to_string(char c) {
string second(1, c);
return second;
}
template <typename T>
inline T gcd(T a, T b) {
return a == 0 ? b : gcd(b % a, a);
}
long long int t, n, second;
pair<long long int, long long int> A[(200006)];
vector<long long int> v;
bool bstar(long long int x) {
v.clear();
for (long long int i = (n - 1); i >= (long long int)(0); --i)
if (A[i].second >= x && v.size() < (n / 2 + 1)) {
v.push_back(A[i].first);
}
if (v.size() < (n / 2 + 1)) return 0;
long long int ans = 0;
for (auto i : v) ans += max(0ll, x - i);
return ans <= second;
}
void solve() {
cin >> n >> second;
for (long long int i = (0); i <= (long long int)(n - 1); ++i) {
long long int a, b;
cin >> a >> b;
second -= a;
A[i] = pair<long long int, long long int>(a, b);
}
sort(A, A + n);
long long int st = 0, en = 2e14 + 10, mid = 0;
while (en - st > 1) {
mid = (st + en) >> 1;
if (bstar(mid))
st = mid;
else
en = mid;
}
cout << st << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> t;
while (t--) {
solve();
}
}
``` |
#include <bits/stdc++.h>
template <typename T>
T GCD(T a, T b) {
return a ? GCD(b % a, a) : b;
}
template <typename T>
T LCM(T a, T b) {
return (a * b) / GCD(a, b);
}
template <typename T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
for (auto ob : v) os << ob << " ";
return os;
}
template <typename T, typename S>
std::ostream &operator<<(std::ostream &os, const std::map<T, S> &v) {
for (auto ob : v) os << ob.first << " : " << ob.second << std::endl;
return os;
}
using ld = double;
using ll = long long int;
using ul = unsigned long long int;
using namespace std;
class DSalaryChanging {
bool Possible(vector<pair<int, int>> money, ll s, int mid, int n) {
ll sum = 0;
int cnt = 0;
for (int i = 0; i < n; ++i) {
if (money[i].second < mid) {
sum += money[i].first;
} else if (money[i].first >= mid) {
cnt++;
sum += money[i].first;
} else if (money[i].first < mid && money[i].second >= mid) {
if (cnt > n / 2) {
sum += money[i].first;
} else {
cnt++;
sum += mid;
}
}
}
return cnt > n / 2 && sum <= s;
}
public:
void solve(std::istream &in, std::ostream &out) {
ll n, s;
in >> n >> s;
vector<pair<int, int>> money;
for (int i = 0; i < n; ++i) {
int l, r;
in >> l >> r;
money.push_back(make_pair(l, r));
}
sort(money.begin(), money.end());
reverse(money.begin(), money.end());
int low = 0, high = 1e9 + 17, ans = 0;
while (low <= high) {
int mid = low + (high - low) / 2;
bool isPossible = Possible(money, s, mid, n);
if (isPossible) {
ans = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
out << ans << endl;
}
};
int main() {
std::ios_base::sync_with_stdio(0);
std::cin.tie(nullptr);
DSalaryChanging solver;
std::istream &in(std::cin);
std::ostream &out(std::cout);
int n;
in >> n;
for (int i = 0; i < n; ++i) {
solver.solve(in, out);
}
return 0;
}
| ### Prompt
Create a solution in CPP for the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
template <typename T>
T GCD(T a, T b) {
return a ? GCD(b % a, a) : b;
}
template <typename T>
T LCM(T a, T b) {
return (a * b) / GCD(a, b);
}
template <typename T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
for (auto ob : v) os << ob << " ";
return os;
}
template <typename T, typename S>
std::ostream &operator<<(std::ostream &os, const std::map<T, S> &v) {
for (auto ob : v) os << ob.first << " : " << ob.second << std::endl;
return os;
}
using ld = double;
using ll = long long int;
using ul = unsigned long long int;
using namespace std;
class DSalaryChanging {
bool Possible(vector<pair<int, int>> money, ll s, int mid, int n) {
ll sum = 0;
int cnt = 0;
for (int i = 0; i < n; ++i) {
if (money[i].second < mid) {
sum += money[i].first;
} else if (money[i].first >= mid) {
cnt++;
sum += money[i].first;
} else if (money[i].first < mid && money[i].second >= mid) {
if (cnt > n / 2) {
sum += money[i].first;
} else {
cnt++;
sum += mid;
}
}
}
return cnt > n / 2 && sum <= s;
}
public:
void solve(std::istream &in, std::ostream &out) {
ll n, s;
in >> n >> s;
vector<pair<int, int>> money;
for (int i = 0; i < n; ++i) {
int l, r;
in >> l >> r;
money.push_back(make_pair(l, r));
}
sort(money.begin(), money.end());
reverse(money.begin(), money.end());
int low = 0, high = 1e9 + 17, ans = 0;
while (low <= high) {
int mid = low + (high - low) / 2;
bool isPossible = Possible(money, s, mid, n);
if (isPossible) {
ans = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
out << ans << endl;
}
};
int main() {
std::ios_base::sync_with_stdio(0);
std::cin.tie(nullptr);
DSalaryChanging solver;
std::istream &in(std::cin);
std::ostream &out(std::cout);
int n;
in >> n;
for (int i = 0; i < n; ++i) {
solver.solve(in, out);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int T, n;
LL s, l[200005], r[200005], bufl[200005];
bool check(LL x) {
int a = 0, b = 0, c = 0;
LL sa = 0;
for (int i = 1; i <= n; i++)
if (r[i] < x)
++a, sa += l[i];
else if (l[i] <= x)
++b, bufl[b] = l[i];
else
++c, sa += l[i];
if (c >= n + 1 >> 1) return true;
if (a >= n + 1 >> 1) return false;
nth_element(bufl + 1, bufl + (n + 1 >> 1) - a, bufl + b + 1);
LL u = ((n + 1 >> 1) - c) * x + sa;
for (int i = 1; i < (n + 1 >> 1) - a; i++) u += bufl[i];
return u <= s;
}
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d%lld", &n, &s);
for (int i = 1; i <= n; i++) scanf("%lld%lld", &l[i], &r[i]);
LL L = 1, R = min(s, (long long)1E9), M;
while (L < R) {
M = L + R + 1 >> 1;
if (check(M))
L = M;
else
R = M - 1;
}
printf("%lld\n", L);
}
return 0;
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int T, n;
LL s, l[200005], r[200005], bufl[200005];
bool check(LL x) {
int a = 0, b = 0, c = 0;
LL sa = 0;
for (int i = 1; i <= n; i++)
if (r[i] < x)
++a, sa += l[i];
else if (l[i] <= x)
++b, bufl[b] = l[i];
else
++c, sa += l[i];
if (c >= n + 1 >> 1) return true;
if (a >= n + 1 >> 1) return false;
nth_element(bufl + 1, bufl + (n + 1 >> 1) - a, bufl + b + 1);
LL u = ((n + 1 >> 1) - c) * x + sa;
for (int i = 1; i < (n + 1 >> 1) - a; i++) u += bufl[i];
return u <= s;
}
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d%lld", &n, &s);
for (int i = 1; i <= n; i++) scanf("%lld%lld", &l[i], &r[i]);
LL L = 1, R = min(s, (long long)1E9), M;
while (L < R) {
M = L + R + 1 >> 1;
if (check(M))
L = M;
else
R = M - 1;
}
printf("%lld\n", L);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long>> emp;
int n;
long long s, minS;
bool bSearch(long long t) {
int neededBigger = n / 2 + 1;
long long curS = s - minS;
for (int i = emp.size() - 1; i >= 0 && neededBigger > 0; i--) {
if (emp[i].first >= t) {
neededBigger--;
continue;
} else if (emp[i].second >= t) {
long long need = t - emp[i].first;
if (curS < need) {
return false;
}
curS -= need;
neededBigger--;
}
}
if (neededBigger > 0) {
return false;
}
return true;
}
int main() {
int t;
cin >> t;
while (t--) {
cin >> n;
cin >> s;
minS = 0;
emp.clear();
emp.resize(n);
for (int i = 0; i < n; i++) {
long long l, r;
cin >> l >> r;
emp[i] = {l, r};
minS += l;
}
sort(emp.begin(), emp.end());
long long a = 0, b = s;
while (a < b) {
long long mid = (a + b + 1) / 2;
bool curB = bSearch(mid);
if (curB) {
a = mid;
} else {
b = mid - 1;
}
}
cout << a << endl;
}
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long>> emp;
int n;
long long s, minS;
bool bSearch(long long t) {
int neededBigger = n / 2 + 1;
long long curS = s - minS;
for (int i = emp.size() - 1; i >= 0 && neededBigger > 0; i--) {
if (emp[i].first >= t) {
neededBigger--;
continue;
} else if (emp[i].second >= t) {
long long need = t - emp[i].first;
if (curS < need) {
return false;
}
curS -= need;
neededBigger--;
}
}
if (neededBigger > 0) {
return false;
}
return true;
}
int main() {
int t;
cin >> t;
while (t--) {
cin >> n;
cin >> s;
minS = 0;
emp.clear();
emp.resize(n);
for (int i = 0; i < n; i++) {
long long l, r;
cin >> l >> r;
emp[i] = {l, r};
minS += l;
}
sort(emp.begin(), emp.end());
long long a = 0, b = s;
while (a < b) {
long long mid = (a + b + 1) / 2;
bool curB = bSearch(mid);
if (curB) {
a = mid;
} else {
b = mid - 1;
}
}
cout << a << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 6;
const long long MOD2 = 1e9 + 7;
const long long MOD = 998244353;
long long s;
pair<long long, long long> a[N];
int n;
int qan;
bool check(long long v) {
long long ans = 0;
qan = n / 2 + 1;
for (int i = (n)-1; i >= 0; i--) {
if (a[i].first > v && qan) {
ans += a[i].first;
qan--;
} else if (a[i].first <= v && v <= a[i].second && qan) {
ans += v;
qan--;
} else {
ans += a[i].first;
}
}
if (qan == 0 && ans <= s) return true;
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int ttt;
cin >> ttt;
while (ttt--) {
cin >> n >> s;
for (int i = 0; i < (n); ++i) {
cin >> a[i].first >> a[i].second;
}
sort(a, a + n);
int l = 0, r = 1e9 + 1;
while (l < r) {
int md = (l + r) / 2;
if (check(md)) {
l = md + 1;
} else {
r = md;
}
}
cout << l - 1 << endl;
}
return 0;
}
| ### Prompt
Please create a solution in cpp to the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 6;
const long long MOD2 = 1e9 + 7;
const long long MOD = 998244353;
long long s;
pair<long long, long long> a[N];
int n;
int qan;
bool check(long long v) {
long long ans = 0;
qan = n / 2 + 1;
for (int i = (n)-1; i >= 0; i--) {
if (a[i].first > v && qan) {
ans += a[i].first;
qan--;
} else if (a[i].first <= v && v <= a[i].second && qan) {
ans += v;
qan--;
} else {
ans += a[i].first;
}
}
if (qan == 0 && ans <= s) return true;
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int ttt;
cin >> ttt;
while (ttt--) {
cin >> n >> s;
for (int i = 0; i < (n); ++i) {
cin >> a[i].first >> a[i].second;
}
sort(a, a + n);
int l = 0, r = 1e9 + 1;
while (l < r) {
int md = (l + r) / 2;
if (check(md)) {
l = md + 1;
} else {
r = md;
}
}
cout << l - 1 << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void optimise() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
bool compare(pair<long long int, long long int> X,
pair<long long int, long long int> Y) {
if (X.second < Y.second)
return 1;
else if (Y.second < X.second)
return 0;
else {
if (X.first <= Y.first)
return 1;
else
return 0;
}
}
void solve() {
long long int n, s;
cin >> n >> s;
vector<pair<long long int, long long int> > v(n);
for (long long int i = 0; i < n; i++) {
cin >> v[i].first >> v[i].second;
}
sort(v.rbegin(), v.rend());
long long int beg = 1;
long long int end = s;
long long int ans = -1;
while (beg <= end) {
long long int mid = (beg + end) / 2;
long long int sum = 0;
long long int curr = 0;
long long int flag = 0;
for (long long int i = 0; i < n; i++) {
if ((v[i].first >= mid || (v[i].first <= mid && v[i].second >= mid)) &&
(curr < (n + 1) / 2)) {
sum += max(v[i].first, mid);
curr++;
} else
sum += v[i].first;
}
if (curr == (n + 1) / 2 && sum <= s) {
beg = mid + 1;
ans = mid;
} else
end = mid - 1;
}
cout << ans;
}
signed main() {
optimise();
long long int t;
t = 1;
cin >> t;
while (t--) {
solve();
cout << "\n";
}
}
| ### Prompt
Please formulate a cpp solution to the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void optimise() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
bool compare(pair<long long int, long long int> X,
pair<long long int, long long int> Y) {
if (X.second < Y.second)
return 1;
else if (Y.second < X.second)
return 0;
else {
if (X.first <= Y.first)
return 1;
else
return 0;
}
}
void solve() {
long long int n, s;
cin >> n >> s;
vector<pair<long long int, long long int> > v(n);
for (long long int i = 0; i < n; i++) {
cin >> v[i].first >> v[i].second;
}
sort(v.rbegin(), v.rend());
long long int beg = 1;
long long int end = s;
long long int ans = -1;
while (beg <= end) {
long long int mid = (beg + end) / 2;
long long int sum = 0;
long long int curr = 0;
long long int flag = 0;
for (long long int i = 0; i < n; i++) {
if ((v[i].first >= mid || (v[i].first <= mid && v[i].second >= mid)) &&
(curr < (n + 1) / 2)) {
sum += max(v[i].first, mid);
curr++;
} else
sum += v[i].first;
}
if (curr == (n + 1) / 2 && sum <= s) {
beg = mid + 1;
ans = mid;
} else
end = mid - 1;
}
cout << ans;
}
signed main() {
optimise();
long long int t;
t = 1;
cin >> t;
while (t--) {
solve();
cout << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long INF = 1e18;
vector<pair<long long, long long> > a;
int n;
long long s;
long long check(long long m) {
int countt = a.size() / 2 + 1;
long long sum = 0;
for (int i = 0; i < a.size(); i++) {
if (a[i].first <= m && a[i].second >= m && countt) {
sum += m;
countt--;
} else {
sum += a[i].first;
if (a[i].first > m) countt--;
}
}
if (countt)
return s + 10;
else
return sum;
}
int main() {
int t;
cin >> t;
while (t--) {
scanf("%d%I64d", &n, &s);
a.clear();
for (int i = 1; i <= n; i++) {
long long x, y;
scanf("%I64d%I64d", &x, &y);
a.push_back(make_pair(x, y));
}
sort(a.begin(), a.end());
reverse(a.begin(), a.end());
long long l = a[a.size() / 2].first + 1, r = s;
while (l <= r) {
long long midd = (l + r) >> 1;
if (check(midd) <= s)
l = midd + 1;
else
r = midd - 1;
}
printf("%I64d\n", l - 1);
}
return 0;
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long INF = 1e18;
vector<pair<long long, long long> > a;
int n;
long long s;
long long check(long long m) {
int countt = a.size() / 2 + 1;
long long sum = 0;
for (int i = 0; i < a.size(); i++) {
if (a[i].first <= m && a[i].second >= m && countt) {
sum += m;
countt--;
} else {
sum += a[i].first;
if (a[i].first > m) countt--;
}
}
if (countt)
return s + 10;
else
return sum;
}
int main() {
int t;
cin >> t;
while (t--) {
scanf("%d%I64d", &n, &s);
a.clear();
for (int i = 1; i <= n; i++) {
long long x, y;
scanf("%I64d%I64d", &x, &y);
a.push_back(make_pair(x, y));
}
sort(a.begin(), a.end());
reverse(a.begin(), a.end());
long long l = a[a.size() / 2].first + 1, r = s;
while (l <= r) {
long long midd = (l + r) >> 1;
if (check(midd) <= s)
l = midd + 1;
else
r = midd - 1;
}
printf("%I64d\n", l - 1);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void fast() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
const long long mas = 200000 + 10;
long long l[mas], r[mas];
long long num[mas];
bool cmp(int x, int y) { return l[x] < l[y]; }
int main() {
fast();
long long t;
cin >> t;
long long n, s, kol, mn, mx, h, tmp;
while (t--) {
cin >> n >> s;
for (int i = 1; i <= n; ++i) cin >> l[i] >> r[i];
for (int i = 1; i <= n; ++i) num[i] = i;
sort(num + 1, num + n + 1, cmp);
for (int i = 1; i <= n; ++i) s -= l[i];
mn = 1;
mx = 10000000000;
while (mn + 1 < mx) {
kol = 0;
h = (mx + mn) / 2;
for (int i = 1; i <= n; ++i) {
if (r[i] >= h) kol++;
}
if (kol <= n / 2) {
mx = h;
continue;
}
kol = 0;
for (int i = n; i >= 1; --i) {
if (r[num[i]] >= h) kol++;
if (kol == n / 2 + 1) {
kol = i;
break;
}
}
tmp = s;
for (int i = n; i >= kol; --i) {
if (r[num[i]] >= h && l[num[i]] < h) {
tmp -= h - l[num[i]];
}
}
if (tmp >= 0) {
mn = h;
} else {
mx = h;
}
}
cout << mn << "\n";
}
return 0;
}
| ### Prompt
Your challenge is to write a cpp solution to the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void fast() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
const long long mas = 200000 + 10;
long long l[mas], r[mas];
long long num[mas];
bool cmp(int x, int y) { return l[x] < l[y]; }
int main() {
fast();
long long t;
cin >> t;
long long n, s, kol, mn, mx, h, tmp;
while (t--) {
cin >> n >> s;
for (int i = 1; i <= n; ++i) cin >> l[i] >> r[i];
for (int i = 1; i <= n; ++i) num[i] = i;
sort(num + 1, num + n + 1, cmp);
for (int i = 1; i <= n; ++i) s -= l[i];
mn = 1;
mx = 10000000000;
while (mn + 1 < mx) {
kol = 0;
h = (mx + mn) / 2;
for (int i = 1; i <= n; ++i) {
if (r[i] >= h) kol++;
}
if (kol <= n / 2) {
mx = h;
continue;
}
kol = 0;
for (int i = n; i >= 1; --i) {
if (r[num[i]] >= h) kol++;
if (kol == n / 2 + 1) {
kol = i;
break;
}
}
tmp = s;
for (int i = n; i >= kol; --i) {
if (r[num[i]] >= h && l[num[i]] < h) {
tmp -= h - l[num[i]];
}
}
if (tmp >= 0) {
mn = h;
} else {
mx = h;
}
}
cout << mn << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long n, i, j, x, t, y, s;
cin >> t;
while (t--) {
cin >> n >> s;
vector<pair<long long, long long> > v;
long long lo = 1e18, mid, hi = 0;
for (i = 0; i < n; i++) {
cin >> x >> y;
v.push_back({x, y});
lo = min(lo, x);
hi = max(hi, y);
}
sort(v.begin(), v.end(), greater<pair<long long, long long> >());
x = n / 2;
long long ans = lo;
while (lo <= hi) {
mid = (lo + hi) / 2;
bool ok = 1;
long long temp = 0;
vector<long long> d;
for (i = 0; i < n; i++) {
if (v[i].second < mid) {
temp += v[i].first;
d.push_back(v[i].first);
}
}
long long cnt = 0;
for (i = 0; i < n; i++) {
if (mid <= v[i].first) {
temp += v[i].first;
d.push_back(v[i].first);
cnt++;
}
}
for (i = 0; i < n; i++) {
if (mid > v[i].first && mid <= v[i].second) {
if (cnt > x) {
temp += v[i].first;
d.push_back(v[i].first);
} else {
temp += mid;
d.push_back(mid);
cnt++;
}
}
}
if (cnt <= x) ok = 0;
if (temp > s) ok = 0;
if (ok) {
sort(d.begin(), d.end());
ans = max(ans, d[x]);
lo = mid + 1;
} else
hi = mid - 1;
}
cout << ans << "\n";
}
}
| ### Prompt
Develop a solution in cpp to the problem described below:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long n, i, j, x, t, y, s;
cin >> t;
while (t--) {
cin >> n >> s;
vector<pair<long long, long long> > v;
long long lo = 1e18, mid, hi = 0;
for (i = 0; i < n; i++) {
cin >> x >> y;
v.push_back({x, y});
lo = min(lo, x);
hi = max(hi, y);
}
sort(v.begin(), v.end(), greater<pair<long long, long long> >());
x = n / 2;
long long ans = lo;
while (lo <= hi) {
mid = (lo + hi) / 2;
bool ok = 1;
long long temp = 0;
vector<long long> d;
for (i = 0; i < n; i++) {
if (v[i].second < mid) {
temp += v[i].first;
d.push_back(v[i].first);
}
}
long long cnt = 0;
for (i = 0; i < n; i++) {
if (mid <= v[i].first) {
temp += v[i].first;
d.push_back(v[i].first);
cnt++;
}
}
for (i = 0; i < n; i++) {
if (mid > v[i].first && mid <= v[i].second) {
if (cnt > x) {
temp += v[i].first;
d.push_back(v[i].first);
} else {
temp += mid;
d.push_back(mid);
cnt++;
}
}
}
if (cnt <= x) ok = 0;
if (temp > s) ok = 0;
if (ok) {
sort(d.begin(), d.end());
ans = max(ans, d[x]);
lo = mid + 1;
} else
hi = mid - 1;
}
cout << ans << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll n, s;
vector<pair<ll, ll>> v;
bool ok(ll x) {
ll ns = 0, cnt = 0;
vector<ll> vx;
for (ll i = 0; i < n; ++i) {
if (v[i].second < x) {
ns += v[i].first;
} else if (v[i].first >= x) {
ns += v[i].first;
cnt++;
} else
vx.push_back(v[i].first);
}
sort(vx.begin(), vx.end());
ll tam = max(0LL, (n + 1LL) / 2LL - cnt);
if (tam > vx.size()) return false;
for (ll i = 0; i < vx.size(); ++i) {
if (i < vx.size() - tam)
ns += vx[i];
else
ns += x;
}
return ns <= s;
}
void solve() {
cin >> n >> s;
v = vector<pair<ll, ll>>(n);
for (ll i = 0; i < n; ++i) {
cin >> v[i].first >> v[i].second;
}
sort(v.begin(), v.end());
ll lo = 1, hi = 1e9 + 10, mid;
while (hi - lo > 1LL) {
mid = (lo + hi) / 2LL;
if (ok(mid)) {
lo = mid;
} else {
hi = mid;
}
}
cout << lo << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll n, s;
vector<pair<ll, ll>> v;
bool ok(ll x) {
ll ns = 0, cnt = 0;
vector<ll> vx;
for (ll i = 0; i < n; ++i) {
if (v[i].second < x) {
ns += v[i].first;
} else if (v[i].first >= x) {
ns += v[i].first;
cnt++;
} else
vx.push_back(v[i].first);
}
sort(vx.begin(), vx.end());
ll tam = max(0LL, (n + 1LL) / 2LL - cnt);
if (tam > vx.size()) return false;
for (ll i = 0; i < vx.size(); ++i) {
if (i < vx.size() - tam)
ns += vx[i];
else
ns += x;
}
return ns <= s;
}
void solve() {
cin >> n >> s;
v = vector<pair<ll, ll>>(n);
for (ll i = 0; i < n; ++i) {
cin >> v[i].first >> v[i].second;
}
sort(v.begin(), v.end());
ll lo = 1, hi = 1e9 + 10, mid;
while (hi - lo > 1LL) {
mid = (lo + hi) / 2LL;
if (ok(mid)) {
lo = mid;
} else {
hi = mid;
}
}
cout << lo << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
bool can(long long mid, vector<pair<long long, long long>> &f, long long n,
long long s) {
long long sum = 0, cnt = 0;
vector<long long> v;
for (long long i = 0; i < n; i++) {
if (f[i].second < mid)
sum += f[i].first;
else if (f[i].first >= mid) {
sum += f[i].first;
cnt++;
} else {
v.push_back(f[i].first);
}
}
long long rem = max(0LL, (n + 1) / 2 - cnt);
if (v.size() < rem) return false;
sort(v.rbegin(), v.rend());
for (long long i = 0; i < v.size(); ++i) {
if (rem > 0) {
sum += mid;
rem--;
} else {
sum += v[i];
}
}
return sum <= s;
}
signed main() {
long long t;
cin >> t;
while (t--) {
long long n, s;
cin >> n >> s;
vector<pair<long long, long long>> f(n);
for (long long i = 0; i < n; i++) {
cin >> f[i].first >> f[i].second;
}
long long left = 0, right = 1e9 + 7;
while (right - left > 1) {
long long mid = right + left >> 1;
if (can(mid, f, n, s))
left = mid;
else
right = mid;
}
cout << left << '\n';
}
}
| ### Prompt
Construct a Cpp code solution to the problem outlined:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
bool can(long long mid, vector<pair<long long, long long>> &f, long long n,
long long s) {
long long sum = 0, cnt = 0;
vector<long long> v;
for (long long i = 0; i < n; i++) {
if (f[i].second < mid)
sum += f[i].first;
else if (f[i].first >= mid) {
sum += f[i].first;
cnt++;
} else {
v.push_back(f[i].first);
}
}
long long rem = max(0LL, (n + 1) / 2 - cnt);
if (v.size() < rem) return false;
sort(v.rbegin(), v.rend());
for (long long i = 0; i < v.size(); ++i) {
if (rem > 0) {
sum += mid;
rem--;
} else {
sum += v[i];
}
}
return sum <= s;
}
signed main() {
long long t;
cin >> t;
while (t--) {
long long n, s;
cin >> n >> s;
vector<pair<long long, long long>> f(n);
for (long long i = 0; i < n; i++) {
cin >> f[i].first >> f[i].second;
}
long long left = 0, right = 1e9 + 7;
while (right - left > 1) {
long long mid = right + left >> 1;
if (can(mid, f, n, s))
left = mid;
else
right = mid;
}
cout << left << '\n';
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
template <typename T>
void scan(T& a) {
cin >> a;
}
template <typename T, typename... Args>
void scan(T& t, Args&... a) {
scan(t);
scan(a...);
}
template <typename T>
void debug(T a) {
cout << a << ' ';
}
template <typename T, typename... Args>
void debug(T t, Args... a) {
debug(t);
debug(a...);
}
template <typename T>
void send(T a) {
cout << a << '\n';
}
template <typename T>
void send(vector<T> a) {
for (int i = 0; i < a.size(); i++) debug(a[i]);
send(' ');
}
template <typename T, typename... Args>
void send(T t, Args... a) {
send(t);
send(a...);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int q;
scan(q);
while (q-- > 0) {
long long p;
int n;
scan(n, p);
vector<pair<long long, long long>> a(n);
vector<long long> res;
for (int i = 0; i < n; i++) {
scan(a[i].first, a[i].second);
res.push_back(a[i].first);
}
sort((a).begin(), (a).end());
int middle = n / 2;
long long l = a[middle].first, r = 2000000000;
while (l < r) {
long long mid = (l + r) / 2ll;
long long answ = 0;
int cnt = middle + 1;
for (int i = n - 1; i >= 0; i--) {
if (a[i].first > mid && cnt) {
answ += a[i].first;
cnt--;
} else if (a[i].first <= mid && mid <= a[i].second && cnt) {
cnt--;
answ += mid;
} else
answ += a[i].first;
}
if (cnt == 0 && answ <= p) {
l = mid + 1;
} else
r = mid;
}
cout << l - 1 << '\n';
}
return 0;
}
| ### Prompt
Please create a solution in CPP to the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
template <typename T>
void scan(T& a) {
cin >> a;
}
template <typename T, typename... Args>
void scan(T& t, Args&... a) {
scan(t);
scan(a...);
}
template <typename T>
void debug(T a) {
cout << a << ' ';
}
template <typename T, typename... Args>
void debug(T t, Args... a) {
debug(t);
debug(a...);
}
template <typename T>
void send(T a) {
cout << a << '\n';
}
template <typename T>
void send(vector<T> a) {
for (int i = 0; i < a.size(); i++) debug(a[i]);
send(' ');
}
template <typename T, typename... Args>
void send(T t, Args... a) {
send(t);
send(a...);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int q;
scan(q);
while (q-- > 0) {
long long p;
int n;
scan(n, p);
vector<pair<long long, long long>> a(n);
vector<long long> res;
for (int i = 0; i < n; i++) {
scan(a[i].first, a[i].second);
res.push_back(a[i].first);
}
sort((a).begin(), (a).end());
int middle = n / 2;
long long l = a[middle].first, r = 2000000000;
while (l < r) {
long long mid = (l + r) / 2ll;
long long answ = 0;
int cnt = middle + 1;
for (int i = n - 1; i >= 0; i--) {
if (a[i].first > mid && cnt) {
answ += a[i].first;
cnt--;
} else if (a[i].first <= mid && mid <= a[i].second && cnt) {
cnt--;
answ += mid;
} else
answ += a[i].first;
}
if (cnt == 0 && answ <= p) {
l = mid + 1;
} else
r = mid;
}
cout << l - 1 << '\n';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7, MAX = 2e5 + 2;
int n;
long long sum, res;
vector<pair<long long, long long>> v;
int b_s(long long beg, long long end) {
if (beg > end) return 0;
long long mid = (beg + end) / 2, total = sum;
int cnt = 0;
vector<pair<long long, long long>> temp = v;
for (int i = n - 1; i >= 0; --i)
if (temp[i].second >= mid && cnt < (n + 1) / 2)
cnt++, temp[i].second = -1, total -= max(temp[i].first, mid);
if (cnt == (n + 1) / 2) {
long long mx = 0;
for (int i = 0; i < n; i++)
if (temp[i].second != -1)
total -= temp[i].first, mx = max(mx, temp[i].first);
if (mx > mid) total = -1;
if (total >= 0) return res = max(res, mid), b_s(mid + 1, end);
}
return b_s(beg, mid - 1);
}
void fast();
int main() {
fast();
int t;
cin >> t;
while (t--) {
cin >> n >> sum;
v.resize(n);
for (int i = 0; i < n; i++) cin >> v[i].first >> v[i].second;
sort((v).begin(), (v).end());
res = 0;
if (n == 1)
cout << min(sum, v[0].second) << "\n";
else {
b_s(v[n / 2].first, sum);
cout << res << "\n";
}
}
}
void fast() {
std::ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7, MAX = 2e5 + 2;
int n;
long long sum, res;
vector<pair<long long, long long>> v;
int b_s(long long beg, long long end) {
if (beg > end) return 0;
long long mid = (beg + end) / 2, total = sum;
int cnt = 0;
vector<pair<long long, long long>> temp = v;
for (int i = n - 1; i >= 0; --i)
if (temp[i].second >= mid && cnt < (n + 1) / 2)
cnt++, temp[i].second = -1, total -= max(temp[i].first, mid);
if (cnt == (n + 1) / 2) {
long long mx = 0;
for (int i = 0; i < n; i++)
if (temp[i].second != -1)
total -= temp[i].first, mx = max(mx, temp[i].first);
if (mx > mid) total = -1;
if (total >= 0) return res = max(res, mid), b_s(mid + 1, end);
}
return b_s(beg, mid - 1);
}
void fast();
int main() {
fast();
int t;
cin >> t;
while (t--) {
cin >> n >> sum;
v.resize(n);
for (int i = 0; i < n; i++) cin >> v[i].first >> v[i].second;
sort((v).begin(), (v).end());
res = 0;
if (n == 1)
cout << min(sum, v[0].second) << "\n";
else {
b_s(v[n / 2].first, sum);
cout << res << "\n";
}
}
}
void fast() {
std::ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
}
``` |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 2;
int t;
int n;
ll s, a[N];
pair<ll, ll> seg[N];
vector<ll> vs;
int main() {
ios::sync_with_stdio(false);
cin >> t;
while (t--) {
cin >> n >> s;
for (int i = 1; i <= n; ++i) {
cin >> seg[i].first >> seg[i].second;
a[i] = seg[i].first;
s -= a[i];
}
ll l = 0, r = 1e9, t;
while (l <= r) {
ll m = (l + r) / 2;
int cnt = 0;
for (int i = 1; i <= n; ++i)
if (a[i] > m) ++cnt;
if (cnt <= n / 2) {
t = m;
r = m - 1;
} else {
l = m + 1;
}
}
l = t;
r = 1e9;
ll ans;
while (l <= r) {
ll m = (l + r) / 2;
vs.clear();
int cs = 0;
for (int i = 1; i <= n; ++i)
if (a[i] < m) {
++cs;
if (seg[i].second >= m) vs.push_back(m - seg[i].first);
}
sort(vs.begin(), vs.end());
cs -= n / 2;
ll sum = 0;
bool ok = true;
if (cs > 0) {
if (cs > vs.size())
ok = false;
else {
for (int i = 0; i < cs; ++i) sum += vs[i];
}
}
if (sum > s) ok = false;
if (ok) {
ans = m;
l = m + 1;
} else {
r = m - 1;
}
}
cout << ans << "\n";
}
return 0;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 2;
int t;
int n;
ll s, a[N];
pair<ll, ll> seg[N];
vector<ll> vs;
int main() {
ios::sync_with_stdio(false);
cin >> t;
while (t--) {
cin >> n >> s;
for (int i = 1; i <= n; ++i) {
cin >> seg[i].first >> seg[i].second;
a[i] = seg[i].first;
s -= a[i];
}
ll l = 0, r = 1e9, t;
while (l <= r) {
ll m = (l + r) / 2;
int cnt = 0;
for (int i = 1; i <= n; ++i)
if (a[i] > m) ++cnt;
if (cnt <= n / 2) {
t = m;
r = m - 1;
} else {
l = m + 1;
}
}
l = t;
r = 1e9;
ll ans;
while (l <= r) {
ll m = (l + r) / 2;
vs.clear();
int cs = 0;
for (int i = 1; i <= n; ++i)
if (a[i] < m) {
++cs;
if (seg[i].second >= m) vs.push_back(m - seg[i].first);
}
sort(vs.begin(), vs.end());
cs -= n / 2;
ll sum = 0;
bool ok = true;
if (cs > 0) {
if (cs > vs.size())
ok = false;
else {
for (int i = 0; i < cs; ++i) sum += vs[i];
}
}
if (sum > s) ok = false;
if (ok) {
ans = m;
l = m + 1;
} else {
r = m - 1;
}
}
cout << ans << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
template <typename T>
T GCD(T a, T b) {
return a ? GCD(b % a, a) : b;
}
template <typename T>
T LCM(T a, T b) {
return (a * b) / GCD(a, b);
}
template <typename T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
for (auto ob : v) os << ob << " ";
return os;
}
template <typename T, typename S>
std::ostream &operator<<(std::ostream &os, const std::map<T, S> &v) {
for (auto ob : v) os << ob.first << " : " << ob.second << std::endl;
return os;
}
using ld = double;
using ll = long long int;
using ul = unsigned long long int;
using namespace std;
class DSalaryChanging {
bool Possible(vector<pair<int, int>> money, ll s, int mid, int n) {
ll sum = 0;
int cnt = 0;
for (int i = 0; i < n; ++i) {
if (money[i].second < mid) {
sum += money[i].first;
} else if (money[i].first >= mid) {
cnt++;
sum += money[i].first;
} else if (money[i].first < mid && money[i].second >= mid) {
if (cnt > n / 2) {
sum += money[i].first;
} else {
cnt++;
sum += mid;
}
}
}
return cnt > n / 2 && sum <= s;
}
public:
void solve(std::istream &in, std::ostream &out) {
ll n, s;
in >> n >> s;
vector<pair<int, int>> money;
for (int i = 0; i < n; ++i) {
int l, r;
in >> l >> r;
money.push_back(make_pair(l, r));
}
sort(money.begin(), money.end());
reverse(money.begin(), money.end());
int low = 1, high = 1e9 + 17, ans = 0;
while (low + 1 < high) {
int mid = (low + high) / 2;
bool isPossible = Possible(money, s, mid, n);
if (isPossible) {
ans = mid;
low = mid;
} else {
high = mid;
}
}
out << ans << endl;
}
};
int main() {
std::ios_base::sync_with_stdio(0);
std::cin.tie(nullptr);
DSalaryChanging solver;
std::istream &in(std::cin);
std::ostream &out(std::cout);
int n;
in >> n;
for (int i = 0; i < n; ++i) {
solver.solve(in, out);
}
return 0;
}
| ### Prompt
Create a solution in cpp for the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
template <typename T>
T GCD(T a, T b) {
return a ? GCD(b % a, a) : b;
}
template <typename T>
T LCM(T a, T b) {
return (a * b) / GCD(a, b);
}
template <typename T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
for (auto ob : v) os << ob << " ";
return os;
}
template <typename T, typename S>
std::ostream &operator<<(std::ostream &os, const std::map<T, S> &v) {
for (auto ob : v) os << ob.first << " : " << ob.second << std::endl;
return os;
}
using ld = double;
using ll = long long int;
using ul = unsigned long long int;
using namespace std;
class DSalaryChanging {
bool Possible(vector<pair<int, int>> money, ll s, int mid, int n) {
ll sum = 0;
int cnt = 0;
for (int i = 0; i < n; ++i) {
if (money[i].second < mid) {
sum += money[i].first;
} else if (money[i].first >= mid) {
cnt++;
sum += money[i].first;
} else if (money[i].first < mid && money[i].second >= mid) {
if (cnt > n / 2) {
sum += money[i].first;
} else {
cnt++;
sum += mid;
}
}
}
return cnt > n / 2 && sum <= s;
}
public:
void solve(std::istream &in, std::ostream &out) {
ll n, s;
in >> n >> s;
vector<pair<int, int>> money;
for (int i = 0; i < n; ++i) {
int l, r;
in >> l >> r;
money.push_back(make_pair(l, r));
}
sort(money.begin(), money.end());
reverse(money.begin(), money.end());
int low = 1, high = 1e9 + 17, ans = 0;
while (low + 1 < high) {
int mid = (low + high) / 2;
bool isPossible = Possible(money, s, mid, n);
if (isPossible) {
ans = mid;
low = mid;
} else {
high = mid;
}
}
out << ans << endl;
}
};
int main() {
std::ios_base::sync_with_stdio(0);
std::cin.tie(nullptr);
DSalaryChanging solver;
std::istream &in(std::cin);
std::ostream &out(std::cout);
int n;
in >> n;
for (int i = 0; i < n; ++i) {
solver.solve(in, out);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
int n;
long long s;
int x[MAXN], y[MAXN];
int tot, cnt, z[MAXN];
inline bool Check(int sl) {
tot = cnt = 0;
long long sum = 0;
for (int i = 1; i <= n; i++) {
if (y[i] >= sl) {
if (x[i] >= sl)
cnt++, sum += x[i];
else
z[++tot] = -x[i];
} else
sum += x[i];
}
if (tot + cnt <= n / 2) return 0;
sort(z + 1, z + tot + 1);
for (int i = 1; i <= tot; i++) {
if (cnt + i - 1 <= n / 2)
sum += sl;
else
sum -= z[i];
}
return sum <= s;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
scanf("%d%I64d", &n, &s);
for (int i = 1; i <= n; i++) scanf("%d%d", x + i, y + i);
int l = 1, r = 1e9;
while (l < r) {
const int mid = l + r + 1 >> 1;
if (Check(mid))
l = mid;
else
r = mid - 1;
}
printf("%d\n", r);
}
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
int n;
long long s;
int x[MAXN], y[MAXN];
int tot, cnt, z[MAXN];
inline bool Check(int sl) {
tot = cnt = 0;
long long sum = 0;
for (int i = 1; i <= n; i++) {
if (y[i] >= sl) {
if (x[i] >= sl)
cnt++, sum += x[i];
else
z[++tot] = -x[i];
} else
sum += x[i];
}
if (tot + cnt <= n / 2) return 0;
sort(z + 1, z + tot + 1);
for (int i = 1; i <= tot; i++) {
if (cnt + i - 1 <= n / 2)
sum += sl;
else
sum -= z[i];
}
return sum <= s;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
scanf("%d%I64d", &n, &s);
for (int i = 1; i <= n; i++) scanf("%d%d", x + i, y + i);
int l = 1, r = 1e9;
while (l < r) {
const int mid = l + r + 1 >> 1;
if (Check(mid))
l = mid;
else
r = mid - 1;
}
printf("%d\n", r);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
const long long MOD = 1000000007;
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
signed main(void) {
long long q;
cin >> q;
for (long long(fjfea) = 0; (fjfea) < (q); (fjfea)++) {
long long n, money;
cin >> n >> money;
vector<pair<long long, long long>> range(n);
for (long long(i) = 0; (i) < (n); (i)++) {
long long a, b;
cin >> a >> b;
range[i] = make_pair(a, b);
}
sort(range.begin(), range.end());
long long l = -1, r = money * 2;
bool paid[n];
while (abs(r - l) > 1) {
long long cost = 0, low = 0, high = 0;
memset(paid, false, n);
long long mid = (r + l) / 2;
for (long long(i) = 0; (i) < (n); (i)++) {
if (range[i].second < mid)
cost += range[i].first, low++, paid[i] = true;
if (range[i].first > mid)
cost += range[i].first, high++, paid[i] = true;
}
if (low > n / 2 || cost > money) {
r = mid;
continue;
}
if (high > n / 2) {
l = mid;
continue;
}
for (long long(i) = 0; (i) < (n); (i)++) {
if (!paid[i]) {
if (low < n / 2) {
low++;
cost += range[i].first;
paid[i] = true;
} else {
cost += mid;
high++;
}
}
}
if (cost > money) {
r = mid;
continue;
} else
l = mid;
}
cout << l << endl;
}
}
| ### Prompt
Please create a solution in Cpp to the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
const long long MOD = 1000000007;
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
signed main(void) {
long long q;
cin >> q;
for (long long(fjfea) = 0; (fjfea) < (q); (fjfea)++) {
long long n, money;
cin >> n >> money;
vector<pair<long long, long long>> range(n);
for (long long(i) = 0; (i) < (n); (i)++) {
long long a, b;
cin >> a >> b;
range[i] = make_pair(a, b);
}
sort(range.begin(), range.end());
long long l = -1, r = money * 2;
bool paid[n];
while (abs(r - l) > 1) {
long long cost = 0, low = 0, high = 0;
memset(paid, false, n);
long long mid = (r + l) / 2;
for (long long(i) = 0; (i) < (n); (i)++) {
if (range[i].second < mid)
cost += range[i].first, low++, paid[i] = true;
if (range[i].first > mid)
cost += range[i].first, high++, paid[i] = true;
}
if (low > n / 2 || cost > money) {
r = mid;
continue;
}
if (high > n / 2) {
l = mid;
continue;
}
for (long long(i) = 0; (i) < (n); (i)++) {
if (!paid[i]) {
if (low < n / 2) {
low++;
cost += range[i].first;
paid[i] = true;
} else {
cost += mid;
high++;
}
}
}
if (cost > money) {
r = mid;
continue;
} else
l = mid;
}
cout << l << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
namespace flyinthesky {
const int MAXN = 200000 + 5;
long long n, s, ll[MAXN], rr[MAXN];
long long tot, orz[MAXN];
struct qj {
long long l, r;
} itv[MAXN];
bool chk(long long x) {
long long gg1 = 0, gg2 = 0, gg3 = 0;
for (int i = 1; i <= n; ++i) {
if (itv[i].r < x)
++gg1;
else if (itv[i].l > x)
++gg2;
else
orz[++gg3] = x - itv[i].l;
}
sort(orz + 1, orz + 1 + gg3);
if (n / 2 - gg1 + n / 2 - gg2 >= gg3) return 0;
for (int i = 2; i <= gg3; ++i) orz[i] += orz[i - 1];
long long hh = orz[n / 2 - gg2 + 1];
if (tot + hh <= s)
return 1;
else
return 0;
}
void clean() {}
int solve() {
clean();
int t;
cin >> t;
while (t--) {
scanf("%lld%lld", &n, &s);
tot = 0;
for (int i = 1; i <= n; ++i) {
scanf("%lld%lld", &ll[i], &rr[i]);
itv[i] = (qj){ll[i], rr[i]};
tot += ll[i];
}
if (n == 1) {
printf("%lld\n", min(s, rr[1]));
continue;
}
sort(ll + 1, ll + 1 + n);
sort(rr + 1, rr + 1 + n);
long long l = ll[(n + 1) / 2], r = rr[(n + 1) / 2] + 1, ans = 0;
while (l < r) {
int mid = (l + r) >> 1;
if (chk(mid))
ans = mid, l = mid + 1;
else
r = mid;
}
printf("%lld\n", ans);
}
return 0;
}
} // namespace flyinthesky
int main() {
flyinthesky::solve();
return 0;
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
namespace flyinthesky {
const int MAXN = 200000 + 5;
long long n, s, ll[MAXN], rr[MAXN];
long long tot, orz[MAXN];
struct qj {
long long l, r;
} itv[MAXN];
bool chk(long long x) {
long long gg1 = 0, gg2 = 0, gg3 = 0;
for (int i = 1; i <= n; ++i) {
if (itv[i].r < x)
++gg1;
else if (itv[i].l > x)
++gg2;
else
orz[++gg3] = x - itv[i].l;
}
sort(orz + 1, orz + 1 + gg3);
if (n / 2 - gg1 + n / 2 - gg2 >= gg3) return 0;
for (int i = 2; i <= gg3; ++i) orz[i] += orz[i - 1];
long long hh = orz[n / 2 - gg2 + 1];
if (tot + hh <= s)
return 1;
else
return 0;
}
void clean() {}
int solve() {
clean();
int t;
cin >> t;
while (t--) {
scanf("%lld%lld", &n, &s);
tot = 0;
for (int i = 1; i <= n; ++i) {
scanf("%lld%lld", &ll[i], &rr[i]);
itv[i] = (qj){ll[i], rr[i]};
tot += ll[i];
}
if (n == 1) {
printf("%lld\n", min(s, rr[1]));
continue;
}
sort(ll + 1, ll + 1 + n);
sort(rr + 1, rr + 1 + n);
long long l = ll[(n + 1) / 2], r = rr[(n + 1) / 2] + 1, ans = 0;
while (l < r) {
int mid = (l + r) >> 1;
if (chk(mid))
ans = mid, l = mid + 1;
else
r = mid;
}
printf("%lld\n", ans);
}
return 0;
}
} // namespace flyinthesky
int main() {
flyinthesky::solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 2;
vector<pair<long long, long long>> v(N);
long long n, s;
bool check(long long mid) {
long long cnt = 0, sum = 0;
vector<long long> tmp;
for (long long i = 0; i < n; i++) {
if (mid > v[i].second)
sum += v[i].first;
else if (mid <= v[i].first) {
sum += v[i].first;
cnt++;
} else
tmp.push_back(v[i].first);
}
long long need = max(0LL, (n + 1) / 2 - cnt);
if (need > tmp.size()) return false;
for (long long i = 0; i < tmp.size(); i++) {
if (i < tmp.size() - need)
sum += tmp[i];
else
sum += mid;
}
return sum <= s;
}
signed main() {
std ::ios_base ::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long t;
cin >> t;
while (t--) {
cin >> n >> s;
v.resize(n);
for (long long i = 0; i < n; i++) cin >> v[i].first >> v[i].second;
sort((v).begin(), (v).end());
long long l = 1, r = 1e9 + 5;
while (l < r - 1) {
long long mid = (l + r) / 2;
if (check(mid))
l = mid;
else
r = mid;
}
cout << l << "\n";
}
}
| ### Prompt
Create a solution in cpp for the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 2;
vector<pair<long long, long long>> v(N);
long long n, s;
bool check(long long mid) {
long long cnt = 0, sum = 0;
vector<long long> tmp;
for (long long i = 0; i < n; i++) {
if (mid > v[i].second)
sum += v[i].first;
else if (mid <= v[i].first) {
sum += v[i].first;
cnt++;
} else
tmp.push_back(v[i].first);
}
long long need = max(0LL, (n + 1) / 2 - cnt);
if (need > tmp.size()) return false;
for (long long i = 0; i < tmp.size(); i++) {
if (i < tmp.size() - need)
sum += tmp[i];
else
sum += mid;
}
return sum <= s;
}
signed main() {
std ::ios_base ::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long t;
cin >> t;
while (t--) {
cin >> n >> s;
v.resize(n);
for (long long i = 0; i < n; i++) cin >> v[i].first >> v[i].second;
sort((v).begin(), (v).end());
long long l = 1, r = 1e9 + 5;
while (l < r - 1) {
long long mid = (l + r) / 2;
if (check(mid))
l = mid;
else
r = mid;
}
cout << l << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 50;
struct Node {
int l, r;
bool operator<(const Node& b) const { return l > b.l; }
};
Node a[N];
bool check(int x, int n, long long s) {
int m = 0;
long long sum = 0;
for (int i = 1; i <= n; i++) {
if (a[i].l > x) {
m++;
sum += a[i].l;
} else {
if (m < n / 2 + 1 && x <= a[i].r) {
m++;
sum += x;
} else {
sum += a[i].l;
}
}
}
return m == n / 2 + 1 && sum <= s;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
long long s;
scanf("%d%lld", &n, &s);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &a[i].l, &a[i].r);
}
sort(a + 1, a + 1 + n);
int l = a[n / 2 + 1].l, r = (int)1e9, ans = l;
while (l <= r) {
int m = (l + r) >> 1;
if (check(m, n, s)) {
ans = m;
l = m + 1;
} else {
r = m - 1;
}
}
printf("%d\n", ans);
}
}
| ### Prompt
Your task is to create a cpp solution to the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 50;
struct Node {
int l, r;
bool operator<(const Node& b) const { return l > b.l; }
};
Node a[N];
bool check(int x, int n, long long s) {
int m = 0;
long long sum = 0;
for (int i = 1; i <= n; i++) {
if (a[i].l > x) {
m++;
sum += a[i].l;
} else {
if (m < n / 2 + 1 && x <= a[i].r) {
m++;
sum += x;
} else {
sum += a[i].l;
}
}
}
return m == n / 2 + 1 && sum <= s;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
long long s;
scanf("%d%lld", &n, &s);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &a[i].l, &a[i].r);
}
sort(a + 1, a + 1 + n);
int l = a[n / 2 + 1].l, r = (int)1e9, ans = l;
while (l <= r) {
int m = (l + r) >> 1;
if (check(m, n, s)) {
ans = m;
l = m + 1;
} else {
r = m - 1;
}
}
printf("%d\n", ans);
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long N = 2000005;
const long long M = 1e9 + 7;
int n;
long long s;
vector<pair<int, int> > v;
vector<int> a, b;
bool check(int x) {
vector<int> t;
int a = 0, b = 0;
long long ss = 0;
for (int i = 0; i < n; i++) {
if (v[i].second < x) {
a++;
ss += v[i].first;
} else if (v[i].first > x) {
b++;
ss += v[i].first;
} else
t.push_back(i);
}
for (auto i : t) {
if (a < n / 2)
a++, ss += v[i].first;
else
b++, ss += x;
}
return (a == n / 2 && ss <= s);
}
void solve() {
v.clear();
a.clear();
b.clear();
cin >> n >> s;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
v.push_back(make_pair(x, y));
a.push_back(x);
b.push_back(y);
}
sort((a).begin(), (a).end());
sort((b).begin(), (b).end());
sort((v).begin(), (v).end());
int low = a[n / 2], high = b[n / 2];
while (high - low > 1) {
int mid = (low + high) >> 1;
if (check(mid))
low = mid;
else
high = mid;
}
if (check(high))
cout << high << "\n";
else
cout << low << "\n";
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
for (int i = 1; i <= t; i++) {
solve();
}
return 0;
}
| ### Prompt
Please create a solution in CPP to the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long N = 2000005;
const long long M = 1e9 + 7;
int n;
long long s;
vector<pair<int, int> > v;
vector<int> a, b;
bool check(int x) {
vector<int> t;
int a = 0, b = 0;
long long ss = 0;
for (int i = 0; i < n; i++) {
if (v[i].second < x) {
a++;
ss += v[i].first;
} else if (v[i].first > x) {
b++;
ss += v[i].first;
} else
t.push_back(i);
}
for (auto i : t) {
if (a < n / 2)
a++, ss += v[i].first;
else
b++, ss += x;
}
return (a == n / 2 && ss <= s);
}
void solve() {
v.clear();
a.clear();
b.clear();
cin >> n >> s;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
v.push_back(make_pair(x, y));
a.push_back(x);
b.push_back(y);
}
sort((a).begin(), (a).end());
sort((b).begin(), (b).end());
sort((v).begin(), (v).end());
int low = a[n / 2], high = b[n / 2];
while (high - low > 1) {
int mid = (low + high) >> 1;
if (check(mid))
low = mid;
else
high = mid;
}
if (check(high))
cout << high << "\n";
else
cout << low << "\n";
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
for (int i = 1; i <= t; i++) {
solve();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
typedef int (*uniopi)(int);
typedef long long (*uniopl)(long long);
typedef int (*binopi)(int, int);
typedef long long (*binopl)(long long, long long);
typedef int (*tupopi)(int, int, int);
typedef long long (*tupopl)(long long, long long, long long);
const long double PI = acos(0) * 2;
const int INF = 0x3f2f1f0f;
const long long LINF = 1ll * INF * INF;
int X4[] = {-1, 0, 1, 0}, Y4[] = {0, -1, 0, 1};
int X8[] = {-1, -1, -1, 0, 1, 1, 1, 0}, Y8[] = {-1, 0, 1, 1, 1, 0, -1, -1};
template <typename T>
inline bool uax(T& a, T b) {
return a < b ? (a = b, 1) : 0;
}
template <typename T>
inline bool uin(T& a, T b) {
return a > b ? (a = b, 1) : 0;
}
template <typename T1, typename T2>
istream& operator>>(istream& is, pair<T1, T2>& p) {
return is >> p.first >> p.second;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) {
return os << '(' << p.first << ", " << p.second << ")";
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
for (int i = 0; i < (int)v.size(); i++) {
os << v[i];
if (i + 1 < (int)v.size()) os << ' ';
}
return os;
}
const int MAX_N = 2e5 + 20;
int A[MAX_N], B[MAX_N], AB[MAX_N], NA, NB, NAB;
long long L[MAX_N], R[MAX_N], N;
int ispos(long long m, long long s) {
int n = N, h = (1 + n) / 2;
NA = NB = NAB = 0;
for (int i = 0; i < (n); i++) {
if (L[i] <= m) {
if (R[i] >= m)
AB[NAB++] = i;
else
A[NA++] = i;
} else
B[NB++] = i;
}
if (NA >= h) return 0;
if (NB >= h) return 1;
long long cost = 0;
for (int j = 0; j < (NA); j++) cost += L[A[j]];
for (int j = 0; j < (NB); j++) cost += L[B[j]];
sort(AB, AB + NAB, [](const int a, const int b) { return L[a] < L[b]; });
for (int j = 0; j < (h - 1 - NA); j++) cost += L[AB[j]];
cost += (h - NB) * m;
return cost <= s;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
cout << std::setprecision(10);
cout << fixed;
int q;
cin >> q;
while (q--) {
long long s;
cin >> N >> s;
for (int i = 0; i < (N); i++) cin >> L[i] >> R[i];
long long bl = *min_element(L, L + N), br = *max_element(R, R + N), bm;
while (bl < br) {
bm = (bl + br + 1) >> 1;
if (ispos(bm, s))
bl = bm;
else
br = bm - 1;
}
cout << bl << endl;
}
return 0;
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
typedef int (*uniopi)(int);
typedef long long (*uniopl)(long long);
typedef int (*binopi)(int, int);
typedef long long (*binopl)(long long, long long);
typedef int (*tupopi)(int, int, int);
typedef long long (*tupopl)(long long, long long, long long);
const long double PI = acos(0) * 2;
const int INF = 0x3f2f1f0f;
const long long LINF = 1ll * INF * INF;
int X4[] = {-1, 0, 1, 0}, Y4[] = {0, -1, 0, 1};
int X8[] = {-1, -1, -1, 0, 1, 1, 1, 0}, Y8[] = {-1, 0, 1, 1, 1, 0, -1, -1};
template <typename T>
inline bool uax(T& a, T b) {
return a < b ? (a = b, 1) : 0;
}
template <typename T>
inline bool uin(T& a, T b) {
return a > b ? (a = b, 1) : 0;
}
template <typename T1, typename T2>
istream& operator>>(istream& is, pair<T1, T2>& p) {
return is >> p.first >> p.second;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) {
return os << '(' << p.first << ", " << p.second << ")";
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
for (int i = 0; i < (int)v.size(); i++) {
os << v[i];
if (i + 1 < (int)v.size()) os << ' ';
}
return os;
}
const int MAX_N = 2e5 + 20;
int A[MAX_N], B[MAX_N], AB[MAX_N], NA, NB, NAB;
long long L[MAX_N], R[MAX_N], N;
int ispos(long long m, long long s) {
int n = N, h = (1 + n) / 2;
NA = NB = NAB = 0;
for (int i = 0; i < (n); i++) {
if (L[i] <= m) {
if (R[i] >= m)
AB[NAB++] = i;
else
A[NA++] = i;
} else
B[NB++] = i;
}
if (NA >= h) return 0;
if (NB >= h) return 1;
long long cost = 0;
for (int j = 0; j < (NA); j++) cost += L[A[j]];
for (int j = 0; j < (NB); j++) cost += L[B[j]];
sort(AB, AB + NAB, [](const int a, const int b) { return L[a] < L[b]; });
for (int j = 0; j < (h - 1 - NA); j++) cost += L[AB[j]];
cost += (h - NB) * m;
return cost <= s;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
cout << std::setprecision(10);
cout << fixed;
int q;
cin >> q;
while (q--) {
long long s;
cin >> N >> s;
for (int i = 0; i < (N); i++) cin >> L[i] >> R[i];
long long bl = *min_element(L, L + N), br = *max_element(R, R + N), bm;
while (bl < br) {
bm = (bl + br + 1) >> 1;
if (ispos(bm, s))
bl = bm;
else
br = bm - 1;
}
cout << bl << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long mx = 200005;
bool comparator(pair<long long, long long> A, pair<long long, long long> B) {
return A.first < B.first;
}
int main() {
ios::sync_with_stdio(0);
long long q;
cin >> q;
while (q--) {
long long n, s;
long long l;
long long r;
long long ans = 0;
vector<pair<long long, long long>> intv;
vector<long long> Min, Max;
cin >> n >> s;
for (int i = 1; i <= n; ++i) {
int l, r;
cin >> l >> r;
intv.push_back(make_pair(l, r));
Min.push_back(l);
Max.push_back(r);
}
sort(Min.begin(), Min.end());
sort(Max.begin(), Max.end());
l = Min[n / 2];
r = Max[n / 2];
while (l <= r) {
long long m = (l + r) / 2;
long long cost = m;
long long cntLeft = 0;
long long cntRight = 0;
vector<pair<int, int>> both;
for (int i = 0; i < n; ++i) {
if (intv[i].second < m)
cost += intv[i].first, ++cntLeft;
else if (intv[i].first > m)
cost += intv[i].first, ++cntRight;
else if (intv[i].first <= m && intv[i].second >= m)
both.push_back(intv[i]);
}
sort(both.begin(), both.end(), comparator);
for (int i = 0; i < both.size() && cntLeft < n / 2; ++i)
cost += both[i].first, ++cntLeft;
for (int i = both.size() - 1; i >= 0 && cntRight < n / 2; --i)
cost += m, ++cntRight;
if (cost <= s) {
ans = max(ans, m);
l = m + 1;
} else {
r = m - 1;
}
}
cout << ans << "\n";
}
return 0;
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long mx = 200005;
bool comparator(pair<long long, long long> A, pair<long long, long long> B) {
return A.first < B.first;
}
int main() {
ios::sync_with_stdio(0);
long long q;
cin >> q;
while (q--) {
long long n, s;
long long l;
long long r;
long long ans = 0;
vector<pair<long long, long long>> intv;
vector<long long> Min, Max;
cin >> n >> s;
for (int i = 1; i <= n; ++i) {
int l, r;
cin >> l >> r;
intv.push_back(make_pair(l, r));
Min.push_back(l);
Max.push_back(r);
}
sort(Min.begin(), Min.end());
sort(Max.begin(), Max.end());
l = Min[n / 2];
r = Max[n / 2];
while (l <= r) {
long long m = (l + r) / 2;
long long cost = m;
long long cntLeft = 0;
long long cntRight = 0;
vector<pair<int, int>> both;
for (int i = 0; i < n; ++i) {
if (intv[i].second < m)
cost += intv[i].first, ++cntLeft;
else if (intv[i].first > m)
cost += intv[i].first, ++cntRight;
else if (intv[i].first <= m && intv[i].second >= m)
both.push_back(intv[i]);
}
sort(both.begin(), both.end(), comparator);
for (int i = 0; i < both.size() && cntLeft < n / 2; ++i)
cost += both[i].first, ++cntLeft;
for (int i = both.size() - 1; i >= 0 && cntRight < n / 2; --i)
cost += m, ++cntRight;
if (cost <= s) {
ans = max(ans, m);
l = m + 1;
} else {
r = m - 1;
}
}
cout << ans << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 2;
pair<long long, long long> sal[N];
bool solve(long long m, long long to, long long n) {
long long sum = 0;
int cnt = 0, rem, i;
vector<long long> ss;
for (i = 0; i < n; i++) {
if (sal[i].second < m)
sum += sal[i].first;
else if (sal[i].first >= m)
sum += sal[i].first, cnt++;
else
ss.push_back(sal[i].first);
}
rem = max(0LL, ((n + 1) / 2) - cnt);
if (rem > ss.size()) return false;
for (i = ss.size() - 1; i > -1; i--) {
if (rem > 0)
sum += m, rem--;
else
sum += ss[i];
}
return (sum <= to ? true : false);
}
int main() {
long long t, n, i, lo, hi, to, mid;
scanf("%lld", &t);
while (t--) {
scanf("%lld %lld", &n, &to);
for (i = 0; i < n; i++) scanf("%lld %lld", &sal[i].first, &sal[i].second);
sort(sal, sal + n);
lo = 1, hi = to;
while (lo <= hi) {
mid = (lo + hi) / 2;
if (solve(mid, to, n))
lo = mid + 1;
else
hi = mid - 1;
}
printf("%lld\n", lo - 1);
}
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 2;
pair<long long, long long> sal[N];
bool solve(long long m, long long to, long long n) {
long long sum = 0;
int cnt = 0, rem, i;
vector<long long> ss;
for (i = 0; i < n; i++) {
if (sal[i].second < m)
sum += sal[i].first;
else if (sal[i].first >= m)
sum += sal[i].first, cnt++;
else
ss.push_back(sal[i].first);
}
rem = max(0LL, ((n + 1) / 2) - cnt);
if (rem > ss.size()) return false;
for (i = ss.size() - 1; i > -1; i--) {
if (rem > 0)
sum += m, rem--;
else
sum += ss[i];
}
return (sum <= to ? true : false);
}
int main() {
long long t, n, i, lo, hi, to, mid;
scanf("%lld", &t);
while (t--) {
scanf("%lld %lld", &n, &to);
for (i = 0; i < n; i++) scanf("%lld %lld", &sal[i].first, &sal[i].second);
sort(sal, sal + n);
lo = 1, hi = to;
while (lo <= hi) {
mid = (lo + hi) / 2;
if (solve(mid, to, n))
lo = mid + 1;
else
hi = mid - 1;
}
printf("%lld\n", lo - 1);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct par {
ll l;
ll r;
};
int n;
ll s;
par mas[200100];
bool cmp(par a, par b) { return (a.l < b.l); }
bool good(ll m) {
vector<par> grp1;
vector<par> grp2;
vector<par> grp3;
for (int i = 0; i < n; i++) {
if (mas[i].r < m)
grp1.push_back(mas[i]);
else if (m <= mas[i].l)
grp2.push_back(mas[i]);
else
grp3.push_back(mas[i]);
}
int rem = max(0, (n + 1) / 2 - (int)grp2.size());
if (rem > grp3.size()) return false;
ll sum = 0;
for (auto el : grp1) sum += el.l;
for (auto el : grp2) sum += el.l;
if (grp3.size() > 0) sort(grp3.begin(), grp3.end(), cmp);
for (int i = 1; i <= rem; i++) sum += m;
for (int i = 0; i < grp3.size() - rem; i++) sum += grp3[i].l;
return (sum <= s);
}
ll bin_s() {
ll l = 1, r = 1e9;
ll m;
while (l < r) {
m = (l + r + 1) / 2;
if (good(m))
l = m;
else
r = m - 1;
}
return l;
}
void solve() {
cin >> n >> s;
for (int i = 0; i < n; i++) {
cin >> mas[i].l >> mas[i].r;
}
ll med = bin_s();
cout << med << '\n';
return;
}
int main() {
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct par {
ll l;
ll r;
};
int n;
ll s;
par mas[200100];
bool cmp(par a, par b) { return (a.l < b.l); }
bool good(ll m) {
vector<par> grp1;
vector<par> grp2;
vector<par> grp3;
for (int i = 0; i < n; i++) {
if (mas[i].r < m)
grp1.push_back(mas[i]);
else if (m <= mas[i].l)
grp2.push_back(mas[i]);
else
grp3.push_back(mas[i]);
}
int rem = max(0, (n + 1) / 2 - (int)grp2.size());
if (rem > grp3.size()) return false;
ll sum = 0;
for (auto el : grp1) sum += el.l;
for (auto el : grp2) sum += el.l;
if (grp3.size() > 0) sort(grp3.begin(), grp3.end(), cmp);
for (int i = 1; i <= rem; i++) sum += m;
for (int i = 0; i < grp3.size() - rem; i++) sum += grp3[i].l;
return (sum <= s);
}
ll bin_s() {
ll l = 1, r = 1e9;
ll m;
while (l < r) {
m = (l + r + 1) / 2;
if (good(m))
l = m;
else
r = m - 1;
}
return l;
}
void solve() {
cin >> n >> s;
for (int i = 0; i < n; i++) {
cin >> mas[i].l >> mas[i].r;
}
ll med = bin_s();
cout << med << '\n';
return;
}
int main() {
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int t, n;
long long s;
pair<int, int> x[N];
bool valid(int mid) {
int cnt = (n + 1) / 2;
long long sum = s;
for (int i = n - 1; ~i; --i) {
if (cnt && mid <= x[i].second)
--cnt, sum -= max(x[i].first, mid);
else
sum -= x[i].first;
}
return sum >= 0 && !cnt;
}
int solve() {
int l = 0, r = 1e9 + 5;
while (l < r) {
int mid = l + (r - l + 1) / 2;
if (valid(mid))
l = mid;
else
r = mid - 1;
}
return l;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t--) {
cin >> n >> s;
for (int i = 0; i < n; ++i) cin >> x[i].first >> x[i].second;
sort(x, x + n, [](pair<int, int>& a, pair<int, int>& b) {
return a.first < b.first;
});
cout << solve() << '\n';
}
return 0;
}
| ### Prompt
In Cpp, your task is to solve the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int t, n;
long long s;
pair<int, int> x[N];
bool valid(int mid) {
int cnt = (n + 1) / 2;
long long sum = s;
for (int i = n - 1; ~i; --i) {
if (cnt && mid <= x[i].second)
--cnt, sum -= max(x[i].first, mid);
else
sum -= x[i].first;
}
return sum >= 0 && !cnt;
}
int solve() {
int l = 0, r = 1e9 + 5;
while (l < r) {
int mid = l + (r - l + 1) / 2;
if (valid(mid))
l = mid;
else
r = mid - 1;
}
return l;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t--) {
cin >> n >> s;
for (int i = 0; i < n; ++i) cin >> x[i].first >> x[i].second;
sort(x, x + n, [](pair<int, int>& a, pair<int, int>& b) {
return a.first < b.first;
});
cout << solve() << '\n';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long n, s;
vector<pair<long long, long long> > r;
bool comp(pair<long long, long long> a, pair<long long, long long> b) {
return a.first < b.first;
}
bool poss(long long cost) {
long long i;
long long tc = 0;
vector<pair<long long, long long> > rem;
long long cl = 0, cr = 0;
for (i = (0); i < (n); i++) {
if (r[i].first > cost) {
tc += r[i].first;
cr++;
} else if (r[i].second < cost) {
tc += r[i].first;
cl++;
} else {
rem.push_back(r[i]);
}
}
if ((cl >= ((n + 1) / 2)) || (cr >= ((n + 1) / 2))) {
return false;
}
sort((rem).begin(), (rem).end(), comp);
long long remm = ((n + 1) / 2 - cl);
long long tot = (n - cl - cr - remm + 1);
for (i = (0); i < (remm - 1); i++) {
tc += (rem[i].first);
}
tc += (tot * cost);
return (tc <= s);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long i, j, k;
long long t;
cin >> t;
while (t--) {
cin >> n >> s;
r.clear();
r.resize(n);
for (i = (0); i < (n); i++) {
cin >> r[i].first >> r[i].second;
}
vector<long long> v1;
for (i = (0); i < (n); i++) v1.push_back(r[i].first);
sort((v1).begin(), (v1).end());
long long l = v1[n / 2];
vector<long long> v2;
for (i = (0); i < (n); i++) v2.push_back(r[i].second);
sort((v2).begin(), (v2).end());
long long r = v2[n / 2];
if (l > r) {
}
while ((r - l) > 1) {
long long mid = ((l + r) / 2);
if (poss(mid)) {
l = mid;
} else {
r = mid - 1;
}
}
if (poss(r))
cout << r << '\n';
else
cout << l << '\n';
}
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long n, s;
vector<pair<long long, long long> > r;
bool comp(pair<long long, long long> a, pair<long long, long long> b) {
return a.first < b.first;
}
bool poss(long long cost) {
long long i;
long long tc = 0;
vector<pair<long long, long long> > rem;
long long cl = 0, cr = 0;
for (i = (0); i < (n); i++) {
if (r[i].first > cost) {
tc += r[i].first;
cr++;
} else if (r[i].second < cost) {
tc += r[i].first;
cl++;
} else {
rem.push_back(r[i]);
}
}
if ((cl >= ((n + 1) / 2)) || (cr >= ((n + 1) / 2))) {
return false;
}
sort((rem).begin(), (rem).end(), comp);
long long remm = ((n + 1) / 2 - cl);
long long tot = (n - cl - cr - remm + 1);
for (i = (0); i < (remm - 1); i++) {
tc += (rem[i].first);
}
tc += (tot * cost);
return (tc <= s);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long i, j, k;
long long t;
cin >> t;
while (t--) {
cin >> n >> s;
r.clear();
r.resize(n);
for (i = (0); i < (n); i++) {
cin >> r[i].first >> r[i].second;
}
vector<long long> v1;
for (i = (0); i < (n); i++) v1.push_back(r[i].first);
sort((v1).begin(), (v1).end());
long long l = v1[n / 2];
vector<long long> v2;
for (i = (0); i < (n); i++) v2.push_back(r[i].second);
sort((v2).begin(), (v2).end());
long long r = v2[n / 2];
if (l > r) {
}
while ((r - l) > 1) {
long long mid = ((l + r) / 2);
if (poss(mid)) {
l = mid;
} else {
r = mid - 1;
}
}
if (poss(r))
cout << r << '\n';
else
cout << l << '\n';
}
return 0;
}
``` |
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("avx2")
using namespace std;
template <class T, class U>
inline void checkmin(T &x, U y) {
if (y < x) x = y;
}
template <class T, class U>
inline void checkmax(T &x, U y) {
if (y > x) x = y;
}
template <class T, class U>
inline bool ifmax(T &x, U y) {
if (y > x) return x = y, true;
return false;
}
template <class T, class U>
inline bool ifmin(T &x, U y) {
if (y < x) return x = y, true;
return false;
}
template <class T>
inline void sort(T &a) {
sort(a.begin(), a.end());
}
template <class T>
inline void rsort(T &a) {
sort(a.rbegin(), a.rend());
}
template <class T>
inline void reverse(T &a) {
reverse(a.begin(), a.end());
}
template <class T, class U>
inline istream &operator>>(istream &str, pair<T, U> &p) {
return str >> p.first >> p.second;
}
template <class T>
inline istream &operator>>(istream &str, vector<T> &a) {
for (auto &i : a) str >> i;
return str;
}
template <class T>
inline T sorted(T a) {
sort(a);
return a;
}
void read(int &x) {
static char c;
while ((c = getchar()) >= '0') x = (x << 3) + (x << 1) + (c - '0');
}
void read(long long &x) {
static char c;
while ((c = getchar()) >= '0') x = (x << 3) + (x << 1) + (c - '0');
}
void print(int x) {
if (x >= 10) {
print(x / 10);
putchar(x % 10 + '0');
} else {
putchar(x + '0');
}
}
void solve() {
int n = 0;
long long second = 0;
read(n);
read(second);
vector<int> l(n), r(n), ind(n);
for (int i = 0; i < n; ++i) read(l[i]), read(r[i]), ind[i] = i;
sort(ind.begin(), ind.end(), [&](int i, int j) { return l[i] > l[j]; });
int left = 0, right = 1e9 + 1;
while (right - left > 1) {
int m = right + left >> 1;
long long check = 0, cnt = n / 2 + 1;
for (auto i : ind) {
int add = l[i];
if (r[i] >= m && cnt) {
--cnt;
checkmax(add, m);
}
check += add;
}
if (cnt || check > second)
right = m;
else
left = m;
}
print(left);
putchar('\n');
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
srand(94385);
int t = 0;
read(t);
while (t--) solve();
return 0;
}
| ### Prompt
Your challenge is to write a cpp solution to the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("avx2")
using namespace std;
template <class T, class U>
inline void checkmin(T &x, U y) {
if (y < x) x = y;
}
template <class T, class U>
inline void checkmax(T &x, U y) {
if (y > x) x = y;
}
template <class T, class U>
inline bool ifmax(T &x, U y) {
if (y > x) return x = y, true;
return false;
}
template <class T, class U>
inline bool ifmin(T &x, U y) {
if (y < x) return x = y, true;
return false;
}
template <class T>
inline void sort(T &a) {
sort(a.begin(), a.end());
}
template <class T>
inline void rsort(T &a) {
sort(a.rbegin(), a.rend());
}
template <class T>
inline void reverse(T &a) {
reverse(a.begin(), a.end());
}
template <class T, class U>
inline istream &operator>>(istream &str, pair<T, U> &p) {
return str >> p.first >> p.second;
}
template <class T>
inline istream &operator>>(istream &str, vector<T> &a) {
for (auto &i : a) str >> i;
return str;
}
template <class T>
inline T sorted(T a) {
sort(a);
return a;
}
void read(int &x) {
static char c;
while ((c = getchar()) >= '0') x = (x << 3) + (x << 1) + (c - '0');
}
void read(long long &x) {
static char c;
while ((c = getchar()) >= '0') x = (x << 3) + (x << 1) + (c - '0');
}
void print(int x) {
if (x >= 10) {
print(x / 10);
putchar(x % 10 + '0');
} else {
putchar(x + '0');
}
}
void solve() {
int n = 0;
long long second = 0;
read(n);
read(second);
vector<int> l(n), r(n), ind(n);
for (int i = 0; i < n; ++i) read(l[i]), read(r[i]), ind[i] = i;
sort(ind.begin(), ind.end(), [&](int i, int j) { return l[i] > l[j]; });
int left = 0, right = 1e9 + 1;
while (right - left > 1) {
int m = right + left >> 1;
long long check = 0, cnt = n / 2 + 1;
for (auto i : ind) {
int add = l[i];
if (r[i] >= m && cnt) {
--cnt;
checkmax(add, m);
}
check += add;
}
if (cnt || check > second)
right = m;
else
left = m;
}
print(left);
putchar('\n');
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
srand(94385);
int t = 0;
read(t);
while (t--) solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
struct node {
int l, r;
friend bool operator<(node a, node b) { return a.l < b.l; }
} ee[200005];
int get(int n, int x, long long k) {
long long sum = 0;
int cnt = n / 2 + 1;
for (int i = n; i >= 1; i--) {
if (ee[i].l <= x && ee[i].r >= x && cnt) {
sum += x;
cnt--;
} else {
sum += ee[i].l;
if (ee[i].l > x) cnt--;
}
}
if (sum > k || cnt > 0)
return 0;
else
return 1;
}
int main() {
int n, t;
long long k;
scanf("%d", &t);
while (t--) {
scanf("%d%lld", &n, &k);
int maxs = 0;
for (int i = 1; i <= n; i++) {
scanf("%d%d", &ee[i].l, &ee[i].r);
maxs = max(maxs, max(ee[i].l, ee[i].r));
}
sort(ee + 1, ee + n + 1);
int l = ee[n / 2 + 1].l, r = maxs, ans = l;
while (l <= r) {
int mid = (l + r) >> 1;
if (get(n, mid, k)) {
l = mid + 1;
ans = mid;
} else {
r = mid - 1;
}
}
printf("%d\n", ans);
}
return 0;
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
struct node {
int l, r;
friend bool operator<(node a, node b) { return a.l < b.l; }
} ee[200005];
int get(int n, int x, long long k) {
long long sum = 0;
int cnt = n / 2 + 1;
for (int i = n; i >= 1; i--) {
if (ee[i].l <= x && ee[i].r >= x && cnt) {
sum += x;
cnt--;
} else {
sum += ee[i].l;
if (ee[i].l > x) cnt--;
}
}
if (sum > k || cnt > 0)
return 0;
else
return 1;
}
int main() {
int n, t;
long long k;
scanf("%d", &t);
while (t--) {
scanf("%d%lld", &n, &k);
int maxs = 0;
for (int i = 1; i <= n; i++) {
scanf("%d%d", &ee[i].l, &ee[i].r);
maxs = max(maxs, max(ee[i].l, ee[i].r));
}
sort(ee + 1, ee + n + 1);
int l = ee[n / 2 + 1].l, r = maxs, ans = l;
while (l <= r) {
int mid = (l + r) >> 1;
if (get(n, mid, k)) {
l = mid + 1;
ans = mid;
} else {
r = mid - 1;
}
}
printf("%d\n", ans);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
struct Node {
long long l, r;
} dict[maxn];
long long N, s;
inline bool check(long long mid) {
long long cnt = 0, cost = 0;
priority_queue<long long, vector<long long>, greater<long long>> q;
for (int i = 1; i <= N; ++i) {
cost += dict[i].l;
if (dict[i].l < mid && dict[i].r >= mid) {
q.push(mid - dict[i].l);
} else if (dict[i].l >= mid) {
++cnt;
}
}
while (!q.empty() && cnt < (N + 1) / 2) {
cost += q.top();
q.pop();
cnt++;
}
return cost <= s && cnt >= (N + 1) / 2;
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
scanf("%lld%lld", &N, &s);
for (int i = 1; i <= N; ++i) {
scanf("%lld%lld", &dict[i].l, &dict[i].r);
}
long long l = 1, r = 1e14, ans;
while (l <= r) {
long long mid = (l + r) >> 1;
if (check(mid))
ans = mid, l = mid + 1;
else
r = mid - 1;
}
cout << ans << endl;
}
return 0;
}
| ### Prompt
Please create a solution in Cpp to the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
struct Node {
long long l, r;
} dict[maxn];
long long N, s;
inline bool check(long long mid) {
long long cnt = 0, cost = 0;
priority_queue<long long, vector<long long>, greater<long long>> q;
for (int i = 1; i <= N; ++i) {
cost += dict[i].l;
if (dict[i].l < mid && dict[i].r >= mid) {
q.push(mid - dict[i].l);
} else if (dict[i].l >= mid) {
++cnt;
}
}
while (!q.empty() && cnt < (N + 1) / 2) {
cost += q.top();
q.pop();
cnt++;
}
return cost <= s && cnt >= (N + 1) / 2;
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
scanf("%lld%lld", &N, &s);
for (int i = 1; i <= N; ++i) {
scanf("%lld%lld", &dict[i].l, &dict[i].r);
}
long long l = 1, r = 1e14, ans;
while (l <= r) {
long long mid = (l + r) >> 1;
if (check(mid))
ans = mid, l = mid + 1;
else
r = mid - 1;
}
cout << ans << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
struct Sal {
int32_t min, max;
};
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int32_t tests;
cin >> tests;
for (int32_t test = 0; test < tests; test++) {
int32_t count;
int64_t total;
cin >> count >> total;
Sal sals[count];
int32_t max_sal = 1000000001;
int32_t min_sal = 0;
for (int32_t i = 0; i < count; i++) {
cin >> sals[i].min >> sals[i].max;
if (sals[i].max > max_sal) {
max_sal = sals[i].max;
}
if (sals[i].min < min_sal) {
min_sal = sals[i].min;
}
}
sort(sals, sals + count,
[](const Sal& s1, const Sal& s2) { return s1.min < s2.min; });
while (max_sal - min_sal > 1) {
bool possible = true;
int32_t mid_sal = (max_sal + min_sal) / 2;
int64_t sum = 0;
int32_t count_left = (count + 1) / 2;
vector<int32_t> undetermined;
for (int32_t i = 0; i < count; i++) {
if (mid_sal > sals[i].max) {
sum += sals[i].min;
} else if (mid_sal <= sals[i].min) {
sum += sals[i].min;
count_left--;
} else {
undetermined.push_back(i);
}
}
count_left = max(0, count_left);
if ((int32_t)undetermined.size() < count_left) {
possible = false;
} else {
for (int32_t i = 0; i < (int32_t)undetermined.size(); i++) {
if (i < (int32_t)undetermined.size() - count_left) {
sum += sals[undetermined[i]].min;
} else {
sum += mid_sal;
}
}
if (sum > total) {
possible = false;
}
}
if (possible) {
min_sal = mid_sal;
} else {
max_sal = mid_sal;
}
}
cout << min_sal << "\n";
}
return 0;
}
| ### Prompt
Create a solution in CPP for the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
struct Sal {
int32_t min, max;
};
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int32_t tests;
cin >> tests;
for (int32_t test = 0; test < tests; test++) {
int32_t count;
int64_t total;
cin >> count >> total;
Sal sals[count];
int32_t max_sal = 1000000001;
int32_t min_sal = 0;
for (int32_t i = 0; i < count; i++) {
cin >> sals[i].min >> sals[i].max;
if (sals[i].max > max_sal) {
max_sal = sals[i].max;
}
if (sals[i].min < min_sal) {
min_sal = sals[i].min;
}
}
sort(sals, sals + count,
[](const Sal& s1, const Sal& s2) { return s1.min < s2.min; });
while (max_sal - min_sal > 1) {
bool possible = true;
int32_t mid_sal = (max_sal + min_sal) / 2;
int64_t sum = 0;
int32_t count_left = (count + 1) / 2;
vector<int32_t> undetermined;
for (int32_t i = 0; i < count; i++) {
if (mid_sal > sals[i].max) {
sum += sals[i].min;
} else if (mid_sal <= sals[i].min) {
sum += sals[i].min;
count_left--;
} else {
undetermined.push_back(i);
}
}
count_left = max(0, count_left);
if ((int32_t)undetermined.size() < count_left) {
possible = false;
} else {
for (int32_t i = 0; i < (int32_t)undetermined.size(); i++) {
if (i < (int32_t)undetermined.size() - count_left) {
sum += sals[undetermined[i]].min;
} else {
sum += mid_sal;
}
}
if (sum > total) {
possible = false;
}
}
if (possible) {
min_sal = mid_sal;
} else {
max_sal = mid_sal;
}
}
cout << min_sal << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const double pai = acos(-1);
const double eps = 1e-10;
const long long mod = 1e9 + 7;
const int MXN = 1e6 + 5;
long long a[MXN], b[MXN];
int vis[MXN];
int n;
long long s;
struct no {
long long l, r, p;
};
bool operator<(const no &x, const no &y) { return x.l > y.l; }
bool check(long long mid) {
vector<no> v;
for (int i = 1; i <= n; i++) {
if (b[i] >= mid) v.push_back(no{a[i], b[i], i});
vis[i] = 0;
}
if ((int)v.size() < n / 2 + 1) return 0;
sort(v.begin(), v.end());
while ((int)v.size() > n / 2 + 1) v.pop_back();
long long need = 0;
for (auto i : v) need += max(mid, i.l), vis[i.p] = 1;
for (int i = 1; i <= n; i++)
if (!vis[i]) need += a[i];
return need <= s;
}
void Main(int avg) {
cin >> n >> s;
for (int i = 1; i <= n; i++) {
int sa, sb;
scanf("%d %d", &sa, &sb);
a[i] = sa, b[i] = sb;
}
long long l = 1, r = 1e9, ans;
while (l <= r) {
long long mid = (l + r) / 2;
if (check(mid))
ans = mid, l = mid + 1;
else
r = mid - 1;
}
cout << ans << '\n';
}
int main() {
int cas;
cin >> cas;
for (int i = 1; i <= cas; i++) Main(i);
return 0;
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double pai = acos(-1);
const double eps = 1e-10;
const long long mod = 1e9 + 7;
const int MXN = 1e6 + 5;
long long a[MXN], b[MXN];
int vis[MXN];
int n;
long long s;
struct no {
long long l, r, p;
};
bool operator<(const no &x, const no &y) { return x.l > y.l; }
bool check(long long mid) {
vector<no> v;
for (int i = 1; i <= n; i++) {
if (b[i] >= mid) v.push_back(no{a[i], b[i], i});
vis[i] = 0;
}
if ((int)v.size() < n / 2 + 1) return 0;
sort(v.begin(), v.end());
while ((int)v.size() > n / 2 + 1) v.pop_back();
long long need = 0;
for (auto i : v) need += max(mid, i.l), vis[i.p] = 1;
for (int i = 1; i <= n; i++)
if (!vis[i]) need += a[i];
return need <= s;
}
void Main(int avg) {
cin >> n >> s;
for (int i = 1; i <= n; i++) {
int sa, sb;
scanf("%d %d", &sa, &sb);
a[i] = sa, b[i] = sb;
}
long long l = 1, r = 1e9, ans;
while (l <= r) {
long long mid = (l + r) / 2;
if (check(mid))
ans = mid, l = mid + 1;
else
r = mid - 1;
}
cout << ans << '\n';
}
int main() {
int cas;
cin >> cas;
for (int i = 1; i <= cas; i++) Main(i);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <class T>
int read(T& x) {
int c = getchar(), f = (x = 0);
while (~c && (c < 48 || c > 57)) {
if (c == '-') {
f = 1;
}
c = getchar();
}
if (!~c) {
return 0;
}
while (c > 47 && c < 58) {
x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
}
if (f) {
x = -x;
}
return 1;
}
template <class T, class U>
inline int read(T& a, U& b) {
return read(a) && read(b);
}
template <class T, class U, class V>
inline int read(T& a, U& b, V& c) {
return read(a) && read(b) && read(c);
}
inline int read(double& _, double& __) { return scanf("%lf%lf", &_, &__); }
inline int read(char* _) { return scanf("%s", _); }
inline int read(double& _) { return scanf("%lf", &_); }
inline void print(long long _) { printf("%lld ", _); }
inline void print(int _) { printf("%d ", _); }
inline void println(long long _) { printf("%lld\n", _); }
inline void println(int _) { printf("%d\n", _); }
template <class T>
inline void cmax(T& _, T __) {
if (_ < __) _ = __;
}
template <class T>
inline void cmin(T& _, T __) {
if (_ > __) _ = __;
}
const int maxn = 2e5 + 10, INF = 0x3f3f3f3f;
const long long LINF = (long long)INF << 32 | INF;
long long mid;
struct Node {
long long L, R;
Node(long long a = 0, long long b = 0) : L(a), R(b) {}
bool operator<(const Node& T) const { return L > T.L; }
} a[maxn];
long long T, n, tot, L = 0, R = INF;
bool Judge() {
int cnt = 0;
long long tmp = tot;
for (int i = 1; i <= (n); i++) {
if (a[i].L >= mid) {
++cnt;
continue;
}
if (a[i].R < mid) continue;
if (tmp < mid - a[i].L) break;
tmp -= mid - a[i].L;
++cnt;
}
return cnt >= (n + 1) / 2;
}
int main() {
int T;
read(T);
while (T--) {
read(n, tot);
L = 0, R = tot;
for (int i = 1; i <= (n); i++) {
read(a[i].L, a[i].R);
tot -= a[i].L;
}
sort(a + 1, a + n + 1);
while (L < R) {
mid = L + (R - L + 1) / 2;
if (Judge())
L = mid;
else
R = mid - 1;
}
printf("%lld\n", L);
}
return 0;
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T>
int read(T& x) {
int c = getchar(), f = (x = 0);
while (~c && (c < 48 || c > 57)) {
if (c == '-') {
f = 1;
}
c = getchar();
}
if (!~c) {
return 0;
}
while (c > 47 && c < 58) {
x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
}
if (f) {
x = -x;
}
return 1;
}
template <class T, class U>
inline int read(T& a, U& b) {
return read(a) && read(b);
}
template <class T, class U, class V>
inline int read(T& a, U& b, V& c) {
return read(a) && read(b) && read(c);
}
inline int read(double& _, double& __) { return scanf("%lf%lf", &_, &__); }
inline int read(char* _) { return scanf("%s", _); }
inline int read(double& _) { return scanf("%lf", &_); }
inline void print(long long _) { printf("%lld ", _); }
inline void print(int _) { printf("%d ", _); }
inline void println(long long _) { printf("%lld\n", _); }
inline void println(int _) { printf("%d\n", _); }
template <class T>
inline void cmax(T& _, T __) {
if (_ < __) _ = __;
}
template <class T>
inline void cmin(T& _, T __) {
if (_ > __) _ = __;
}
const int maxn = 2e5 + 10, INF = 0x3f3f3f3f;
const long long LINF = (long long)INF << 32 | INF;
long long mid;
struct Node {
long long L, R;
Node(long long a = 0, long long b = 0) : L(a), R(b) {}
bool operator<(const Node& T) const { return L > T.L; }
} a[maxn];
long long T, n, tot, L = 0, R = INF;
bool Judge() {
int cnt = 0;
long long tmp = tot;
for (int i = 1; i <= (n); i++) {
if (a[i].L >= mid) {
++cnt;
continue;
}
if (a[i].R < mid) continue;
if (tmp < mid - a[i].L) break;
tmp -= mid - a[i].L;
++cnt;
}
return cnt >= (n + 1) / 2;
}
int main() {
int T;
read(T);
while (T--) {
read(n, tot);
L = 0, R = tot;
for (int i = 1; i <= (n); i++) {
read(a[i].L, a[i].R);
tot -= a[i].L;
}
sort(a + 1, a + n + 1);
while (L < R) {
mid = L + (R - L + 1) / 2;
if (Judge())
L = mid;
else
R = mid - 1;
}
printf("%lld\n", L);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long M = 1e9 + 7;
const long long N = 2e5;
long long po(long long, long long);
vector<pair<long long, long long>> v;
long long n, s;
bool check(long long mid) {
long long sum = 0;
long long c = 0;
for (long long i = n - 1; i >= 0; i--) {
if (mid >= v[i].first && mid <= v[i].second) {
if (c < n / 2 + 1) {
sum += mid;
c++;
} else {
sum += v[i].first;
}
} else if (mid < v[i].first) {
if (c < n / 2 + 1) {
sum += v[i].first;
c++;
} else {
sum += v[i].first;
}
} else {
sum += v[i].first;
}
}
if (c == n / 2 + 1 && sum <= s) {
return 1;
} else
return 0;
}
void solve() {
cin >> n >> s;
v.clear();
for (long long i = 0; i < n; i++) {
long long l, r;
cin >> l >> r;
v.push_back({l, r});
}
sort((v).begin(), (v).end());
long long l = v[n / 2].first;
long long r = s + 5;
while (l < r - 1) {
long long mid = l + (r - l) / 2;
if (check(mid)) {
l = mid;
} else {
r = mid;
}
}
cout << l << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t = 1;
cin >> t;
for (long long i = 1; i < t + 1; i++) {
solve();
}
return 0;
}
long long po(long long a, long long b) {
if (b == 0) return 1;
long long ans = 1;
if (b % 2 == 0) {
ans = po(a, b / 2) % M;
ans = (ans % M * ans % M) % M;
} else {
ans = po(a, (b - 1) / 2) % M;
ans = (ans % M * ans % M * a) % M;
}
return ans % M;
}
| ### Prompt
In CPP, your task is to solve the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long M = 1e9 + 7;
const long long N = 2e5;
long long po(long long, long long);
vector<pair<long long, long long>> v;
long long n, s;
bool check(long long mid) {
long long sum = 0;
long long c = 0;
for (long long i = n - 1; i >= 0; i--) {
if (mid >= v[i].first && mid <= v[i].second) {
if (c < n / 2 + 1) {
sum += mid;
c++;
} else {
sum += v[i].first;
}
} else if (mid < v[i].first) {
if (c < n / 2 + 1) {
sum += v[i].first;
c++;
} else {
sum += v[i].first;
}
} else {
sum += v[i].first;
}
}
if (c == n / 2 + 1 && sum <= s) {
return 1;
} else
return 0;
}
void solve() {
cin >> n >> s;
v.clear();
for (long long i = 0; i < n; i++) {
long long l, r;
cin >> l >> r;
v.push_back({l, r});
}
sort((v).begin(), (v).end());
long long l = v[n / 2].first;
long long r = s + 5;
while (l < r - 1) {
long long mid = l + (r - l) / 2;
if (check(mid)) {
l = mid;
} else {
r = mid;
}
}
cout << l << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t = 1;
cin >> t;
for (long long i = 1; i < t + 1; i++) {
solve();
}
return 0;
}
long long po(long long a, long long b) {
if (b == 0) return 1;
long long ans = 1;
if (b % 2 == 0) {
ans = po(a, b / 2) % M;
ans = (ans % M * ans % M) % M;
} else {
ans = po(a, (b - 1) / 2) % M;
ans = (ans % M * ans % M * a) % M;
}
return ans % M;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long int N = 5e5 + 5, mod = 1000000007, bit = 60;
bool comp(pair<long long int, long long int> &a,
pair<long long int, long long int> &b) {
if (a.second != b.second) {
return a.second < b.second;
}
return a.first < b.first;
}
bool comp1(pair<long long int, long long int> &a,
pair<long long int, long long int> &b) {
if (a.first != b.first) {
return a.first < b.first;
}
return a.second < b.second;
}
bool check(long long int mid, long long int k,
vector<pair<long long int, long long int> > &p) {
long long int n = p.size(), cost = 0, i, compulsory = 0, need, rem;
vector<pair<long long int, long long int> > v;
for (i = 0; i < n; i++) {
if (p[i].second < mid) {
cost += p[i].first;
} else if (p[i].first >= mid) {
compulsory++;
cost += p[i].first;
} else {
v.push_back({p[i].first, p[i].second});
}
}
need = max(0ll, ((n + 1) >> 1) - compulsory);
rem = v.size() - need;
sort(v.begin(), v.end(), comp1);
reverse(v.begin(), v.end());
while (rem--) {
cost += v.back().first;
v.pop_back();
}
while (need--) {
cost += max(mid, v.back().first);
v.pop_back();
}
return cost <= k;
}
signed main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
long long int pro = 1, temp, t, i, j, l, r, n, m, mid, z, k, x, y, rem,
carry = 0, ind, ans = 0, mx = -LONG_LONG_MAX,
mn = LONG_LONG_MAX, cnt = 0, curr = 0, prev, next, sum = 0,
flag = 1, i1 = -1, i2 = -1;
cin >> t;
while (t--) {
cin >> n >> k;
vector<pair<long long int, long long int> > p(n);
for (i = 0; i < n; i++) {
cin >> p[i].first >> p[i].second;
}
sort(p.begin(), p.end());
l = p[n / 2].first;
sort(p.begin(), p.end(), comp);
r = p[n / 2].second;
while (l <= r) {
mid = (l + r) >> 1;
if (check(mid, k, p)) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
cout << ans << '\n';
}
return 0;
}
| ### Prompt
Your task is to create a cpp solution to the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long int N = 5e5 + 5, mod = 1000000007, bit = 60;
bool comp(pair<long long int, long long int> &a,
pair<long long int, long long int> &b) {
if (a.second != b.second) {
return a.second < b.second;
}
return a.first < b.first;
}
bool comp1(pair<long long int, long long int> &a,
pair<long long int, long long int> &b) {
if (a.first != b.first) {
return a.first < b.first;
}
return a.second < b.second;
}
bool check(long long int mid, long long int k,
vector<pair<long long int, long long int> > &p) {
long long int n = p.size(), cost = 0, i, compulsory = 0, need, rem;
vector<pair<long long int, long long int> > v;
for (i = 0; i < n; i++) {
if (p[i].second < mid) {
cost += p[i].first;
} else if (p[i].first >= mid) {
compulsory++;
cost += p[i].first;
} else {
v.push_back({p[i].first, p[i].second});
}
}
need = max(0ll, ((n + 1) >> 1) - compulsory);
rem = v.size() - need;
sort(v.begin(), v.end(), comp1);
reverse(v.begin(), v.end());
while (rem--) {
cost += v.back().first;
v.pop_back();
}
while (need--) {
cost += max(mid, v.back().first);
v.pop_back();
}
return cost <= k;
}
signed main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
long long int pro = 1, temp, t, i, j, l, r, n, m, mid, z, k, x, y, rem,
carry = 0, ind, ans = 0, mx = -LONG_LONG_MAX,
mn = LONG_LONG_MAX, cnt = 0, curr = 0, prev, next, sum = 0,
flag = 1, i1 = -1, i2 = -1;
cin >> t;
while (t--) {
cin >> n >> k;
vector<pair<long long int, long long int> > p(n);
for (i = 0; i < n; i++) {
cin >> p[i].first >> p[i].second;
}
sort(p.begin(), p.end());
l = p[n / 2].first;
sort(p.begin(), p.end(), comp);
r = p[n / 2].second;
while (l <= r) {
mid = (l + r) >> 1;
if (check(mid, k, p)) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
cout << ans << '\n';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = int(2e5) + 99;
const int INF = int(1e9) + 100;
int t;
int n;
long long s;
pair<int, int> p[N];
bool ok(int mid) {
long long sum = 0;
int cnt = 0;
vector<int> v;
for (int i = 0; i < n; ++i) {
if (p[i].second < mid)
sum += p[i].first;
else if (p[i].first >= mid) {
sum += p[i].first;
++cnt;
} else
v.push_back(p[i].first);
}
assert(is_sorted(v.begin(), v.end()));
int need = max(0, (n + 1) / 2 - cnt);
if (need > v.size()) return false;
for (int i = 0; i < v.size(); ++i) {
if (i < v.size() - need)
sum += v[i];
else
sum += mid;
}
return sum <= s;
}
int main() {
scanf("%d", &t);
for (int tc = 0; tc < t; ++tc) {
scanf("%d %lld", &n, &s);
for (int i = 0; i < n; ++i) scanf("%d %d", &p[i].first, &p[i].second);
sort(p, p + n);
int lf = 1, rg = INF;
while (rg - lf > 1) {
int mid = (lf + rg) / 2;
if (ok(mid))
lf = mid;
else
rg = mid;
}
printf("%d\n", lf);
}
return 0;
}
| ### Prompt
Develop a solution in CPP to the problem described below:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = int(2e5) + 99;
const int INF = int(1e9) + 100;
int t;
int n;
long long s;
pair<int, int> p[N];
bool ok(int mid) {
long long sum = 0;
int cnt = 0;
vector<int> v;
for (int i = 0; i < n; ++i) {
if (p[i].second < mid)
sum += p[i].first;
else if (p[i].first >= mid) {
sum += p[i].first;
++cnt;
} else
v.push_back(p[i].first);
}
assert(is_sorted(v.begin(), v.end()));
int need = max(0, (n + 1) / 2 - cnt);
if (need > v.size()) return false;
for (int i = 0; i < v.size(); ++i) {
if (i < v.size() - need)
sum += v[i];
else
sum += mid;
}
return sum <= s;
}
int main() {
scanf("%d", &t);
for (int tc = 0; tc < t; ++tc) {
scanf("%d %lld", &n, &s);
for (int i = 0; i < n; ++i) scanf("%d %d", &p[i].first, &p[i].second);
sort(p, p + n);
int lf = 1, rg = INF;
while (rg - lf > 1) {
int mid = (lf + rg) / 2;
if (ok(mid))
lf = mid;
else
rg = mid;
}
printf("%d\n", lf);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void __read(T& a) {
cin >> a;
}
template <typename T, typename... Args>
void __read(T& a, Args&... args) {
cin >> a;
__read(args...);
}
constexpr long long M7 = 1000000007ll;
constexpr long long M9 = 1000000009ll;
constexpr long long MFFT = 998244353ll;
template <class T>
void outv(T& a) {
for (auto& x : a) cout << x << ' ';
}
mt19937 rnd(static_cast<unsigned>(
chrono::steady_clock::now().time_since_epoch().count()));
template <class T>
void random_shuffle(T s, T e) {
shuffle(s, e, rnd);
};
static auto __super_speed__ = (ios_base::sync_with_stdio(0), cin.tie(0));
int32_t main() {
long long t;
__read(t);
while (t--) {
long long n, s;
__read(n, s);
vector<pair<long long, long long> > a(n);
for (signed i = 0; i < (n); i++) cin >> a[i].first >> a[i].second;
sort((a).begin(), (a).end());
long long l = 0, r = 1000000000;
while (l < r) {
long long mid = (l + r + 1) / 2;
long long cnt = 0;
vector<pair<long long, long long> > b;
for (long long i = 0; i < n; ++i) {
if (a[i].second < mid)
cnt += a[i].first;
else
b.push_back(a[i]);
}
long long d = b.size() - (n + 1) / 2;
if (d < 0) {
r = mid - 1;
continue;
}
for (long long i = 0; i < d; ++i) cnt += b[i].first;
for (long long i = d; i < b.size(); ++i) cnt += max(mid, b[i].first);
if (cnt <= s)
l = mid;
else
r = mid - 1;
}
cout << l << '\n';
}
return 0;
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void __read(T& a) {
cin >> a;
}
template <typename T, typename... Args>
void __read(T& a, Args&... args) {
cin >> a;
__read(args...);
}
constexpr long long M7 = 1000000007ll;
constexpr long long M9 = 1000000009ll;
constexpr long long MFFT = 998244353ll;
template <class T>
void outv(T& a) {
for (auto& x : a) cout << x << ' ';
}
mt19937 rnd(static_cast<unsigned>(
chrono::steady_clock::now().time_since_epoch().count()));
template <class T>
void random_shuffle(T s, T e) {
shuffle(s, e, rnd);
};
static auto __super_speed__ = (ios_base::sync_with_stdio(0), cin.tie(0));
int32_t main() {
long long t;
__read(t);
while (t--) {
long long n, s;
__read(n, s);
vector<pair<long long, long long> > a(n);
for (signed i = 0; i < (n); i++) cin >> a[i].first >> a[i].second;
sort((a).begin(), (a).end());
long long l = 0, r = 1000000000;
while (l < r) {
long long mid = (l + r + 1) / 2;
long long cnt = 0;
vector<pair<long long, long long> > b;
for (long long i = 0; i < n; ++i) {
if (a[i].second < mid)
cnt += a[i].first;
else
b.push_back(a[i]);
}
long long d = b.size() - (n + 1) / 2;
if (d < 0) {
r = mid - 1;
continue;
}
for (long long i = 0; i < d; ++i) cnt += b[i].first;
for (long long i = d; i < b.size(); ++i) cnt += max(mid, b[i].first);
if (cnt <= s)
l = mid;
else
r = mid - 1;
}
cout << l << '\n';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int dr8[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dc8[] = {1, -1, 0, 0, -1, 1, -1, 1};
int dr4[] = {0, 1, -1, 0};
int dc4[] = {1, 0, 0, -1};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
long long int n, s;
cin >> n >> s;
long long int r[n][2];
vector<pair<pair<long long int, long long int>, long long int> > v;
for (int i = 0; i < n; i++) {
cin >> r[i][0] >> r[i][1];
v.push_back({{r[i][1], r[i][0]}, i});
}
sort(v.begin(), v.end());
long long int an, l = 1, h = 1000000000;
while (l <= h) {
long long int m = (l + h) / 2;
vector<long long int> vv, less;
long long int cn = 0, mx = 0, sm = 0, gr = 0;
for (int i = 0; i < n; i++) {
if (r[i][1] < m) {
less.push_back(r[i][0]);
} else
vv.push_back(r[i][0]);
if (r[i][0] <= m) {
cn++;
mx = max(mx, r[i][0]);
}
if (r[i][1] >= m) gr++;
sm += r[i][0];
}
if (cn < (n + 1) / 2) {
l = m + 1;
continue;
} else if (gr < (n + 1) / 2) {
h = m - 1;
continue;
}
sort(less.begin(), less.end());
sort(vv.begin(), vv.end());
long long int sz = less.size();
sm = 0;
for (int i = 0; i < min(n / 2, sz); i++) sm += less[i];
int i = 0;
for (; i < (n / 2) - less.size(); i++) {
sm += vv[i];
}
sm += m;
i++;
for (; i < vv.size(); i++) sm += max(m, vv[i]);
{
if (sm <= s) {
an = m;
l = m + 1;
} else
h = m - 1;
}
}
cout << an << "\n";
}
}
| ### Prompt
Generate a Cpp solution to the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int dr8[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dc8[] = {1, -1, 0, 0, -1, 1, -1, 1};
int dr4[] = {0, 1, -1, 0};
int dc4[] = {1, 0, 0, -1};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
long long int n, s;
cin >> n >> s;
long long int r[n][2];
vector<pair<pair<long long int, long long int>, long long int> > v;
for (int i = 0; i < n; i++) {
cin >> r[i][0] >> r[i][1];
v.push_back({{r[i][1], r[i][0]}, i});
}
sort(v.begin(), v.end());
long long int an, l = 1, h = 1000000000;
while (l <= h) {
long long int m = (l + h) / 2;
vector<long long int> vv, less;
long long int cn = 0, mx = 0, sm = 0, gr = 0;
for (int i = 0; i < n; i++) {
if (r[i][1] < m) {
less.push_back(r[i][0]);
} else
vv.push_back(r[i][0]);
if (r[i][0] <= m) {
cn++;
mx = max(mx, r[i][0]);
}
if (r[i][1] >= m) gr++;
sm += r[i][0];
}
if (cn < (n + 1) / 2) {
l = m + 1;
continue;
} else if (gr < (n + 1) / 2) {
h = m - 1;
continue;
}
sort(less.begin(), less.end());
sort(vv.begin(), vv.end());
long long int sz = less.size();
sm = 0;
for (int i = 0; i < min(n / 2, sz); i++) sm += less[i];
int i = 0;
for (; i < (n / 2) - less.size(); i++) {
sm += vv[i];
}
sm += m;
i++;
for (; i < vv.size(); i++) sm += max(m, vv[i]);
{
if (sm <= s) {
an = m;
l = m + 1;
} else
h = m - 1;
}
}
cout << an << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 2 * 1e5 + 1;
struct Emp {
int l, r;
} emp[N];
bool search(int m, int n, long long s) {
long long sum = 0, cnt = 0;
for (int i = n - 1; i >= 0; i--) {
if (emp[i].l >= m)
cnt++;
else if (emp[i].l <= m && emp[i].r >= m && cnt <= n / 2) {
sum += m - emp[i].l;
cnt++;
}
}
return sum <= s && cnt > n / 2;
}
void solve() {
int n;
long long s;
cin >> n >> s;
for (int i = 0; i < n; i++) {
int l, r;
cin >> l >> r;
emp[i].l = l;
emp[i].r = r;
s -= l;
}
sort(emp, emp + n, [](const Emp &x, const Emp &y) { return x.l < y.l; });
int l = 0, r = 1e9 + 1;
while (l < r - 1) {
int m = l + (r - l) / 2;
bool f = search(m, n, s);
if (f)
l = m;
else
r = m;
}
cout << l << "\n";
}
int t;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> t;
while (t--) solve();
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 2 * 1e5 + 1;
struct Emp {
int l, r;
} emp[N];
bool search(int m, int n, long long s) {
long long sum = 0, cnt = 0;
for (int i = n - 1; i >= 0; i--) {
if (emp[i].l >= m)
cnt++;
else if (emp[i].l <= m && emp[i].r >= m && cnt <= n / 2) {
sum += m - emp[i].l;
cnt++;
}
}
return sum <= s && cnt > n / 2;
}
void solve() {
int n;
long long s;
cin >> n >> s;
for (int i = 0; i < n; i++) {
int l, r;
cin >> l >> r;
emp[i].l = l;
emp[i].r = r;
s -= l;
}
sort(emp, emp + n, [](const Emp &x, const Emp &y) { return x.l < y.l; });
int l = 0, r = 1e9 + 1;
while (l < r - 1) {
int m = l + (r - l) / 2;
bool f = search(m, n, s);
if (f)
l = m;
else
r = m;
}
cout << l << "\n";
}
int t;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> t;
while (t--) solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n;
int T, l, r, ans, mid;
pair<int, int> a[maxn];
int todo[maxn], top;
long long s;
bool check(int mid) {
long long ss = s;
int tot = 0, top = 0;
for (int i = 0; i < n; i++) {
if (a[i].second >= mid) {
if (a[i].first >= mid)
tot++;
else
todo[top++] = mid - a[i].first;
}
}
sort(todo, todo + top);
for (int i = 0; i < top && ss >= todo[i]; i++) {
tot++;
ss -= todo[i];
}
return tot * 2 > n;
}
int main() {
cin >> T;
while (T--) {
cin >> n >> s;
for (int i = 0; i < n; i++) {
scanf("%d%d", &l, &r);
a[i] = pair<int, int>(l, r);
s -= l;
}
l = 0, r = 1e9;
ans = 0;
while (l <= r) {
mid = (l + r) >> 1;
if (check(mid))
ans = mid, l = mid + 1;
else
r = mid - 1;
}
cout << ans << endl;
}
return 0;
}
| ### Prompt
In CPP, your task is to solve the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n;
int T, l, r, ans, mid;
pair<int, int> a[maxn];
int todo[maxn], top;
long long s;
bool check(int mid) {
long long ss = s;
int tot = 0, top = 0;
for (int i = 0; i < n; i++) {
if (a[i].second >= mid) {
if (a[i].first >= mid)
tot++;
else
todo[top++] = mid - a[i].first;
}
}
sort(todo, todo + top);
for (int i = 0; i < top && ss >= todo[i]; i++) {
tot++;
ss -= todo[i];
}
return tot * 2 > n;
}
int main() {
cin >> T;
while (T--) {
cin >> n >> s;
for (int i = 0; i < n; i++) {
scanf("%d%d", &l, &r);
a[i] = pair<int, int>(l, r);
s -= l;
}
l = 0, r = 1e9;
ans = 0;
while (l <= r) {
mid = (l + r) >> 1;
if (check(mid))
ans = mid, l = mid + 1;
else
r = mid - 1;
}
cout << ans << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long n, second;
long long minS = 0;
vector<int> ptats;
vector<pair<int, int>> lr;
bool check(long long median) {
long long excess = 0;
int it = upper_bound(ptats.begin(), ptats.end(), median) - ptats.begin() - 1;
int left = it;
int right = (n - it - 1);
if (left < right) {
return false;
}
int cnt = (left - right) / 2 + 1;
for (int i = it; i >= 0 && cnt; i--) {
if (lr[i].second >= median) {
excess += (median - lr[i].first);
cnt--;
}
}
return excess + minS <= second && cnt == 0;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--) {
ptats.clear();
lr.clear();
minS = 0;
cin >> n >> second;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
lr.push_back({x, y});
minS += x;
}
ptats.resize(n);
sort(lr.begin(), lr.end());
for (int i = 0; i < n; i++) {
ptats[i] = lr[i].first;
}
int l = lr[n / 2].first;
long long r = 1e15;
while (l < r) {
long long mid = l + (r - l + 1) / 2;
if (check(mid)) {
l = mid;
} else {
r = mid - 1;
}
}
cout << l << "\n";
}
return 0;
}
| ### Prompt
Develop a solution in CPP to the problem described below:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long n, second;
long long minS = 0;
vector<int> ptats;
vector<pair<int, int>> lr;
bool check(long long median) {
long long excess = 0;
int it = upper_bound(ptats.begin(), ptats.end(), median) - ptats.begin() - 1;
int left = it;
int right = (n - it - 1);
if (left < right) {
return false;
}
int cnt = (left - right) / 2 + 1;
for (int i = it; i >= 0 && cnt; i--) {
if (lr[i].second >= median) {
excess += (median - lr[i].first);
cnt--;
}
}
return excess + minS <= second && cnt == 0;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--) {
ptats.clear();
lr.clear();
minS = 0;
cin >> n >> second;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
lr.push_back({x, y});
minS += x;
}
ptats.resize(n);
sort(lr.begin(), lr.end());
for (int i = 0; i < n; i++) {
ptats[i] = lr[i].first;
}
int l = lr[n / 2].first;
long long r = 1e15;
while (l < r) {
long long mid = l + (r - l + 1) / 2;
if (check(mid)) {
l = mid;
} else {
r = mid - 1;
}
}
cout << l << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long add(long long a, long long b, long long mod) {
return (a % mod + b % mod) % mod;
}
long long sub(long long a, long long b, long long mod) {
return (a % mod - b % mod + mod) % mod;
}
long long mul(long long a, long long b, long long mod) {
return ((a % mod) * (b % mod)) % mod;
}
long long sumodd(long long num, long long mod) { return mul(num, num, mod); }
long long sumeven(long long num, long long mod) {
return mul(num, num + 1, mod);
}
long long sum_any_range(long long st, long long en, long long num) {
return (num * (st + en) / 2);
}
long long gcd(long long a, long long b) {
while (b != 0) {
long long a2 = a;
a = b;
b = a2 % b;
}
return a;
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
string makeitbinary(long long n) {
string s;
while (n) {
s = s + (char)((n % 2) + '0');
n /= 2;
}
reverse(s.begin(), s.end());
return s;
}
bool bit(int num, int i) { return ((num >> i) & 1); }
long long fastpowermod(long long b, long long p, long long mod) {
long long ans = 1;
while (p) {
if (p % 2) {
ans = mul(ans, b, mod);
}
b = mul(b, b, mod);
p /= 2;
}
return ans;
}
long long fastpower(long long b, long long p) {
long long ans = 1;
while (p) {
if (p % 2) {
ans = ans * b;
}
b = b * b;
p /= 2;
}
return ans;
}
double fastpower_double(double b, long long p) {
double ans = 1;
while (p) {
if (p % 2) {
ans = ans * b;
}
b = b * b;
p /= 2;
}
return ans;
}
long long summation_formula(long long n) { return (n * (n + 1) / 2); }
bool lower_vowel(char c) {
return (c == 'i' || c == 'o' || c == 'u' || c == 'a' || c == 'e');
}
string bigint_mini(string s1, string s2) {
if (s1.size() > s2.size()) {
return s2;
} else if (s2.size() > s1.size()) {
return s1;
}
for (int i = 0; i < s1.size(); i++) {
if ((s1[i] - '0') > (s2[i] - '0')) {
return s2;
} else if ((s2[i] - '0') > (s1[i] - '0')) {
return s1;
}
}
return s1;
}
double polygon_area(int n, vector<double> X, vector<double> Y) {
double area = 0.0;
int j = n - 1;
for (int i = 0; i < n; i++) {
area += (X[j] + X[i]) * (Y[j] - Y[i]);
j = i;
}
return abs(area / 2.0);
}
long long sum_of_digits(string s) {
long long sum = 0;
for (int i = 0; i < s.size(); i++) {
sum += s[i] - '0';
}
return sum;
}
string makeitbase(long long num, long long base) {
string s;
while (num) {
long long mod = num % base;
s += (mod + '0');
num /= base;
}
reverse(s.begin(), s.end());
return s;
}
bool intersect(long long l1, long long r1, long long l2, long long r2) {
return (max(l1, l2) <= min(r1, r2));
}
pair<long long, long long> find_intersection(long long l1, long long r1,
long long l2, long long r2) {
return {max(l1, l2), min(r1, r2)};
}
long long sum_ranges(long long l, long long r) {
return summation_formula(r) - summation_formula(l);
}
double power_2(double num) { return num * num; }
bool isPowerOfTwo(int x) { return (x && !(x & (x - 1))); }
long long modInverse(long long A, long long M) {
return fastpowermod(A, M - 2, M);
}
long long num_inrange(long long l, long long r) {
l = min(l, r);
r = max(l, r);
return r - l + 1;
}
long long how_many_factor(long long num, long long t) {
long long cnt = 0;
while (num != 0 && num % t == 0) {
num /= t;
cnt++;
}
return cnt;
}
pair<pair<long long, long long>, pair<long long, long long> >
formula_rotate_left_(long long x, long long y, long long row_len,
long long col_len) {
long long nx = col_len - y - 1;
long long ny = x;
return {{nx, ny}, {col_len, row_len}};
}
pair<pair<long long, long long>, pair<long long, long long> >
formula_rotate_right_(long long x, long long y, long long row_len,
long long col_len) {
long long nx = y;
long long ny = row_len - x - 1;
return {{nx, ny}, {col_len, row_len}};
}
pair<pair<long long, long long>, pair<long long, long long> > horizontal_rotate(
long long x, long long y, long long row_len, long long col_len) {
long long nx = x;
long long ny = col_len - y - 1;
return {{nx, ny}, {row_len, col_len}};
}
long long mod_big_integer(string s, long long M) {
long long num = 0;
for (long long i = 0; i < s.size(); i++) {
num = add(mul(num, 10, M), (s[i] - '0'), M);
}
return num;
}
struct Point {
long long x, y;
};
long long overlappingArea(Point l1, Point r1, Point l2, Point r2) {
long long area1 = abs(l1.x - r1.x) * abs(l1.y - r1.y);
long long area2 = abs(l2.x - r2.x) * abs(l2.y - r2.y);
long long areaI =
(min(r1.x, r2.x) - max(l1.x, l2.x)) * (min(r1.y, r2.y) - max(l1.y, l2.y));
return areaI;
}
bool doOverlap(Point l1, Point r1, Point l2, Point r2) {
if (l1.x > r2.x || l2.x > r1.x) {
return false;
}
if (l1.y > r2.y || l2.y > r1.y) {
return false;
}
return true;
}
Point intersection_botl(Point l1, Point r1, Point l2, Point r2) {
Point topr, botl;
topr.y = min(r1.y, r2.y);
topr.x = min(r1.x, r2.x);
botl.y = max(l1.y, l2.y);
botl.x = max(l1.x, l2.x);
return botl;
}
Point intersection_topr(Point l1, Point r1, Point l2, Point r2) {
Point topr, botl;
topr.y = min(r1.y, r2.y);
topr.x = min(r1.x, r2.x);
botl.y = max(l1.y, l2.y);
botl.x = max(l1.x, l2.x);
return topr;
}
long long comp_two(Point l1, Point l2, Point r1, Point r2) {
if (doOverlap(l1, r1, l2, r2)) {
return overlappingArea(l1, r1, l2, r2);
} else {
return 0;
}
}
long long geometric_progression_sum(long long endd, long long st, long long k) {
return (endd * k - st) / (k - 1);
}
long long geometric_progression_sum_mod(long long endd, long long st,
long long k, long long M) {
return mul(sub(mul(endd, k, M), st, M), modInverse(k - 1, M), M);
}
long long M = 1e9 + 7;
const int sz = 1e5 + 10;
const int OO = 0x3f3f3f3f;
vector<pair<long long, long long> > v;
bool solve(long long median_val, long long salary, long long n) {
long long median = (n / 2) + 1;
for (int i = n - 1; i >= 0; i--) {
if (v[i].second >= median_val && median > 0) {
salary -= max(v[i].first, median_val);
median--;
} else {
salary -= v[i].first;
}
}
return (salary >= 0 && median == 0);
}
long long bs(long long l, long long r, long long salary, long long n) {
long long last = -1;
while (l <= r) {
long long mid = l + (r - l) / 2;
if (solve(mid, salary, n)) {
l = mid + 1;
last = mid;
} else {
r = mid - 1;
}
}
return last;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
;
int t;
cin >> t;
while (t--) {
long long n, s;
cin >> n >> s;
v.clear();
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
v.push_back({x, y});
}
sort(v.begin(), v.end());
cout << bs(0, 1e18, s, n) << endl;
}
return 0;
}
| ### Prompt
Generate a CPP solution to the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long add(long long a, long long b, long long mod) {
return (a % mod + b % mod) % mod;
}
long long sub(long long a, long long b, long long mod) {
return (a % mod - b % mod + mod) % mod;
}
long long mul(long long a, long long b, long long mod) {
return ((a % mod) * (b % mod)) % mod;
}
long long sumodd(long long num, long long mod) { return mul(num, num, mod); }
long long sumeven(long long num, long long mod) {
return mul(num, num + 1, mod);
}
long long sum_any_range(long long st, long long en, long long num) {
return (num * (st + en) / 2);
}
long long gcd(long long a, long long b) {
while (b != 0) {
long long a2 = a;
a = b;
b = a2 % b;
}
return a;
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
string makeitbinary(long long n) {
string s;
while (n) {
s = s + (char)((n % 2) + '0');
n /= 2;
}
reverse(s.begin(), s.end());
return s;
}
bool bit(int num, int i) { return ((num >> i) & 1); }
long long fastpowermod(long long b, long long p, long long mod) {
long long ans = 1;
while (p) {
if (p % 2) {
ans = mul(ans, b, mod);
}
b = mul(b, b, mod);
p /= 2;
}
return ans;
}
long long fastpower(long long b, long long p) {
long long ans = 1;
while (p) {
if (p % 2) {
ans = ans * b;
}
b = b * b;
p /= 2;
}
return ans;
}
double fastpower_double(double b, long long p) {
double ans = 1;
while (p) {
if (p % 2) {
ans = ans * b;
}
b = b * b;
p /= 2;
}
return ans;
}
long long summation_formula(long long n) { return (n * (n + 1) / 2); }
bool lower_vowel(char c) {
return (c == 'i' || c == 'o' || c == 'u' || c == 'a' || c == 'e');
}
string bigint_mini(string s1, string s2) {
if (s1.size() > s2.size()) {
return s2;
} else if (s2.size() > s1.size()) {
return s1;
}
for (int i = 0; i < s1.size(); i++) {
if ((s1[i] - '0') > (s2[i] - '0')) {
return s2;
} else if ((s2[i] - '0') > (s1[i] - '0')) {
return s1;
}
}
return s1;
}
double polygon_area(int n, vector<double> X, vector<double> Y) {
double area = 0.0;
int j = n - 1;
for (int i = 0; i < n; i++) {
area += (X[j] + X[i]) * (Y[j] - Y[i]);
j = i;
}
return abs(area / 2.0);
}
long long sum_of_digits(string s) {
long long sum = 0;
for (int i = 0; i < s.size(); i++) {
sum += s[i] - '0';
}
return sum;
}
string makeitbase(long long num, long long base) {
string s;
while (num) {
long long mod = num % base;
s += (mod + '0');
num /= base;
}
reverse(s.begin(), s.end());
return s;
}
bool intersect(long long l1, long long r1, long long l2, long long r2) {
return (max(l1, l2) <= min(r1, r2));
}
pair<long long, long long> find_intersection(long long l1, long long r1,
long long l2, long long r2) {
return {max(l1, l2), min(r1, r2)};
}
long long sum_ranges(long long l, long long r) {
return summation_formula(r) - summation_formula(l);
}
double power_2(double num) { return num * num; }
bool isPowerOfTwo(int x) { return (x && !(x & (x - 1))); }
long long modInverse(long long A, long long M) {
return fastpowermod(A, M - 2, M);
}
long long num_inrange(long long l, long long r) {
l = min(l, r);
r = max(l, r);
return r - l + 1;
}
long long how_many_factor(long long num, long long t) {
long long cnt = 0;
while (num != 0 && num % t == 0) {
num /= t;
cnt++;
}
return cnt;
}
pair<pair<long long, long long>, pair<long long, long long> >
formula_rotate_left_(long long x, long long y, long long row_len,
long long col_len) {
long long nx = col_len - y - 1;
long long ny = x;
return {{nx, ny}, {col_len, row_len}};
}
pair<pair<long long, long long>, pair<long long, long long> >
formula_rotate_right_(long long x, long long y, long long row_len,
long long col_len) {
long long nx = y;
long long ny = row_len - x - 1;
return {{nx, ny}, {col_len, row_len}};
}
pair<pair<long long, long long>, pair<long long, long long> > horizontal_rotate(
long long x, long long y, long long row_len, long long col_len) {
long long nx = x;
long long ny = col_len - y - 1;
return {{nx, ny}, {row_len, col_len}};
}
long long mod_big_integer(string s, long long M) {
long long num = 0;
for (long long i = 0; i < s.size(); i++) {
num = add(mul(num, 10, M), (s[i] - '0'), M);
}
return num;
}
struct Point {
long long x, y;
};
long long overlappingArea(Point l1, Point r1, Point l2, Point r2) {
long long area1 = abs(l1.x - r1.x) * abs(l1.y - r1.y);
long long area2 = abs(l2.x - r2.x) * abs(l2.y - r2.y);
long long areaI =
(min(r1.x, r2.x) - max(l1.x, l2.x)) * (min(r1.y, r2.y) - max(l1.y, l2.y));
return areaI;
}
bool doOverlap(Point l1, Point r1, Point l2, Point r2) {
if (l1.x > r2.x || l2.x > r1.x) {
return false;
}
if (l1.y > r2.y || l2.y > r1.y) {
return false;
}
return true;
}
Point intersection_botl(Point l1, Point r1, Point l2, Point r2) {
Point topr, botl;
topr.y = min(r1.y, r2.y);
topr.x = min(r1.x, r2.x);
botl.y = max(l1.y, l2.y);
botl.x = max(l1.x, l2.x);
return botl;
}
Point intersection_topr(Point l1, Point r1, Point l2, Point r2) {
Point topr, botl;
topr.y = min(r1.y, r2.y);
topr.x = min(r1.x, r2.x);
botl.y = max(l1.y, l2.y);
botl.x = max(l1.x, l2.x);
return topr;
}
long long comp_two(Point l1, Point l2, Point r1, Point r2) {
if (doOverlap(l1, r1, l2, r2)) {
return overlappingArea(l1, r1, l2, r2);
} else {
return 0;
}
}
long long geometric_progression_sum(long long endd, long long st, long long k) {
return (endd * k - st) / (k - 1);
}
long long geometric_progression_sum_mod(long long endd, long long st,
long long k, long long M) {
return mul(sub(mul(endd, k, M), st, M), modInverse(k - 1, M), M);
}
long long M = 1e9 + 7;
const int sz = 1e5 + 10;
const int OO = 0x3f3f3f3f;
vector<pair<long long, long long> > v;
bool solve(long long median_val, long long salary, long long n) {
long long median = (n / 2) + 1;
for (int i = n - 1; i >= 0; i--) {
if (v[i].second >= median_val && median > 0) {
salary -= max(v[i].first, median_val);
median--;
} else {
salary -= v[i].first;
}
}
return (salary >= 0 && median == 0);
}
long long bs(long long l, long long r, long long salary, long long n) {
long long last = -1;
while (l <= r) {
long long mid = l + (r - l) / 2;
if (solve(mid, salary, n)) {
l = mid + 1;
last = mid;
} else {
r = mid - 1;
}
}
return last;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
;
int t;
cin >> t;
while (t--) {
long long n, s;
cin >> n >> s;
v.clear();
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
v.push_back({x, y});
}
sort(v.begin(), v.end());
cout << bs(0, 1e18, s, n) << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long n, s;
vector<pair<int, int> > v;
bool moze(int x) {
long long temp = 0LL;
int br = 0;
vector<int> nz;
for (int i = 0; i < n; ++i) {
if (v[i].second < x)
temp += v[i].first * 1LL;
else if (v[i].first >= x) {
temp += v[i].first * 1LL;
++br;
} else
nz.push_back(v[i].first);
}
if ((n + 1) / 2 - br > (int)(nz).size()) return 0;
int izraz = max(0LL, (n + 1) / 2 - br);
for (int i = 0; i < (int)(nz).size(); ++i) {
if (i < (int)(nz).size() - izraz)
temp += 1LL * nz[i];
else
temp += x * 1LL;
}
return temp <= s;
}
void solve() {
cin >> n >> s;
v.resize(n);
for (auto &z : v) cin >> z.first >> z.second;
int l = 1e9 + 1, r = -1e9 - 10;
for (int i = 0; i < n; ++i) {
l = min(l, v[i].first);
r = max(r, v[i].second);
}
sort((v).begin(), (v).end());
int ans = l;
while (l <= r) {
int mid = l + (r - l) / 2;
if (moze(mid)) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) solve();
}
| ### Prompt
In Cpp, your task is to solve the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long n, s;
vector<pair<int, int> > v;
bool moze(int x) {
long long temp = 0LL;
int br = 0;
vector<int> nz;
for (int i = 0; i < n; ++i) {
if (v[i].second < x)
temp += v[i].first * 1LL;
else if (v[i].first >= x) {
temp += v[i].first * 1LL;
++br;
} else
nz.push_back(v[i].first);
}
if ((n + 1) / 2 - br > (int)(nz).size()) return 0;
int izraz = max(0LL, (n + 1) / 2 - br);
for (int i = 0; i < (int)(nz).size(); ++i) {
if (i < (int)(nz).size() - izraz)
temp += 1LL * nz[i];
else
temp += x * 1LL;
}
return temp <= s;
}
void solve() {
cin >> n >> s;
v.resize(n);
for (auto &z : v) cin >> z.first >> z.second;
int l = 1e9 + 1, r = -1e9 - 10;
for (int i = 0; i < n; ++i) {
l = min(l, v[i].first);
r = max(r, v[i].second);
}
sort((v).begin(), (v).end());
int ans = l;
while (l <= r) {
int mid = l + (r - l) / 2;
if (moze(mid)) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) solve();
}
``` |
#include <bits/stdc++.h>
using namespace std;
int l[200010], r[200010];
vector<long long> d;
void solve() {
long long n, s;
scanf("%lld%lld", &n, &s);
for (int i = 1; i <= n; i++) scanf("%lld%lld", &l[i], &r[i]);
int L = 1, R = 1e9, m = (1 + n) / 2;
long long sum0 = accumulate(l + 1, l + 1 + n, 0LL);
while (L < R) {
int mid = (L + R + 1) / 2;
d.clear();
for (int i = 1; i <= n; i++)
if (r[i] >= mid) d.push_back(max(0, mid - l[i]));
sort(d.begin(), d.end());
if (d.size() >= m && sum0 + accumulate(d.begin(), d.begin() + m, 0LL) <= s)
L = mid;
else
R = mid - 1;
}
printf("%d\n", L);
}
int main() {
int T;
scanf("%d", &T);
while (T--) solve();
}
| ### Prompt
Please create a solution in cpp to the following problem:
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
* the median of the sequence [5, 1, 10, 17, 6] is 6,
* the median of the sequence [1, 2, 1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n β€ s.
Note that you don't have to spend all your s dollars on salaries.
You have to answer t test cases.
Input
The first line contains one integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The first line of each query contains two integers n and s (1 β€ n < 2 β
10^5, 1 β€ s β€ 2 β
10^{14}) β the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ 10^9).
It is guaranteed that the sum of all n over all queries does not exceed 2 β
10^5.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. β_{i=1}^{n} l_i β€ s.
Output
For each test case print one integer β the maximum median salary that you can obtain.
Example
Input
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11.
In the second test case, you have to pay 1337 dollars to the only employee.
In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int l[200010], r[200010];
vector<long long> d;
void solve() {
long long n, s;
scanf("%lld%lld", &n, &s);
for (int i = 1; i <= n; i++) scanf("%lld%lld", &l[i], &r[i]);
int L = 1, R = 1e9, m = (1 + n) / 2;
long long sum0 = accumulate(l + 1, l + 1 + n, 0LL);
while (L < R) {
int mid = (L + R + 1) / 2;
d.clear();
for (int i = 1; i <= n; i++)
if (r[i] >= mid) d.push_back(max(0, mid - l[i]));
sort(d.begin(), d.end());
if (d.size() >= m && sum0 + accumulate(d.begin(), d.begin() + m, 0LL) <= s)
L = mid;
else
R = mid - 1;
}
printf("%d\n", L);
}
int main() {
int T;
scanf("%d", &T);
while (T--) solve();
}
``` |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.