output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
int possibleKoutsu(string str1, string str2) {
if (str1[0] == str2[0] && str1[1] == str2[1]) return 1;
return 0;
}
int possibleShuntsu(string str1, string str2) {
int diff = (int)str1[0] - (int)str2[0];
if (str1[1] == str2[1] && (abs(diff) == 1 || abs(diff) == 2)) return 1;
return 0;
}
int mentsu(string str1, string str2, string str3) {
int koutsu = possibleKoutsu(str1, str2) && possibleKoutsu(str2, str3);
int shuntsu = possibleShuntsu(str1, str2) && possibleShuntsu(str1, str3) &&
possibleShuntsu(str2, str3);
return (koutsu || shuntsu);
}
int main() {
int tst = 0, i = 0, tilesToDraw = 0;
string str1, str2, str3;
tst = 1;
while (tst > 0) {
cin >> str1 >> str2 >> str3;
if (mentsu(str1, str2, str3))
tilesToDraw = 0;
else if (possibleKoutsu(str1, str2) || possibleKoutsu(str1, str3) ||
possibleKoutsu(str2, str3) || possibleShuntsu(str1, str2) ||
possibleShuntsu(str1, str3) || possibleShuntsu(str3, str2)) {
tilesToDraw = 1;
} else {
tilesToDraw = 2;
}
tst--;
}
cout << tilesToDraw;
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int possibleKoutsu(string str1, string str2) {
if (str1[0] == str2[0] && str1[1] == str2[1]) return 1;
return 0;
}
int possibleShuntsu(string str1, string str2) {
int diff = (int)str1[0] - (int)str2[0];
if (str1[1] == str2[1] && (abs(diff) == 1 || abs(diff) == 2)) return 1;
return 0;
}
int mentsu(string str1, string str2, string str3) {
int koutsu = possibleKoutsu(str1, str2) && possibleKoutsu(str2, str3);
int shuntsu = possibleShuntsu(str1, str2) && possibleShuntsu(str1, str3) &&
possibleShuntsu(str2, str3);
return (koutsu || shuntsu);
}
int main() {
int tst = 0, i = 0, tilesToDraw = 0;
string str1, str2, str3;
tst = 1;
while (tst > 0) {
cin >> str1 >> str2 >> str3;
if (mentsu(str1, str2, str3))
tilesToDraw = 0;
else if (possibleKoutsu(str1, str2) || possibleKoutsu(str1, str3) ||
possibleKoutsu(str2, str3) || possibleShuntsu(str1, str2) ||
possibleShuntsu(str1, str3) || possibleShuntsu(str3, str2)) {
tilesToDraw = 1;
} else {
tilesToDraw = 2;
}
tst--;
}
cout << tilesToDraw;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
bitset<65000000> f;
long long m[200995];
vector<long long> z;
map<long long, long long> u;
map<long long, long long> u1;
map<string, int> v;
string w[200008];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
long long t, c, d, e, i, j[4000], k, n;
string a, b, x;
cin >> a >> b >> x;
j[a[1] - '0']++;
j[b[1] - '0']++;
j[x[1] - '0']++;
c = a[0] + b[0] + (x[0] - '0') - '0' - '0';
if (j['s' - '0'] == 3 || j['m' - '0'] == 3 || j['p' - '0'] == 3) {
k = max(max((a[0] - '0'), (b[0] - '0')), (x[0] - '0'));
n = min(min((a[0] - '0'), (b[0] - '0')), (x[0] - '0'));
c = c - (k + n);
if ((k == n && n == c) || (k == c + 1 && k == n + 2))
cout << "0" << endl;
else if (k - n < 3 || c - n < 3 || k - c < 3)
cout << "1" << endl;
else
cout << "2" << endl;
} else if (j['s' - '0'] == 2 || j['m' - '0'] == 2 || j['p' - '0'] == 2) {
if (a[1] == b[1] && abs(a[0] - b[0]) < 3)
cout << "1" << endl;
else if (a[1] == x[1] && abs(a[0] - x[0]) < 3)
cout << "1" << endl;
else if (x[1] == b[1] && abs(x[0] - b[0]) < 3)
cout << "1" << endl;
else
cout << "2" << endl;
} else
cout << "2" << endl;
return 0;
}
| ### Prompt
Generate a Cpp solution to the following problem:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
bitset<65000000> f;
long long m[200995];
vector<long long> z;
map<long long, long long> u;
map<long long, long long> u1;
map<string, int> v;
string w[200008];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
long long t, c, d, e, i, j[4000], k, n;
string a, b, x;
cin >> a >> b >> x;
j[a[1] - '0']++;
j[b[1] - '0']++;
j[x[1] - '0']++;
c = a[0] + b[0] + (x[0] - '0') - '0' - '0';
if (j['s' - '0'] == 3 || j['m' - '0'] == 3 || j['p' - '0'] == 3) {
k = max(max((a[0] - '0'), (b[0] - '0')), (x[0] - '0'));
n = min(min((a[0] - '0'), (b[0] - '0')), (x[0] - '0'));
c = c - (k + n);
if ((k == n && n == c) || (k == c + 1 && k == n + 2))
cout << "0" << endl;
else if (k - n < 3 || c - n < 3 || k - c < 3)
cout << "1" << endl;
else
cout << "2" << endl;
} else if (j['s' - '0'] == 2 || j['m' - '0'] == 2 || j['p' - '0'] == 2) {
if (a[1] == b[1] && abs(a[0] - b[0]) < 3)
cout << "1" << endl;
else if (a[1] == x[1] && abs(a[0] - x[0]) < 3)
cout << "1" << endl;
else if (x[1] == b[1] && abs(x[0] - b[0]) < 3)
cout << "1" << endl;
else
cout << "2" << endl;
} else
cout << "2" << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string tiles[4];
cin >> tiles[0] >> tiles[1] >> tiles[2];
sort(tiles, tiles + 3);
map<string, int> k;
map<char, vector<int> > s;
for (int i = 0; i < 3; ++i)
k[tiles[i]]++, s[tiles[i][1]].push_back(tiles[i][0] - '0');
int mx = -1;
for (auto tl : k) mx = max(mx, tl.second);
int mn = 10, cards;
for (auto tl : s) {
if (tl.second.size() == 1) cards = 2;
if (tl.second.size() == 2) {
int dif = tl.second[1] - tl.second[0];
if (dif == 1 || dif == 2)
cards = 1;
else
cards = 2;
}
if (tl.second.size() == 3) {
int dif1 = tl.second[1] - tl.second[0];
int dif2 = tl.second[2] - tl.second[1];
if (dif1 == 1 && dif2 == 1)
cards = 0;
else if ((dif1 != 1 && dif2 == 1) || (dif1 == 1 && dif2 != 1))
cards = 1;
else if ((dif1 != 1 && dif2 == 2) || (dif1 == 2 && dif2 != 1))
cards = 1;
else
cards = 2;
}
mn = min(mn, cards);
}
cout << min(3 - mx, mn) << "\n";
return 0;
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string tiles[4];
cin >> tiles[0] >> tiles[1] >> tiles[2];
sort(tiles, tiles + 3);
map<string, int> k;
map<char, vector<int> > s;
for (int i = 0; i < 3; ++i)
k[tiles[i]]++, s[tiles[i][1]].push_back(tiles[i][0] - '0');
int mx = -1;
for (auto tl : k) mx = max(mx, tl.second);
int mn = 10, cards;
for (auto tl : s) {
if (tl.second.size() == 1) cards = 2;
if (tl.second.size() == 2) {
int dif = tl.second[1] - tl.second[0];
if (dif == 1 || dif == 2)
cards = 1;
else
cards = 2;
}
if (tl.second.size() == 3) {
int dif1 = tl.second[1] - tl.second[0];
int dif2 = tl.second[2] - tl.second[1];
if (dif1 == 1 && dif2 == 1)
cards = 0;
else if ((dif1 != 1 && dif2 == 1) || (dif1 == 1 && dif2 != 1))
cards = 1;
else if ((dif1 != 1 && dif2 == 2) || (dif1 == 2 && dif2 != 1))
cards = 1;
else
cards = 2;
}
mn = min(mn, cards);
}
cout << min(3 - mx, mn) << "\n";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s[3];
for (int i = 0; i < 3; i++) {
cin >> s[i];
}
if (s[0][0] > s[1][0]) swap(s[0], s[1]);
if (s[1][0] > s[2][0]) swap(s[1], s[2]);
if (s[0][0] > s[1][0]) swap(s[0], s[1]);
int c1 = 1, c2 = 1;
for (int i = 0; i < 2; i++) {
if (s[i] == s[i + 1]) c1++;
}
for (int i = 0; i < 2; i++) {
if (s[i + 1][0] - s[i][0] == 1 && s[i][1] == s[i + 1][1]) c2++;
}
if (c1 == 1) {
if (s[0] == s[2]) c1++;
}
if (c1 == 1 && c2 == 1) {
if (s[2][0] - s[0][0] == 1 && s[0][1] == s[2][1])
c2++;
else if (s[1][0] - s[0][0] == 2 && s[0][1] == s[1][1])
c2++;
else if (s[2][0] - s[0][0] == 2 && s[0][1] == s[2][1])
c2++;
else if (s[2][0] - s[1][0] == 2 && s[1][1] == s[2][1])
c2++;
}
cout << 3 - max(c1, c2);
return 0;
}
| ### Prompt
Generate a Cpp solution to the following problem:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
string s[3];
for (int i = 0; i < 3; i++) {
cin >> s[i];
}
if (s[0][0] > s[1][0]) swap(s[0], s[1]);
if (s[1][0] > s[2][0]) swap(s[1], s[2]);
if (s[0][0] > s[1][0]) swap(s[0], s[1]);
int c1 = 1, c2 = 1;
for (int i = 0; i < 2; i++) {
if (s[i] == s[i + 1]) c1++;
}
for (int i = 0; i < 2; i++) {
if (s[i + 1][0] - s[i][0] == 1 && s[i][1] == s[i + 1][1]) c2++;
}
if (c1 == 1) {
if (s[0] == s[2]) c1++;
}
if (c1 == 1 && c2 == 1) {
if (s[2][0] - s[0][0] == 1 && s[0][1] == s[2][1])
c2++;
else if (s[1][0] - s[0][0] == 2 && s[0][1] == s[1][1])
c2++;
else if (s[2][0] - s[0][0] == 2 && s[0][1] == s[2][1])
c2++;
else if (s[2][0] - s[1][0] == 2 && s[1][1] == s[2][1])
c2++;
}
cout << 3 - max(c1, c2);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89};
string alpha = "abcdefghijklmnopqrstuvwxyz";
signed main() {
string s[3];
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> s[0] >> s[1] >> s[2];
swap(s[0][0], s[0][1]);
swap(s[1][0], s[1][1]);
swap(s[2][0], s[2][1]);
sort(s, s + 3);
swap(s[0][0], s[0][1]);
swap(s[1][0], s[1][1]);
swap(s[2][0], s[2][1]);
int IDENTICAL = 0, MAXIDENTICAL = 0, folo = 0, MAXFOLOING = 0, special = 3;
for (int i = 1; i < 3; i++) {
if (s[i] == s[i - 1]) {
IDENTICAL++;
MAXIDENTICAL = max(MAXIDENTICAL, IDENTICAL);
} else {
IDENTICAL = 0;
}
if (s[i][1] == s[i - 1][1] and char(int(s[i - 1][0]) + 1) == s[i][0]) {
folo++;
MAXFOLOING = max(MAXFOLOING, folo);
} else {
folo = 0;
}
if (s[i][1] == s[i - 1][1] && char(int(s[i - 1][0]) + 2) == s[i][0]) {
special = 1;
}
}
MAXIDENTICAL = 2 - MAXIDENTICAL;
MAXFOLOING = 2 - MAXFOLOING;
int ans = min(MAXIDENTICAL, MAXFOLOING);
ans = min(ans, special);
cout << ans;
cout << "\n";
return 0;
}
| ### Prompt
Generate a Cpp solution to the following problem:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89};
string alpha = "abcdefghijklmnopqrstuvwxyz";
signed main() {
string s[3];
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> s[0] >> s[1] >> s[2];
swap(s[0][0], s[0][1]);
swap(s[1][0], s[1][1]);
swap(s[2][0], s[2][1]);
sort(s, s + 3);
swap(s[0][0], s[0][1]);
swap(s[1][0], s[1][1]);
swap(s[2][0], s[2][1]);
int IDENTICAL = 0, MAXIDENTICAL = 0, folo = 0, MAXFOLOING = 0, special = 3;
for (int i = 1; i < 3; i++) {
if (s[i] == s[i - 1]) {
IDENTICAL++;
MAXIDENTICAL = max(MAXIDENTICAL, IDENTICAL);
} else {
IDENTICAL = 0;
}
if (s[i][1] == s[i - 1][1] and char(int(s[i - 1][0]) + 1) == s[i][0]) {
folo++;
MAXFOLOING = max(MAXFOLOING, folo);
} else {
folo = 0;
}
if (s[i][1] == s[i - 1][1] && char(int(s[i - 1][0]) + 2) == s[i][0]) {
special = 1;
}
}
MAXIDENTICAL = 2 - MAXIDENTICAL;
MAXFOLOING = 2 - MAXFOLOING;
int ans = min(MAXIDENTICAL, MAXFOLOING);
ans = min(ans, special);
cout << ans;
cout << "\n";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7, siz = 1e6 + 5;
long long t, n, m, k, a[siz];
vector<long long> v[4];
string s1, s2, s3;
void chk(string s) {
if (s[1] == 'm')
v[1].push_back(s[0] - '0');
else if (s[1] == 'p')
v[2].push_back(s[0] - '0');
else
v[3].push_back(s[0] - '0');
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> s1 >> s2 >> s3;
chk(s1);
chk(s2);
chk(s3);
sort(v[1].begin(), v[1].end());
sort(v[2].begin(), v[2].end());
sort(v[3].begin(), v[3].end());
if (v[1].size() and v[2].size() and v[3].size()) return cout << 2 << endl, 0;
for (long long i = 1; i <= 3; i++) {
if (v[i].size() == 2) {
if ((v[i][0] == v[i][1]) or (v[i][1] == v[i][0] + 1) or
(v[i][1] - v[i][0] == 2))
return cout << 1 << endl, 0;
}
}
for (long long i = 1; i <= 3; i++) {
if (v[i].size() == 3) {
if (v[i][0] == v[i][2]) return cout << 0 << endl, 0;
if ((v[i][0] == v[i][1] - 1) and (v[i][1] == v[i][2] - 1))
return cout << 0 << endl, 0;
if ((v[i][0] == v[i][1] - 1) or (v[i][1] == v[i][2] - 1))
return cout << 1 << endl, 0;
if ((v[i][0] == v[i][1] and v[i][1] != v[i][2]) or
(v[i][0] != v[i][1] and v[i][1] == v[i][2]))
return cout << 1 << endl, 0;
if (v[i][1] - v[i][0] == 2 or v[i][2] - v[i][1] == 2)
return cout << 1 << endl, 0;
}
}
cout << 2 << endl;
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7, siz = 1e6 + 5;
long long t, n, m, k, a[siz];
vector<long long> v[4];
string s1, s2, s3;
void chk(string s) {
if (s[1] == 'm')
v[1].push_back(s[0] - '0');
else if (s[1] == 'p')
v[2].push_back(s[0] - '0');
else
v[3].push_back(s[0] - '0');
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> s1 >> s2 >> s3;
chk(s1);
chk(s2);
chk(s3);
sort(v[1].begin(), v[1].end());
sort(v[2].begin(), v[2].end());
sort(v[3].begin(), v[3].end());
if (v[1].size() and v[2].size() and v[3].size()) return cout << 2 << endl, 0;
for (long long i = 1; i <= 3; i++) {
if (v[i].size() == 2) {
if ((v[i][0] == v[i][1]) or (v[i][1] == v[i][0] + 1) or
(v[i][1] - v[i][0] == 2))
return cout << 1 << endl, 0;
}
}
for (long long i = 1; i <= 3; i++) {
if (v[i].size() == 3) {
if (v[i][0] == v[i][2]) return cout << 0 << endl, 0;
if ((v[i][0] == v[i][1] - 1) and (v[i][1] == v[i][2] - 1))
return cout << 0 << endl, 0;
if ((v[i][0] == v[i][1] - 1) or (v[i][1] == v[i][2] - 1))
return cout << 1 << endl, 0;
if ((v[i][0] == v[i][1] and v[i][1] != v[i][2]) or
(v[i][0] != v[i][1] and v[i][1] == v[i][2]))
return cout << 1 << endl, 0;
if (v[i][1] - v[i][0] == 2 or v[i][2] - v[i][1] == 2)
return cout << 1 << endl, 0;
}
}
cout << 2 << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
char s[100];
pair<int, int> arr[10];
int ttt(char a) {
if (a == 's') return 1;
if (a == 'p') return 2;
return 3;
}
int main() {
gets(s);
int pos = 0;
for (int i = 1; i <= 3; i++) {
int x = s[pos] - '0';
pos++;
int y = ttt(s[pos]);
pos += 2;
arr[i] = pair<int, int>(y, x);
}
sort(arr + 1, arr + 1 + 3);
if (arr[1].first == arr[2].first && arr[2].first == arr[3].first) {
if (arr[1].second == arr[2].second && arr[2].second == arr[3].second) {
cout << "0" << endl;
return 0;
}
if (arr[1].second + 1 == arr[2].second &&
arr[2].second + 1 == arr[3].second) {
cout << "0" << endl;
return 0;
}
}
if (arr[1].first == arr[2].first) {
if (arr[1].second == arr[2].second) {
cout << "1" << endl;
return 0;
}
if (arr[1].second == arr[2].second - 1) {
cout << "1" << endl;
return 0;
}
if (arr[1].second == arr[2].second - 2) {
cout << "1" << endl;
return 0;
}
}
if (arr[2].first == arr[3].first) {
if (arr[3].second == arr[2].second) {
cout << "1" << endl;
return 0;
}
if (arr[3].second == arr[2].second + 1) {
cout << "1" << endl;
return 0;
}
if (arr[3].second == arr[2].second + 2) {
cout << "1" << endl;
return 0;
}
}
cout << "2" << endl;
return 0;
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
char s[100];
pair<int, int> arr[10];
int ttt(char a) {
if (a == 's') return 1;
if (a == 'p') return 2;
return 3;
}
int main() {
gets(s);
int pos = 0;
for (int i = 1; i <= 3; i++) {
int x = s[pos] - '0';
pos++;
int y = ttt(s[pos]);
pos += 2;
arr[i] = pair<int, int>(y, x);
}
sort(arr + 1, arr + 1 + 3);
if (arr[1].first == arr[2].first && arr[2].first == arr[3].first) {
if (arr[1].second == arr[2].second && arr[2].second == arr[3].second) {
cout << "0" << endl;
return 0;
}
if (arr[1].second + 1 == arr[2].second &&
arr[2].second + 1 == arr[3].second) {
cout << "0" << endl;
return 0;
}
}
if (arr[1].first == arr[2].first) {
if (arr[1].second == arr[2].second) {
cout << "1" << endl;
return 0;
}
if (arr[1].second == arr[2].second - 1) {
cout << "1" << endl;
return 0;
}
if (arr[1].second == arr[2].second - 2) {
cout << "1" << endl;
return 0;
}
}
if (arr[2].first == arr[3].first) {
if (arr[3].second == arr[2].second) {
cout << "1" << endl;
return 0;
}
if (arr[3].second == arr[2].second + 1) {
cout << "1" << endl;
return 0;
}
if (arr[3].second == arr[2].second + 2) {
cout << "1" << endl;
return 0;
}
}
cout << "2" << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<string> v(3);
bool f0() {
if (v[0] == v[1] && v[0] == v[2]) return true;
if (v[0][1] == v[1][1] && v[0][1] == v[2][1] && v[1][0] == v[0][0] + 1 &&
v[2][0] == v[1][0] + 1)
return true;
return false;
}
bool f1() {
for (int i = 0; i < 3; ++i)
for (int j = i + 1; j < 3; ++j)
if (v[i][1] == v[j][1] && abs(v[i][0] - v[j][0]) < 3) return true;
return false;
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
cin >> v[0] >> v[1] >> v[2];
sort(v.begin(), v.end());
if (f0())
cout << 0;
else if (f1())
cout << 1;
else
cout << 2;
return 0;
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<string> v(3);
bool f0() {
if (v[0] == v[1] && v[0] == v[2]) return true;
if (v[0][1] == v[1][1] && v[0][1] == v[2][1] && v[1][0] == v[0][0] + 1 &&
v[2][0] == v[1][0] + 1)
return true;
return false;
}
bool f1() {
for (int i = 0; i < 3; ++i)
for (int j = i + 1; j < 3; ++j)
if (v[i][1] == v[j][1] && abs(v[i][0] - v[j][0]) < 3) return true;
return false;
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
cin >> v[0] >> v[1] >> v[2];
sort(v.begin(), v.end());
if (f0())
cout << 0;
else if (f1())
cout << 1;
else
cout << 2;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int i;
vector<int> v;
string s1, s2, s3;
cin >> s1 >> s2 >> s3;
if (s1[1] == s2[1] && s1[1] == s3[1]) {
v.push_back(s1[0]);
v.push_back(s2[0]);
v.push_back(s3[0]);
sort(v.begin(), v.end());
if ((v[0] + 1) == v[1] && (v[1] + 1 == v[2]))
cout << "0";
else if (v[0] == v[1] && v[0] == v[2])
cout << "0";
else if (v[0] == v[1] || v[1] == v[2])
cout << "1";
else if (v[0] + 1 == v[1] || v[1] + 1 == v[2] || v[0] + 2 == v[1] ||
v[1] + 2 == v[2])
cout << "1";
else
cout << "2";
} else if (s1 == s2 || s2 == s3 || s3 == s1)
cout << "1";
else if (s1[1] == s2[1]) {
if (abs(s1[0] - s2[0]) == 1 || abs(s1[0] - s2[0]) == 2)
cout << "1";
else
cout << "2";
} else if (s3[1] == s2[1]) {
if (abs(s3[0] - s2[0]) == 1 || abs(s3[0] - s2[0]) == 2)
cout << "1";
else
cout << "2";
} else if (s1[1] == s3[1]) {
if (abs(s1[0] - s3[0]) == 1 || abs(s1[0] - s3[0]) == 2)
cout << "1";
else
cout << "2";
} else
cout << "2";
}
| ### Prompt
Please create a solution in CPP to the following problem:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int i;
vector<int> v;
string s1, s2, s3;
cin >> s1 >> s2 >> s3;
if (s1[1] == s2[1] && s1[1] == s3[1]) {
v.push_back(s1[0]);
v.push_back(s2[0]);
v.push_back(s3[0]);
sort(v.begin(), v.end());
if ((v[0] + 1) == v[1] && (v[1] + 1 == v[2]))
cout << "0";
else if (v[0] == v[1] && v[0] == v[2])
cout << "0";
else if (v[0] == v[1] || v[1] == v[2])
cout << "1";
else if (v[0] + 1 == v[1] || v[1] + 1 == v[2] || v[0] + 2 == v[1] ||
v[1] + 2 == v[2])
cout << "1";
else
cout << "2";
} else if (s1 == s2 || s2 == s3 || s3 == s1)
cout << "1";
else if (s1[1] == s2[1]) {
if (abs(s1[0] - s2[0]) == 1 || abs(s1[0] - s2[0]) == 2)
cout << "1";
else
cout << "2";
} else if (s3[1] == s2[1]) {
if (abs(s3[0] - s2[0]) == 1 || abs(s3[0] - s2[0]) == 2)
cout << "1";
else
cout << "2";
} else if (s1[1] == s3[1]) {
if (abs(s1[0] - s3[0]) == 1 || abs(s1[0] - s3[0]) == 2)
cout << "1";
else
cout << "2";
} else
cout << "2";
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
string t1, t2, t3;
char m1, m2, m3;
cin >> t1 >> t2 >> t3;
int n1, n2, n3;
vector<int> a(3);
n1 = t1[0] - '0';
n2 = t2[0] - '0';
n3 = t3[0] - '0';
m1 = t1[1];
m2 = t2[1];
m3 = t3[1];
a[0] = n1;
a[1] = n2;
a[2] = n3;
sort(a.begin(), a.end());
if (t1 == t2 && t2 == t3) {
cout << 0;
return 0;
} else if (t1 == t2 || t2 == t3 || t3 == t1) {
cout << 1;
return 0;
}
if (m1 == m2 && m2 == m3 && a[0] + 1 == a[1] && a[1] + 1 == a[2]) {
cout << 0;
return 0;
} else if (m1 == m2 && m2 == m3 && abs(a[0] - a[1]) <= 2) {
cout << 1;
return 0;
} else if (m1 == m2 && m2 == m3 && abs(a[1] - a[2]) <= 2) {
cout << 1;
return 0;
} else if (m1 == m2 && m2 == m3 && abs(a[0] - a[2]) <= 2) {
cout << 1;
return 0;
} else if (m1 == m2 && m2 == m3) {
cout << 2;
return 0;
}
if (m1 != m2 && m2 != m3 && m1 != m3) {
cout << 2;
return 0;
}
if (m1 == m2 && abs(n1 - n2) <= 2) {
cout << 1;
return 0;
} else if (m1 == m2) {
cout << 2;
return 0;
}
if (m1 == m3 && abs(n1 - n3) <= 2) {
cout << 1;
return 0;
} else if (m1 == m3) {
cout << 2;
return 0;
}
if (m3 == m2 && abs(n3 - n2) <= 2) {
cout << 1;
return 0;
} else if (m3 == m2)
cout << 2;
}
| ### Prompt
In cpp, your task is to solve the following problem:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
string t1, t2, t3;
char m1, m2, m3;
cin >> t1 >> t2 >> t3;
int n1, n2, n3;
vector<int> a(3);
n1 = t1[0] - '0';
n2 = t2[0] - '0';
n3 = t3[0] - '0';
m1 = t1[1];
m2 = t2[1];
m3 = t3[1];
a[0] = n1;
a[1] = n2;
a[2] = n3;
sort(a.begin(), a.end());
if (t1 == t2 && t2 == t3) {
cout << 0;
return 0;
} else if (t1 == t2 || t2 == t3 || t3 == t1) {
cout << 1;
return 0;
}
if (m1 == m2 && m2 == m3 && a[0] + 1 == a[1] && a[1] + 1 == a[2]) {
cout << 0;
return 0;
} else if (m1 == m2 && m2 == m3 && abs(a[0] - a[1]) <= 2) {
cout << 1;
return 0;
} else if (m1 == m2 && m2 == m3 && abs(a[1] - a[2]) <= 2) {
cout << 1;
return 0;
} else if (m1 == m2 && m2 == m3 && abs(a[0] - a[2]) <= 2) {
cout << 1;
return 0;
} else if (m1 == m2 && m2 == m3) {
cout << 2;
return 0;
}
if (m1 != m2 && m2 != m3 && m1 != m3) {
cout << 2;
return 0;
}
if (m1 == m2 && abs(n1 - n2) <= 2) {
cout << 1;
return 0;
} else if (m1 == m2) {
cout << 2;
return 0;
}
if (m1 == m3 && abs(n1 - n3) <= 2) {
cout << 1;
return 0;
} else if (m1 == m3) {
cout << 2;
return 0;
}
if (m3 == m2 && abs(n3 - n2) <= 2) {
cout << 1;
return 0;
} else if (m3 == m2)
cout << 2;
}
``` |
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
string s[3];
cin >> s[0] >> s[1] >> s[2];
if (s[0] == s[1] and s[1] == s[2]) {
cout << "0\n";
return 0;
}
sort(s, s + 3);
char a = s[0][0] + 1;
char c = s[2][0] - 1;
if (s[0][1] == s[1][1] and s[1][1] == s[2][1]) {
if (a == c and c == s[1][0]) {
cout << "0\n";
return 0;
}
}
if (s[0] == s[1] or s[1] == s[2] or s[0] == s[2]) {
cout << "1\n";
return 0;
}
if (s[0][1] == s[1][1] and abs((long long)(s[0][0] - s[1][0])) <= 2 and
abs((long long)(s[0][0] - s[1][0])) > 0) {
cout << "1\n";
return 0;
}
if (s[0][1] == s[2][1] and abs((long long)(s[0][0] - s[2][0])) <= 2 and
abs((long long)(s[0][0] - s[2][0])) > 0) {
cout << "1\n";
return 0;
}
if (s[2][1] == s[1][1] and abs((long long)(s[2][0] - s[1][0])) <= 2 and
abs((long long)(s[2][0] - s[1][0])) > 0) {
cout << "1\n";
return 0;
}
cout << "2\n";
return 0;
}
| ### Prompt
Please create a solution in cpp to the following problem:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
string s[3];
cin >> s[0] >> s[1] >> s[2];
if (s[0] == s[1] and s[1] == s[2]) {
cout << "0\n";
return 0;
}
sort(s, s + 3);
char a = s[0][0] + 1;
char c = s[2][0] - 1;
if (s[0][1] == s[1][1] and s[1][1] == s[2][1]) {
if (a == c and c == s[1][0]) {
cout << "0\n";
return 0;
}
}
if (s[0] == s[1] or s[1] == s[2] or s[0] == s[2]) {
cout << "1\n";
return 0;
}
if (s[0][1] == s[1][1] and abs((long long)(s[0][0] - s[1][0])) <= 2 and
abs((long long)(s[0][0] - s[1][0])) > 0) {
cout << "1\n";
return 0;
}
if (s[0][1] == s[2][1] and abs((long long)(s[0][0] - s[2][0])) <= 2 and
abs((long long)(s[0][0] - s[2][0])) > 0) {
cout << "1\n";
return 0;
}
if (s[2][1] == s[1][1] and abs((long long)(s[2][0] - s[1][0])) <= 2 and
abs((long long)(s[2][0] - s[1][0])) > 0) {
cout << "1\n";
return 0;
}
cout << "2\n";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long smallest(long long x, long long y, long long z) {
long long c = 0;
while (x && y && z) {
x--;
y--;
z--;
c++;
}
return c;
}
long long smallest(long long x, long long y) { return (x < y) ? x : y; }
void solve() {
string input[3];
long long countType1 = 0;
long long countType2 = 0;
long long countType3 = 0;
for (long long i = 0; i < 3; i++) cin >> input[i];
long long count = 0;
if (input[1] == input[0]) {
if (input[2] == input[1]) {
count = 3;
} else
count = 2;
} else {
if (input[1] == input[2] || input[0] == input[2]) {
count = 2;
} else
count = 1;
}
if (countType1 == 2) {
cout << 0 << endl;
} else {
long long moreReqd[3][3];
vector<long long> k(3, 0);
for (long long i = 0; i < 3; i++) {
if (input[i][1] == 'm') {
moreReqd[0][k[0]++] = input[i][0];
} else if (input[i][1] == 'p') {
moreReqd[1][k[1]++] = input[i][0];
} else {
moreReqd[2][k[2]++] = input[i][0];
}
}
sort(moreReqd[0], moreReqd[0] + k[0]);
sort(moreReqd[1], moreReqd[1] + k[1]);
sort(moreReqd[2], moreReqd[2] + k[2]);
long long maxi = INT_MIN;
for (long long i = 0; i < 3; i++) {
if (k[i] == 0) {
} else if (k[i] == 1) {
if (maxi < 1) {
maxi = 1;
}
} else if (k[i] == 2) {
if (moreReqd[i][1] - moreReqd[i][0] > 2) {
if (maxi < 1) {
maxi = 1;
}
} else {
if (maxi < 2) {
maxi = 2;
}
}
} else {
if (moreReqd[i][0] == moreReqd[i][1] - 1 &&
moreReqd[i][0] == moreReqd[i][2] - 2) {
maxi = 3;
break;
} else if (moreReqd[i][0] == moreReqd[i][1] - 1 ||
moreReqd[i][0] == moreReqd[i][1] - 2 ||
moreReqd[i][1] == moreReqd[i][2] - 1 ||
moreReqd[i][1] == moreReqd[i][2] - 2) {
if (maxi < 2) maxi = 2;
} else if (moreReqd[i][0] == moreReqd[i][1] &&
moreReqd[i][2] - moreReqd[i][0] <= 2 ||
moreReqd[i][1] == moreReqd[i][2] &&
moreReqd[i][2] - moreReqd[i][0] <= 2) {
if (maxi < 2) {
maxi = 2;
}
} else {
if (maxi < 1) maxi = 1;
}
}
}
long long min2 = 3 - maxi;
long long min1 = 3 - count;
cout << min({min1, min2}) << endl;
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t = 1;
while (t--) {
solve();
}
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long smallest(long long x, long long y, long long z) {
long long c = 0;
while (x && y && z) {
x--;
y--;
z--;
c++;
}
return c;
}
long long smallest(long long x, long long y) { return (x < y) ? x : y; }
void solve() {
string input[3];
long long countType1 = 0;
long long countType2 = 0;
long long countType3 = 0;
for (long long i = 0; i < 3; i++) cin >> input[i];
long long count = 0;
if (input[1] == input[0]) {
if (input[2] == input[1]) {
count = 3;
} else
count = 2;
} else {
if (input[1] == input[2] || input[0] == input[2]) {
count = 2;
} else
count = 1;
}
if (countType1 == 2) {
cout << 0 << endl;
} else {
long long moreReqd[3][3];
vector<long long> k(3, 0);
for (long long i = 0; i < 3; i++) {
if (input[i][1] == 'm') {
moreReqd[0][k[0]++] = input[i][0];
} else if (input[i][1] == 'p') {
moreReqd[1][k[1]++] = input[i][0];
} else {
moreReqd[2][k[2]++] = input[i][0];
}
}
sort(moreReqd[0], moreReqd[0] + k[0]);
sort(moreReqd[1], moreReqd[1] + k[1]);
sort(moreReqd[2], moreReqd[2] + k[2]);
long long maxi = INT_MIN;
for (long long i = 0; i < 3; i++) {
if (k[i] == 0) {
} else if (k[i] == 1) {
if (maxi < 1) {
maxi = 1;
}
} else if (k[i] == 2) {
if (moreReqd[i][1] - moreReqd[i][0] > 2) {
if (maxi < 1) {
maxi = 1;
}
} else {
if (maxi < 2) {
maxi = 2;
}
}
} else {
if (moreReqd[i][0] == moreReqd[i][1] - 1 &&
moreReqd[i][0] == moreReqd[i][2] - 2) {
maxi = 3;
break;
} else if (moreReqd[i][0] == moreReqd[i][1] - 1 ||
moreReqd[i][0] == moreReqd[i][1] - 2 ||
moreReqd[i][1] == moreReqd[i][2] - 1 ||
moreReqd[i][1] == moreReqd[i][2] - 2) {
if (maxi < 2) maxi = 2;
} else if (moreReqd[i][0] == moreReqd[i][1] &&
moreReqd[i][2] - moreReqd[i][0] <= 2 ||
moreReqd[i][1] == moreReqd[i][2] &&
moreReqd[i][2] - moreReqd[i][0] <= 2) {
if (maxi < 2) {
maxi = 2;
}
} else {
if (maxi < 1) maxi = 1;
}
}
}
long long min2 = 3 - maxi;
long long min1 = 3 - count;
cout << min({min1, min2}) << endl;
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t = 1;
while (t--) {
solve();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a_, long long b_) {
if (b_ > a_) swap(b_, a_);
return (b_ == 0 ? a_ : gcd(b_, a_ % b_));
}
int n, arr[15][10];
char c;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(NULL);
for (long long i = 0; i < 3; i++) {
cin >> n >> c;
if (c == 'm') {
arr[n][0]++;
} else if (c == 'p') {
arr[n][1]++;
} else {
arr[n][2]++;
}
}
for (int i = 1; i < 10; i++) {
for (int j = 0; j < 3; j++) {
if (arr[i][j] == 3) {
cout << 0 << "\n";
return 0;
}
}
}
for (int i = 1; i < 10; i++) {
for (int j = 0; j < 3; j++) {
if (arr[i][j] == 1 && arr[i + 1][j] == 1 && arr[i + 2][j] == 1) {
cout << 0 << "\n";
return 0;
}
}
}
for (int i = 1; i < 10; i++) {
for (int j = 0; j < 3; j++) {
if (arr[i][j] == 2) {
cout << 1 << "\n";
return 0;
}
}
}
for (int i = 1; i < 10; i++) {
for (int j = 0; j < 3; j++) {
if (arr[i][j] == 1 && (arr[i + 1][j] == 1 || arr[i + 2][j] == 1)) {
cout << 1 << "\n";
return 0;
}
}
}
cout << 2 << "\n";
return 0;
}
| ### Prompt
Please create a solution in cpp to the following problem:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a_, long long b_) {
if (b_ > a_) swap(b_, a_);
return (b_ == 0 ? a_ : gcd(b_, a_ % b_));
}
int n, arr[15][10];
char c;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(NULL);
for (long long i = 0; i < 3; i++) {
cin >> n >> c;
if (c == 'm') {
arr[n][0]++;
} else if (c == 'p') {
arr[n][1]++;
} else {
arr[n][2]++;
}
}
for (int i = 1; i < 10; i++) {
for (int j = 0; j < 3; j++) {
if (arr[i][j] == 3) {
cout << 0 << "\n";
return 0;
}
}
}
for (int i = 1; i < 10; i++) {
for (int j = 0; j < 3; j++) {
if (arr[i][j] == 1 && arr[i + 1][j] == 1 && arr[i + 2][j] == 1) {
cout << 0 << "\n";
return 0;
}
}
}
for (int i = 1; i < 10; i++) {
for (int j = 0; j < 3; j++) {
if (arr[i][j] == 2) {
cout << 1 << "\n";
return 0;
}
}
}
for (int i = 1; i < 10; i++) {
for (int j = 0; j < 3; j++) {
if (arr[i][j] == 1 && (arr[i + 1][j] == 1 || arr[i + 2][j] == 1)) {
cout << 1 << "\n";
return 0;
}
}
}
cout << 2 << "\n";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
const int mod = 1e9 + 7;
const int maxn = 1e6 + 5;
string s[3];
bool isShun1(int i, int j) {
return (s[i][1] == s[j][1] && s[i][0] == s[j][0] - 1);
}
bool isShun2(int i, int j) {
return (s[i][1] == s[j][1] && s[i][0] == s[j][0] - 2);
}
int main() {
cin >> s[0] >> s[1] >> s[2];
sort(s, s + 3);
int ans = 2;
if (s[0] == s[1] && s[1] == s[2])
ans = 0;
else if (s[0] == s[1] || s[1] == s[2] || s[0] == s[2])
ans = 1;
if (isShun1(0, 1) || isShun1(1, 2) || isShun1(0, 2)) ans = min(ans, 1);
if (isShun2(0, 1) || isShun2(1, 2) || isShun2(0, 2)) ans = min(ans, 1);
if (isShun1(0, 1) && isShun1(1, 2)) ans = 0;
cout << ans << '\n';
}
| ### Prompt
Generate a Cpp solution to the following problem:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
const int mod = 1e9 + 7;
const int maxn = 1e6 + 5;
string s[3];
bool isShun1(int i, int j) {
return (s[i][1] == s[j][1] && s[i][0] == s[j][0] - 1);
}
bool isShun2(int i, int j) {
return (s[i][1] == s[j][1] && s[i][0] == s[j][0] - 2);
}
int main() {
cin >> s[0] >> s[1] >> s[2];
sort(s, s + 3);
int ans = 2;
if (s[0] == s[1] && s[1] == s[2])
ans = 0;
else if (s[0] == s[1] || s[1] == s[2] || s[0] == s[2])
ans = 1;
if (isShun1(0, 1) || isShun1(1, 2) || isShun1(0, 2)) ans = min(ans, 1);
if (isShun2(0, 1) || isShun2(1, 2) || isShun2(0, 2)) ans = min(ans, 1);
if (isShun1(0, 1) && isShun1(1, 2)) ans = 0;
cout << ans << '\n';
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long mx = 1e6 + 9;
const long long inf = 1e18 + 9;
long long A[mx], B[mx], freq[mx], fr[mx];
vector<pair<long long, long long> > vp1, vp2;
vector<long long> v1;
vector<char> v2;
bool sign[mx];
map<char, bool> mp;
int32_t main() {
long long n = 0, m = 0, k = 0, a = 0, b = 0, c = 0, p = 0, q = 0, x = 0,
y = 0, sum = 0, ans = 0, maxi = -inf, mini = inf;
bool f1 = false, f2 = false;
char ch1, ch2, ch3;
cin >> n >> ch1 >> m >> ch2 >> k >> ch3;
v1.push_back(n);
v1.push_back(m);
v1.push_back(k);
sort(v1.begin(), v1.end());
if (n == m && k == m && ch1 == ch2 && ch2 == ch3)
cout << 0 << endl;
else if (ch1 == ch2 && ch2 == ch3 && v1[0] + 1 == v1[1] && v1[1] + 1 == v1[2])
cout << 0 << endl;
else {
if ((ch1 == ch2 && n == m) || (ch2 == ch3 && m == k) ||
(ch3 == ch1 && n == k))
cout << 1 << endl;
else if ((ch1 == ch2 && ((n == m + 1) || (m == n + 1))) ||
(ch2 == ch3 && ((k == m + 1) || (m == k + 1))) ||
((ch1 == ch3 && ((n == k + 1) || (k == n + 1)))))
cout << 1 << endl;
else if ((ch1 == ch2 && ((n == m + 2) || (m == n + 2))) ||
(ch2 == ch3 && ((k == m + 2) || (m == k + 2))) ||
((ch1 == ch3 && ((n == k + 2) || (k == n + 2)))))
cout << 1 << endl;
else
cout << 2 << endl;
}
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long mx = 1e6 + 9;
const long long inf = 1e18 + 9;
long long A[mx], B[mx], freq[mx], fr[mx];
vector<pair<long long, long long> > vp1, vp2;
vector<long long> v1;
vector<char> v2;
bool sign[mx];
map<char, bool> mp;
int32_t main() {
long long n = 0, m = 0, k = 0, a = 0, b = 0, c = 0, p = 0, q = 0, x = 0,
y = 0, sum = 0, ans = 0, maxi = -inf, mini = inf;
bool f1 = false, f2 = false;
char ch1, ch2, ch3;
cin >> n >> ch1 >> m >> ch2 >> k >> ch3;
v1.push_back(n);
v1.push_back(m);
v1.push_back(k);
sort(v1.begin(), v1.end());
if (n == m && k == m && ch1 == ch2 && ch2 == ch3)
cout << 0 << endl;
else if (ch1 == ch2 && ch2 == ch3 && v1[0] + 1 == v1[1] && v1[1] + 1 == v1[2])
cout << 0 << endl;
else {
if ((ch1 == ch2 && n == m) || (ch2 == ch3 && m == k) ||
(ch3 == ch1 && n == k))
cout << 1 << endl;
else if ((ch1 == ch2 && ((n == m + 1) || (m == n + 1))) ||
(ch2 == ch3 && ((k == m + 1) || (m == k + 1))) ||
((ch1 == ch3 && ((n == k + 1) || (k == n + 1)))))
cout << 1 << endl;
else if ((ch1 == ch2 && ((n == m + 2) || (m == n + 2))) ||
(ch2 == ch3 && ((k == m + 2) || (m == k + 2))) ||
((ch1 == ch3 && ((n == k + 2) || (k == n + 2)))))
cout << 1 << endl;
else
cout << 2 << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
map<string, int> m;
int main() {
string s;
string b[4];
int ind = 0;
for (int i = 1; i <= 3; i++) {
string a;
cin >> a;
b[i] = a;
if (i > 1) {
if (a != s) ind = 1;
}
m[a]++;
s = a;
}
if (ind == 0) {
cout << 0;
return 0;
}
int ans = 3;
for (int i = 1; i <= 3; i++) {
int ans1 = 3 - m[b[i]];
int ans2 = 4;
int x = b[i][0] - 48;
int j = max(0, x - 2);
for (; j <= x; j++) {
char n = (j + 1) + 48;
char l = (j + 48);
char p = (j + 2) + 48;
string s1 = "", s2 = "", s3 = "";
s1 += l, s2 += n, s3 += p;
s1 += b[i][1], s2 += b[i][1], s3 += b[i][1];
int o = m[s1], y = m[s2], x = m[s3];
x = min(1, x), o = min(o, 1), y = min(1, y);
ans2 = min(ans2, 3 - (x + o + y));
}
ans = min(ans, min(ans1, ans2));
}
cout << ans;
return 0;
}
| ### Prompt
Please formulate a CPP solution to the following problem:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
map<string, int> m;
int main() {
string s;
string b[4];
int ind = 0;
for (int i = 1; i <= 3; i++) {
string a;
cin >> a;
b[i] = a;
if (i > 1) {
if (a != s) ind = 1;
}
m[a]++;
s = a;
}
if (ind == 0) {
cout << 0;
return 0;
}
int ans = 3;
for (int i = 1; i <= 3; i++) {
int ans1 = 3 - m[b[i]];
int ans2 = 4;
int x = b[i][0] - 48;
int j = max(0, x - 2);
for (; j <= x; j++) {
char n = (j + 1) + 48;
char l = (j + 48);
char p = (j + 2) + 48;
string s1 = "", s2 = "", s3 = "";
s1 += l, s2 += n, s3 += p;
s1 += b[i][1], s2 += b[i][1], s3 += b[i][1];
int o = m[s1], y = m[s2], x = m[s3];
x = min(1, x), o = min(o, 1), y = min(1, y);
ans2 = min(ans2, 3 - (x + o + y));
}
ans = min(ans, min(ans1, ans2));
}
cout << ans;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int inf = 100000000;
long long INF = 4000000000000000000;
long long MOD = 1000000007;
int main() {
vector<string> s(3);
vector<int> a(3);
for (int(i) = 0; (i) < (3); (i)++) cin >> s.at(i);
sort(s.begin(), s.end());
for (int(i) = 0; (i) < (3); (i)++) a.at(i) = s.at(i).at(0) - '0';
if (s.at(0) == s.at(1) && s.at(1) == s.at(2)) {
cout << 0 << endl;
return 0;
}
if (s.at(0).at(1) == s.at(1).at(1) && s.at(1).at(1) == s.at(2).at(1) &&
a.at(0) == a.at(1) - 1 && a.at(1) == a.at(2) - 1) {
cout << 0 << endl;
return 0;
}
for (int(i) = 0; (i) < (2); (i)++) {
for (int j = i + 1; j < 3; j++) {
if (s.at(i).at(1) == s.at(j).at(1) &&
(a.at(i) == a.at(j) || a.at(i) == a.at(j) - 1 ||
a.at(i) == a.at(j) + 1 || a.at(i) == a.at(j) + 2 ||
a.at(i) == a.at(j) - 2)) {
cout << 1 << endl;
return 0;
}
}
}
cout << 2 << endl;
}
| ### Prompt
In Cpp, your task is to solve the following problem:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int inf = 100000000;
long long INF = 4000000000000000000;
long long MOD = 1000000007;
int main() {
vector<string> s(3);
vector<int> a(3);
for (int(i) = 0; (i) < (3); (i)++) cin >> s.at(i);
sort(s.begin(), s.end());
for (int(i) = 0; (i) < (3); (i)++) a.at(i) = s.at(i).at(0) - '0';
if (s.at(0) == s.at(1) && s.at(1) == s.at(2)) {
cout << 0 << endl;
return 0;
}
if (s.at(0).at(1) == s.at(1).at(1) && s.at(1).at(1) == s.at(2).at(1) &&
a.at(0) == a.at(1) - 1 && a.at(1) == a.at(2) - 1) {
cout << 0 << endl;
return 0;
}
for (int(i) = 0; (i) < (2); (i)++) {
for (int j = i + 1; j < 3; j++) {
if (s.at(i).at(1) == s.at(j).at(1) &&
(a.at(i) == a.at(j) || a.at(i) == a.at(j) - 1 ||
a.at(i) == a.at(j) + 1 || a.at(i) == a.at(j) + 2 ||
a.at(i) == a.at(j) - 2)) {
cout << 1 << endl;
return 0;
}
}
}
cout << 2 << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long MOD = 1e9 + 7;
long long powmod(long long a, long long l, long long md) {
long long res = 1;
while (l) {
if (l & 1) res = res * a % md;
l /= 2;
a = a * a % md;
}
return res;
}
long long binpow(long long a, long long l) {
long long res = 1;
while (l) {
if (l & 1) res = res * a;
l /= 2;
a = a * a;
}
return res;
}
long long __set(long long b, long long i) { return b | (1 << i); }
long long __unset(long long b, long long i) { return b & (~(1UL << i)); }
long long __check(long long b, long long i) { return b & (1 << i); }
long long mulmod(long long a, long long b, long long md) {
return ((a % md) * (b % md)) % md;
}
long long addmod(long long a, long long b, long long md) {
return (a + b) % md;
}
long long submod(long long a, long long b, long long md) {
return (((a - b) % md) + md) % md;
}
long long divmod(long long a, long long b, long long md) {
return mulmod(a, powmod(b, md - 2, md), md);
}
const long long inf = 0xFFFFFFFFFFFFFFFL;
priority_queue<long long, vector<long long>, greater<long long> > pq;
clock_t time_p = clock();
void time() {
time_p = clock() - time_p;
cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n";
}
long long d[3][10];
signed main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
for (long long i = 0; i < 3; i++) {
string s;
cin >> s;
if (s[1] == 'm') {
d[0][s[0] - '0']++;
} else if (s[1] == 'p') {
d[1][s[0] - '0']++;
} else {
d[2][s[0] - '0']++;
}
}
long long mx = 3;
for (long long i = 0; i < 3; i++) {
for (long long j = 1; j <= 9; j++) mx = min(mx, 3 - min(3ll, d[i][j]));
for (long long j = 1; j <= 7; j++) {
long long tp = 0;
if (!d[i][j]) tp++;
if (!d[i][j + 1]) tp++;
if (!d[i][j + 2]) tp++;
mx = min(mx, tp);
}
}
cout << mx << "\n";
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long MOD = 1e9 + 7;
long long powmod(long long a, long long l, long long md) {
long long res = 1;
while (l) {
if (l & 1) res = res * a % md;
l /= 2;
a = a * a % md;
}
return res;
}
long long binpow(long long a, long long l) {
long long res = 1;
while (l) {
if (l & 1) res = res * a;
l /= 2;
a = a * a;
}
return res;
}
long long __set(long long b, long long i) { return b | (1 << i); }
long long __unset(long long b, long long i) { return b & (~(1UL << i)); }
long long __check(long long b, long long i) { return b & (1 << i); }
long long mulmod(long long a, long long b, long long md) {
return ((a % md) * (b % md)) % md;
}
long long addmod(long long a, long long b, long long md) {
return (a + b) % md;
}
long long submod(long long a, long long b, long long md) {
return (((a - b) % md) + md) % md;
}
long long divmod(long long a, long long b, long long md) {
return mulmod(a, powmod(b, md - 2, md), md);
}
const long long inf = 0xFFFFFFFFFFFFFFFL;
priority_queue<long long, vector<long long>, greater<long long> > pq;
clock_t time_p = clock();
void time() {
time_p = clock() - time_p;
cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n";
}
long long d[3][10];
signed main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
for (long long i = 0; i < 3; i++) {
string s;
cin >> s;
if (s[1] == 'm') {
d[0][s[0] - '0']++;
} else if (s[1] == 'p') {
d[1][s[0] - '0']++;
} else {
d[2][s[0] - '0']++;
}
}
long long mx = 3;
for (long long i = 0; i < 3; i++) {
for (long long j = 1; j <= 9; j++) mx = min(mx, 3 - min(3ll, d[i][j]));
for (long long j = 1; j <= 7; j++) {
long long tp = 0;
if (!d[i][j]) tp++;
if (!d[i][j + 1]) tp++;
if (!d[i][j + 2]) tp++;
mx = min(mx, tp);
}
}
cout << mx << "\n";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
string s[2000];
int main() {
cin >> s[0] >> s[1] >> s[2];
sort(s, s + 3);
if (s[0] == s[1] && s[1] == s[2]) {
cout << 0 << endl;
return 0;
}
if (s[0][1] == s[1][1] && s[1][1] == s[2][1]) {
if (s[1][0] - s[0][0] == 1 && s[2][0] - s[1][0] == 1) {
cout << 0 << endl;
return 0;
}
}
if (s[0] == s[1] || s[1] == s[2] || s[0] == s[2]) {
cout << 1 << endl;
return 0;
}
if (s[0][1] == s[1][1] && s[1][0] - s[0][0] <= 2 ||
s[0][1] == s[2][1] && s[2][0] - s[0][0] <= 2 ||
s[1][1] == s[2][1] && s[2][0] - s[1][0] <= 2) {
cout << 1 << endl;
return 0;
}
cout << 2 << endl;
return 0;
}
| ### Prompt
Create a solution in CPP for the following problem:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
string s[2000];
int main() {
cin >> s[0] >> s[1] >> s[2];
sort(s, s + 3);
if (s[0] == s[1] && s[1] == s[2]) {
cout << 0 << endl;
return 0;
}
if (s[0][1] == s[1][1] && s[1][1] == s[2][1]) {
if (s[1][0] - s[0][0] == 1 && s[2][0] - s[1][0] == 1) {
cout << 0 << endl;
return 0;
}
}
if (s[0] == s[1] || s[1] == s[2] || s[0] == s[2]) {
cout << 1 << endl;
return 0;
}
if (s[0][1] == s[1][1] && s[1][0] - s[0][0] <= 2 ||
s[0][1] == s[2][1] && s[2][0] - s[0][0] <= 2 ||
s[1][1] == s[2][1] && s[2][0] - s[1][0] <= 2) {
cout << 1 << endl;
return 0;
}
cout << 2 << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long i, l, j, m, n, k;
string a, b, c;
cin >> a >> b >> c;
if (a == b && b == c) {
cout << 0;
return 0;
}
if (a[1] == b[1] && b[1] == c[1]) {
long long s[3];
s[0] = a[0] - '0';
s[1] = b[0] - '0';
s[2] = c[0] - '0';
sort(s, s + 3);
if (s[0] == s[1] - 1 && s[1] == s[2] - 1) {
cout << 0;
return 0;
} else {
if (a == b || b == c || a == c) {
cout << 1;
return 0;
}
if (s[0] == s[1] - 1) {
cout << 1;
return 0;
}
if (s[0] == s[2] - 2) {
cout << 1;
return 0;
}
if (s[1] == s[2] - 1) {
cout << 1;
return 0;
}
}
}
if (a == b || b == c || a == c) {
cout << 1;
return 0;
}
if (a[1] == b[1]) {
if (max(a[0], b[0]) == min(a[0], b[0]) + 1 ||
max(a[0], b[0]) == min(a[0], b[0]) + 2) {
cout << 1;
return 0;
}
}
if (a[1] == c[1]) {
if (max(a[0], c[0]) == min(a[0], c[0]) + 1 ||
max(a[0], c[0]) == min(a[0], c[0]) + 2) {
cout << 1;
return 0;
}
}
if (b[1] == c[1]) {
if (max(b[0], c[0]) == min(b[0], c[0]) + 1 ||
max(b[0], c[0]) == min(b[0], c[0]) + 2) {
cout << 1;
return 0;
}
}
cout << 2;
}
| ### Prompt
Please create a solution in Cpp to the following problem:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
long long i, l, j, m, n, k;
string a, b, c;
cin >> a >> b >> c;
if (a == b && b == c) {
cout << 0;
return 0;
}
if (a[1] == b[1] && b[1] == c[1]) {
long long s[3];
s[0] = a[0] - '0';
s[1] = b[0] - '0';
s[2] = c[0] - '0';
sort(s, s + 3);
if (s[0] == s[1] - 1 && s[1] == s[2] - 1) {
cout << 0;
return 0;
} else {
if (a == b || b == c || a == c) {
cout << 1;
return 0;
}
if (s[0] == s[1] - 1) {
cout << 1;
return 0;
}
if (s[0] == s[2] - 2) {
cout << 1;
return 0;
}
if (s[1] == s[2] - 1) {
cout << 1;
return 0;
}
}
}
if (a == b || b == c || a == c) {
cout << 1;
return 0;
}
if (a[1] == b[1]) {
if (max(a[0], b[0]) == min(a[0], b[0]) + 1 ||
max(a[0], b[0]) == min(a[0], b[0]) + 2) {
cout << 1;
return 0;
}
}
if (a[1] == c[1]) {
if (max(a[0], c[0]) == min(a[0], c[0]) + 1 ||
max(a[0], c[0]) == min(a[0], c[0]) + 2) {
cout << 1;
return 0;
}
}
if (b[1] == c[1]) {
if (max(b[0], c[0]) == min(b[0], c[0]) + 1 ||
max(b[0], c[0]) == min(b[0], c[0]) + 2) {
cout << 1;
return 0;
}
}
cout << 2;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int inf = 1e9 + 1;
const int prime_100000000 = 5761460;
const double eps = 1e-15;
const double EPS = 1e-9;
const double PI = acos(-1.0);
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
int main() {
ios_base ::sync_with_stdio(false);
cin.tie(NULL);
string a, b, c;
cin >> a >> b >> c;
vector<string> l;
swap(a[0], a[1]);
swap(b[0], b[1]);
swap(c[0], c[1]);
l.push_back(a);
l.push_back(b);
l.push_back(c);
sort(l.begin(), l.end());
swap(a[0], a[1]);
swap(b[0], b[1]);
swap(c[0], c[1]);
if ((a[0] == b[0] && b[0] == c[0] && a[1] == b[1] && b[1] == c[1]) ||
(l[0][0] == l[1][0] && l[1][0] == l[2][0] &&
(int)l[0][1] == (int)l[1][1] - 1 && (int)l[1][1] == (int)l[2][1] - 1)) {
cout << 0;
return 0;
}
if (a[0] == b[0] && a[1] == b[1] || b[0] == c[0] && b[1] == c[1] ||
a[0] == c[0] && a[1] == c[1]) {
cout << 1;
return 0;
}
if (l[0][0] == l[1][0]) {
if (abs((int)l[0][1] - (int)l[1][1]) <= 2) {
cout << 1;
return 0;
}
}
if (l[1][0] == l[2][0]) {
if (abs((int)l[2][1] - (int)l[1][1]) <= 2) {
cout << 1;
return 0;
}
}
cout << 2;
return 0;
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int inf = 1e9 + 1;
const int prime_100000000 = 5761460;
const double eps = 1e-15;
const double EPS = 1e-9;
const double PI = acos(-1.0);
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
int main() {
ios_base ::sync_with_stdio(false);
cin.tie(NULL);
string a, b, c;
cin >> a >> b >> c;
vector<string> l;
swap(a[0], a[1]);
swap(b[0], b[1]);
swap(c[0], c[1]);
l.push_back(a);
l.push_back(b);
l.push_back(c);
sort(l.begin(), l.end());
swap(a[0], a[1]);
swap(b[0], b[1]);
swap(c[0], c[1]);
if ((a[0] == b[0] && b[0] == c[0] && a[1] == b[1] && b[1] == c[1]) ||
(l[0][0] == l[1][0] && l[1][0] == l[2][0] &&
(int)l[0][1] == (int)l[1][1] - 1 && (int)l[1][1] == (int)l[2][1] - 1)) {
cout << 0;
return 0;
}
if (a[0] == b[0] && a[1] == b[1] || b[0] == c[0] && b[1] == c[1] ||
a[0] == c[0] && a[1] == c[1]) {
cout << 1;
return 0;
}
if (l[0][0] == l[1][0]) {
if (abs((int)l[0][1] - (int)l[1][1]) <= 2) {
cout << 1;
return 0;
}
}
if (l[1][0] == l[2][0]) {
if (abs((int)l[2][1] - (int)l[1][1]) <= 2) {
cout << 1;
return 0;
}
}
cout << 2;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
vector<string> v(3);
map<string, int> m;
for (int i = 0; i < 3; i++) cin >> v[i];
m[v[0]] = 1;
m[v[1]] = 1;
m[v[2]] = 1;
vector<int> ans;
for (int i = 0; i < 3; i++) {
int temp = 2;
for (int j = 0; j < 3; j++) {
if (i != j and v[i] == v[j]) temp--;
}
ans.push_back(temp);
}
string bb = "spm";
for (int i = 0; i < 3; i++) {
vector<string> gg;
for (int j = 1; j <= 9; j++) {
string kb = "";
kb += char(j + '0');
kb += bb[i];
gg.push_back(kb);
}
for (int j = 0; j <= 6; j++) {
int temp = 3;
for (int k = j; k < j + 3; k++) temp -= m[gg[k]];
ans.push_back(temp);
}
}
sort(ans.begin(), ans.end());
cout << ans[0];
return 0;
}
| ### Prompt
In Cpp, your task is to solve the following problem:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
vector<string> v(3);
map<string, int> m;
for (int i = 0; i < 3; i++) cin >> v[i];
m[v[0]] = 1;
m[v[1]] = 1;
m[v[2]] = 1;
vector<int> ans;
for (int i = 0; i < 3; i++) {
int temp = 2;
for (int j = 0; j < 3; j++) {
if (i != j and v[i] == v[j]) temp--;
}
ans.push_back(temp);
}
string bb = "spm";
for (int i = 0; i < 3; i++) {
vector<string> gg;
for (int j = 1; j <= 9; j++) {
string kb = "";
kb += char(j + '0');
kb += bb[i];
gg.push_back(kb);
}
for (int j = 0; j <= 6; j++) {
int temp = 3;
for (int k = j; k < j + 3; k++) temp -= m[gg[k]];
ans.push_back(temp);
}
}
sort(ans.begin(), ans.end());
cout << ans[0];
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s[3];
cin >> s[0] >> s[1] >> s[2];
if (s[0][0] > s[1][0]) swap(s[0], s[1]);
if (s[0][0] > s[2][0]) swap(s[0], s[2]);
if (s[1][0] > s[2][0]) swap(s[2], s[1]);
if (s[0] == s[1] and s[1] == s[2]) {
cout << 0;
return 0;
} else if (s[0] == s[1] or s[1] == s[2]) {
cout << 1;
return 0;
} else if (s[0][1] == s[1][1] and s[1][1] == s[2][1]) {
if (s[2][0] - s[1][0] == 1 and s[1][0] - s[0][0] == 1) {
cout << 0;
return 0;
} else if (s[2][0] - s[1][0] == 1 or s[1][0] - s[0][0] == 1) {
cout << 1;
return 0;
} else if (s[2][0] - s[1][0] == 2 or s[1][0] - s[0][0] == 2) {
cout << 1;
return 0;
} else {
cout << 2;
return 0;
}
} else if (s[0][1] == s[1][1]) {
int temp = abs(s[0][0] - s[1][0]);
if (temp == 1 or temp == 2) {
cout << 1;
return 0;
} else {
cout << 2;
return 0;
}
} else if (s[1][1] == s[2][1]) {
int temp = abs(s[1][0] - s[2][0]);
if (temp == 1 or temp == 2) {
cout << 1;
return 0;
} else {
cout << 2;
return 0;
}
} else if (s[0][1] == s[2][1]) {
int temp = abs(s[0][0] - s[2][0]);
if (temp == 1 or temp == 2) {
cout << 1;
return 0;
} else {
cout << 2;
return 0;
}
} else {
cout << 2;
return 0;
}
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
string s[3];
cin >> s[0] >> s[1] >> s[2];
if (s[0][0] > s[1][0]) swap(s[0], s[1]);
if (s[0][0] > s[2][0]) swap(s[0], s[2]);
if (s[1][0] > s[2][0]) swap(s[2], s[1]);
if (s[0] == s[1] and s[1] == s[2]) {
cout << 0;
return 0;
} else if (s[0] == s[1] or s[1] == s[2]) {
cout << 1;
return 0;
} else if (s[0][1] == s[1][1] and s[1][1] == s[2][1]) {
if (s[2][0] - s[1][0] == 1 and s[1][0] - s[0][0] == 1) {
cout << 0;
return 0;
} else if (s[2][0] - s[1][0] == 1 or s[1][0] - s[0][0] == 1) {
cout << 1;
return 0;
} else if (s[2][0] - s[1][0] == 2 or s[1][0] - s[0][0] == 2) {
cout << 1;
return 0;
} else {
cout << 2;
return 0;
}
} else if (s[0][1] == s[1][1]) {
int temp = abs(s[0][0] - s[1][0]);
if (temp == 1 or temp == 2) {
cout << 1;
return 0;
} else {
cout << 2;
return 0;
}
} else if (s[1][1] == s[2][1]) {
int temp = abs(s[1][0] - s[2][0]);
if (temp == 1 or temp == 2) {
cout << 1;
return 0;
} else {
cout << 2;
return 0;
}
} else if (s[0][1] == s[2][1]) {
int temp = abs(s[0][0] - s[2][0]);
if (temp == 1 or temp == 2) {
cout << 1;
return 0;
} else {
cout << 2;
return 0;
}
} else {
cout << 2;
return 0;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10000;
int a[3];
int main() {
string s;
for (int i = 0; i < 3; ++i) {
cin >> s;
a[i] = (s[0] - '0') + (s[1] - 'a') * 100;
}
sort(a, a + 3);
int t1 = a[1] - a[0], t2 = a[2] - a[1];
if (t1 == t2 && (t1 == 1 || t1 == 0))
puts("0");
else if (t1 == 0 || t1 == 1 || t1 == 2 || t2 == 0 || t2 == 1 || t2 == 2)
puts("1");
else
puts("2");
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10000;
int a[3];
int main() {
string s;
for (int i = 0; i < 3; ++i) {
cin >> s;
a[i] = (s[0] - '0') + (s[1] - 'a') * 100;
}
sort(a, a + 3);
int t1 = a[1] - a[0], t2 = a[2] - a[1];
if (t1 == t2 && (t1 == 1 || t1 == 0))
puts("0");
else if (t1 == 0 || t1 == 1 || t1 == 2 || t2 == 0 || t2 == 1 || t2 == 2)
puts("1");
else
puts("2");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
char s1[20], s2[20], s3[20];
int ques(char a, char b, char c) {
if ((a - b == 1 && b - c == 1) || (a - c == 1 && c - b == 1) ||
(b - c == 1 && c - a == 1) || (b - a == 1 && a - c == 1) ||
(c - a == 1 && a - b == 1) || (c - b == 1 && b - a == 1))
return 1;
else
return 0;
}
int xiangtong3(char s[], char ss[], char sss[]) {
if (strcmp(s, ss) == 0 && strcmp(ss, sss) == 0)
return 1;
else
return 0;
}
int xiangtong2(char s[], char ss[], char sss[]) {
if (strcmp(s, ss) == 0 || strcmp(ss, sss) == 0 || strcmp(s, sss) == 0)
return 1;
else
return 0;
}
int lianxu3(char s[], char ss[], char sss[]) {
if (s[1] == ss[1] && ss[1] == sss[1] && ques(ss[0], s[0], sss[0])) {
return 1;
} else
return 0;
}
int lianxu2(char s[], char ss[], char sss[]) {
if (s[1] == ss[1] && ((abs(s[0] - ss[0]) == 1) || (abs(s[0] - ss[0]) == 2)))
return 1;
if (s[1] == sss[1] &&
((abs(s[0] - sss[0]) == 1) || (abs(s[0] - sss[0]) == 2)))
return 1;
if (((abs(ss[0] - sss[0]) == 1) || (abs(ss[0] - sss[0]) == 2)) &&
(ss[1] == sss[1]))
return 1;
return 0;
}
int main() {
while (cin >> s1 >> s2 >> s3) {
if (lianxu3(s1, s2, s3) || xiangtong3(s1, s2, s3))
printf("0\n");
else if (lianxu2(s1, s2, s3) || xiangtong2(s1, s2, s3))
printf("1\n");
else
printf("2\n");
}
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
char s1[20], s2[20], s3[20];
int ques(char a, char b, char c) {
if ((a - b == 1 && b - c == 1) || (a - c == 1 && c - b == 1) ||
(b - c == 1 && c - a == 1) || (b - a == 1 && a - c == 1) ||
(c - a == 1 && a - b == 1) || (c - b == 1 && b - a == 1))
return 1;
else
return 0;
}
int xiangtong3(char s[], char ss[], char sss[]) {
if (strcmp(s, ss) == 0 && strcmp(ss, sss) == 0)
return 1;
else
return 0;
}
int xiangtong2(char s[], char ss[], char sss[]) {
if (strcmp(s, ss) == 0 || strcmp(ss, sss) == 0 || strcmp(s, sss) == 0)
return 1;
else
return 0;
}
int lianxu3(char s[], char ss[], char sss[]) {
if (s[1] == ss[1] && ss[1] == sss[1] && ques(ss[0], s[0], sss[0])) {
return 1;
} else
return 0;
}
int lianxu2(char s[], char ss[], char sss[]) {
if (s[1] == ss[1] && ((abs(s[0] - ss[0]) == 1) || (abs(s[0] - ss[0]) == 2)))
return 1;
if (s[1] == sss[1] &&
((abs(s[0] - sss[0]) == 1) || (abs(s[0] - sss[0]) == 2)))
return 1;
if (((abs(ss[0] - sss[0]) == 1) || (abs(ss[0] - sss[0]) == 2)) &&
(ss[1] == sss[1]))
return 1;
return 0;
}
int main() {
while (cin >> s1 >> s2 >> s3) {
if (lianxu3(s1, s2, s3) || xiangtong3(s1, s2, s3))
printf("0\n");
else if (lianxu2(s1, s2, s3) || xiangtong2(s1, s2, s3))
printf("1\n");
else
printf("2\n");
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
string a, b, c;
cin >> a >> b >> c;
char x = '1';
char y[3] = {'m', 'p', 's'};
map<string, int> ump;
for (int i = 0; i < 3; i++) {
string make = "";
make += y[i];
for (int j = 1; j <= 9; j++) {
make = (char)(j + '0') + make;
ump[make] = 0;
make = y[i];
}
x = '1';
}
ump[a]++;
ump[b]++;
ump[c]++;
for (auto uj : ump) {
if (uj.second == 3) {
cout << "0" << endl;
exit(0);
}
}
for (int i = 1; i <= 9; i++) {
if (i > 7) break;
string make = "";
make += (char)(i + '0');
string make1 = "";
make1 += (char)(i + 1 + '0');
string make2 = "";
make2 += (char)(i + 2 + '0');
for (int j = 0; j < 3; j++) {
make += y[j];
make1 += y[j];
make2 += y[j];
if (ump[make] && ump[make1] && ump[make2]) {
cout << "0" << endl;
exit(0);
}
make = (char)(i + '0');
make1 = (char)(i + 1 + '0');
make2 = (char)(i + 2 + '0');
}
}
int ans = min(3 - ump[a], 3 - ump[b]);
ans = min(ans, 3 - ump[c]);
int ans2 = INT_MAX;
for (int i = 1; i <= 9; i++) {
if (i > 7) break;
string make = "";
make += (char)(i + '0');
string make1 = "";
make1 += (char)(i + 1 + '0');
string make2 = "";
make2 += (char)(i + 2 + '0');
for (int j = 0; j < 3; j++) {
make += y[j];
make1 += y[j];
make2 += y[j];
int zz = 0;
if (ump[make]) zz++;
if (ump[make1]) zz++;
if (ump[make2]) zz++;
ans2 = min(3 - zz, ans2);
make = (char)(i + '0');
make1 = (char)(i + 1 + '0');
make2 = (char)(i + 2 + '0');
}
}
cout << min(ans, ans2) << endl;
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
string a, b, c;
cin >> a >> b >> c;
char x = '1';
char y[3] = {'m', 'p', 's'};
map<string, int> ump;
for (int i = 0; i < 3; i++) {
string make = "";
make += y[i];
for (int j = 1; j <= 9; j++) {
make = (char)(j + '0') + make;
ump[make] = 0;
make = y[i];
}
x = '1';
}
ump[a]++;
ump[b]++;
ump[c]++;
for (auto uj : ump) {
if (uj.second == 3) {
cout << "0" << endl;
exit(0);
}
}
for (int i = 1; i <= 9; i++) {
if (i > 7) break;
string make = "";
make += (char)(i + '0');
string make1 = "";
make1 += (char)(i + 1 + '0');
string make2 = "";
make2 += (char)(i + 2 + '0');
for (int j = 0; j < 3; j++) {
make += y[j];
make1 += y[j];
make2 += y[j];
if (ump[make] && ump[make1] && ump[make2]) {
cout << "0" << endl;
exit(0);
}
make = (char)(i + '0');
make1 = (char)(i + 1 + '0');
make2 = (char)(i + 2 + '0');
}
}
int ans = min(3 - ump[a], 3 - ump[b]);
ans = min(ans, 3 - ump[c]);
int ans2 = INT_MAX;
for (int i = 1; i <= 9; i++) {
if (i > 7) break;
string make = "";
make += (char)(i + '0');
string make1 = "";
make1 += (char)(i + 1 + '0');
string make2 = "";
make2 += (char)(i + 2 + '0');
for (int j = 0; j < 3; j++) {
make += y[j];
make1 += y[j];
make2 += y[j];
int zz = 0;
if (ump[make]) zz++;
if (ump[make1]) zz++;
if (ump[make2]) zz++;
ans2 = min(3 - zz, ans2);
make = (char)(i + '0');
make1 = (char)(i + 1 + '0');
make2 = (char)(i + 2 + '0');
}
}
cout << min(ans, ans2) << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int x, d, e, f;
string a, b, c;
cin >> a >> b >> c;
d = a[0] - '0';
e = b[0] - '0';
f = c[0] - '0';
if (a == b && b == c && c == a) {
x = 0;
} else if ((a == b) || a == c || b == c) {
x = 1;
} else if ((a[1] == b[1] && abs(d - e) <= 2) &&
(a[1] == c[1] && abs(d - f) <= 2) &&
(b[1] == c[1] && abs(e - f) <= 2)) {
x = 0;
} else if ((a[1] == b[1] && abs(d - e) <= 2) ||
(a[1] == c[1] && abs(d - f) <= 2) ||
(b[1] == c[1] && abs(e - f) <= 2)) {
x = 1;
} else {
x = 2;
}
cout << x << endl;
return 0;
}
| ### Prompt
In cpp, your task is to solve the following problem:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int x, d, e, f;
string a, b, c;
cin >> a >> b >> c;
d = a[0] - '0';
e = b[0] - '0';
f = c[0] - '0';
if (a == b && b == c && c == a) {
x = 0;
} else if ((a == b) || a == c || b == c) {
x = 1;
} else if ((a[1] == b[1] && abs(d - e) <= 2) &&
(a[1] == c[1] && abs(d - f) <= 2) &&
(b[1] == c[1] && abs(e - f) <= 2)) {
x = 0;
} else if ((a[1] == b[1] && abs(d - e) <= 2) ||
(a[1] == c[1] && abs(d - f) <= 2) ||
(b[1] == c[1] && abs(e - f) <= 2)) {
x = 1;
} else {
x = 2;
}
cout << x << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s[3];
map<string, long long> map1;
map<char, long long> map2;
map2['m'] = 0, map2['p'] = 1, map2['s'] = 2;
long long a[10][3] = {};
for (int i = 0; i < 3; i++) {
cin >> s[i];
map1[s[i]]++;
a[s[i][0] - '0'][map2[s[i][1]]] = 1;
}
for (int i = 0; i < 3; i++) {
for (int j = 1; j < 10; j++) {
a[j][i] += a[j - 1][i];
}
}
long long count1 = 0;
for (int i = 0; i < 3; i++) {
for (int j = 3; j < 10; j++) {
count1 = max(count1, a[j][i] - a[j - 3][i]);
}
}
map<string, long long>::iterator itr;
long long max1 = 1;
for (itr = map1.begin(); itr != map1.end(); itr++) {
max1 = max(max1, itr->second);
}
cout << 3 - max(max1, count1);
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
string s[3];
map<string, long long> map1;
map<char, long long> map2;
map2['m'] = 0, map2['p'] = 1, map2['s'] = 2;
long long a[10][3] = {};
for (int i = 0; i < 3; i++) {
cin >> s[i];
map1[s[i]]++;
a[s[i][0] - '0'][map2[s[i][1]]] = 1;
}
for (int i = 0; i < 3; i++) {
for (int j = 1; j < 10; j++) {
a[j][i] += a[j - 1][i];
}
}
long long count1 = 0;
for (int i = 0; i < 3; i++) {
for (int j = 3; j < 10; j++) {
count1 = max(count1, a[j][i] - a[j - 3][i]);
}
}
map<string, long long>::iterator itr;
long long max1 = 1;
for (itr = map1.begin(); itr != map1.end(); itr++) {
max1 = max(max1, itr->second);
}
cout << 3 - max(max1, count1);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void putitin(string ss, vector<int> &s, vector<int> &m, vector<int> &p) {
int ele = (int)ss[0] - (int)'0';
if (ss[1] == 's') {
s.push_back(ele);
}
if (ss[1] == 'm') {
m.push_back(ele);
}
if (ss[1] == 'p') {
p.push_back(ele);
}
}
void solve() {
string s1, s2, s3;
cin >> s1 >> s2 >> s3;
vector<int> s;
vector<int> m;
vector<int> p;
putitin(s1, s, m, p);
putitin(s2, s, m, p);
putitin(s3, s, m, p);
sort(s.begin(), s.end());
sort(m.begin(), m.end());
sort(p.begin(), p.end());
if (s.size() == 3) {
sort(s.begin(), s.end());
bool identical = true;
bool sequential = true;
int id = 0;
int sq = 0;
for (int i = 0; i < s.size() - 1; i++) {
if (s[i] != s[i + 1]) {
identical = false;
} else {
id++;
}
}
for (int i = 0; i < s.size() - 1; i++) {
if (s[i] + 1 != s[i + 1]) {
sequential = false;
} else {
sq++;
}
}
if (identical || sequential) {
cout << 0 << endl;
} else if (id || sq) {
cout << 1 << endl;
} else if ((s[0] + 2 == s[1]) || (s[1] + 2 == s[2])) {
cout << 1 << endl;
} else {
cout << 2 << endl;
}
} else if (m.size() == 3) {
sort(m.begin(), m.end());
bool identical = true;
bool sequential = true;
int id = 0;
int sq = 0;
for (int i = 0; i < m.size() - 1; i++) {
if (m[i] != m[i + 1]) {
identical = false;
} else {
id++;
}
}
for (int i = 0; i < m.size() - 1; i++) {
if (m[i] + 1 != m[i + 1]) {
sequential = false;
} else {
sq++;
}
}
if (identical || sequential) {
cout << 0 << endl;
} else if (id || sq) {
cout << 1 << endl;
} else if ((m[0] + 2 == m[1]) || (m[1] + 2 == m[2])) {
cout << 1 << endl;
} else {
cout << 2 << endl;
}
} else if (p.size() == 3) {
sort(p.begin(), p.end());
bool identical = true;
bool sequential = true;
int id = 0;
int sq = 0;
for (int i = 0; i < p.size() - 1; i++) {
if (p[i] != p[i + 1]) {
identical = false;
} else {
id++;
}
}
for (int i = 0; i < p.size() - 1; i++) {
if (p[i] + 1 != p[i + 1]) {
sequential = false;
} else {
sq++;
}
}
if (identical || sequential) {
cout << 0 << endl;
} else if (id || sq) {
cout << 1 << endl;
} else if ((p[0] + 2 == p[1]) || (p[1] + 2 == p[2])) {
cout << 1 << endl;
} else {
cout << 2 << endl;
}
} else if (s.size() == 2) {
if ((s[0] == s[1]) || (s[0] + 1 == s[1])) {
cout << 1 << endl;
} else if ((s[0] + 2 == s[1])) {
cout << 1 << endl;
} else {
cout << 2 << endl;
}
} else if (m.size() == 2) {
if ((m[0] == m[1]) || (m[0] + 1 == m[1])) {
cout << 1 << endl;
} else if ((m[0] + 2 == m[1])) {
cout << 1 << endl;
} else {
cout << 2 << endl;
}
} else if (p.size() == 2) {
if ((p[0] == p[1]) || (p[0] + 1 == p[1])) {
cout << 1 << endl;
} else if ((p[0] + 2 == p[1])) {
cout << 1 << endl;
} else {
cout << 2 << endl;
}
} else {
cout << 2 << endl;
}
}
int main() {
int t = 1;
while (t--) {
solve();
}
}
| ### Prompt
In Cpp, your task is to solve the following problem:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void putitin(string ss, vector<int> &s, vector<int> &m, vector<int> &p) {
int ele = (int)ss[0] - (int)'0';
if (ss[1] == 's') {
s.push_back(ele);
}
if (ss[1] == 'm') {
m.push_back(ele);
}
if (ss[1] == 'p') {
p.push_back(ele);
}
}
void solve() {
string s1, s2, s3;
cin >> s1 >> s2 >> s3;
vector<int> s;
vector<int> m;
vector<int> p;
putitin(s1, s, m, p);
putitin(s2, s, m, p);
putitin(s3, s, m, p);
sort(s.begin(), s.end());
sort(m.begin(), m.end());
sort(p.begin(), p.end());
if (s.size() == 3) {
sort(s.begin(), s.end());
bool identical = true;
bool sequential = true;
int id = 0;
int sq = 0;
for (int i = 0; i < s.size() - 1; i++) {
if (s[i] != s[i + 1]) {
identical = false;
} else {
id++;
}
}
for (int i = 0; i < s.size() - 1; i++) {
if (s[i] + 1 != s[i + 1]) {
sequential = false;
} else {
sq++;
}
}
if (identical || sequential) {
cout << 0 << endl;
} else if (id || sq) {
cout << 1 << endl;
} else if ((s[0] + 2 == s[1]) || (s[1] + 2 == s[2])) {
cout << 1 << endl;
} else {
cout << 2 << endl;
}
} else if (m.size() == 3) {
sort(m.begin(), m.end());
bool identical = true;
bool sequential = true;
int id = 0;
int sq = 0;
for (int i = 0; i < m.size() - 1; i++) {
if (m[i] != m[i + 1]) {
identical = false;
} else {
id++;
}
}
for (int i = 0; i < m.size() - 1; i++) {
if (m[i] + 1 != m[i + 1]) {
sequential = false;
} else {
sq++;
}
}
if (identical || sequential) {
cout << 0 << endl;
} else if (id || sq) {
cout << 1 << endl;
} else if ((m[0] + 2 == m[1]) || (m[1] + 2 == m[2])) {
cout << 1 << endl;
} else {
cout << 2 << endl;
}
} else if (p.size() == 3) {
sort(p.begin(), p.end());
bool identical = true;
bool sequential = true;
int id = 0;
int sq = 0;
for (int i = 0; i < p.size() - 1; i++) {
if (p[i] != p[i + 1]) {
identical = false;
} else {
id++;
}
}
for (int i = 0; i < p.size() - 1; i++) {
if (p[i] + 1 != p[i + 1]) {
sequential = false;
} else {
sq++;
}
}
if (identical || sequential) {
cout << 0 << endl;
} else if (id || sq) {
cout << 1 << endl;
} else if ((p[0] + 2 == p[1]) || (p[1] + 2 == p[2])) {
cout << 1 << endl;
} else {
cout << 2 << endl;
}
} else if (s.size() == 2) {
if ((s[0] == s[1]) || (s[0] + 1 == s[1])) {
cout << 1 << endl;
} else if ((s[0] + 2 == s[1])) {
cout << 1 << endl;
} else {
cout << 2 << endl;
}
} else if (m.size() == 2) {
if ((m[0] == m[1]) || (m[0] + 1 == m[1])) {
cout << 1 << endl;
} else if ((m[0] + 2 == m[1])) {
cout << 1 << endl;
} else {
cout << 2 << endl;
}
} else if (p.size() == 2) {
if ((p[0] == p[1]) || (p[0] + 1 == p[1])) {
cout << 1 << endl;
} else if ((p[0] + 2 == p[1])) {
cout << 1 << endl;
} else {
cout << 2 << endl;
}
} else {
cout << 2 << endl;
}
}
int main() {
int t = 1;
while (t--) {
solve();
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<string> v;
for (int i = 0; i < 3; i++) {
string s;
cin >> s;
v.push_back(s);
}
vector<string> so;
vector<string> po;
vector<string> mo;
for (int i = 0; i < v.size(); i++) {
if (v[i][1] == 's')
so.push_back(v[i]);
else if (v[i][1] == 'p')
po.push_back(v[i]);
else if (v[i][1] == 'm')
mo.push_back(v[i]);
}
sort(so.begin(), so.end());
sort(po.begin(), po.end());
sort(mo.begin(), mo.end());
if (v[0] == v[1] && v[1] == v[2])
cout << 0 << endl;
else if (v[0] == v[1] || v[0] == v[2] || v[1] == v[2])
cout << 1 << endl;
else if (so.size() == 3) {
int m1 = (so[2][0] - '0') - (so[1][0] - '0');
int m2 = (so[1][0] - '0') - (so[0][0] - '0');
if (m1 == 1 && m2 == 1)
cout << "0" << endl;
else if (m1 <= 2 || m2 <= 2)
cout << "1" << endl;
else
cout << "2" << endl;
} else if (mo.size() == 3) {
int m1 = (mo[2][0] - '0') - (mo[1][0] - '0');
int m2 = (mo[1][0] - '0') - (mo[0][0] - '0');
if (m1 == 1 && m2 == 1)
cout << "0" << endl;
else if (m1 <= 2 || m2 <= 2)
cout << "1" << endl;
else
cout << "2" << endl;
} else if (po.size() == 3) {
int m1 = (po[2][0] - '0') - (po[1][0] - '0');
int m2 = (po[1][0] - '0') - (po[0][0] - '0');
if (m1 == 1 && m2 == 1)
cout << "0" << endl;
else if (m1 <= 2 || m2 <= 2)
cout << "1" << endl;
else
cout << "2" << endl;
} else if (so.size() == 2) {
int m1 = (so[1][0] - '0') - (so[0][0] - '0');
if (m1 == 1 || m1 == 2)
cout << "1" << endl;
else
cout << "2\n";
} else if (mo.size() == 2) {
int m1 = (mo[1][0] - '0') - (mo[0][0] - '0');
if (m1 == 1 || m1 == 2)
cout << "1" << endl;
else
cout << "2\n";
} else if (po.size() == 2) {
int m1 = (po[1][0] - '0') - (po[0][0] - '0');
if (m1 == 1 || m1 == 2)
cout << "1" << endl;
else
cout << "2\n";
} else
cout << "2\n";
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<string> v;
for (int i = 0; i < 3; i++) {
string s;
cin >> s;
v.push_back(s);
}
vector<string> so;
vector<string> po;
vector<string> mo;
for (int i = 0; i < v.size(); i++) {
if (v[i][1] == 's')
so.push_back(v[i]);
else if (v[i][1] == 'p')
po.push_back(v[i]);
else if (v[i][1] == 'm')
mo.push_back(v[i]);
}
sort(so.begin(), so.end());
sort(po.begin(), po.end());
sort(mo.begin(), mo.end());
if (v[0] == v[1] && v[1] == v[2])
cout << 0 << endl;
else if (v[0] == v[1] || v[0] == v[2] || v[1] == v[2])
cout << 1 << endl;
else if (so.size() == 3) {
int m1 = (so[2][0] - '0') - (so[1][0] - '0');
int m2 = (so[1][0] - '0') - (so[0][0] - '0');
if (m1 == 1 && m2 == 1)
cout << "0" << endl;
else if (m1 <= 2 || m2 <= 2)
cout << "1" << endl;
else
cout << "2" << endl;
} else if (mo.size() == 3) {
int m1 = (mo[2][0] - '0') - (mo[1][0] - '0');
int m2 = (mo[1][0] - '0') - (mo[0][0] - '0');
if (m1 == 1 && m2 == 1)
cout << "0" << endl;
else if (m1 <= 2 || m2 <= 2)
cout << "1" << endl;
else
cout << "2" << endl;
} else if (po.size() == 3) {
int m1 = (po[2][0] - '0') - (po[1][0] - '0');
int m2 = (po[1][0] - '0') - (po[0][0] - '0');
if (m1 == 1 && m2 == 1)
cout << "0" << endl;
else if (m1 <= 2 || m2 <= 2)
cout << "1" << endl;
else
cout << "2" << endl;
} else if (so.size() == 2) {
int m1 = (so[1][0] - '0') - (so[0][0] - '0');
if (m1 == 1 || m1 == 2)
cout << "1" << endl;
else
cout << "2\n";
} else if (mo.size() == 2) {
int m1 = (mo[1][0] - '0') - (mo[0][0] - '0');
if (m1 == 1 || m1 == 2)
cout << "1" << endl;
else
cout << "2\n";
} else if (po.size() == 2) {
int m1 = (po[1][0] - '0') - (po[0][0] - '0');
if (m1 == 1 || m1 == 2)
cout << "1" << endl;
else
cout << "2\n";
} else
cout << "2\n";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long MX5 = 3 * 1e5 + 3;
const long long MX6 = 1 * 1e6 + 37;
const long long INF = 8e18;
const long long MOD = 1e9 + 7;
const long long MOD2 = 998244353;
long long power(long long a, long long b, long long md) {
return (!b ? 1
: (b & 1 ? a * power(a * a % md, b / 2, md) % md
: power(a * a % md, b / 2, md) % md));
}
long long bmm(long long a, long long b) {
return (a % b == 0 ? b : bmm(b, a % b));
}
long long A[MX5];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string a, b, c;
cin >> a >> b >> c;
int a1 = a[0] - '0';
int a2 = b[0] - '0';
int a3 = c[0] - '0';
if (a == b && b == c) {
cout << 0;
return 0;
}
if (a[1] == b[1]) {
if (b[1] == c[1]) {
if (a1 == a2 && a2 == a3) {
cout << 0;
return 0;
}
if (min({a1, a2, a3}) + 2 == max({a1, a2, a3}) && a1 != a2 && a2 != a3 &&
a3 != a1) {
cout << 0;
return 0;
}
if (a1 == a2 || a2 == a3 || a1 == a3) {
cout << 1;
return 0;
}
if (min({abs(a1 - a2), abs(a1 - a3), abs(a2 - a3)}) == 1 ||
min({abs(a1 - a2), abs(a1 - a3), abs(a2 - a3)}) == 2) {
cout << 1;
return 0;
}
cout << 2;
return 0;
} else {
if (a1 == a2) {
cout << 1;
return 0;
}
if (abs(a1 - a2) == 1 || abs(a1 - a2) == 2) {
cout << 1;
return 0;
}
cout << 2;
return 0;
}
}
if (a[1] == c[1]) {
if (a1 == a3) {
cout << 1;
return 0;
}
if (abs(a1 - a3) == 1 || abs(a1 - a3) == 2) {
cout << 1;
return 0;
}
cout << 2;
return 0;
} else if (b[1] == c[1]) {
if (a3 == a2) {
cout << 1;
return 0;
}
if (abs(a3 - a2) == 1 || abs(a3 - a2) == 2) {
cout << 1;
return 0;
}
cout << 2;
return 0;
} else {
cout << 2;
}
return 0;
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MX5 = 3 * 1e5 + 3;
const long long MX6 = 1 * 1e6 + 37;
const long long INF = 8e18;
const long long MOD = 1e9 + 7;
const long long MOD2 = 998244353;
long long power(long long a, long long b, long long md) {
return (!b ? 1
: (b & 1 ? a * power(a * a % md, b / 2, md) % md
: power(a * a % md, b / 2, md) % md));
}
long long bmm(long long a, long long b) {
return (a % b == 0 ? b : bmm(b, a % b));
}
long long A[MX5];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string a, b, c;
cin >> a >> b >> c;
int a1 = a[0] - '0';
int a2 = b[0] - '0';
int a3 = c[0] - '0';
if (a == b && b == c) {
cout << 0;
return 0;
}
if (a[1] == b[1]) {
if (b[1] == c[1]) {
if (a1 == a2 && a2 == a3) {
cout << 0;
return 0;
}
if (min({a1, a2, a3}) + 2 == max({a1, a2, a3}) && a1 != a2 && a2 != a3 &&
a3 != a1) {
cout << 0;
return 0;
}
if (a1 == a2 || a2 == a3 || a1 == a3) {
cout << 1;
return 0;
}
if (min({abs(a1 - a2), abs(a1 - a3), abs(a2 - a3)}) == 1 ||
min({abs(a1 - a2), abs(a1 - a3), abs(a2 - a3)}) == 2) {
cout << 1;
return 0;
}
cout << 2;
return 0;
} else {
if (a1 == a2) {
cout << 1;
return 0;
}
if (abs(a1 - a2) == 1 || abs(a1 - a2) == 2) {
cout << 1;
return 0;
}
cout << 2;
return 0;
}
}
if (a[1] == c[1]) {
if (a1 == a3) {
cout << 1;
return 0;
}
if (abs(a1 - a3) == 1 || abs(a1 - a3) == 2) {
cout << 1;
return 0;
}
cout << 2;
return 0;
} else if (b[1] == c[1]) {
if (a3 == a2) {
cout << 1;
return 0;
}
if (abs(a3 - a2) == 1 || abs(a3 - a2) == 2) {
cout << 1;
return 0;
}
cout << 2;
return 0;
} else {
cout << 2;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int k, ans;
int a[4];
string s1, s2, s3;
map<string, int> fix;
int main() {
cin >> s1 >> s2 >> s3;
fix[s1]++;
fix[s2]++;
fix[s3]++;
ans = min(min((3 - fix[s2]), (3 - fix[s3])), (3 - fix[s1]));
if (s1[1] == s2[1] && s1[1] == s3[1]) {
a[1] = s1[0];
a[2] = s2[0];
a[3] = s3[0];
sort(a + 1, a + 3 + 1);
if (a[2] - a[1] == 1 && a[3] - a[2] == 1) {
cout << 0 << endl;
return 0;
}
if (abs(int(s1[0]) - int(s2[0])) == 2 || abs(int(s1[0]) - int(s2[0])) == 1)
ans = min(ans, 1);
if (abs(int(s1[0]) - int(s3[0])) == 2 || abs(int(s1[0]) - int(s3[0])) == 1)
ans = min(ans, 1);
if (abs(int(s3[0]) - int(s2[0])) == 2 || abs(int(s3[0]) - int(s2[0])) == 1)
ans = min(ans, 1);
cout << ans << endl;
return 0;
}
if (s1[1] == s2[1]) {
if (abs(int(s1[0]) - int(s2[0])) == 2 || abs(int(s1[0]) - int(s2[0])) == 1)
ans = min(ans, 1);
cout << ans << endl;
return 0;
}
if (s1[1] == s3[1]) {
if (abs(int(s1[0]) - int(s3[0])) == 2 || abs(int(s1[0]) - int(s3[0])) == 1)
ans = min(ans, 1);
cout << ans << endl;
return 0;
}
if (s2[1] == s3[1]) {
if (abs(int(s2[0]) - int(s3[0])) == 2 || abs(int(s2[0]) - int(s3[0])) == 1)
ans = min(ans, 1);
cout << ans << endl;
return 0;
}
cout << ans << endl;
}
| ### Prompt
Please create a solution in cpp to the following problem:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int k, ans;
int a[4];
string s1, s2, s3;
map<string, int> fix;
int main() {
cin >> s1 >> s2 >> s3;
fix[s1]++;
fix[s2]++;
fix[s3]++;
ans = min(min((3 - fix[s2]), (3 - fix[s3])), (3 - fix[s1]));
if (s1[1] == s2[1] && s1[1] == s3[1]) {
a[1] = s1[0];
a[2] = s2[0];
a[3] = s3[0];
sort(a + 1, a + 3 + 1);
if (a[2] - a[1] == 1 && a[3] - a[2] == 1) {
cout << 0 << endl;
return 0;
}
if (abs(int(s1[0]) - int(s2[0])) == 2 || abs(int(s1[0]) - int(s2[0])) == 1)
ans = min(ans, 1);
if (abs(int(s1[0]) - int(s3[0])) == 2 || abs(int(s1[0]) - int(s3[0])) == 1)
ans = min(ans, 1);
if (abs(int(s3[0]) - int(s2[0])) == 2 || abs(int(s3[0]) - int(s2[0])) == 1)
ans = min(ans, 1);
cout << ans << endl;
return 0;
}
if (s1[1] == s2[1]) {
if (abs(int(s1[0]) - int(s2[0])) == 2 || abs(int(s1[0]) - int(s2[0])) == 1)
ans = min(ans, 1);
cout << ans << endl;
return 0;
}
if (s1[1] == s3[1]) {
if (abs(int(s1[0]) - int(s3[0])) == 2 || abs(int(s1[0]) - int(s3[0])) == 1)
ans = min(ans, 1);
cout << ans << endl;
return 0;
}
if (s2[1] == s3[1]) {
if (abs(int(s2[0]) - int(s3[0])) == 2 || abs(int(s2[0]) - int(s3[0])) == 1)
ans = min(ans, 1);
cout << ans << endl;
return 0;
}
cout << ans << endl;
}
``` |
#include <bits/stdc++.h>
const long long INF = (1LL << 60) - 1;
using namespace std;
long long power(long long x, long long y) {
long long res = 1;
x = x % 1000000007;
while (y > 0) {
if (y & 1) res = (res * x) % 1000000007;
y = y >> 1;
x = (x * x) % 1000000007;
}
return res;
}
long long BINARY_SEARCH(long long dp[], long long n, long long key) {
long long s = 1;
long long e = n;
while (s <= e) {
long long mid = (s + e) / 2;
if (dp[mid] == key)
return mid;
else if (dp[mid] > key)
e = mid - 1;
else
s = mid + 1;
}
return -1;
}
string CONVERT_TO_BINARY(long long s) {
string res = "";
while (s != 0) {
res += (char)('0' + s % 2);
s /= 2;
}
reverse(res.begin(), res.end());
return res;
}
bool PALIN(string s) {
long long i = 0;
long long j = s.length() - 1;
while (i <= j) {
if (s[i] != s[j]) return false;
j--, i++;
}
return true;
}
long long STOI(string s) {
long long num = 0;
long long po = 1;
for (long long i = s.length() - 1; i >= 0; i--) {
num += po * (s[i] - '0');
po *= 10;
}
return num;
}
long long modInverse(long long a, long long m) { return power(a, m - 2); }
int findLongestSub(string bin) {
int n = bin.length(), i;
int sum = 0;
unordered_map<int, int> prevSum;
int maxlen = 0;
int currlen;
for (i = 0; i < n; i++) {
if (bin[i] == '1')
sum++;
else
sum--;
if (sum > 0) {
maxlen = i + 1;
} else if (sum <= 0) {
if (prevSum.find(sum - 1) != prevSum.end()) {
currlen = i - prevSum[sum - 1];
maxlen = max(maxlen, currlen);
}
}
if (prevSum.find(sum) == prevSum.end()) prevSum[sum] = i;
}
return maxlen;
}
vector<long long> prefix_KMP(string &s) {
vector<long long> pi;
pi[0] = 0;
for (long long i = 1; i <= s.length() - 1; i++) {
long long j = pi[i - 1];
while (j > 0 && s[i] != s[j]) {
j = pi[j - 1];
}
if (s[i] == s[j]) {
j++;
}
pi[i] = j;
}
return pi;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string s[3];
vector<pair<long long, char>> v;
map<char, vector<long long>> vis;
for (long long i = 0; i < 3; i++) {
cin >> s[i];
vis[s[i][1]].push_back(s[i][0] - '0');
v.push_back({s[i][0] - '0', s[i][1]});
}
long long ans = INT_MAX;
for (auto i : vis) {
map<long long, long long> cnt;
for (auto j : i.second) {
cnt[j]++;
}
for (auto j : cnt) {
ans = min(3 - j.second, ans);
}
if (ans == 0) {
cout << ans;
return 0;
}
long long prev = -1;
long long chk = 0;
for (auto j : cnt) {
if (prev != -1) {
if (j.first - 1 == prev) {
chk++;
}
}
prev = j.first;
}
ans = min(2 - chk, ans);
if (ans == 0) {
cout << ans;
return 0;
}
prev = -1;
chk = 0;
for (auto j : cnt) {
if (prev != -1) {
if (j.first - 2 == prev) {
chk++;
cout << 1;
return 0;
}
}
prev = j.first;
}
ans = min(2 - chk, ans);
}
cout << ans;
}
| ### Prompt
Develop a solution in CPP to the problem described below:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
const long long INF = (1LL << 60) - 1;
using namespace std;
long long power(long long x, long long y) {
long long res = 1;
x = x % 1000000007;
while (y > 0) {
if (y & 1) res = (res * x) % 1000000007;
y = y >> 1;
x = (x * x) % 1000000007;
}
return res;
}
long long BINARY_SEARCH(long long dp[], long long n, long long key) {
long long s = 1;
long long e = n;
while (s <= e) {
long long mid = (s + e) / 2;
if (dp[mid] == key)
return mid;
else if (dp[mid] > key)
e = mid - 1;
else
s = mid + 1;
}
return -1;
}
string CONVERT_TO_BINARY(long long s) {
string res = "";
while (s != 0) {
res += (char)('0' + s % 2);
s /= 2;
}
reverse(res.begin(), res.end());
return res;
}
bool PALIN(string s) {
long long i = 0;
long long j = s.length() - 1;
while (i <= j) {
if (s[i] != s[j]) return false;
j--, i++;
}
return true;
}
long long STOI(string s) {
long long num = 0;
long long po = 1;
for (long long i = s.length() - 1; i >= 0; i--) {
num += po * (s[i] - '0');
po *= 10;
}
return num;
}
long long modInverse(long long a, long long m) { return power(a, m - 2); }
int findLongestSub(string bin) {
int n = bin.length(), i;
int sum = 0;
unordered_map<int, int> prevSum;
int maxlen = 0;
int currlen;
for (i = 0; i < n; i++) {
if (bin[i] == '1')
sum++;
else
sum--;
if (sum > 0) {
maxlen = i + 1;
} else if (sum <= 0) {
if (prevSum.find(sum - 1) != prevSum.end()) {
currlen = i - prevSum[sum - 1];
maxlen = max(maxlen, currlen);
}
}
if (prevSum.find(sum) == prevSum.end()) prevSum[sum] = i;
}
return maxlen;
}
vector<long long> prefix_KMP(string &s) {
vector<long long> pi;
pi[0] = 0;
for (long long i = 1; i <= s.length() - 1; i++) {
long long j = pi[i - 1];
while (j > 0 && s[i] != s[j]) {
j = pi[j - 1];
}
if (s[i] == s[j]) {
j++;
}
pi[i] = j;
}
return pi;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string s[3];
vector<pair<long long, char>> v;
map<char, vector<long long>> vis;
for (long long i = 0; i < 3; i++) {
cin >> s[i];
vis[s[i][1]].push_back(s[i][0] - '0');
v.push_back({s[i][0] - '0', s[i][1]});
}
long long ans = INT_MAX;
for (auto i : vis) {
map<long long, long long> cnt;
for (auto j : i.second) {
cnt[j]++;
}
for (auto j : cnt) {
ans = min(3 - j.second, ans);
}
if (ans == 0) {
cout << ans;
return 0;
}
long long prev = -1;
long long chk = 0;
for (auto j : cnt) {
if (prev != -1) {
if (j.first - 1 == prev) {
chk++;
}
}
prev = j.first;
}
ans = min(2 - chk, ans);
if (ans == 0) {
cout << ans;
return 0;
}
prev = -1;
chk = 0;
for (auto j : cnt) {
if (prev != -1) {
if (j.first - 2 == prev) {
chk++;
cout << 1;
return 0;
}
}
prev = j.first;
}
ans = min(2 - chk, ans);
}
cout << ans;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
map<string, int> mp;
string s1, s2, s3;
cin >> s1 >> s2 >> s3;
mp[s1]++;
mp[s2]++;
mp[s3]++;
int ans = 3;
for (char c = '1'; c <= '9'; c++) {
string x;
x += c;
string a = x + 'm';
ans = min(ans, 3 - mp[a]);
a = x + 'p';
ans = min(ans, 3 - mp[a]);
a = x + 's';
ans = min(ans, 3 - mp[a]);
}
for (int i = 1; i <= 7; i++) {
string c;
c = i + '0';
int qn = 0;
string s = c + 'p';
qn += (mp[s] != 0);
c = (i + 1) + '0';
s = c + 'p';
qn += (mp[s] != 0);
c = (i + 2) + '0';
s = c + 'p';
qn += (mp[s] != 0);
ans = min(ans, 3 - qn);
c = i + '0';
qn = 0;
s = c + 'm';
qn += (mp[s] != 0);
c = (i + 1) + '0';
s = c + 'm';
qn += (mp[s] != 0);
c = (i + 2) + '0';
s = c + 'm';
qn += (mp[s] != 0);
ans = min(ans, 3 - qn);
c = i + '0';
qn = 0;
s = c + 's';
qn += (mp[s] != 0);
c = (i + 1) + '0';
s = c + 's';
qn += (mp[s] != 0);
c = (i + 2) + '0';
s = c + 's';
qn += (mp[s] != 0);
ans = min(ans, 3 - qn);
}
cout << ans << endl;
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
map<string, int> mp;
string s1, s2, s3;
cin >> s1 >> s2 >> s3;
mp[s1]++;
mp[s2]++;
mp[s3]++;
int ans = 3;
for (char c = '1'; c <= '9'; c++) {
string x;
x += c;
string a = x + 'm';
ans = min(ans, 3 - mp[a]);
a = x + 'p';
ans = min(ans, 3 - mp[a]);
a = x + 's';
ans = min(ans, 3 - mp[a]);
}
for (int i = 1; i <= 7; i++) {
string c;
c = i + '0';
int qn = 0;
string s = c + 'p';
qn += (mp[s] != 0);
c = (i + 1) + '0';
s = c + 'p';
qn += (mp[s] != 0);
c = (i + 2) + '0';
s = c + 'p';
qn += (mp[s] != 0);
ans = min(ans, 3 - qn);
c = i + '0';
qn = 0;
s = c + 'm';
qn += (mp[s] != 0);
c = (i + 1) + '0';
s = c + 'm';
qn += (mp[s] != 0);
c = (i + 2) + '0';
s = c + 'm';
qn += (mp[s] != 0);
ans = min(ans, 3 - qn);
c = i + '0';
qn = 0;
s = c + 's';
qn += (mp[s] != 0);
c = (i + 1) + '0';
s = c + 's';
qn += (mp[s] != 0);
c = (i + 2) + '0';
s = c + 's';
qn += (mp[s] != 0);
ans = min(ans, 3 - qn);
}
cout << ans << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
pair<int, char> arr[3];
cin >> s;
arr[0].first = s[0] - '0';
arr[0].second = s[1];
cin >> s;
arr[1].first = s[0] - '0';
arr[1].second = s[1];
cin >> s;
arr[2].first = s[0] - '0';
arr[2].second = s[1];
if (arr[0].first == arr[1].first && arr[1].first == arr[2].first &&
arr[0].second == arr[1].second && arr[1].second == arr[2].second) {
cout << 0;
return 0;
}
sort(arr, arr + 3);
if (arr[0].second == arr[1].second && arr[1].second == arr[2].second &&
arr[0].first + 1 == arr[1].first && arr[1].first + 1 == arr[2].first) {
cout << 0;
return 0;
}
if (arr[0].second == arr[1].second) {
if (arr[1].first - arr[0].first <= 2) {
cout << 1;
return 0;
}
}
if (arr[0].second == arr[2].second) {
if (arr[2].first - arr[0].first <= 2) {
cout << 1;
return 0;
}
}
if (arr[2].second == arr[1].second) {
if (arr[2].first - arr[1].first <= 2) {
cout << 1;
return 0;
}
}
cout << 2;
return 0;
}
| ### Prompt
Please create a solution in cpp to the following problem:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
pair<int, char> arr[3];
cin >> s;
arr[0].first = s[0] - '0';
arr[0].second = s[1];
cin >> s;
arr[1].first = s[0] - '0';
arr[1].second = s[1];
cin >> s;
arr[2].first = s[0] - '0';
arr[2].second = s[1];
if (arr[0].first == arr[1].first && arr[1].first == arr[2].first &&
arr[0].second == arr[1].second && arr[1].second == arr[2].second) {
cout << 0;
return 0;
}
sort(arr, arr + 3);
if (arr[0].second == arr[1].second && arr[1].second == arr[2].second &&
arr[0].first + 1 == arr[1].first && arr[1].first + 1 == arr[2].first) {
cout << 0;
return 0;
}
if (arr[0].second == arr[1].second) {
if (arr[1].first - arr[0].first <= 2) {
cout << 1;
return 0;
}
}
if (arr[0].second == arr[2].second) {
if (arr[2].first - arr[0].first <= 2) {
cout << 1;
return 0;
}
}
if (arr[2].second == arr[1].second) {
if (arr[2].first - arr[1].first <= 2) {
cout << 1;
return 0;
}
}
cout << 2;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long modInverse(long long a, long long m);
long long gcd(long long a, long long b);
long long power(long long x, unsigned long long y, unsigned long long m);
long long logint(long long x, long long y);
long long gcd(long long a, long long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
long long power(long long x, unsigned long long y, unsigned long long m) {
if (y == 0) return 1;
long long p = power(x, y / 2, m) % m;
p = (p * p) % m;
return (y % 2 == 0) ? p : (x * p) % m;
}
long long modInverse(long long a, long long m) {
long long m0 = m;
long long y = 0, x = 1;
if (m == 1) return 0;
while (a > 1) {
long long q = a / m;
long long t = m;
m = a % m, a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0) x += m0;
return x;
}
void pairsort(long long a[], long long b[], long long n) {
pair<long long, long long> pairt[n];
for (long long i = 0; i < n; i++) {
pairt[i].first = a[i];
pairt[i].second = b[i];
}
sort(pairt, pairt + n);
for (long long i = 0; i < n; i++) {
a[i] = pairt[i].first;
b[i] = pairt[i].second;
}
}
long long logint(long long x, long long y) {
long long ans = 0;
long long a = 1;
for (long long i = 0; i < x; i++) {
if (x <= a) {
return ans;
}
ans++;
a *= y;
}
return -1;
}
const int MAX = 1e4 + 5;
long long ar[150001];
vector<long long> ans[150001];
long long fin(long long x) {
if (x == ar[x]) {
return x;
}
return ar[x] = fin(ar[x]);
}
void uni(long long x, long long y) {
x = fin(x);
y = fin(y);
if (ans[x].size() > ans[y].size()) {
for (long long i = 0; i < ans[y].size(); i++) {
ans[x].push_back(ans[y][i]);
ar[ans[y][i]] = x;
}
} else {
for (long long i = 0; i < ans[x].size(); i++) {
ans[y].push_back(ans[x][i]);
ar[ans[x][i]] = y;
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string ar[3];
for (long long i = 0; i < 3; i++) {
cin >> ar[i];
}
sort(ar, ar + 3);
long long k = ar[0][0] - '0';
long long l = ar[1][0] - '0';
long long m = ar[2][0] - '0';
if (ar[0][1] == ar[1][1] && ar[0][1] == ar[2][1]) {
if (l - k == 1 && m - l == 1) {
cout << '0';
} else if (l == k && l == m) {
cout << '0';
} else {
if (min(m - l, l - k) <= 2) {
cout << '1';
} else {
cout << '2';
}
}
} else if (ar[0][1] == ar[1][1]) {
if (l - k <= 2) {
cout << '1';
} else {
cout << '2';
}
} else if (ar[0][1] == ar[2][1]) {
if (m - k <= 2) {
cout << '1';
} else {
cout << '2';
}
} else if (ar[1][1] == ar[2][1]) {
if (m - l <= 2) {
cout << '1';
} else {
cout << '2';
}
} else {
cout << '2';
}
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long modInverse(long long a, long long m);
long long gcd(long long a, long long b);
long long power(long long x, unsigned long long y, unsigned long long m);
long long logint(long long x, long long y);
long long gcd(long long a, long long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
long long power(long long x, unsigned long long y, unsigned long long m) {
if (y == 0) return 1;
long long p = power(x, y / 2, m) % m;
p = (p * p) % m;
return (y % 2 == 0) ? p : (x * p) % m;
}
long long modInverse(long long a, long long m) {
long long m0 = m;
long long y = 0, x = 1;
if (m == 1) return 0;
while (a > 1) {
long long q = a / m;
long long t = m;
m = a % m, a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0) x += m0;
return x;
}
void pairsort(long long a[], long long b[], long long n) {
pair<long long, long long> pairt[n];
for (long long i = 0; i < n; i++) {
pairt[i].first = a[i];
pairt[i].second = b[i];
}
sort(pairt, pairt + n);
for (long long i = 0; i < n; i++) {
a[i] = pairt[i].first;
b[i] = pairt[i].second;
}
}
long long logint(long long x, long long y) {
long long ans = 0;
long long a = 1;
for (long long i = 0; i < x; i++) {
if (x <= a) {
return ans;
}
ans++;
a *= y;
}
return -1;
}
const int MAX = 1e4 + 5;
long long ar[150001];
vector<long long> ans[150001];
long long fin(long long x) {
if (x == ar[x]) {
return x;
}
return ar[x] = fin(ar[x]);
}
void uni(long long x, long long y) {
x = fin(x);
y = fin(y);
if (ans[x].size() > ans[y].size()) {
for (long long i = 0; i < ans[y].size(); i++) {
ans[x].push_back(ans[y][i]);
ar[ans[y][i]] = x;
}
} else {
for (long long i = 0; i < ans[x].size(); i++) {
ans[y].push_back(ans[x][i]);
ar[ans[x][i]] = y;
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string ar[3];
for (long long i = 0; i < 3; i++) {
cin >> ar[i];
}
sort(ar, ar + 3);
long long k = ar[0][0] - '0';
long long l = ar[1][0] - '0';
long long m = ar[2][0] - '0';
if (ar[0][1] == ar[1][1] && ar[0][1] == ar[2][1]) {
if (l - k == 1 && m - l == 1) {
cout << '0';
} else if (l == k && l == m) {
cout << '0';
} else {
if (min(m - l, l - k) <= 2) {
cout << '1';
} else {
cout << '2';
}
}
} else if (ar[0][1] == ar[1][1]) {
if (l - k <= 2) {
cout << '1';
} else {
cout << '2';
}
} else if (ar[0][1] == ar[2][1]) {
if (m - k <= 2) {
cout << '1';
} else {
cout << '2';
}
} else if (ar[1][1] == ar[2][1]) {
if (m - l <= 2) {
cout << '1';
} else {
cout << '2';
}
} else {
cout << '2';
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool read(T &x) {
int c = getchar();
int sgn = 1;
while (~c && c<'0' | c> '9') {
if (c == '-') sgn = -1;
c = getchar();
}
for (x = 0; ~c && '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0';
x *= sgn;
return ~c;
}
const long long N = 5005;
const long long MOD = 1e9 + 7;
long long ara[N];
int main() {
string s, s2, s3;
cin >> s >> s2 >> s3;
reverse(s.begin(), s.end());
reverse(s2.begin(), s2.end());
reverse(s3.begin(), s3.end());
ara[0] = s[1] - '0';
ara[1] = s2[1] - '0';
ara[2] = s3[1] - '0';
sort(ara, ara + 3);
if (s == s2 && s2 == s3) return cout << 0, 0;
if (s[0] == s2[0] && s[0] == s3[0]) {
if (ara[1] - ara[0] == 1 && ara[2] - ara[1] == 1) return cout << 0, 0;
}
if (s[0] == s2[0]) {
if (s == s2) return cout << 1, 0;
long long x = s[1] - '0';
long long y = s2[1] - '0';
if (abs(x - y) <= 2) return cout << 1, 0;
}
if (s[0] == s3[0]) {
if (s == s3) return cout << 1, 0;
long long x = s[1] - '0';
long long y = s3[1] - '0';
if (abs(x - y) <= 2) return cout << 1, 0;
}
if (s2[0] == s3[0]) {
if (s2 == s3) return cout << 1, 0;
long long x = s2[1] - '0';
long long y = s3[1] - '0';
if (abs(x - y) <= 2) return cout << 1, 0;
}
cout << 2 << endl;
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool read(T &x) {
int c = getchar();
int sgn = 1;
while (~c && c<'0' | c> '9') {
if (c == '-') sgn = -1;
c = getchar();
}
for (x = 0; ~c && '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0';
x *= sgn;
return ~c;
}
const long long N = 5005;
const long long MOD = 1e9 + 7;
long long ara[N];
int main() {
string s, s2, s3;
cin >> s >> s2 >> s3;
reverse(s.begin(), s.end());
reverse(s2.begin(), s2.end());
reverse(s3.begin(), s3.end());
ara[0] = s[1] - '0';
ara[1] = s2[1] - '0';
ara[2] = s3[1] - '0';
sort(ara, ara + 3);
if (s == s2 && s2 == s3) return cout << 0, 0;
if (s[0] == s2[0] && s[0] == s3[0]) {
if (ara[1] - ara[0] == 1 && ara[2] - ara[1] == 1) return cout << 0, 0;
}
if (s[0] == s2[0]) {
if (s == s2) return cout << 1, 0;
long long x = s[1] - '0';
long long y = s2[1] - '0';
if (abs(x - y) <= 2) return cout << 1, 0;
}
if (s[0] == s3[0]) {
if (s == s3) return cout << 1, 0;
long long x = s[1] - '0';
long long y = s3[1] - '0';
if (abs(x - y) <= 2) return cout << 1, 0;
}
if (s2[0] == s3[0]) {
if (s2 == s3) return cout << 1, 0;
long long x = s2[1] - '0';
long long y = s3[1] - '0';
if (abs(x - y) <= 2) return cout << 1, 0;
}
cout << 2 << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int a[3], i;
int main() {
string s;
for (i = 0; i < 3; i++) {
cin >> s;
a[i] = (s[0] - '0') + (s[1] - 'a') * 100;
}
sort(a, a + 3);
int t1 = a[1] - a[0];
int t2 = a[2] - a[1];
if (t1 == t2 && (t1 == 0 || t1 == 1))
cout << "0";
else if (t1 == 1 || t1 == 0 || t1 == 2 || t2 == 0 || t2 == 1 || t2 == 2)
cout << "1";
else
cout << "2";
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int a[3], i;
int main() {
string s;
for (i = 0; i < 3; i++) {
cin >> s;
a[i] = (s[0] - '0') + (s[1] - 'a') * 100;
}
sort(a, a + 3);
int t1 = a[1] - a[0];
int t2 = a[2] - a[1];
if (t1 == t2 && (t1 == 0 || t1 == 1))
cout << "0";
else if (t1 == 1 || t1 == 0 || t1 == 2 || t2 == 0 || t2 == 1 || t2 == 2)
cout << "1";
else
cout << "2";
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long int arr[10] = {0};
long long int brr[10] = {0};
long long int crr[10] = {0};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string s1, s2, s3;
cin >> s1 >> s2 >> s3;
if (s1[1] == 'm')
arr[int(s1[0]) - 48]++;
else if (s1[1] == 'p')
brr[int(s1[0]) - 48]++;
else if (s1[1] == 's')
crr[int(s1[0]) - 48]++;
if (s2[1] == 'm')
arr[int(s2[0]) - 48]++;
else if (s2[1] == 'p')
brr[int(s2[0]) - 48]++;
else if (s2[1] == 's')
crr[int(s2[0]) - 48]++;
if (s3[1] == 'm')
arr[int(s3[0]) - 48]++;
else if (s3[1] == 'p')
brr[int(s3[0]) - 48]++;
else if (s3[1] == 's')
crr[int(s3[0]) - 48]++;
for (int i = 0; i < 10; i++) {
if (arr[i] == 3 || brr[i] == 3 || crr[i] == 3) {
cout << "0";
exit(0);
}
}
for (int i = 0; i < 8; i++) {
if ((arr[i] != 0 && arr[i + 1] != 0 && arr[i + 2] != 0) ||
(brr[i] != 0 && brr[i + 1] != 0 && brr[i + 2] != 0) ||
(crr[i] != 0 && crr[i + 1] != 0 && crr[i + 2] != 0)) {
cout << "0";
exit(0);
}
}
for (int i = 0; i < 10; i++) {
if (arr[i] == 2 || brr[i] == 2 || crr[i] == 2) {
cout << "1";
exit(0);
}
}
for (int i = 0; i < 9; i++) {
if ((arr[i] != 0 && arr[i + 1] != 0) || (brr[i] != 0 && brr[i + 1] != 0) ||
(crr[i] != 0 && crr[i + 1] != 0)) {
cout << "1";
exit(0);
}
}
for (int i = 0; i < 8; i++) {
if ((arr[i] != 0 && arr[i + 2] != 0) || (brr[i] != 0 && brr[i + 2] != 0) ||
(crr[i] != 0 && crr[i + 2] != 0)) {
cout << "1";
exit(0);
}
}
cout << "2";
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long int arr[10] = {0};
long long int brr[10] = {0};
long long int crr[10] = {0};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string s1, s2, s3;
cin >> s1 >> s2 >> s3;
if (s1[1] == 'm')
arr[int(s1[0]) - 48]++;
else if (s1[1] == 'p')
brr[int(s1[0]) - 48]++;
else if (s1[1] == 's')
crr[int(s1[0]) - 48]++;
if (s2[1] == 'm')
arr[int(s2[0]) - 48]++;
else if (s2[1] == 'p')
brr[int(s2[0]) - 48]++;
else if (s2[1] == 's')
crr[int(s2[0]) - 48]++;
if (s3[1] == 'm')
arr[int(s3[0]) - 48]++;
else if (s3[1] == 'p')
brr[int(s3[0]) - 48]++;
else if (s3[1] == 's')
crr[int(s3[0]) - 48]++;
for (int i = 0; i < 10; i++) {
if (arr[i] == 3 || brr[i] == 3 || crr[i] == 3) {
cout << "0";
exit(0);
}
}
for (int i = 0; i < 8; i++) {
if ((arr[i] != 0 && arr[i + 1] != 0 && arr[i + 2] != 0) ||
(brr[i] != 0 && brr[i + 1] != 0 && brr[i + 2] != 0) ||
(crr[i] != 0 && crr[i + 1] != 0 && crr[i + 2] != 0)) {
cout << "0";
exit(0);
}
}
for (int i = 0; i < 10; i++) {
if (arr[i] == 2 || brr[i] == 2 || crr[i] == 2) {
cout << "1";
exit(0);
}
}
for (int i = 0; i < 9; i++) {
if ((arr[i] != 0 && arr[i + 1] != 0) || (brr[i] != 0 && brr[i + 1] != 0) ||
(crr[i] != 0 && crr[i + 1] != 0)) {
cout << "1";
exit(0);
}
}
for (int i = 0; i < 8; i++) {
if ((arr[i] != 0 && arr[i + 2] != 0) || (brr[i] != 0 && brr[i + 2] != 0) ||
(crr[i] != 0 && crr[i + 2] != 0)) {
cout << "1";
exit(0);
}
}
cout << "2";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void pr(vector<T> &v) {
for (int i = 0; i < (int)(v).size(); i++) cout << v[i] << " ";
cout << '\n';
;
}
template <typename T>
void pr(vector<vector<T>> &v) {
for (int i = 0; i < (int)(v).size(); i++) {
pr(v[i]);
}
}
template <typename T>
void re(T &x) {
cin >> x;
}
template <typename T>
void re(vector<T> &a) {
for (int i = 0; i < (int)(a).size(); i++) re(a[i]);
}
template <class Arg, class... Args>
void re(Arg &first, Args &...rest) {
re(first);
re(rest...);
}
template <typename T>
void pr(T x) {
cout << x << '\n';
;
}
template <class Arg, class... Args>
void pr(const Arg &first, const Args &...rest) {
cout << first << " ";
pr(rest...);
cout << '\n';
;
}
void ps() {
cout << '\n';
;
}
template <class T, class... Ts>
void ps(const T &t, const Ts &...ts) {
cout << t;
if (sizeof...(ts)) cout << " ";
ps(ts...);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
vector<string> s(3);
re(s);
set<string> x;
int mn = INT_MAX;
for (int i = 0; i < 3; i++) {
x.insert(s[i]);
}
mn = min(mn, (int)(x).size() - 1);
int num_k = 0;
for (int i = 0; i < 3; i++) {
for (int j = i + 1; j < 3; j++) {
if (s[i][1] == s[j][1] && abs(s[i][0] - s[j][0]) == 1) {
num_k++;
}
}
}
if (num_k == 2 && (int)(x).size() == 3) {
mn = min(0, mn);
} else if (num_k == 1) {
mn = min(1, mn);
}
int num_k2 = 0;
for (int i = 0; i < 3; i++) {
for (int j = i + 1; j < 3; j++) {
if (s[i][1] == s[j][1] && abs(s[i][0] - s[j][0]) == 2) {
num_k2++;
}
}
}
if (num_k2 >= 1) {
mn = min(1, mn);
}
pr(mn);
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void pr(vector<T> &v) {
for (int i = 0; i < (int)(v).size(); i++) cout << v[i] << " ";
cout << '\n';
;
}
template <typename T>
void pr(vector<vector<T>> &v) {
for (int i = 0; i < (int)(v).size(); i++) {
pr(v[i]);
}
}
template <typename T>
void re(T &x) {
cin >> x;
}
template <typename T>
void re(vector<T> &a) {
for (int i = 0; i < (int)(a).size(); i++) re(a[i]);
}
template <class Arg, class... Args>
void re(Arg &first, Args &...rest) {
re(first);
re(rest...);
}
template <typename T>
void pr(T x) {
cout << x << '\n';
;
}
template <class Arg, class... Args>
void pr(const Arg &first, const Args &...rest) {
cout << first << " ";
pr(rest...);
cout << '\n';
;
}
void ps() {
cout << '\n';
;
}
template <class T, class... Ts>
void ps(const T &t, const Ts &...ts) {
cout << t;
if (sizeof...(ts)) cout << " ";
ps(ts...);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
vector<string> s(3);
re(s);
set<string> x;
int mn = INT_MAX;
for (int i = 0; i < 3; i++) {
x.insert(s[i]);
}
mn = min(mn, (int)(x).size() - 1);
int num_k = 0;
for (int i = 0; i < 3; i++) {
for (int j = i + 1; j < 3; j++) {
if (s[i][1] == s[j][1] && abs(s[i][0] - s[j][0]) == 1) {
num_k++;
}
}
}
if (num_k == 2 && (int)(x).size() == 3) {
mn = min(0, mn);
} else if (num_k == 1) {
mn = min(1, mn);
}
int num_k2 = 0;
for (int i = 0; i < 3; i++) {
for (int j = i + 1; j < 3; j++) {
if (s[i][1] == s[j][1] && abs(s[i][0] - s[j][0]) == 2) {
num_k2++;
}
}
}
if (num_k2 >= 1) {
mn = min(1, mn);
}
pr(mn);
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string s[3];
cin >> s[0] >> s[1] >> s[2];
int ans = 2;
if (s[0] == s[1] && s[1] == s[2]) return cout << 0, 0;
if (s[0] == s[1] || s[1] == s[2] || s[0] == s[2]) return cout << 1, 0;
if (s[0][1] == s[1][1] && s[0][1] == s[2][1]) {
int a[3];
a[0] = s[0][0] - '0';
a[1] = s[1][0] - '0';
a[2] = s[2][0] - '0';
sort(a, a + 3);
if (a[0] == a[1] - 1 && a[1] == a[2] - 1)
return cout << 0, 0;
else if (a[0] == a[1] - 1 || a[1] == a[2] - 1 || a[0] == a[1] - 2 ||
a[1] == a[2] - 2)
return cout << 1, 0;
return cout << 2, 0;
}
if (s[0][1] == s[1][1]) {
int a[2];
a[0] = s[0][0] - '0';
a[1] = s[1][0] - '0';
sort(a, a + 2);
if (a[0] == a[1] - 1 || a[0] == a[1] - 2)
ans = min(ans, 1);
else
ans = min(ans, 2);
}
if (s[0][1] == s[2][1]) {
int a[2];
a[0] = s[0][0] - '0';
a[1] = s[2][0] - '0';
sort(a, a + 2);
if (a[0] == a[1] - 1 || a[0] == a[1] - 2)
ans = min(ans, 1);
else
ans = min(ans, 2);
}
if (s[1][1] == s[2][1]) {
int a[2];
a[0] = s[1][0] - '0';
a[1] = s[2][0] - '0';
sort(a, a + 2);
if (a[0] == a[1] - 1 || a[0] == a[1] - 2)
ans = min(ans, 1);
else
ans = min(ans, 2);
}
return cout << ans, 0;
}
| ### Prompt
Please create a solution in CPP to the following problem:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string s[3];
cin >> s[0] >> s[1] >> s[2];
int ans = 2;
if (s[0] == s[1] && s[1] == s[2]) return cout << 0, 0;
if (s[0] == s[1] || s[1] == s[2] || s[0] == s[2]) return cout << 1, 0;
if (s[0][1] == s[1][1] && s[0][1] == s[2][1]) {
int a[3];
a[0] = s[0][0] - '0';
a[1] = s[1][0] - '0';
a[2] = s[2][0] - '0';
sort(a, a + 3);
if (a[0] == a[1] - 1 && a[1] == a[2] - 1)
return cout << 0, 0;
else if (a[0] == a[1] - 1 || a[1] == a[2] - 1 || a[0] == a[1] - 2 ||
a[1] == a[2] - 2)
return cout << 1, 0;
return cout << 2, 0;
}
if (s[0][1] == s[1][1]) {
int a[2];
a[0] = s[0][0] - '0';
a[1] = s[1][0] - '0';
sort(a, a + 2);
if (a[0] == a[1] - 1 || a[0] == a[1] - 2)
ans = min(ans, 1);
else
ans = min(ans, 2);
}
if (s[0][1] == s[2][1]) {
int a[2];
a[0] = s[0][0] - '0';
a[1] = s[2][0] - '0';
sort(a, a + 2);
if (a[0] == a[1] - 1 || a[0] == a[1] - 2)
ans = min(ans, 1);
else
ans = min(ans, 2);
}
if (s[1][1] == s[2][1]) {
int a[2];
a[0] = s[1][0] - '0';
a[1] = s[2][0] - '0';
sort(a, a + 2);
if (a[0] == a[1] - 1 || a[0] == a[1] - 2)
ans = min(ans, 1);
else
ans = min(ans, 2);
}
return cout << ans, 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void solve() {
string a[3];
cin >> a[0] >> a[1] >> a[2];
map<char, vector<long long> > m;
for (auto i : a) m[i[1]].push_back(i[0]);
int ok = 2;
for (auto i : m) {
vector<long long> r = i.second;
sort(r.begin(), r.end());
if (r.size() == 1) {
continue;
} else if (r.size() == 2) {
if (r[1] - r[0] <= 2) ok = min(ok, 1);
} else {
if ((r[2] - r[0] == 0) || ((r[1] == r[0] + 1) && (r[2] == r[1] + 1)))
ok = 0;
else if ((r[2] - r[1] <= 2) || (r[1] - r[0] <= 2))
ok = min(ok, 1);
}
}
cout << ok << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int t = 1;
while (t--) {
solve();
}
return 0;
}
| ### Prompt
Please formulate a CPP solution to the following problem:
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, β¦, 9m, 1p, 2p, β¦, 9p, 1s, 2s, β¦, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
* A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
* A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
* A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
* [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu;
* [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
* [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings β the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer β the minimum number of extra suited tiles she needs to draw.
Examples
Input
1s 2s 3s
Output
0
Input
9m 9m 9m
Output
0
Input
3p 9m 2p
Output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void solve() {
string a[3];
cin >> a[0] >> a[1] >> a[2];
map<char, vector<long long> > m;
for (auto i : a) m[i[1]].push_back(i[0]);
int ok = 2;
for (auto i : m) {
vector<long long> r = i.second;
sort(r.begin(), r.end());
if (r.size() == 1) {
continue;
} else if (r.size() == 2) {
if (r[1] - r[0] <= 2) ok = min(ok, 1);
} else {
if ((r[2] - r[0] == 0) || ((r[1] == r[0] + 1) && (r[2] == r[1] + 1)))
ok = 0;
else if ((r[2] - r[1] <= 2) || (r[1] - r[0] <= 2))
ok = min(ok, 1);
}
}
cout << ok << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int t = 1;
while (t--) {
solve();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const bool online_judge = true;
const long long inf = 1LL << 60;
long long toInt(string s) {
long long res;
stringstream ss;
ss << s;
ss >> res;
return res;
}
string toString(long long n) {
stringstream ss;
ss << n;
return ss.str();
}
void run() {
long long n, k;
cin >> n >> k;
vector<long long> v;
for (long long i = 0; i < n; i++) {
long long tmp;
cin >> tmp;
v.push_back(tmp);
}
for (long long i = k - 1;; i = (i + 1) % n) {
if (v[i] == 1) {
cout << (i + 1) << endl;
return;
}
}
}
int main(int argc, char *argv[]) {
if (online_judge) {
ifstream ifile("input.txt");
if (ifile) freopen("input.txt", "rt", stdin);
if (ifile) freopen("output.txt", "wt", stdout);
}
run();
return 0;
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
A team quiz game called "What? Where? When?" is very popular in Berland. The game is centered on two teams competing. They are the team of six Experts versus the team of the Audience. A person from the audience asks a question and the experts are allowed a minute on brainstorming and finding the right answer to the question. All it takes to answer a typical question is general knowledge and common logic. The question sent be the audience are in envelops lain out in a circle on a round table. Each envelop is marked by the name of the asker's town. Each question is positioned in a separate sector. In the centre of the table is a spinning arrow. Thus, the table rather resembles a roulette table with no ball but with a spinning arrow instead. The host sets off the spinning arrow to choose a question for the experts: when the arrow stops spinning, the question it is pointing at is chosen. If the arrow points at the question that has already been asked, the host chooses the next unanswered question in the clockwise direction. Your task is to determine which will be the number of the next asked question if the arrow points at sector number k.
Input
The first line contains two positive integers n and k (1 β€ n β€ 1000 and 1 β€ k β€ n) β the numbers of sectors on the table and the number of the sector where the arrow is pointing. The second line contains n numbers: ai = 0 if the question from sector i has already been asked and ai = 1 if the question from sector i hasn't been asked yet (1 β€ i β€ n). The sectors are given in the clockwise order, the first sector follows after the n-th one.
Output
Print the single number β the number of the sector containing the question the experts will be asked. It is guaranteed that the answer exists, that is that not all the questions have already been asked.
Examples
Input
5 5
0 1 0 1 0
Output
2
Input
2 1
1 1
Output
1
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const bool online_judge = true;
const long long inf = 1LL << 60;
long long toInt(string s) {
long long res;
stringstream ss;
ss << s;
ss >> res;
return res;
}
string toString(long long n) {
stringstream ss;
ss << n;
return ss.str();
}
void run() {
long long n, k;
cin >> n >> k;
vector<long long> v;
for (long long i = 0; i < n; i++) {
long long tmp;
cin >> tmp;
v.push_back(tmp);
}
for (long long i = k - 1;; i = (i + 1) % n) {
if (v[i] == 1) {
cout << (i + 1) << endl;
return;
}
}
}
int main(int argc, char *argv[]) {
if (online_judge) {
ifstream ifile("input.txt");
if (ifile) freopen("input.txt", "rt", stdin);
if (ifile) freopen("output.txt", "wt", stdout);
}
run();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int oo = 0x3f3f3f3f;
const double eps = 1e-9;
vector<int> lucky;
void generate(long long n) {
if (n > 1000000000LL)
return;
else {
generate(n * 10 + 4);
generate(n * 10 + 7);
if (n > 0) lucky.push_back(n);
}
}
void unrank(vector<int> &perm, int n, long long num) {
bool used[n];
memset(used, false, n * sizeof(bool));
long long fak = 1;
for (int i = (1); i < (n + 1); ++i) fak *= i;
for (int i = (0); i < (n); ++i) {
fak /= n - i;
int k = num / fak;
num = num % fak;
int j = 0;
while (k >= 0) {
if (!used[j]) --k;
j++;
}
perm[i] = j - 1;
used[j - 1] = true;
}
}
bool isLucky(int n) {
while (n > 0) {
int c = n % 10;
if (c != 7 && c != 4) return false;
n /= 10;
}
return true;
}
int main() {
int N, M = 0, K;
scanf("%d %d", &N, &K);
generate(0);
long long faculty = 1;
for (int i = (1); i < (N + 1); ++i) {
faculty *= (M = i);
if (faculty >= K) break;
}
if (faculty < K) {
printf("-1\n");
return 0;
}
vector<int> permutation(M);
unrank(permutation, M, K - 1);
int res = 0;
sort((lucky).begin(), (lucky).end());
int changeIndex = N - M + 1;
for (int i = (0); i < (int((lucky).size())); ++i)
if (lucky[i] < changeIndex) ++res;
for (int i = (changeIndex); i < (N + 1); ++i) {
int position = i;
int element = changeIndex + permutation[i - changeIndex];
if (isLucky(position) && isLucky(element)) ++res;
}
printf("%d\n", res);
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 oo = 0x3f3f3f3f;
const double eps = 1e-9;
vector<int> lucky;
void generate(long long n) {
if (n > 1000000000LL)
return;
else {
generate(n * 10 + 4);
generate(n * 10 + 7);
if (n > 0) lucky.push_back(n);
}
}
void unrank(vector<int> &perm, int n, long long num) {
bool used[n];
memset(used, false, n * sizeof(bool));
long long fak = 1;
for (int i = (1); i < (n + 1); ++i) fak *= i;
for (int i = (0); i < (n); ++i) {
fak /= n - i;
int k = num / fak;
num = num % fak;
int j = 0;
while (k >= 0) {
if (!used[j]) --k;
j++;
}
perm[i] = j - 1;
used[j - 1] = true;
}
}
bool isLucky(int n) {
while (n > 0) {
int c = n % 10;
if (c != 7 && c != 4) return false;
n /= 10;
}
return true;
}
int main() {
int N, M = 0, K;
scanf("%d %d", &N, &K);
generate(0);
long long faculty = 1;
for (int i = (1); i < (N + 1); ++i) {
faculty *= (M = i);
if (faculty >= K) break;
}
if (faculty < K) {
printf("-1\n");
return 0;
}
vector<int> permutation(M);
unrank(permutation, M, K - 1);
int res = 0;
sort((lucky).begin(), (lucky).end());
int changeIndex = N - M + 1;
for (int i = (0); i < (int((lucky).size())); ++i)
if (lucky[i] < changeIndex) ++res;
for (int i = (changeIndex); i < (N + 1); ++i) {
int position = i;
int element = changeIndex + permutation[i - changeIndex];
if (isLucky(position) && isLucky(element)) ++res;
}
printf("%d\n", res);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int N, K, A[14 + 1];
int u[14 + 1];
long long fact[2 * 14 + 1];
vector<long long> all_lucky;
void gen_lucky(int x, int len) {
if (len == 9) {
all_lucky.push_back(x);
return;
}
if (len >= 1) {
all_lucky.push_back(x);
}
gen_lucky(x * 10 + 4, len + 1);
gen_lucky(x * 10 + 7, len + 1);
}
int check_lucky(int lucky) {
while (lucky) {
if (lucky % 10 != 4 && lucky % 10 != 7) {
return 0;
}
lucky /= 10;
}
return 1;
}
int check(int lucky) {
if (lucky > N) return 0;
if (N <= 14) return check_lucky(A[lucky]);
int cutoff = N - 14 + 1;
if (lucky >= cutoff) {
return check_lucky(A[lucky - cutoff + 1]);
} else
return 1;
}
int main() {
cin >> N >> K;
gen_lucky(0, 0);
sort(all_lucky.begin(), all_lucky.end());
fact[0] = 1;
for (int i = 1; i <= 14 + 1; ++i) fact[i] = fact[i - 1] * i;
int n = N;
if (N > 14) N = 14;
K--;
for (int i = 1; i <= N; ++i) {
int at = 0;
for (int j = 1; j <= N; ++j) {
if (!u[j]) {
at++;
if (K < fact[N - i]) {
u[j] = 1;
A[i] = j;
goto L1;
}
K -= fact[N - i];
}
}
cout << -1 << endl;
return 0;
L1:;
}
int res = 0;
N = n;
if (N > 14) {
for (int i = 1; i <= 14; ++i) A[i] = N - 14 + A[i];
}
for (int i = 0; i < all_lucky.size(); ++i) {
int lucky = all_lucky[i];
res += check(lucky);
}
cout << res << 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;
int N, K, A[14 + 1];
int u[14 + 1];
long long fact[2 * 14 + 1];
vector<long long> all_lucky;
void gen_lucky(int x, int len) {
if (len == 9) {
all_lucky.push_back(x);
return;
}
if (len >= 1) {
all_lucky.push_back(x);
}
gen_lucky(x * 10 + 4, len + 1);
gen_lucky(x * 10 + 7, len + 1);
}
int check_lucky(int lucky) {
while (lucky) {
if (lucky % 10 != 4 && lucky % 10 != 7) {
return 0;
}
lucky /= 10;
}
return 1;
}
int check(int lucky) {
if (lucky > N) return 0;
if (N <= 14) return check_lucky(A[lucky]);
int cutoff = N - 14 + 1;
if (lucky >= cutoff) {
return check_lucky(A[lucky - cutoff + 1]);
} else
return 1;
}
int main() {
cin >> N >> K;
gen_lucky(0, 0);
sort(all_lucky.begin(), all_lucky.end());
fact[0] = 1;
for (int i = 1; i <= 14 + 1; ++i) fact[i] = fact[i - 1] * i;
int n = N;
if (N > 14) N = 14;
K--;
for (int i = 1; i <= N; ++i) {
int at = 0;
for (int j = 1; j <= N; ++j) {
if (!u[j]) {
at++;
if (K < fact[N - i]) {
u[j] = 1;
A[i] = j;
goto L1;
}
K -= fact[N - i];
}
}
cout << -1 << endl;
return 0;
L1:;
}
int res = 0;
N = n;
if (N > 14) {
for (int i = 1; i <= 14; ++i) A[i] = N - 14 + A[i];
}
for (int i = 0; i < all_lucky.size(); ++i) {
int lucky = all_lucky[i];
res += check(lucky);
}
cout << res << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, k, N;
int fact[13], p[13], a[13];
int first(int n) {
queue<long long> q;
q.push(0);
int res = -1;
while (q.front() <= 1LL * n) {
long long cur = q.front();
res++;
q.pop();
q.push(10 * cur + 4);
q.push(10 * cur + 7);
}
return res;
}
int ok(int x) {
while (x) {
if (!(x % 10 == 4 || x % 10 == 7)) {
return 0;
}
x /= 10;
}
return 1;
}
int main() {
fact[0] = 1;
for (int i = 1; i <= 12; i++) {
fact[i] = fact[i - 1] * i;
a[i] = i;
}
a[13] = 13;
scanf("%d", &n);
scanf("%d", &k);
if (n < 13 && fact[n] < k) {
puts("-1");
return 0;
}
N = min(n, 13);
k--;
for (int i = 1; i <= N; i++) {
int l = k / fact[N - i];
k %= fact[N - i];
p[i] = a[l + 1];
for (int j = l + 2; j <= N; j++) a[j - 1] = a[j];
}
int ans = 0;
if (n <= 13) {
for (int i = 1; i <= n; i++) {
if ((i == 4 || i == 7) && (p[i] == 4 || p[i] == 7)) ans++;
}
} else {
for (int i = n; i > n - 13; i--) {
if (ok(i) && ok(p[i - n + 13] + n - 13)) {
ans++;
}
}
ans = ans + first(n - 13);
}
printf("%d\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;
int n, k, N;
int fact[13], p[13], a[13];
int first(int n) {
queue<long long> q;
q.push(0);
int res = -1;
while (q.front() <= 1LL * n) {
long long cur = q.front();
res++;
q.pop();
q.push(10 * cur + 4);
q.push(10 * cur + 7);
}
return res;
}
int ok(int x) {
while (x) {
if (!(x % 10 == 4 || x % 10 == 7)) {
return 0;
}
x /= 10;
}
return 1;
}
int main() {
fact[0] = 1;
for (int i = 1; i <= 12; i++) {
fact[i] = fact[i - 1] * i;
a[i] = i;
}
a[13] = 13;
scanf("%d", &n);
scanf("%d", &k);
if (n < 13 && fact[n] < k) {
puts("-1");
return 0;
}
N = min(n, 13);
k--;
for (int i = 1; i <= N; i++) {
int l = k / fact[N - i];
k %= fact[N - i];
p[i] = a[l + 1];
for (int j = l + 2; j <= N; j++) a[j - 1] = a[j];
}
int ans = 0;
if (n <= 13) {
for (int i = 1; i <= n; i++) {
if ((i == 4 || i == 7) && (p[i] == 4 || p[i] == 7)) ans++;
}
} else {
for (int i = n; i > n - 13; i--) {
if (ok(i) && ok(p[i - n + 13] + n - 13)) {
ans++;
}
}
ans = ans + first(n - 13);
}
printf("%d\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long double PI = 3.14159265358979323;
const long long int linf = 1000111000111000111LL;
const long long int inf = 1011111111;
const long long int N = 100005;
vector<long long int> v1[10], v2;
set<long long int> st2;
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int test = 1;
while (test--) {
long long int n, k;
cin >> n >> k;
long long int fac[13];
fac[0] = 1;
for (long long int i = 1; i <= 12; i++) fac[i] = fac[i - 1] * i;
if (n <= 12) {
if (k > fac[n]) {
cout << "-1\n";
continue;
}
}
v1[1].push_back(4);
v1[1].push_back(7);
for (long long int i = 2; i <= 9; i++) {
for (long long int j = 0; j < (long long int)(v1[i - 1]).size(); j++) {
v1[i].push_back(v1[i - 1][j] * 10 + 4);
v1[i].push_back(v1[i - 1][j] * 10 + 7);
}
}
for (long long int i = 1; i <= 9; i++) {
for (long long int j = 0; j < (long long int)(v1[i]).size(); j++) {
v2.push_back({v1[i][j]});
st2.insert(v1[i][j]);
}
}
long long int ans = 0;
if (n > 13) {
long long int ele = n - 13;
long long int p1 =
upper_bound((v2).begin(), (v2).end(), ele) - v2.begin();
ans += p1;
}
set<long long int> st1;
for (long long int i = n; i >= max((long long int)1, n - 12); i--)
st1.insert(i);
long long int pos = max((long long int)1, n - 12);
while (!st1.empty()) {
long long int len = (long long int)(st1).size();
long long int q1 = fac[len - 1];
long long int num = k / q1;
if (k % q1 != 0) num++;
auto it = st1.begin();
for (long long int i = 0; i < num - 1; i++) it++;
long long int ele = *it;
st1.erase(ele);
if (st2.find(ele) != st2.end() and st2.find(pos) != st2.end()) ans++;
pos++;
k = k % q1;
if (k == 0) k = q1;
}
cout << ans << "\n";
}
cerr << '\n'
<< "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n";
}
| ### 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 double PI = 3.14159265358979323;
const long long int linf = 1000111000111000111LL;
const long long int inf = 1011111111;
const long long int N = 100005;
vector<long long int> v1[10], v2;
set<long long int> st2;
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int test = 1;
while (test--) {
long long int n, k;
cin >> n >> k;
long long int fac[13];
fac[0] = 1;
for (long long int i = 1; i <= 12; i++) fac[i] = fac[i - 1] * i;
if (n <= 12) {
if (k > fac[n]) {
cout << "-1\n";
continue;
}
}
v1[1].push_back(4);
v1[1].push_back(7);
for (long long int i = 2; i <= 9; i++) {
for (long long int j = 0; j < (long long int)(v1[i - 1]).size(); j++) {
v1[i].push_back(v1[i - 1][j] * 10 + 4);
v1[i].push_back(v1[i - 1][j] * 10 + 7);
}
}
for (long long int i = 1; i <= 9; i++) {
for (long long int j = 0; j < (long long int)(v1[i]).size(); j++) {
v2.push_back({v1[i][j]});
st2.insert(v1[i][j]);
}
}
long long int ans = 0;
if (n > 13) {
long long int ele = n - 13;
long long int p1 =
upper_bound((v2).begin(), (v2).end(), ele) - v2.begin();
ans += p1;
}
set<long long int> st1;
for (long long int i = n; i >= max((long long int)1, n - 12); i--)
st1.insert(i);
long long int pos = max((long long int)1, n - 12);
while (!st1.empty()) {
long long int len = (long long int)(st1).size();
long long int q1 = fac[len - 1];
long long int num = k / q1;
if (k % q1 != 0) num++;
auto it = st1.begin();
for (long long int i = 0; i < num - 1; i++) it++;
long long int ele = *it;
st1.erase(ele);
if (st2.find(ele) != st2.end() and st2.find(pos) != st2.end()) ans++;
pos++;
k = k % q1;
if (k == 0) k = q1;
}
cout << ans << "\n";
}
cerr << '\n'
<< "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n";
}
``` |
#include <bits/stdc++.h>
using namespace std;
int lucky[3000], cnt;
void dfs(long long now) {
if (now > 1000000000) return;
if (now) lucky[cnt++] = (int)now;
dfs(now * 10 + 4);
dfs(now * 10 + 7);
}
long long fact[20];
int n, m, num[20];
bool flag[20];
int find(int k) {
for (int i = 1;; i++) {
if (!flag[i]) {
k--;
if (k == 0) return i;
}
}
}
void sol(int n, int m) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
m -= max(1ll, fact[n - i]);
if (m <= 0) {
num[i] = find(j);
flag[num[i]] = true;
m += fact[n - i];
break;
}
}
}
}
bool ck(int i) {
while (i) {
if (i % 10 != 4 && i % 10 != 7) return false;
i /= 10;
}
return true;
}
int main() {
cnt = 0;
dfs(0);
fact[0] = 1;
for (int i = 1; i <= 15; i++) fact[i] = fact[i - 1] * i;
fact[0] = 0;
scanf("%d%d", &n, &m);
if (n <= 15 && fact[n] < m) {
printf("-1\n");
return 0;
}
for (int i = max(1, n - 14); i <= n; i++) {
if (fact[n - i + 1] >= m && fact[n - i] < m) {
sol(n - i + 1, m);
int ans = 0;
for (int j = 0; j < cnt; j++)
if (lucky[j] < i) ans++;
for (int j = i; j <= n; j++) {
if (ck(j) && ck(num[j - i + 1] + i - 1)) ans++;
}
printf("%d\n", ans);
break;
}
}
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;
int lucky[3000], cnt;
void dfs(long long now) {
if (now > 1000000000) return;
if (now) lucky[cnt++] = (int)now;
dfs(now * 10 + 4);
dfs(now * 10 + 7);
}
long long fact[20];
int n, m, num[20];
bool flag[20];
int find(int k) {
for (int i = 1;; i++) {
if (!flag[i]) {
k--;
if (k == 0) return i;
}
}
}
void sol(int n, int m) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
m -= max(1ll, fact[n - i]);
if (m <= 0) {
num[i] = find(j);
flag[num[i]] = true;
m += fact[n - i];
break;
}
}
}
}
bool ck(int i) {
while (i) {
if (i % 10 != 4 && i % 10 != 7) return false;
i /= 10;
}
return true;
}
int main() {
cnt = 0;
dfs(0);
fact[0] = 1;
for (int i = 1; i <= 15; i++) fact[i] = fact[i - 1] * i;
fact[0] = 0;
scanf("%d%d", &n, &m);
if (n <= 15 && fact[n] < m) {
printf("-1\n");
return 0;
}
for (int i = max(1, n - 14); i <= n; i++) {
if (fact[n - i + 1] >= m && fact[n - i] < m) {
sol(n - i + 1, m);
int ans = 0;
for (int j = 0; j < cnt; j++)
if (lucky[j] < i) ans++;
for (int j = i; j <= n; j++) {
if (ck(j) && ck(num[j - i + 1] + i - 1)) ans++;
}
printf("%d\n", ans);
break;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long fact[15];
vector<long long> luckies;
long long next(int x) {
return *lower_bound(luckies.begin(), luckies.end(), x);
}
long long to_lucky(int x, int l) {
long long res = 0;
for (int i = 0; i < (l); ++i)
if (x & (1 << i))
res = res * 10 + 4;
else
res = res * 10 + 7;
return res;
}
class A {
public:
A() {
fact[0] = 1;
for (int i = (1); i < (15); ++i) fact[i] = i * fact[i - 1];
}
} __A;
class B {
public:
B() {
for (int i = 0; i < (11); ++i)
for (int j = 0; j < (1 << i); ++j) luckies.push_back(to_lucky(j, i));
sort(luckies.begin(), luckies.end());
}
} __B;
int res = 0;
int offset = 0;
vector<int> cutit(int n, int k) {
if (n == 0) return vector<int>();
long long base = k / fact[n - 1];
long long mod = k % fact[n - 1];
vector<int> t = cutit(n - 1, mod);
t.push_back(base + 1);
return t;
}
vector<int> to_permutation(vector<int>& vi) {
vector<int> res;
map<int, int> num;
reverse(vi.begin(), vi.end());
for (int i = 0; i < (vi.size()); ++i) num[i + offset + 1];
for (int i = 0; i < (vi.size()); ++i) {
int d = vi[i];
int v = 0;
for (typeof(num.begin()) it = num.begin(); it != num.end(); ++it) {
d--;
if (d == 0) {
v = it->first;
num.erase(it);
break;
}
}
res.push_back(v);
}
return res;
}
int isLucky(int x) {
while (x) {
if (x % 10 != 4 && x % 10 != 7) return false;
x /= 10;
}
return true;
}
int main() {
int n, k;
cin >> n >> k;
if (n > 14) {
offset = n - 14;
res = lower_bound(luckies.begin(), luckies.end(), offset) - luckies.begin();
if (next(offset) > offset) res -= 1;
n = 14;
}
if (k > fact[n]) {
cout << -1;
return 0;
}
vector<int> vi = cutit(n, k - 1);
vi = to_permutation(vi);
for (int i = 0; i < (vi.size()); ++i)
if (isLucky(vi[i]) && isLucky(i + offset + 1)) res++;
cout << res;
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 fact[15];
vector<long long> luckies;
long long next(int x) {
return *lower_bound(luckies.begin(), luckies.end(), x);
}
long long to_lucky(int x, int l) {
long long res = 0;
for (int i = 0; i < (l); ++i)
if (x & (1 << i))
res = res * 10 + 4;
else
res = res * 10 + 7;
return res;
}
class A {
public:
A() {
fact[0] = 1;
for (int i = (1); i < (15); ++i) fact[i] = i * fact[i - 1];
}
} __A;
class B {
public:
B() {
for (int i = 0; i < (11); ++i)
for (int j = 0; j < (1 << i); ++j) luckies.push_back(to_lucky(j, i));
sort(luckies.begin(), luckies.end());
}
} __B;
int res = 0;
int offset = 0;
vector<int> cutit(int n, int k) {
if (n == 0) return vector<int>();
long long base = k / fact[n - 1];
long long mod = k % fact[n - 1];
vector<int> t = cutit(n - 1, mod);
t.push_back(base + 1);
return t;
}
vector<int> to_permutation(vector<int>& vi) {
vector<int> res;
map<int, int> num;
reverse(vi.begin(), vi.end());
for (int i = 0; i < (vi.size()); ++i) num[i + offset + 1];
for (int i = 0; i < (vi.size()); ++i) {
int d = vi[i];
int v = 0;
for (typeof(num.begin()) it = num.begin(); it != num.end(); ++it) {
d--;
if (d == 0) {
v = it->first;
num.erase(it);
break;
}
}
res.push_back(v);
}
return res;
}
int isLucky(int x) {
while (x) {
if (x % 10 != 4 && x % 10 != 7) return false;
x /= 10;
}
return true;
}
int main() {
int n, k;
cin >> n >> k;
if (n > 14) {
offset = n - 14;
res = lower_bound(luckies.begin(), luckies.end(), offset) - luckies.begin();
if (next(offset) > offset) res -= 1;
n = 14;
}
if (k > fact[n]) {
cout << -1;
return 0;
}
vector<int> vi = cutit(n, k - 1);
vi = to_permutation(vi);
for (int i = 0; i < (vi.size()); ++i)
if (isLucky(vi[i]) && isLucky(i + offset + 1)) res++;
cout << res;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, count = 0, ch = 0, c = 0;
int mas[15] = {1, 2, 6, 24, 120, 720,
5040, 40320, 362880, 3628800, 39916800, 479001600};
int isp[1000] = {}, dl = 0;
long long int mas2[1030] = {
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 int q = 1, numb = 11, num = 1, w = 0;
char mas3[10000] = {};
cin >> n >> k;
int start = 1;
if (n > 16) {
for (w = 0; mas2[w] < n - 16; count++, w++)
;
start = n - 16;
}
for (int j = start; j <= n; j++) {
for (; k <= mas[numb]; numb--)
;
if (numb + 1 >= n) {
cout << "-1";
return 0;
}
if (numb + 1 == n - j) {
if (numb >= 0) {
c = ((k - 1) / (mas[numb]));
k -= (mas[numb] * c);
}
}
int t = j - dl;
for (int i = 0; i < dl; i++) {
if (t + c >= isp[i]) c++;
}
ch = t + c;
mas3[j - 1] = ch + 48;
if (c > 0) {
isp[dl] = ch;
dl++;
sort(isp, isp + dl);
}
c = 0;
if (j == ch && ch == mas2[w]) {
count++;
w++;
}
if (j == mas2[w] && j != ch) {
for (int e = 0; ch >= mas2[e]; e++) {
if (ch == mas2[e]) count++;
}
w++;
}
}
cout << count;
}
| ### 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;
int main() {
int n, k, count = 0, ch = 0, c = 0;
int mas[15] = {1, 2, 6, 24, 120, 720,
5040, 40320, 362880, 3628800, 39916800, 479001600};
int isp[1000] = {}, dl = 0;
long long int mas2[1030] = {
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 int q = 1, numb = 11, num = 1, w = 0;
char mas3[10000] = {};
cin >> n >> k;
int start = 1;
if (n > 16) {
for (w = 0; mas2[w] < n - 16; count++, w++)
;
start = n - 16;
}
for (int j = start; j <= n; j++) {
for (; k <= mas[numb]; numb--)
;
if (numb + 1 >= n) {
cout << "-1";
return 0;
}
if (numb + 1 == n - j) {
if (numb >= 0) {
c = ((k - 1) / (mas[numb]));
k -= (mas[numb] * c);
}
}
int t = j - dl;
for (int i = 0; i < dl; i++) {
if (t + c >= isp[i]) c++;
}
ch = t + c;
mas3[j - 1] = ch + 48;
if (c > 0) {
isp[dl] = ch;
dl++;
sort(isp, isp + dl);
}
c = 0;
if (j == ch && ch == mas2[w]) {
count++;
w++;
}
if (j == mas2[w] && j != ch) {
for (int e = 0; ch >= mas2[e]; e++) {
if (ch == mas2[e]) count++;
}
w++;
}
}
cout << count;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long int fact[14];
long long int perm[13];
set<long long int> lucky;
void genfact() {
fact[0] = 1;
for (long long int i = 1; i < 14; ++i) fact[i] = i * fact[i - 1];
}
void genperm(long long int n, long long int k) {
list<long long int> a;
n = min(n, 13ll);
for (long long int i = 0; i < n; ++i) a.push_back(i + 1);
for (long long int i = 0; i < n; ++i) {
long long int j = k / fact[n - i - 1];
k %= fact[n - i - 1];
list<long long int>::iterator it = a.begin();
while (j--) it++;
perm[i] = *it;
a.erase(it);
}
}
long long int getperm(long long int n, long long int i) {
if (n <= 13)
return perm[i];
else {
if (i < n - 13)
return i + 1;
else
return perm[i - n + 13] + n - 13;
}
}
void genlucky(int level, long long int n) {
lucky.insert(n * 10 + 4);
lucky.insert(n * 10 + 7);
if (level == 10) return;
genlucky(level + 1, n * 10 + 4);
genlucky(level + 1, n * 10 + 7);
}
int main() {
long long int n, k;
cin >> n >> k;
k--;
genfact();
if (n <= 13 && fact[n] <= k) {
cout << -1 << endl;
} else {
genlucky(1, 0);
genperm(n, k);
long long int res = 0;
for (set<long long int>::iterator it = lucky.begin();
it != lucky.end() && *it <= n; ++it)
if (lucky.find(getperm(n, *it - 1)) != lucky.end()) ++res;
cout << res << 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;
long long int fact[14];
long long int perm[13];
set<long long int> lucky;
void genfact() {
fact[0] = 1;
for (long long int i = 1; i < 14; ++i) fact[i] = i * fact[i - 1];
}
void genperm(long long int n, long long int k) {
list<long long int> a;
n = min(n, 13ll);
for (long long int i = 0; i < n; ++i) a.push_back(i + 1);
for (long long int i = 0; i < n; ++i) {
long long int j = k / fact[n - i - 1];
k %= fact[n - i - 1];
list<long long int>::iterator it = a.begin();
while (j--) it++;
perm[i] = *it;
a.erase(it);
}
}
long long int getperm(long long int n, long long int i) {
if (n <= 13)
return perm[i];
else {
if (i < n - 13)
return i + 1;
else
return perm[i - n + 13] + n - 13;
}
}
void genlucky(int level, long long int n) {
lucky.insert(n * 10 + 4);
lucky.insert(n * 10 + 7);
if (level == 10) return;
genlucky(level + 1, n * 10 + 4);
genlucky(level + 1, n * 10 + 7);
}
int main() {
long long int n, k;
cin >> n >> k;
k--;
genfact();
if (n <= 13 && fact[n] <= k) {
cout << -1 << endl;
} else {
genlucky(1, 0);
genperm(n, k);
long long int res = 0;
for (set<long long int>::iterator it = lucky.begin();
it != lucky.end() && *it <= n; ++it)
if (lucky.find(getperm(n, *it - 1)) != lucky.end()) ++res;
cout << res << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long t[1500], n, k;
long long fac[20], base, tmp, rec[20], Ans;
bool used[20];
void init(long long x) {
if (x > 1e8) return;
t[++t[0]] = x * 10 + 4;
init(x * 10 + 4);
t[++t[0]] = x * 10 + 7;
init(x * 10 + 7);
}
bool check(long long x) {
for (long long i = 1; i <= t[0]; i++) {
if (t[i] == x) {
return true;
}
}
return false;
}
int main() {
t[0] = 0;
init(0);
sort(t + 1, t + 1 + t[0]);
scanf("%I64d%I64d", &n, &k);
Ans = 0;
base = 0;
if (n > 13) {
for (long long i = 1; i <= t[0]; i++) {
if (t[i] <= n - 13) Ans++;
}
base = n - 13;
n = 13;
}
fac[0] = 1;
for (long long i = 1; i <= 13; i++) fac[i] = 1LL * fac[i - 1] * i;
if (k > fac[n]) {
puts("-1");
return 0;
}
k--;
memset(used, false, sizeof(used));
memset(rec, 0, sizeof(rec));
for (long long i = n; i >= 1; i--) {
tmp = k / fac[i - 1];
k -= tmp * fac[i - 1];
for (long long j = 1; j <= n; j++) {
if (!used[j] && tmp == 0) {
used[j] = true;
rec[n - i + 1] = j;
break;
} else if (!used[j])
tmp--;
}
}
for (int i = 1; i <= n; i++) {
Ans += (check(base + i) && check(base + rec[i]));
}
printf("%I64d\n", Ans);
}
| ### 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;
long long t[1500], n, k;
long long fac[20], base, tmp, rec[20], Ans;
bool used[20];
void init(long long x) {
if (x > 1e8) return;
t[++t[0]] = x * 10 + 4;
init(x * 10 + 4);
t[++t[0]] = x * 10 + 7;
init(x * 10 + 7);
}
bool check(long long x) {
for (long long i = 1; i <= t[0]; i++) {
if (t[i] == x) {
return true;
}
}
return false;
}
int main() {
t[0] = 0;
init(0);
sort(t + 1, t + 1 + t[0]);
scanf("%I64d%I64d", &n, &k);
Ans = 0;
base = 0;
if (n > 13) {
for (long long i = 1; i <= t[0]; i++) {
if (t[i] <= n - 13) Ans++;
}
base = n - 13;
n = 13;
}
fac[0] = 1;
for (long long i = 1; i <= 13; i++) fac[i] = 1LL * fac[i - 1] * i;
if (k > fac[n]) {
puts("-1");
return 0;
}
k--;
memset(used, false, sizeof(used));
memset(rec, 0, sizeof(rec));
for (long long i = n; i >= 1; i--) {
tmp = k / fac[i - 1];
k -= tmp * fac[i - 1];
for (long long j = 1; j <= n; j++) {
if (!used[j] && tmp == 0) {
used[j] = true;
rec[n - i + 1] = j;
break;
} else if (!used[j])
tmp--;
}
}
for (int i = 1; i <= n; i++) {
Ans += (check(base + i) && check(base + rec[i]));
}
printf("%I64d\n", Ans);
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <class T>
T abs(T x) {
return x > 0 ? x : (-x);
}
template <class T>
T sqr(T x) {
return x * x;
}
vector<long long> gen() {
vector<long long> res;
for (int len = 1; len <= 11; ++len)
for (int mask = 0; mask < (1 << len); ++mask) {
long long cur = 0;
for (int i = 0; i < len; ++i)
cur = cur * 10LL + (long long)(((mask >> i) & 1) ? 7 : 4);
res.push_back(cur);
}
sort((res).begin(), (res).end());
return res;
}
long long f[40];
vector<int> solve(int n, int k) {
--k;
f[0] = 1;
for (int i = 1; i < 40; ++i) f[i] = f[i - 1] * (long long)i;
if (k >= f[n]) {
printf("-1\n");
exit(0);
}
vector<int> res;
vector<int> u(n, 0);
for (int i = 0; i < n; ++i) {
int cur = 0;
while (k >= f[n - i - 1]) ++cur, k -= f[n - i - 1];
int x = 0;
while (cur > 0) {
if (!u[x]) --cur;
++x;
}
while (u[x]) ++x;
u[x] = true;
res.push_back(x);
}
return res;
}
int main() {
vector<long long> v = gen();
int n, k;
cin >> n >> k;
int res = 0;
for (int i = 0; i < ((int)(v).size()); ++i)
if (v[i] < n - 14) ++res;
vector<int> w;
for (int i = max(n - 14, 1); i <= n; ++i) w.push_back(i);
vector<int> p = solve(((int)(w).size()), k);
vector<int> ww(((int)(w).size()));
for (int i = 0; i < ((int)(w).size()); ++i) ww[i] = w[p[i]];
for (int i = 0; i < ((int)(w).size()); ++i) {
int x = max(n - 14, 1) + i;
if (!binary_search((v).begin(), (v).end(), (long long)x)) continue;
if (!binary_search((v).begin(), (v).end(), (long long)ww[i])) continue;
++res;
}
cout << res << "\n";
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;
template <class T>
T abs(T x) {
return x > 0 ? x : (-x);
}
template <class T>
T sqr(T x) {
return x * x;
}
vector<long long> gen() {
vector<long long> res;
for (int len = 1; len <= 11; ++len)
for (int mask = 0; mask < (1 << len); ++mask) {
long long cur = 0;
for (int i = 0; i < len; ++i)
cur = cur * 10LL + (long long)(((mask >> i) & 1) ? 7 : 4);
res.push_back(cur);
}
sort((res).begin(), (res).end());
return res;
}
long long f[40];
vector<int> solve(int n, int k) {
--k;
f[0] = 1;
for (int i = 1; i < 40; ++i) f[i] = f[i - 1] * (long long)i;
if (k >= f[n]) {
printf("-1\n");
exit(0);
}
vector<int> res;
vector<int> u(n, 0);
for (int i = 0; i < n; ++i) {
int cur = 0;
while (k >= f[n - i - 1]) ++cur, k -= f[n - i - 1];
int x = 0;
while (cur > 0) {
if (!u[x]) --cur;
++x;
}
while (u[x]) ++x;
u[x] = true;
res.push_back(x);
}
return res;
}
int main() {
vector<long long> v = gen();
int n, k;
cin >> n >> k;
int res = 0;
for (int i = 0; i < ((int)(v).size()); ++i)
if (v[i] < n - 14) ++res;
vector<int> w;
for (int i = max(n - 14, 1); i <= n; ++i) w.push_back(i);
vector<int> p = solve(((int)(w).size()), k);
vector<int> ww(((int)(w).size()));
for (int i = 0; i < ((int)(w).size()); ++i) ww[i] = w[p[i]];
for (int i = 0; i < ((int)(w).size()); ++i) {
int x = max(n - 14, 1) + i;
if (!binary_search((v).begin(), (v).end(), (long long)x)) continue;
if (!binary_search((v).begin(), (v).end(), (long long)ww[i])) continue;
++res;
}
cout << res << "\n";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long fac[15];
vector<int> getKthPerm(vector<int> arr, long long k) {
if (k == 1) return arr;
int n = arr.size();
for (int i = 1; i <= n; i++) {
if (k <= fac[n - 1] * i) {
vector<int> brr;
for (int j = 0; j < n; j++) {
if (j + 1 == i) continue;
brr.push_back(arr[j]);
}
vector<int> tmp = getKthPerm(brr, k - fac[n - 1] * i + fac[n - 1]);
vector<int> ret;
ret.push_back(arr[i - 1]);
for (auto x : tmp) {
ret.push_back(x);
}
return ret;
}
}
}
vector<int> getLuckyNumbers() {
vector<int> ret({4, 7});
for (int i = 1; i < 9; i++) {
vector<int> tmp({4, 7});
for (auto x : ret) {
tmp.push_back(10 * x + 4);
tmp.push_back(10 * x + 7);
}
ret = tmp;
}
sort(ret.begin(), ret.end());
return ret;
}
bool isLucky(int n) {
while (n) {
int d = n % 10;
if (d != 4 && d != 7) return false;
n /= 10;
}
return true;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout << setprecision(32);
long long n, k;
cin >> n >> k;
fac[0] = 1;
for (int i = 1; i < 15; i++) {
fac[i] = fac[i - 1] * i;
}
if (n < 15) {
if (fac[n] < k) {
cout << -1 << endl;
return 0;
}
vector<int> arr;
for (int i = 1; i <= n; i++) {
arr.push_back(i);
}
arr = getKthPerm(arr, k);
int ans = 0;
if (n >= 4 && isLucky(arr[3])) ans++;
if (n >= 7 && isLucky(arr[6])) ans++;
cout << ans << endl;
} else {
vector<int> arr;
for (int i = 1; i < 15; i++) {
arr.push_back(n - 14 + i);
}
arr = getKthPerm(arr, k);
vector<int> lk = getLuckyNumbers();
int ans = 0;
for (auto x : lk) {
if (x > n) break;
if (x < n - 13)
ans++;
else if (isLucky(arr[x - n + 13]))
ans++;
}
cout << ans << 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 fac[15];
vector<int> getKthPerm(vector<int> arr, long long k) {
if (k == 1) return arr;
int n = arr.size();
for (int i = 1; i <= n; i++) {
if (k <= fac[n - 1] * i) {
vector<int> brr;
for (int j = 0; j < n; j++) {
if (j + 1 == i) continue;
brr.push_back(arr[j]);
}
vector<int> tmp = getKthPerm(brr, k - fac[n - 1] * i + fac[n - 1]);
vector<int> ret;
ret.push_back(arr[i - 1]);
for (auto x : tmp) {
ret.push_back(x);
}
return ret;
}
}
}
vector<int> getLuckyNumbers() {
vector<int> ret({4, 7});
for (int i = 1; i < 9; i++) {
vector<int> tmp({4, 7});
for (auto x : ret) {
tmp.push_back(10 * x + 4);
tmp.push_back(10 * x + 7);
}
ret = tmp;
}
sort(ret.begin(), ret.end());
return ret;
}
bool isLucky(int n) {
while (n) {
int d = n % 10;
if (d != 4 && d != 7) return false;
n /= 10;
}
return true;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout << setprecision(32);
long long n, k;
cin >> n >> k;
fac[0] = 1;
for (int i = 1; i < 15; i++) {
fac[i] = fac[i - 1] * i;
}
if (n < 15) {
if (fac[n] < k) {
cout << -1 << endl;
return 0;
}
vector<int> arr;
for (int i = 1; i <= n; i++) {
arr.push_back(i);
}
arr = getKthPerm(arr, k);
int ans = 0;
if (n >= 4 && isLucky(arr[3])) ans++;
if (n >= 7 && isLucky(arr[6])) ans++;
cout << ans << endl;
} else {
vector<int> arr;
for (int i = 1; i < 15; i++) {
arr.push_back(n - 14 + i);
}
arr = getKthPerm(arr, k);
vector<int> lk = getLuckyNumbers();
int ans = 0;
for (auto x : lk) {
if (x > n) break;
if (x < n - 13)
ans++;
else if (isLucky(arr[x - n + 13]))
ans++;
}
cout << ans << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, kk, ans, a[15];
long long ci[15];
bool ok;
bool check(int i) {
while (i) {
if ((i % 10 != 4) && (i % 10 != 7)) return false;
i = i / 10;
}
return true;
}
int cal(int r) {
long long now = 0;
int ans = 0, a[11] = {0};
while (now < r) {
int j = 1;
while ((j <= a[0]) && (a[j] == 7)) j++;
if (j == a[0] + 1) {
a[++a[0]] = 4;
now += ci[j - 1] * 4;
} else {
a[j] = 7;
now += ci[j - 1] * 3;
}
for (int i = (1); i <= (j - 1); i++) {
a[i] = 4;
now -= ci[i - 1] * 3;
}
if (now <= r) ans++;
}
return ans;
}
int main() {
scanf("%d%d", &n, &kk);
kk--;
ci[0] = 1;
for (int i = (1); i <= (9); i++) ci[i] = ci[i - 1] * 10;
ans += cal(n - 13);
for (int i = (max(n - 12, 1)); i <= (n); i++) a[++a[0]] = i;
for (int i = (max(n - 12, 1)); i <= (n); i++) {
int t = 1;
ok = false;
for (int j = (1); j <= (n - i); j++) t *= j;
for (int k = (1); k <= (a[0]); k++)
if (a[k])
if (kk >= t)
kk -= t;
else {
ans += check(i) && check(a[k]);
a[k] = 0;
ok = true;
break;
}
if (!ok) break;
}
if (ok)
printf("%d", ans);
else
printf("-1");
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;
int n, kk, ans, a[15];
long long ci[15];
bool ok;
bool check(int i) {
while (i) {
if ((i % 10 != 4) && (i % 10 != 7)) return false;
i = i / 10;
}
return true;
}
int cal(int r) {
long long now = 0;
int ans = 0, a[11] = {0};
while (now < r) {
int j = 1;
while ((j <= a[0]) && (a[j] == 7)) j++;
if (j == a[0] + 1) {
a[++a[0]] = 4;
now += ci[j - 1] * 4;
} else {
a[j] = 7;
now += ci[j - 1] * 3;
}
for (int i = (1); i <= (j - 1); i++) {
a[i] = 4;
now -= ci[i - 1] * 3;
}
if (now <= r) ans++;
}
return ans;
}
int main() {
scanf("%d%d", &n, &kk);
kk--;
ci[0] = 1;
for (int i = (1); i <= (9); i++) ci[i] = ci[i - 1] * 10;
ans += cal(n - 13);
for (int i = (max(n - 12, 1)); i <= (n); i++) a[++a[0]] = i;
for (int i = (max(n - 12, 1)); i <= (n); i++) {
int t = 1;
ok = false;
for (int j = (1); j <= (n - i); j++) t *= j;
for (int k = (1); k <= (a[0]); k++)
if (a[k])
if (kk >= t)
kk -= t;
else {
ans += check(i) && check(a[k]);
a[k] = 0;
ok = true;
break;
}
if (!ok) break;
}
if (ok)
printf("%d", ans);
else
printf("-1");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 20;
int n;
long long int k, fat[MAX], tmp[MAX];
vector<long long int> lk;
int lucky(long long int x) {
while (x > 0LL) {
long long int a = (x % 10LL);
if (a != 4LL && a != 7LL) return 0;
x /= 10LL;
}
return 1;
}
void lk_gen(int pos) {
if (pos == 9) {
bool allzero = true, valido = true;
for (int i = 0; i < 9; i++) {
if (tmp[i] != 0LL) allzero = false;
if (tmp[i] == 0LL && allzero == false) valido = false;
}
if (allzero || !valido) return;
long long int x = 0LL, base = 1LL;
for (int i = 8; i >= 0; i--) {
x += (tmp[i] * base);
base *= 10LL;
}
lk.push_back(x);
return;
}
tmp[pos] = 0LL;
lk_gen(pos + 1);
tmp[pos] = 4LL;
lk_gen(pos + 1);
tmp[pos] = 7LL;
lk_gen(pos + 1);
return;
}
void solve(int ini) {
long long int cnt = k, v[20];
int jump, ans = 0;
for (int i = 0; i < n - ini + 1; i++) v[i] = i + ini;
for (int i = ini; i <= n; i++) {
jump = 1;
while (fat[n - i] < cnt) {
swap(v[i - ini], v[i + jump - ini]);
jump++;
cnt -= fat[n - i];
}
}
for (int i = 0; i < n - ini + 1; i++)
if (lucky(i + ini) && lucky(v[i])) ans++;
for (int i = (int)lk.size() - 1; i >= 0; i--) {
if (lk[i] < ini) {
ans += i + 1;
break;
}
}
printf("%d\n", ans);
}
int main() {
scanf(" %d %lld", &n, &k);
fat[0] = 1;
for (int i = 1; i <= 13; i++) fat[i] = (long long int)i * fat[i - 1];
lk.clear();
lk_gen(0);
if (n <= 13 && fat[n] < k)
printf("-1\n");
else {
int ini;
for (ini = n; ini >= 0; ini--)
if (fat[n - ini + 1] >= k) break;
solve(ini);
}
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 int MAX = 20;
int n;
long long int k, fat[MAX], tmp[MAX];
vector<long long int> lk;
int lucky(long long int x) {
while (x > 0LL) {
long long int a = (x % 10LL);
if (a != 4LL && a != 7LL) return 0;
x /= 10LL;
}
return 1;
}
void lk_gen(int pos) {
if (pos == 9) {
bool allzero = true, valido = true;
for (int i = 0; i < 9; i++) {
if (tmp[i] != 0LL) allzero = false;
if (tmp[i] == 0LL && allzero == false) valido = false;
}
if (allzero || !valido) return;
long long int x = 0LL, base = 1LL;
for (int i = 8; i >= 0; i--) {
x += (tmp[i] * base);
base *= 10LL;
}
lk.push_back(x);
return;
}
tmp[pos] = 0LL;
lk_gen(pos + 1);
tmp[pos] = 4LL;
lk_gen(pos + 1);
tmp[pos] = 7LL;
lk_gen(pos + 1);
return;
}
void solve(int ini) {
long long int cnt = k, v[20];
int jump, ans = 0;
for (int i = 0; i < n - ini + 1; i++) v[i] = i + ini;
for (int i = ini; i <= n; i++) {
jump = 1;
while (fat[n - i] < cnt) {
swap(v[i - ini], v[i + jump - ini]);
jump++;
cnt -= fat[n - i];
}
}
for (int i = 0; i < n - ini + 1; i++)
if (lucky(i + ini) && lucky(v[i])) ans++;
for (int i = (int)lk.size() - 1; i >= 0; i--) {
if (lk[i] < ini) {
ans += i + 1;
break;
}
}
printf("%d\n", ans);
}
int main() {
scanf(" %d %lld", &n, &k);
fat[0] = 1;
for (int i = 1; i <= 13; i++) fat[i] = (long long int)i * fat[i - 1];
lk.clear();
lk_gen(0);
if (n <= 13 && fat[n] < k)
printf("-1\n");
else {
int ini;
for (ini = n; ini >= 0; ini--)
if (fat[n - ini + 1] >= k) break;
solve(ini);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 50;
const int inf = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int n, k;
int fac[15] = {1, 1, 2, 6, 24, 120, 720,
5040, 40320, 362880, 3628800, 39916800, 479001600};
int Hash[20], p[20];
vector<long long> V;
long long trans(string s) {
long long res = 0;
for (int i = 0; s[i]; i++) res = res * 10 + s[i] - '0';
return res;
}
void dfs(string p) {
if (p.length() == 10) return;
V.push_back(trans(p + '4'));
V.push_back(trans(p + '7'));
dfs(p + '4');
dfs(p + '7');
}
void CantorReverse(int index, int t) {
index--;
int i, j;
for (i = 0; i < t; i++) {
int tmp = index / fac[t - 1 - i];
for (j = 0; j <= tmp; j++)
if (Hash[j]) tmp++;
p[i] = tmp + 1;
Hash[tmp] = 1;
index %= fac[t - 1 - i];
}
return;
}
int main() {
cin >> n >> k;
if (n <= 12 && k > fac[n])
cout << "-1\n";
else {
int amount = 13;
for (int i = 1; i <= 12; i++)
if (k < fac[i]) {
amount = i;
break;
}
CantorReverse(k, amount);
int dis = n - amount;
dfs("");
sort(V.begin(), V.end());
int i = 0;
int ans = 0;
for (int i = 0; i < V.size(); i++) {
if (V[i] > n) {
break;
}
if (V[i] <= dis)
ans++;
else {
int num = p[V[i] - dis - 1] + dis;
for (int j = 0; j < V.size() && V[j] <= n; j++)
if (num == V[j]) ans++;
}
}
cout << ans << 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;
const int maxn = 1e5 + 50;
const int inf = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int n, k;
int fac[15] = {1, 1, 2, 6, 24, 120, 720,
5040, 40320, 362880, 3628800, 39916800, 479001600};
int Hash[20], p[20];
vector<long long> V;
long long trans(string s) {
long long res = 0;
for (int i = 0; s[i]; i++) res = res * 10 + s[i] - '0';
return res;
}
void dfs(string p) {
if (p.length() == 10) return;
V.push_back(trans(p + '4'));
V.push_back(trans(p + '7'));
dfs(p + '4');
dfs(p + '7');
}
void CantorReverse(int index, int t) {
index--;
int i, j;
for (i = 0; i < t; i++) {
int tmp = index / fac[t - 1 - i];
for (j = 0; j <= tmp; j++)
if (Hash[j]) tmp++;
p[i] = tmp + 1;
Hash[tmp] = 1;
index %= fac[t - 1 - i];
}
return;
}
int main() {
cin >> n >> k;
if (n <= 12 && k > fac[n])
cout << "-1\n";
else {
int amount = 13;
for (int i = 1; i <= 12; i++)
if (k < fac[i]) {
amount = i;
break;
}
CantorReverse(k, amount);
int dis = n - amount;
dfs("");
sort(V.begin(), V.end());
int i = 0;
int ans = 0;
for (int i = 0; i < V.size(); i++) {
if (V[i] > n) {
break;
}
if (V[i] <= dis)
ans++;
else {
int num = p[V[i] - dis - 1] + dis;
for (int j = 0; j < V.size() && V[j] <= n; j++)
if (num == V[j]) ans++;
}
}
cout << ans << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<long long> v;
void CalcLucky(long long num, long long limit) {
long long temp4;
temp4 = num * 10 + 4;
long long temp7;
temp7 = num * 10 + 7;
v.push_back(temp4);
v.push_back(temp7);
if (temp4 <= limit) CalcLucky(temp4, limit);
if (temp7 <= limit) CalcLucky(temp7, limit);
}
int main() {
long long fact[20] = {0};
fact[0] = 1;
for (int i = 1; i <= 13; i++) {
fact[i] = i * fact[i - 1];
}
long long n, k;
cin >> n >> k;
if (n <= 13) {
if (k > fact[n]) {
cout << -1 << endl;
return 0;
}
}
k = k - 1;
long long limit;
for (int i = 0; i <= 13; i++) {
if (fact[i] > k) {
limit = i;
break;
}
}
int numbers[20];
int indices[20];
int divisor = 1;
for (int place = 1; place <= n; place++) {
if ((k / divisor) == 0) break;
indices[limit - place] = (k / divisor) % place;
divisor *= place;
}
long long stIn = n - limit + 1;
for (int i = 0; i < limit; i++) numbers[i] = stIn + i;
for (int i = 0; i < limit; i++) {
long long index = indices[i] + i;
if (index != i) {
long long temp = numbers[index];
for (int j = index; j > i; j--) numbers[j] = numbers[j - 1];
numbers[i] = temp;
}
}
CalcLucky(0, stIn);
int LuckyNum = 0;
sort(v.begin(), v.end());
for (int i = 0; i < v.size(); i++)
if (v[i] < stIn) LuckyNum++;
for (int i = 0; i < limit; i++) {
long long index = stIn + i;
long long num = numbers[i];
if (binary_search(v.begin(), v.end(), index) &&
binary_search(v.begin(), v.end(), num))
LuckyNum++;
}
cout << LuckyNum << 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;
vector<long long> v;
void CalcLucky(long long num, long long limit) {
long long temp4;
temp4 = num * 10 + 4;
long long temp7;
temp7 = num * 10 + 7;
v.push_back(temp4);
v.push_back(temp7);
if (temp4 <= limit) CalcLucky(temp4, limit);
if (temp7 <= limit) CalcLucky(temp7, limit);
}
int main() {
long long fact[20] = {0};
fact[0] = 1;
for (int i = 1; i <= 13; i++) {
fact[i] = i * fact[i - 1];
}
long long n, k;
cin >> n >> k;
if (n <= 13) {
if (k > fact[n]) {
cout << -1 << endl;
return 0;
}
}
k = k - 1;
long long limit;
for (int i = 0; i <= 13; i++) {
if (fact[i] > k) {
limit = i;
break;
}
}
int numbers[20];
int indices[20];
int divisor = 1;
for (int place = 1; place <= n; place++) {
if ((k / divisor) == 0) break;
indices[limit - place] = (k / divisor) % place;
divisor *= place;
}
long long stIn = n - limit + 1;
for (int i = 0; i < limit; i++) numbers[i] = stIn + i;
for (int i = 0; i < limit; i++) {
long long index = indices[i] + i;
if (index != i) {
long long temp = numbers[index];
for (int j = index; j > i; j--) numbers[j] = numbers[j - 1];
numbers[i] = temp;
}
}
CalcLucky(0, stIn);
int LuckyNum = 0;
sort(v.begin(), v.end());
for (int i = 0; i < v.size(); i++)
if (v[i] < stIn) LuckyNum++;
for (int i = 0; i < limit; i++) {
long long index = stIn + i;
long long num = numbers[i];
if (binary_search(v.begin(), v.end(), index) &&
binary_search(v.begin(), v.end(), num))
LuckyNum++;
}
cout << LuckyNum << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, p;
long long fac[30];
long long Pow[30];
long long who[30];
int v[30];
set<long long> Set;
set<int> Hash;
void dfs(int dep, long long now) {
if (dep < 0) {
Set.insert(now);
return;
}
dfs(dep - 1, now + Pow[dep] * 4);
dfs(dep - 1, now + Pow[dep] * 7);
}
void init() {
Pow[0] = 1;
for (int i = 1; i < 12; i++) Pow[i] = Pow[i - 1] * 10;
Set.clear();
for (int st = 0; st <= 9; st++) dfs(st, 0);
}
void gao() {
Hash.clear();
for (int i = p; i >= 0; i--) {
int Num = who[i] + 1;
int x = n - p - 1;
for (int j = 0; j < Num; j++) {
++x;
while (Hash.count(x)) x++;
}
Hash.insert(x);
who[i] = x;
}
}
bool Luck(long long x) {
char buf[30];
sprintf(buf, "%I64d", x);
int L = strlen(buf);
for (int i = 0; i < L; i++)
if (buf[i] != '4' && buf[i] != '7') return false;
return true;
}
int main() {
init();
cin >> n >> m;
m--;
fac[0] = 1;
for (int i = 1;; i++) {
fac[i] = fac[i - 1] * i;
if (fac[i] > m) {
p = i - 1;
break;
}
}
long long tmp = m;
for (int i = p; i >= 0; i--) {
who[i] = tmp / fac[i];
tmp %= fac[i];
}
if (p >= n) {
puts("-1");
return 0;
}
gao();
long long ans = 0;
for (int i = 0; i <= p; i++)
if (Luck(n - i) && Luck(who[i])) ans++;
for (set<long long>::iterator it = Set.begin(); it != Set.end(); it++)
if (*it <= n && *it <= n - (p + 1)) ans++;
cout << ans << 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;
int n, m, p;
long long fac[30];
long long Pow[30];
long long who[30];
int v[30];
set<long long> Set;
set<int> Hash;
void dfs(int dep, long long now) {
if (dep < 0) {
Set.insert(now);
return;
}
dfs(dep - 1, now + Pow[dep] * 4);
dfs(dep - 1, now + Pow[dep] * 7);
}
void init() {
Pow[0] = 1;
for (int i = 1; i < 12; i++) Pow[i] = Pow[i - 1] * 10;
Set.clear();
for (int st = 0; st <= 9; st++) dfs(st, 0);
}
void gao() {
Hash.clear();
for (int i = p; i >= 0; i--) {
int Num = who[i] + 1;
int x = n - p - 1;
for (int j = 0; j < Num; j++) {
++x;
while (Hash.count(x)) x++;
}
Hash.insert(x);
who[i] = x;
}
}
bool Luck(long long x) {
char buf[30];
sprintf(buf, "%I64d", x);
int L = strlen(buf);
for (int i = 0; i < L; i++)
if (buf[i] != '4' && buf[i] != '7') return false;
return true;
}
int main() {
init();
cin >> n >> m;
m--;
fac[0] = 1;
for (int i = 1;; i++) {
fac[i] = fac[i - 1] * i;
if (fac[i] > m) {
p = i - 1;
break;
}
}
long long tmp = m;
for (int i = p; i >= 0; i--) {
who[i] = tmp / fac[i];
tmp %= fac[i];
}
if (p >= n) {
puts("-1");
return 0;
}
gao();
long long ans = 0;
for (int i = 0; i <= p; i++)
if (Luck(n - i) && Luck(who[i])) ans++;
for (set<long long>::iterator it = Set.begin(); it != Set.end(); it++)
if (*it <= n && *it <= n - (p + 1)) ans++;
cout << ans << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long bigmod(long long b, long long p) {
if (p == 0) return 1;
long long my = bigmod(b, p / 2);
my *= my;
my %= 1000000007;
if (p & 1) my *= b, my %= 1000000007;
return my;
}
int setb(int n, int pos) { return n = n | (1 << pos); }
int resb(int n, int pos) { return n = n & ~(1 << pos); }
bool checkb(int n, int pos) { return (bool)(n & (1 << pos)); }
vector<long long> v;
long long n, k, len;
void fun(long long sum) {
if (sum > 1000000000) return;
v.push_back(sum);
fun(sum * 10 + 4);
fun(sum * 10 + 7);
}
long long last, ara[20], d, taken[20], place[20], fact[20];
bool oka(long long x) {
while (x) {
long long my = x % 10;
x /= 10;
if (!(my == 4 || my == 7)) return 0;
}
return 1;
}
long long go() {
long long x = k, i, j;
for (i = d; i >= 1; i--) {
long long val = x / fact[i - 1];
if (x % fact[i - 1]) val++;
long long y = 0, idx = 0;
for (j = 1; j <= d; j++) {
if (taken[j] == 0) y++;
if (y == val) {
idx = j;
break;
}
}
taken[idx] = 1;
place[d - i + 1] = idx;
x -= ((val - 1) * fact[i - 1]);
}
long long cnt = 0;
if (last) {
for (i = 1; i < len; i++) {
long long my = v[i];
if (my > last) break;
cnt++;
}
for (i = 1; i <= d; i++) {
long long my = place[i] + last;
long long pos = last + i;
if (oka(pos) && oka(my)) cnt++;
}
return cnt;
} else {
for (i = 1; i <= d; i++) {
long long my = place[i] + last;
long long pos = last + i;
if (oka(pos) && oka(my)) cnt++;
}
return cnt;
}
}
int main() {
fun(0);
len = v.size();
sort(v.begin(), v.end());
long long i;
fact[0] = 1;
for (i = 1; i <= 16; i++) fact[i] = fact[i - 1] * i;
scanf("%lld %lld", &n, &k);
if (n <= 15) {
if (fact[n] < k) {
puts("-1");
return 0;
}
}
if (n <= 15) {
for (i = 1; i <= n; i++) ara[i] = i;
d = n;
long long ret = go();
printf("%lld", ret);
printf("\n");
} else {
last = n - 15;
for (i = 1; i <= 15; i++) ara[i] = i;
d = 15;
long long ret = go();
printf("%lld", ret);
printf("\n");
}
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 bigmod(long long b, long long p) {
if (p == 0) return 1;
long long my = bigmod(b, p / 2);
my *= my;
my %= 1000000007;
if (p & 1) my *= b, my %= 1000000007;
return my;
}
int setb(int n, int pos) { return n = n | (1 << pos); }
int resb(int n, int pos) { return n = n & ~(1 << pos); }
bool checkb(int n, int pos) { return (bool)(n & (1 << pos)); }
vector<long long> v;
long long n, k, len;
void fun(long long sum) {
if (sum > 1000000000) return;
v.push_back(sum);
fun(sum * 10 + 4);
fun(sum * 10 + 7);
}
long long last, ara[20], d, taken[20], place[20], fact[20];
bool oka(long long x) {
while (x) {
long long my = x % 10;
x /= 10;
if (!(my == 4 || my == 7)) return 0;
}
return 1;
}
long long go() {
long long x = k, i, j;
for (i = d; i >= 1; i--) {
long long val = x / fact[i - 1];
if (x % fact[i - 1]) val++;
long long y = 0, idx = 0;
for (j = 1; j <= d; j++) {
if (taken[j] == 0) y++;
if (y == val) {
idx = j;
break;
}
}
taken[idx] = 1;
place[d - i + 1] = idx;
x -= ((val - 1) * fact[i - 1]);
}
long long cnt = 0;
if (last) {
for (i = 1; i < len; i++) {
long long my = v[i];
if (my > last) break;
cnt++;
}
for (i = 1; i <= d; i++) {
long long my = place[i] + last;
long long pos = last + i;
if (oka(pos) && oka(my)) cnt++;
}
return cnt;
} else {
for (i = 1; i <= d; i++) {
long long my = place[i] + last;
long long pos = last + i;
if (oka(pos) && oka(my)) cnt++;
}
return cnt;
}
}
int main() {
fun(0);
len = v.size();
sort(v.begin(), v.end());
long long i;
fact[0] = 1;
for (i = 1; i <= 16; i++) fact[i] = fact[i - 1] * i;
scanf("%lld %lld", &n, &k);
if (n <= 15) {
if (fact[n] < k) {
puts("-1");
return 0;
}
}
if (n <= 15) {
for (i = 1; i <= n; i++) ara[i] = i;
d = n;
long long ret = go();
printf("%lld", ret);
printf("\n");
} else {
last = n - 15;
for (i = 1; i <= 15; i++) ara[i] = i;
d = 15;
long long ret = go();
printf("%lld", ret);
printf("\n");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 4e3 + 5;
const long long mod = 1e9 + 7;
long long power(long long a, long long b, long long c) {
long long ret = 1;
while (b > 0) {
if (b % 2 == 1) ret = (ret * a) % c;
b /= 2;
a = (a * a) % c;
}
return ret;
}
int fact[13];
vector<int> values;
void pre() {
fact[1] = fact[0] = 1;
for (int i = 2; i <= 12; i++) {
fact[i] = fact[i - 1] * i;
}
}
void func(int ind, int val) {
if (ind == 9) return;
values.push_back(val * 10 + 4);
values.push_back(val * 10 + 7);
func(ind + 1, val * 10 + 4);
func(ind + 1, val * 10 + 7);
}
int main() {
pre();
int n, k;
cin >> n >> k;
k--;
func(0, 0);
set<int> se;
sort(values.begin(), values.end());
for (int it : values) {
se.insert(it);
}
vector<int> vec, res;
int fl = -1;
if (n <= 12 and fact[n] <= k) {
cout << "-1\n";
return 0;
}
for (int i = 12; i >= 0; i--) {
if (k >= fact[i]) {
if (fl == -1) {
for (int j = 0; j <= i; j++) {
vec.push_back(j + 1);
}
fl = i;
}
int times = k / fact[i];
res.push_back(vec[times] + n - fl - 1);
vec.erase(vec.begin() + times, vec.begin() + times + 1);
k -= times * fact[i];
} else if (fl >= 0) {
res.push_back(vec[0] + n - fl - 1);
vec.erase(vec.begin(), vec.begin() + 1);
}
}
int ret = 0;
for (int it : values) {
if (it < n - fl) {
ret++;
}
}
int ind = n - fl;
for (auto it : res) {
if (se.find(it) != se.end() and se.find(ind) != se.end()) {
ret++;
}
ind++;
}
cout << ret << 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;
const int N = 4e3 + 5;
const long long mod = 1e9 + 7;
long long power(long long a, long long b, long long c) {
long long ret = 1;
while (b > 0) {
if (b % 2 == 1) ret = (ret * a) % c;
b /= 2;
a = (a * a) % c;
}
return ret;
}
int fact[13];
vector<int> values;
void pre() {
fact[1] = fact[0] = 1;
for (int i = 2; i <= 12; i++) {
fact[i] = fact[i - 1] * i;
}
}
void func(int ind, int val) {
if (ind == 9) return;
values.push_back(val * 10 + 4);
values.push_back(val * 10 + 7);
func(ind + 1, val * 10 + 4);
func(ind + 1, val * 10 + 7);
}
int main() {
pre();
int n, k;
cin >> n >> k;
k--;
func(0, 0);
set<int> se;
sort(values.begin(), values.end());
for (int it : values) {
se.insert(it);
}
vector<int> vec, res;
int fl = -1;
if (n <= 12 and fact[n] <= k) {
cout << "-1\n";
return 0;
}
for (int i = 12; i >= 0; i--) {
if (k >= fact[i]) {
if (fl == -1) {
for (int j = 0; j <= i; j++) {
vec.push_back(j + 1);
}
fl = i;
}
int times = k / fact[i];
res.push_back(vec[times] + n - fl - 1);
vec.erase(vec.begin() + times, vec.begin() + times + 1);
k -= times * fact[i];
} else if (fl >= 0) {
res.push_back(vec[0] + n - fl - 1);
vec.erase(vec.begin(), vec.begin() + 1);
}
}
int ret = 0;
for (int it : values) {
if (it < n - fl) {
ret++;
}
}
int ind = n - fl;
for (auto it : res) {
if (se.find(it) != se.end() and se.find(ind) != se.end()) {
ret++;
}
ind++;
}
cout << ret << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 2005;
long long is_luck[N];
long long permu[20];
int get_luck();
int get_permu(long long, long long, long long, long long);
bool judge_luck(long long);
int main() {
long long n, m, res = 0;
cin >> n >> m;
int cnt = 0;
cnt = get_luck();
long long remain = 1, data = 1;
while (data < m) {
data *= (remain++);
}
remain--;
if (remain > n) {
puts("-1");
return 0;
}
long long end = n - remain;
for (int i = 0; i < cnt; i++) {
if (is_luck[i] <= end)
res++;
else
break;
}
cnt = get_permu(remain, end + 1, m - 1, data);
for (int i = 0; i < cnt; i++) {
if (judge_luck(permu[i]) && judge_luck(i + end + 1)) res++;
}
cout << res << endl;
return 0;
}
int get_luck() {
int cnt = 0;
is_luck[cnt++] = 4;
is_luck[cnt++] = 7;
for (int i = 0; i < cnt; i++) {
if (is_luck[cnt - 1] > (1e9))
break;
else {
is_luck[cnt++] = (is_luck[i] * 10 + 4);
is_luck[cnt++] = (is_luck[i] * 10 + 7);
}
}
return cnt;
}
int get_permu(long long num, long long begin, long long m, long long data) {
int cnt = 0;
set<long long> choice;
for (int i = 0; i < num; i++) choice.insert(begin + i);
for (int i = num; i > 0; i--) {
data /= i;
int pos = m / (data);
m -= (pos) * (data);
set<long long>::iterator it = choice.begin();
while (it != choice.end() && pos) {
it++;
pos--;
};
permu[cnt++] = (*it);
choice.erase(it);
}
return cnt;
}
bool judge_luck(long long n) {
while (n) {
if (n % 10 != 4 && n % 10 != 7)
return false;
else
n /= 10;
}
return true;
}
| ### 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 N = 2005;
long long is_luck[N];
long long permu[20];
int get_luck();
int get_permu(long long, long long, long long, long long);
bool judge_luck(long long);
int main() {
long long n, m, res = 0;
cin >> n >> m;
int cnt = 0;
cnt = get_luck();
long long remain = 1, data = 1;
while (data < m) {
data *= (remain++);
}
remain--;
if (remain > n) {
puts("-1");
return 0;
}
long long end = n - remain;
for (int i = 0; i < cnt; i++) {
if (is_luck[i] <= end)
res++;
else
break;
}
cnt = get_permu(remain, end + 1, m - 1, data);
for (int i = 0; i < cnt; i++) {
if (judge_luck(permu[i]) && judge_luck(i + end + 1)) res++;
}
cout << res << endl;
return 0;
}
int get_luck() {
int cnt = 0;
is_luck[cnt++] = 4;
is_luck[cnt++] = 7;
for (int i = 0; i < cnt; i++) {
if (is_luck[cnt - 1] > (1e9))
break;
else {
is_luck[cnt++] = (is_luck[i] * 10 + 4);
is_luck[cnt++] = (is_luck[i] * 10 + 7);
}
}
return cnt;
}
int get_permu(long long num, long long begin, long long m, long long data) {
int cnt = 0;
set<long long> choice;
for (int i = 0; i < num; i++) choice.insert(begin + i);
for (int i = num; i > 0; i--) {
data /= i;
int pos = m / (data);
m -= (pos) * (data);
set<long long>::iterator it = choice.begin();
while (it != choice.end() && pos) {
it++;
pos--;
};
permu[cnt++] = (*it);
choice.erase(it);
}
return cnt;
}
bool judge_luck(long long n) {
while (n) {
if (n % 10 != 4 && n % 10 != 7)
return false;
else
n /= 10;
}
return true;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long n, k, num = 1;
long long kol = 1, RES = 0;
vector<int> mas, other;
vector<int> all;
int main() {
cin >> n >> k;
long long num = 4;
while (num <= n) {
all.push_back(num);
string s = "";
int nn = num;
while (nn) {
if (nn % 10 == 4)
s = s + '4';
else
s = s + '7';
nn /= 10;
}
reverse(s.begin(), s.end());
int ch = -1;
for (int i = 0; i < s.length(); ++i)
if (s[i] == '4') ch = i;
if (ch == -1) {
for (int i = 0; i < s.length(); ++i) s[i] = '4';
s = s + '4';
} else {
s[ch] = '7';
for (int i = ch + 1; i < s.length(); ++i) s[i] = '4';
}
num = 0;
for (int i = 0; i < s.length(); ++i) num = num * 10 + (int)(s[i] - '0');
}
if (k == 1) {
cout << all.size();
return 0;
}
num = 1;
while (num * kol < k) {
num *= kol;
++kol;
}
if (kol > n) {
cout << -1;
return 0;
}
for (int i = 0; i < all.size(); ++i) {
if (all[i] > n - kol) break;
++RES;
}
for (int i = n - kol; i < n; ++i) other.push_back(i + 1);
while (true) {
int p = k / num;
if (k % num) ++p;
if (p == 0) {
for (int i = other.size() - 1; i >= 0; --i) mas.push_back(other[i]);
break;
}
mas.push_back(other[p - 1]);
other.erase(other.begin() + p - 1);
--kol;
k = k % num;
num /= kol;
if (kol == 0) break;
}
for (int i = 0; i < all.size(); ++i) {
int ch;
if (all[i] - (n - mas.size()) - 1 >= 0)
ch = mas[all[i] - (n - mas.size()) - 1];
for (int j = 0; j < all.size(); ++j)
if (ch == all[j]) ++RES;
}
cout << RES;
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;
long long n, k, num = 1;
long long kol = 1, RES = 0;
vector<int> mas, other;
vector<int> all;
int main() {
cin >> n >> k;
long long num = 4;
while (num <= n) {
all.push_back(num);
string s = "";
int nn = num;
while (nn) {
if (nn % 10 == 4)
s = s + '4';
else
s = s + '7';
nn /= 10;
}
reverse(s.begin(), s.end());
int ch = -1;
for (int i = 0; i < s.length(); ++i)
if (s[i] == '4') ch = i;
if (ch == -1) {
for (int i = 0; i < s.length(); ++i) s[i] = '4';
s = s + '4';
} else {
s[ch] = '7';
for (int i = ch + 1; i < s.length(); ++i) s[i] = '4';
}
num = 0;
for (int i = 0; i < s.length(); ++i) num = num * 10 + (int)(s[i] - '0');
}
if (k == 1) {
cout << all.size();
return 0;
}
num = 1;
while (num * kol < k) {
num *= kol;
++kol;
}
if (kol > n) {
cout << -1;
return 0;
}
for (int i = 0; i < all.size(); ++i) {
if (all[i] > n - kol) break;
++RES;
}
for (int i = n - kol; i < n; ++i) other.push_back(i + 1);
while (true) {
int p = k / num;
if (k % num) ++p;
if (p == 0) {
for (int i = other.size() - 1; i >= 0; --i) mas.push_back(other[i]);
break;
}
mas.push_back(other[p - 1]);
other.erase(other.begin() + p - 1);
--kol;
k = k % num;
num /= kol;
if (kol == 0) break;
}
for (int i = 0; i < all.size(); ++i) {
int ch;
if (all[i] - (n - mas.size()) - 1 >= 0)
ch = mas[all[i] - (n - mas.size()) - 1];
for (int j = 0; j < all.size(); ++j)
if (ch == all[j]) ++RES;
}
cout << RES;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
vector<long long> a;
int main() {
for (int _b = (10), len = (1); len <= _b; len++)
for (int mask = 0, _n = (1 << len); mask < _n; mask++) {
long long x = 0;
for (int i = (len)-1; i >= 0; i--)
x = x * 10 + ((mask & (1 << i)) ? 7 : 4);
a.push_back(x);
}
sort((a).begin(), (a).end());
int n, k;
cin >> n >> k;
int suflen = min(n, 13);
long long suffact = 1;
for (int _b = (suflen), i = (1); i <= _b; i++) suffact *= i;
if (k > suffact) {
printf("-1\n");
return 0;
}
vector<int> left;
for (int i = 0, _n = (suflen); i < _n; i++) left.push_back(i);
--k;
long long fact = suffact;
int res = upper_bound((a).begin(), (a).end(), n - suflen) - a.begin();
for (int i = 0, _n = (suflen); i < _n; i++) {
fact /= (suflen - i);
int ord = k / fact;
k -= fact * ord;
int val = left[ord];
if (binary_search((a).begin(), (a).end(), n - suflen + i + 1) &&
binary_search((a).begin(), (a).end(), n - suflen + val + 1))
++res;
left.erase(left.begin() + ord);
}
printf("%d\n", res);
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 pi = acos(-1.0);
vector<long long> a;
int main() {
for (int _b = (10), len = (1); len <= _b; len++)
for (int mask = 0, _n = (1 << len); mask < _n; mask++) {
long long x = 0;
for (int i = (len)-1; i >= 0; i--)
x = x * 10 + ((mask & (1 << i)) ? 7 : 4);
a.push_back(x);
}
sort((a).begin(), (a).end());
int n, k;
cin >> n >> k;
int suflen = min(n, 13);
long long suffact = 1;
for (int _b = (suflen), i = (1); i <= _b; i++) suffact *= i;
if (k > suffact) {
printf("-1\n");
return 0;
}
vector<int> left;
for (int i = 0, _n = (suflen); i < _n; i++) left.push_back(i);
--k;
long long fact = suffact;
int res = upper_bound((a).begin(), (a).end(), n - suflen) - a.begin();
for (int i = 0, _n = (suflen); i < _n; i++) {
fact /= (suflen - i);
int ord = k / fact;
k -= fact * ord;
int val = left[ord];
if (binary_search((a).begin(), (a).end(), n - suflen + i + 1) &&
binary_search((a).begin(), (a).end(), n - suflen + val + 1))
++res;
left.erase(left.begin() + ord);
}
printf("%d\n", res);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
struct FenwickT {
long long t[2100];
void add(long long i, long long val) {
for (; i < 2100; i += ((i) & (-i))) t[i] += val;
}
long long query(long long i) {
long long sol = 0;
for (; i > 0; i -= ((i) & (-i))) sol += t[i];
return sol;
}
};
struct FN {
long long num[2100], len, per[2100];
FN() {
memset(num, 0, sizeof num);
memset(per, 0, sizeof per);
len = 0;
}
FN(long long *per, long long n) {
len = n;
memset(num, 0, sizeof num);
FenwickT t = FenwickT();
for (long long i = 1; i <= n; i++) t.add(i, 1);
for (long long i = 0; i < n; i++) {
long long k = t.query(per[i] + 1);
num[i] = k - 1;
t.add(per[i] + 1, -1);
}
}
FN(long long idx, long long n) {
memset(num, 0, sizeof num);
long long iter = 1;
do {
long long val = idx % iter;
num[iter - 1] = val;
idx /= iter++;
} while (idx > 0);
reverse(num, num + n);
len = n;
}
long long get_idx() {
long long sol = 0;
for (long long i = 0; i < len; i++) sol = sol * (len - i) + num[i];
return sol;
}
void find_per() {
FenwickT t = FenwickT();
for (long long i = 1; i <= len; i++) t.add(i, 1);
for (long long i = 0; i < len; i++) {
long long lo = 1, hi = len;
long long k = num[i] + 1;
while (lo < hi) {
long long mid = (lo + hi) >> 1;
long long q = t.query(mid);
if (q >= k)
hi = mid;
else
lo = mid + 1;
}
per[i] = lo - 1;
t.add(lo, -1);
}
}
FN operator+(const FN &o) const {
FN sol = FN();
long long add = 0, carrie = 0;
for (long long i = len - 1; i >= 0; i--) {
add = (num[i] + o.num[i] + carrie) % (len - i);
sol.num[i] = add;
carrie = (num[i] + o.num[i] + carrie) / (len - i);
}
sol.len = len;
return sol;
}
void prll() {
for (long long J = 0; J < (len); J++)
cout << (num)[J] << " \n"[J == (len)-1];
}
};
vector<long long> s;
const long long oo = 1e10 + 10;
void solve(long long x) {
if (x > oo) return;
if (x > 0) s.push_back(x);
solve(x * 10 + 4);
solve(x * 10 + 7);
}
bool is(long long x) {
if (x == 0) return false;
while (x > 0) {
if (x % 10 != 7 && (x % 10 != 4)) return false;
x /= 10;
}
return true;
}
long long n, k;
int main() {
ios::sync_with_stdio(0);
solve(0);
sort(s.begin(), s.end());
cin >> n >> k;
long long len = -1;
if (n <= 14) {
long long fact = 1;
for (long long i = 1; i <= n; i++) fact *= i;
if (k > fact) {
cout << -1 << endl;
return 0;
}
}
long long fact = 1;
for (long long i = 1; i <= n; i++) {
fact *= i;
if (fact >= k) {
len = i;
break;
}
}
long long sol = 0;
for (long long i = 0; i < s.size(); i++) {
if (s[i] <= n - len) sol++;
}
FN fx = FN(k - 1, len);
fx.find_per();
long long iter = 0;
for (long long i = n - len + 1; i <= n; i++, iter++) {
long long val = n - len + 1 + fx.per[iter];
if (is(val) && is(i)) sol++;
}
cout << sol << 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;
struct FenwickT {
long long t[2100];
void add(long long i, long long val) {
for (; i < 2100; i += ((i) & (-i))) t[i] += val;
}
long long query(long long i) {
long long sol = 0;
for (; i > 0; i -= ((i) & (-i))) sol += t[i];
return sol;
}
};
struct FN {
long long num[2100], len, per[2100];
FN() {
memset(num, 0, sizeof num);
memset(per, 0, sizeof per);
len = 0;
}
FN(long long *per, long long n) {
len = n;
memset(num, 0, sizeof num);
FenwickT t = FenwickT();
for (long long i = 1; i <= n; i++) t.add(i, 1);
for (long long i = 0; i < n; i++) {
long long k = t.query(per[i] + 1);
num[i] = k - 1;
t.add(per[i] + 1, -1);
}
}
FN(long long idx, long long n) {
memset(num, 0, sizeof num);
long long iter = 1;
do {
long long val = idx % iter;
num[iter - 1] = val;
idx /= iter++;
} while (idx > 0);
reverse(num, num + n);
len = n;
}
long long get_idx() {
long long sol = 0;
for (long long i = 0; i < len; i++) sol = sol * (len - i) + num[i];
return sol;
}
void find_per() {
FenwickT t = FenwickT();
for (long long i = 1; i <= len; i++) t.add(i, 1);
for (long long i = 0; i < len; i++) {
long long lo = 1, hi = len;
long long k = num[i] + 1;
while (lo < hi) {
long long mid = (lo + hi) >> 1;
long long q = t.query(mid);
if (q >= k)
hi = mid;
else
lo = mid + 1;
}
per[i] = lo - 1;
t.add(lo, -1);
}
}
FN operator+(const FN &o) const {
FN sol = FN();
long long add = 0, carrie = 0;
for (long long i = len - 1; i >= 0; i--) {
add = (num[i] + o.num[i] + carrie) % (len - i);
sol.num[i] = add;
carrie = (num[i] + o.num[i] + carrie) / (len - i);
}
sol.len = len;
return sol;
}
void prll() {
for (long long J = 0; J < (len); J++)
cout << (num)[J] << " \n"[J == (len)-1];
}
};
vector<long long> s;
const long long oo = 1e10 + 10;
void solve(long long x) {
if (x > oo) return;
if (x > 0) s.push_back(x);
solve(x * 10 + 4);
solve(x * 10 + 7);
}
bool is(long long x) {
if (x == 0) return false;
while (x > 0) {
if (x % 10 != 7 && (x % 10 != 4)) return false;
x /= 10;
}
return true;
}
long long n, k;
int main() {
ios::sync_with_stdio(0);
solve(0);
sort(s.begin(), s.end());
cin >> n >> k;
long long len = -1;
if (n <= 14) {
long long fact = 1;
for (long long i = 1; i <= n; i++) fact *= i;
if (k > fact) {
cout << -1 << endl;
return 0;
}
}
long long fact = 1;
for (long long i = 1; i <= n; i++) {
fact *= i;
if (fact >= k) {
len = i;
break;
}
}
long long sol = 0;
for (long long i = 0; i < s.size(); i++) {
if (s[i] <= n - len) sol++;
}
FN fx = FN(k - 1, len);
fx.find_per();
long long iter = 0;
for (long long i = n - len + 1; i <= n; i++, iter++) {
long long val = n - len + 1 + fx.per[iter];
if (is(val) && is(i)) sol++;
}
cout << sol << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,ssse3,sse3,sse4,popcnt,avx,mmx,abm,tune=native")
using namespace std;
long long n, k, cnt, i, f[20] = {1, 1}, j;
vector<long long> vec, rem;
set<long long> luck;
void gen(long long n, long long tot) {
if (n == -1)
vec.push_back(tot), luck.insert(tot);
else {
gen(n - 1, tot * 10 + 4LL);
gen(n - 1, tot * 10 + 7LL);
}
}
int main() {
scanf("%lld%lld", &n, &k);
for (i = 2; i <= 15; i++) f[i] = f[i - 1] * i;
for (i = 0; i <= 10; i++) gen(i, 0);
for (auto v : vec) {
if (v < n - 15) cnt++;
}
long long tot = 1;
for (i = max(1LL, n - 15); i <= n; i++) rem.push_back(i);
for (i = max(1LL, n - 15); i <= n; i++) {
for (j = 0; j < 20; j++) {
if (tot + j * f[n - i] > k) {
tot += (--j) * f[n - i];
break;
}
}
if (j >= rem.size()) {
printf("-1\n");
return 0;
}
if (luck.find(i) != luck.end() && luck.find(rem[j]) != luck.end()) cnt++;
rem.erase(rem.begin() + j);
}
printf("%lld\n", cnt);
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>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,ssse3,sse3,sse4,popcnt,avx,mmx,abm,tune=native")
using namespace std;
long long n, k, cnt, i, f[20] = {1, 1}, j;
vector<long long> vec, rem;
set<long long> luck;
void gen(long long n, long long tot) {
if (n == -1)
vec.push_back(tot), luck.insert(tot);
else {
gen(n - 1, tot * 10 + 4LL);
gen(n - 1, tot * 10 + 7LL);
}
}
int main() {
scanf("%lld%lld", &n, &k);
for (i = 2; i <= 15; i++) f[i] = f[i - 1] * i;
for (i = 0; i <= 10; i++) gen(i, 0);
for (auto v : vec) {
if (v < n - 15) cnt++;
}
long long tot = 1;
for (i = max(1LL, n - 15); i <= n; i++) rem.push_back(i);
for (i = max(1LL, n - 15); i <= n; i++) {
for (j = 0; j < 20; j++) {
if (tot + j * f[n - i] > k) {
tot += (--j) * f[n - i];
break;
}
}
if (j >= rem.size()) {
printf("-1\n");
return 0;
}
if (luck.find(i) != luck.end() && luck.find(rem[j]) != luck.end()) cnt++;
rem.erase(rem.begin() + j);
}
printf("%lld\n", cnt);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, k;
int ans = 0;
long long fac[30];
map<int, bool> M;
bool checklucky(long long x) {
for (; x; x /= 10)
if (x % 10 != 4 && x % 10 != 7) return 0;
return 1;
}
int check(int cur) {
if (cur < max(n - 20 + 1, 1)) return checklucky(cur);
int tmp = k;
M.clear();
for (int i = max(n - 20 + 1, 1); i <= cur; i++) {
int x = max(n - 20 + 1, 1);
while (M[x]) x++;
while (tmp > fac[n - i]) {
x++;
tmp -= fac[n - i];
while (M[x]) x++;
}
M[x] = 1;
if (i == cur) return checklucky(x);
}
}
void dfs(long long cur) {
if (cur > n) return;
ans += check(cur);
dfs(cur * 10 + 4);
dfs(cur * 10 + 7);
}
int main() {
fac[0] = 1;
for (int i = 1; i <= 23; i++) fac[i] = fac[i - 1] * i;
cin >> n >> k;
dfs(4);
dfs(7);
if (n <= 18 && k > fac[n]) {
puts("-1");
return 0;
}
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;
int n, k;
int ans = 0;
long long fac[30];
map<int, bool> M;
bool checklucky(long long x) {
for (; x; x /= 10)
if (x % 10 != 4 && x % 10 != 7) return 0;
return 1;
}
int check(int cur) {
if (cur < max(n - 20 + 1, 1)) return checklucky(cur);
int tmp = k;
M.clear();
for (int i = max(n - 20 + 1, 1); i <= cur; i++) {
int x = max(n - 20 + 1, 1);
while (M[x]) x++;
while (tmp > fac[n - i]) {
x++;
tmp -= fac[n - i];
while (M[x]) x++;
}
M[x] = 1;
if (i == cur) return checklucky(x);
}
}
void dfs(long long cur) {
if (cur > n) return;
ans += check(cur);
dfs(cur * 10 + 4);
dfs(cur * 10 + 7);
}
int main() {
fac[0] = 1;
for (int i = 1; i <= 23; i++) fac[i] = fac[i - 1] * i;
cin >> n >> k;
dfs(4);
dfs(7);
if (n <= 18 && k > fac[n]) {
puts("-1");
return 0;
}
cout << ans << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long fact[22] = {1};
vector<int> solve(int n, long long k) {
int i, j, r;
vector<int> ans;
set<int> st;
for (i = n - 1; i >= 0; i--) {
int t = (k - 1) / fact[i] + 1;
k -= (t - 1) * fact[i];
r = 0;
for (j = 1; j <= n; j++)
if (st.find(j) == st.end()) {
r++;
if (r == t) break;
}
ans.push_back(j);
st.insert(j);
}
return ans;
}
long long luck[2024];
int size = 0;
void back(int i, long long num) {
if (i > 8) return;
luck[size++] = num;
back(i + 1, 10 * num + 4);
back(i + 1, 10 * num + 7);
}
int go(int n) {
int i;
for (i = 0; luck[i] <= n; i++)
;
return i;
}
bool islucky(int n) {
while (n != 0) {
if (!(n % 10 == 4 || n % 10 == 7)) return false;
n /= 10;
}
return true;
}
int main() {
int n, i, j, r, ans = 0;
long long k;
back(0, 4);
back(0, 7);
luck[size++] = 4444444444L;
sort(luck, luck + size);
for (i = 1; i <= 18; i++) fact[i] = i * fact[i - 1];
cin >> n >> k;
if (n < 15) {
if (k > fact[n]) {
cout << -1 << endl;
return 0;
}
}
vector<int> ans1;
if (n > 13) {
ans1 = solve(13, k);
for (i = 0; i < 13; i++) {
if (islucky(i + n - 13 + 1) && islucky(ans1[i] + n - 13)) ans++;
}
ans += go(n - 13);
} else {
ans1 = solve(n, k);
for (i = 0; i < n; i++) {
if (islucky(i + 1) && islucky(ans1[i])) ans++;
}
}
cout << ans << 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[22] = {1};
vector<int> solve(int n, long long k) {
int i, j, r;
vector<int> ans;
set<int> st;
for (i = n - 1; i >= 0; i--) {
int t = (k - 1) / fact[i] + 1;
k -= (t - 1) * fact[i];
r = 0;
for (j = 1; j <= n; j++)
if (st.find(j) == st.end()) {
r++;
if (r == t) break;
}
ans.push_back(j);
st.insert(j);
}
return ans;
}
long long luck[2024];
int size = 0;
void back(int i, long long num) {
if (i > 8) return;
luck[size++] = num;
back(i + 1, 10 * num + 4);
back(i + 1, 10 * num + 7);
}
int go(int n) {
int i;
for (i = 0; luck[i] <= n; i++)
;
return i;
}
bool islucky(int n) {
while (n != 0) {
if (!(n % 10 == 4 || n % 10 == 7)) return false;
n /= 10;
}
return true;
}
int main() {
int n, i, j, r, ans = 0;
long long k;
back(0, 4);
back(0, 7);
luck[size++] = 4444444444L;
sort(luck, luck + size);
for (i = 1; i <= 18; i++) fact[i] = i * fact[i - 1];
cin >> n >> k;
if (n < 15) {
if (k > fact[n]) {
cout << -1 << endl;
return 0;
}
}
vector<int> ans1;
if (n > 13) {
ans1 = solve(13, k);
for (i = 0; i < 13; i++) {
if (islucky(i + n - 13 + 1) && islucky(ans1[i] + n - 13)) ans++;
}
ans += go(n - 13);
} else {
ans1 = solve(n, k);
for (i = 0; i < n; i++) {
if (islucky(i + 1) && islucky(ans1[i])) ans++;
}
}
cout << ans << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 14;
long long f[maxn];
long long n, k, ANS;
vector<long long> ans;
vector<long long> v, p;
bool is(long long a) {
while (a) {
if (a % 10 != 4 && a % 10 != 7) {
return false;
}
a /= 10;
}
return true;
}
int32_t main() {
cin >> n >> k;
v.push_back(0);
p.push_back(0);
while (v.back() < 1000000005) {
vector<long long> tmp;
for (long long i : p) {
v.push_back((i * 10) + 4);
tmp.push_back((i * 10) + 4);
v.push_back((i * 10) + 7);
tmp.push_back((i * 10) + 7);
}
p.clear();
p = tmp;
}
f[0] = 1;
f[1] = 1;
for (long long i = 2; i < maxn; i++) f[i] = f[i - 1] * i;
if (n < 14)
if (f[n] < k) return cout << -1, 0;
set<long long> st;
for (long long i = max(1LL, n - 12); i <= n; i++) {
st.insert(i);
}
for (long long i = max(1LL, n - 12); i <= n; i++) {
if (!k) break;
long long j = n - i;
long long l = 0;
while (f[j] < k) {
l++;
k -= f[j];
}
auto it = st.begin();
for (long long tmp = 0; tmp < l; tmp++) it++;
ans.push_back(*(it));
st.erase(*(it));
}
if (n > 13) {
long long now = n - 13;
auto it = upper_bound(v.begin(), v.end(), now);
long long x = distance(v.begin(), it);
ANS += x - 1;
}
for (long long i = 0; i < ans.size(); i++) {
long long cnt = 0;
if (n > 13) cnt = n - 13;
if (is(i + 1 + cnt) && is(ans[i])) 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;
const long long maxn = 14;
long long f[maxn];
long long n, k, ANS;
vector<long long> ans;
vector<long long> v, p;
bool is(long long a) {
while (a) {
if (a % 10 != 4 && a % 10 != 7) {
return false;
}
a /= 10;
}
return true;
}
int32_t main() {
cin >> n >> k;
v.push_back(0);
p.push_back(0);
while (v.back() < 1000000005) {
vector<long long> tmp;
for (long long i : p) {
v.push_back((i * 10) + 4);
tmp.push_back((i * 10) + 4);
v.push_back((i * 10) + 7);
tmp.push_back((i * 10) + 7);
}
p.clear();
p = tmp;
}
f[0] = 1;
f[1] = 1;
for (long long i = 2; i < maxn; i++) f[i] = f[i - 1] * i;
if (n < 14)
if (f[n] < k) return cout << -1, 0;
set<long long> st;
for (long long i = max(1LL, n - 12); i <= n; i++) {
st.insert(i);
}
for (long long i = max(1LL, n - 12); i <= n; i++) {
if (!k) break;
long long j = n - i;
long long l = 0;
while (f[j] < k) {
l++;
k -= f[j];
}
auto it = st.begin();
for (long long tmp = 0; tmp < l; tmp++) it++;
ans.push_back(*(it));
st.erase(*(it));
}
if (n > 13) {
long long now = n - 13;
auto it = upper_bound(v.begin(), v.end(), now);
long long x = distance(v.begin(), it);
ANS += x - 1;
}
for (long long i = 0; i < ans.size(); i++) {
long long cnt = 0;
if (n > 13) cnt = n - 13;
if (is(i + 1 + cnt) && is(ans[i])) ANS++;
}
cout << ANS << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long SQ = 100000;
const long long MAXN = SQ * SQ;
long long fact[100];
void init() {
fact[0] = 1;
for (long long i = 1; fact[i - 1] < MAXN; i++) fact[i] = fact[i - 1] * i;
}
long long p[100];
void k_perm(long long n, long long k) {
k--;
for (long long i = 1; i <= n; i++) {
long long s = (long long)(k / fact[n - i]);
long long t = p[i + s];
for (long long j = i + s; j > i; j--) p[j] = p[j - 1];
p[i] = t;
k %= fact[n - i];
}
}
bool lucky(long long n) {
while (n) {
if (n % 10 != 4 && n % 10 != 7) return false;
n /= 10;
}
return true;
}
deque<long long> tod(long long n) {
deque<long long> d;
do {
d.push_front(n % 10);
n /= 10;
} while (n);
return d;
}
long long toi(const deque<long long>& d) {
long long k = 0;
for (size_t i = 0; i < d.size(); i++) k *= 10, k += d[i];
return k;
}
long long next(long long n) {
deque<long long> d = tod(n);
for (long long i = 0; i < (long long)d.size(); i++)
if (d[i] > 7) {
for (long long j = i + 1; j < (long long)d.size(); j++) d[j] = 4;
while (i >= 0 && d[i] > 7) {
d[i] = 4;
if (i)
d[i - 1]++;
else
d.push_front(1);
i--;
}
break;
}
for (long long i = 0; i < (long long)d.size(); i++) {
if (d[i] > 7) throw 1;
if (d[i] == 4 || d[i] == 7) continue;
while (d[i] != 4 && d[i] != 7) d[i]++;
for (long long j = i + 1; j < (long long)d.size(); j++) d[j] = 4;
break;
}
return toi(d);
}
long long number_lucky(long long n) {
long long s = 0;
for (long long i = 4; i <= n; i = next(++i)) s++;
return s;
}
int main() {
long long n, k, s = 0;
init();
cin >> n >> k;
if (n <= 13 && k > fact[n])
s = -1;
else if (n <= 13) {
for (long long i = 1; i <= n; i++) p[i] = i;
k_perm(n, k);
for (long long i = 1; i <= n; i++)
if (lucky(i) && lucky(p[i])) s++;
} else {
for (long long i = 1; i <= 13; i++) p[i] = n - 13 + i;
k_perm(13, k);
for (long long i = 1; i <= 13; i++)
if (lucky(n - 13 + i) && lucky(p[i])) s++;
s += number_lucky(n - 13);
}
cout << s << '\n';
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 long long SQ = 100000;
const long long MAXN = SQ * SQ;
long long fact[100];
void init() {
fact[0] = 1;
for (long long i = 1; fact[i - 1] < MAXN; i++) fact[i] = fact[i - 1] * i;
}
long long p[100];
void k_perm(long long n, long long k) {
k--;
for (long long i = 1; i <= n; i++) {
long long s = (long long)(k / fact[n - i]);
long long t = p[i + s];
for (long long j = i + s; j > i; j--) p[j] = p[j - 1];
p[i] = t;
k %= fact[n - i];
}
}
bool lucky(long long n) {
while (n) {
if (n % 10 != 4 && n % 10 != 7) return false;
n /= 10;
}
return true;
}
deque<long long> tod(long long n) {
deque<long long> d;
do {
d.push_front(n % 10);
n /= 10;
} while (n);
return d;
}
long long toi(const deque<long long>& d) {
long long k = 0;
for (size_t i = 0; i < d.size(); i++) k *= 10, k += d[i];
return k;
}
long long next(long long n) {
deque<long long> d = tod(n);
for (long long i = 0; i < (long long)d.size(); i++)
if (d[i] > 7) {
for (long long j = i + 1; j < (long long)d.size(); j++) d[j] = 4;
while (i >= 0 && d[i] > 7) {
d[i] = 4;
if (i)
d[i - 1]++;
else
d.push_front(1);
i--;
}
break;
}
for (long long i = 0; i < (long long)d.size(); i++) {
if (d[i] > 7) throw 1;
if (d[i] == 4 || d[i] == 7) continue;
while (d[i] != 4 && d[i] != 7) d[i]++;
for (long long j = i + 1; j < (long long)d.size(); j++) d[j] = 4;
break;
}
return toi(d);
}
long long number_lucky(long long n) {
long long s = 0;
for (long long i = 4; i <= n; i = next(++i)) s++;
return s;
}
int main() {
long long n, k, s = 0;
init();
cin >> n >> k;
if (n <= 13 && k > fact[n])
s = -1;
else if (n <= 13) {
for (long long i = 1; i <= n; i++) p[i] = i;
k_perm(n, k);
for (long long i = 1; i <= n; i++)
if (lucky(i) && lucky(p[i])) s++;
} else {
for (long long i = 1; i <= 13; i++) p[i] = n - 13 + i;
k_perm(13, k);
for (long long i = 1; i <= 13; i++)
if (lucky(n - 13 + i) && lucky(p[i])) s++;
s += number_lucky(n - 13);
}
cout << s << '\n';
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<long long> sil(14);
vector<long long> lucky;
set<long long> luckyS;
void gen() {
for (int i = 1; i <= 10; ++i) {
for (int j = 0; j < (1LL << i); ++j) {
long long x = 0;
for (int k = 0; k < i; ++k) {
if ((j & (1LL << k)) > 0)
x = x * 10 + 4;
else
x = x * 10 + 7;
}
lucky.push_back(x);
luckyS.insert(x);
}
}
}
int main() {
ios_base::sync_with_stdio(0);
sil[0] = 1;
for (int i = 1; i < 14; ++i) sil[i] = sil[i - 1] * i;
long long n, k;
cin >> n >> k;
if (n <= 13 && k > sil[n]) {
cout << "-1";
return 0;
}
gen();
int norm = 0;
if (n > 14) norm = n - 14;
int odp = 0;
if (norm != 0)
for (int i = 0; i < lucky.size(); ++i)
if (lucky[i] <= norm) ++odp;
map<int, int> M;
vector<int> zb;
for (int i = norm + 1; i <= n; ++i) {
zb.push_back(i);
M[i] = -1;
}
for (int i = norm + 1; i <= n; ++i) {
int suf = n - i;
int nr = (k - 1) / sil[suf];
M[i] = zb[nr];
k -= nr * sil[suf];
vector<int> t;
for (int j = 0; j < zb.size(); ++j)
if (j != nr) t.push_back(zb[j]);
sort(t.begin(), t.end());
zb = t;
}
for (int i = norm + 1; i <= n; ++i)
if (luckyS.find(i) != luckyS.end() && luckyS.find(M[i]) != luckyS.end())
++odp;
cout << odp;
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<long long> sil(14);
vector<long long> lucky;
set<long long> luckyS;
void gen() {
for (int i = 1; i <= 10; ++i) {
for (int j = 0; j < (1LL << i); ++j) {
long long x = 0;
for (int k = 0; k < i; ++k) {
if ((j & (1LL << k)) > 0)
x = x * 10 + 4;
else
x = x * 10 + 7;
}
lucky.push_back(x);
luckyS.insert(x);
}
}
}
int main() {
ios_base::sync_with_stdio(0);
sil[0] = 1;
for (int i = 1; i < 14; ++i) sil[i] = sil[i - 1] * i;
long long n, k;
cin >> n >> k;
if (n <= 13 && k > sil[n]) {
cout << "-1";
return 0;
}
gen();
int norm = 0;
if (n > 14) norm = n - 14;
int odp = 0;
if (norm != 0)
for (int i = 0; i < lucky.size(); ++i)
if (lucky[i] <= norm) ++odp;
map<int, int> M;
vector<int> zb;
for (int i = norm + 1; i <= n; ++i) {
zb.push_back(i);
M[i] = -1;
}
for (int i = norm + 1; i <= n; ++i) {
int suf = n - i;
int nr = (k - 1) / sil[suf];
M[i] = zb[nr];
k -= nr * sil[suf];
vector<int> t;
for (int j = 0; j < zb.size(); ++j)
if (j != nr) t.push_back(zb[j]);
sort(t.begin(), t.end());
zb = t;
}
for (int i = norm + 1; i <= n; ++i)
if (luckyS.find(i) != luckyS.end() && luckyS.find(M[i]) != luckyS.end())
++odp;
cout << odp;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<int> lucky_nums;
vector<int> datos;
vector<long long> factorial;
set<long long> numeros;
int idx_cambio;
void obten_lucky(int n) {
numeros.insert(0);
for (int i = 0; i < 10; ++i) {
set<long long> temp = numeros;
for (const auto& act : temp) {
long long nuevo;
nuevo = act * 10 + 7;
if (nuevo <= n) numeros.insert(nuevo);
nuevo = act * 10 + 4;
if (nuevo <= n) numeros.insert(nuevo);
}
}
numeros.erase(0);
for (const auto& act : numeros) {
lucky_nums.push_back(act);
}
}
int toma(vector<int>& disponibles, int idx) {
int resp = disponibles[idx];
for (int i = idx; i < disponibles.size() - 1; ++i) {
disponibles[i] = disponibles[i + 1];
}
disponibles.pop_back();
return resp;
}
void init(int n, int k) {
factorial.push_back(1);
for (int i = 0; i < 14; ++i) {
factorial.push_back(factorial[i] * (i + 1LL));
}
if (n <= 13) {
if (factorial[n] < k) {
cout << "-1\n";
exit(0);
}
}
obten_lucky(n);
long long acc = 1;
idx_cambio = 0;
do {
idx_cambio++;
acc = acc * (long long)idx_cambio;
} while (acc < k);
datos.resize(idx_cambio);
int restantes = idx_cambio - 1;
vector<int> disponibles(idx_cambio);
for (int i = 0; i < idx_cambio; ++i) {
disponibles[i] = i;
}
for (int i = 0; i < idx_cambio; ++i) {
int num_act = (k + factorial[restantes] - 1) / factorial[restantes];
datos[i] = num_act;
k -= (num_act - 1) * factorial[restantes];
datos[i] = toma(disponibles, num_act - 1);
restantes--;
}
int temp = idx_cambio;
idx_cambio = n - idx_cambio + 1;
for (int i = 0; i < temp; ++i) {
datos[i] += idx_cambio;
}
}
int obten_val(int n, int k, int idx) {
if (idx < idx_cambio) {
return idx;
}
return datos[idx - idx_cambio];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
init(n, k);
int resp = 0;
for (const auto& idx : lucky_nums) {
resp += (numeros.find(obten_val(n, k, idx)) != numeros.end());
}
cout << resp << '\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;
vector<int> lucky_nums;
vector<int> datos;
vector<long long> factorial;
set<long long> numeros;
int idx_cambio;
void obten_lucky(int n) {
numeros.insert(0);
for (int i = 0; i < 10; ++i) {
set<long long> temp = numeros;
for (const auto& act : temp) {
long long nuevo;
nuevo = act * 10 + 7;
if (nuevo <= n) numeros.insert(nuevo);
nuevo = act * 10 + 4;
if (nuevo <= n) numeros.insert(nuevo);
}
}
numeros.erase(0);
for (const auto& act : numeros) {
lucky_nums.push_back(act);
}
}
int toma(vector<int>& disponibles, int idx) {
int resp = disponibles[idx];
for (int i = idx; i < disponibles.size() - 1; ++i) {
disponibles[i] = disponibles[i + 1];
}
disponibles.pop_back();
return resp;
}
void init(int n, int k) {
factorial.push_back(1);
for (int i = 0; i < 14; ++i) {
factorial.push_back(factorial[i] * (i + 1LL));
}
if (n <= 13) {
if (factorial[n] < k) {
cout << "-1\n";
exit(0);
}
}
obten_lucky(n);
long long acc = 1;
idx_cambio = 0;
do {
idx_cambio++;
acc = acc * (long long)idx_cambio;
} while (acc < k);
datos.resize(idx_cambio);
int restantes = idx_cambio - 1;
vector<int> disponibles(idx_cambio);
for (int i = 0; i < idx_cambio; ++i) {
disponibles[i] = i;
}
for (int i = 0; i < idx_cambio; ++i) {
int num_act = (k + factorial[restantes] - 1) / factorial[restantes];
datos[i] = num_act;
k -= (num_act - 1) * factorial[restantes];
datos[i] = toma(disponibles, num_act - 1);
restantes--;
}
int temp = idx_cambio;
idx_cambio = n - idx_cambio + 1;
for (int i = 0; i < temp; ++i) {
datos[i] += idx_cambio;
}
}
int obten_val(int n, int k, int idx) {
if (idx < idx_cambio) {
return idx;
}
return datos[idx - idx_cambio];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
init(n, k);
int resp = 0;
for (const auto& idx : lucky_nums) {
resp += (numeros.find(obten_val(n, k, idx)) != numeros.end());
}
cout << resp << '\n';
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1000ll * 1000 * 1000;
const int size = 100;
long long smb, n, k;
int ans;
bool used[size];
int perm[size];
void genthemall(long long num) {
if (num > inf) return;
if (num <= n - 15 && num >= 1) ans++;
genthemall(num * 10 + 4);
genthemall(num * 10 + 7);
}
bool islucky(long long num) {
if (num == 0) return false;
while (num > 0) {
if (num % 10 != 4 && num % 10 != 7) return false;
num /= 10;
}
return true;
}
int main() {
cin >> n >> k;
long long fct = 1;
int h = min(n, 15ll), i, j;
for (i = 1; i <= h; i++) fct *= i;
if (fct < k) {
cout << -1 << endl;
return 0;
}
smb = 15;
ans = 0;
genthemall(0);
for (i = 0; i < h; i++) used[i] = false;
for (i = 0; i < h; i++) {
fct /= (h - i);
for (j = 0; j < h; j++)
if (!used[j])
if (fct >= k) {
used[j] = true;
perm[i] = j;
break;
} else
k -= fct;
ans += islucky(n - h + i + 1) && islucky(n - h + perm[i] + 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 long long inf = 1000ll * 1000 * 1000;
const int size = 100;
long long smb, n, k;
int ans;
bool used[size];
int perm[size];
void genthemall(long long num) {
if (num > inf) return;
if (num <= n - 15 && num >= 1) ans++;
genthemall(num * 10 + 4);
genthemall(num * 10 + 7);
}
bool islucky(long long num) {
if (num == 0) return false;
while (num > 0) {
if (num % 10 != 4 && num % 10 != 7) return false;
num /= 10;
}
return true;
}
int main() {
cin >> n >> k;
long long fct = 1;
int h = min(n, 15ll), i, j;
for (i = 1; i <= h; i++) fct *= i;
if (fct < k) {
cout << -1 << endl;
return 0;
}
smb = 15;
ans = 0;
genthemall(0);
for (i = 0; i < h; i++) used[i] = false;
for (i = 0; i < h; i++) {
fct /= (h - i);
for (j = 0; j < h; j++)
if (!used[j])
if (fct >= k) {
used[j] = true;
perm[i] = j;
break;
} else
k -= fct;
ans += islucky(n - h + i + 1) && islucky(n - h + perm[i] + 1);
}
cout << ans << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <class T>
void pv(ostream &out, T a, T b) {
for (T i = a; i != b; ++i) {
out << *i << " ";
}
out << endl;
}
template <class T>
void pvp(ostream &out, T a, T b) {
for (T i = a; i != b; ++i) {
out << "(" << i->first << ", " << i->second << ") ";
}
out << endl;
}
const int INF = numeric_limits<int>::max();
const double EPS = 1e-9;
vector<long long> a, perm;
vector<bool> used;
long long get_next(long long a) {
int d = 1;
while (a) {
if (a % 10 == 4) {
a += 3;
d--;
break;
}
a /= 10;
d++;
}
for (int i = 0; i < d; ++i) {
a *= 10;
a += 4;
}
return a;
}
int fact(int a) {
int res = 1;
for (int i = 2; i <= a; ++i) {
res *= i;
}
return res;
}
int main() {
int n, k;
cin >> n >> k;
long long cur = 4;
while (cur <= n) {
a.push_back(cur);
cur = get_next(cur);
}
long long f = 1, d = 0;
for (int i = 2; i < 1000; ++i) {
if (f >= k) {
d = i - 1;
break;
}
f *= i;
}
if (k == 1) {
d = 0;
}
if (d > n) {
cout << -1 << endl;
} else {
int safe = n - d;
int ans = int(lower_bound(a.begin(), a.end(), (long long)safe) - a.begin());
if (ans < a.size() && a[ans] == safe) {
ans++;
}
perm.resize(d);
used.assign(d, false);
int n = d;
int m = k - 1;
for (int i = 0; i < n; ++i) {
int f = fact(n - i - 1);
int j = m / f;
m -= j * f;
for (int k = 0; k < n; ++k) {
if (!used[k]) {
if (j-- == 0) {
perm[i] = k + 1;
used[k] = true;
break;
}
}
}
}
for (int i = 0; i < n; ++i) {
int x = int(lower_bound(a.begin(), a.end(), perm[i] + safe) - a.begin());
int y = int(lower_bound(a.begin(), a.end(), i + 1 + safe) - a.begin());
if (a[x] == perm[i] + safe && a[y] == i + 1 + safe) {
ans++;
}
}
cout << ans << 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;
template <class T>
void pv(ostream &out, T a, T b) {
for (T i = a; i != b; ++i) {
out << *i << " ";
}
out << endl;
}
template <class T>
void pvp(ostream &out, T a, T b) {
for (T i = a; i != b; ++i) {
out << "(" << i->first << ", " << i->second << ") ";
}
out << endl;
}
const int INF = numeric_limits<int>::max();
const double EPS = 1e-9;
vector<long long> a, perm;
vector<bool> used;
long long get_next(long long a) {
int d = 1;
while (a) {
if (a % 10 == 4) {
a += 3;
d--;
break;
}
a /= 10;
d++;
}
for (int i = 0; i < d; ++i) {
a *= 10;
a += 4;
}
return a;
}
int fact(int a) {
int res = 1;
for (int i = 2; i <= a; ++i) {
res *= i;
}
return res;
}
int main() {
int n, k;
cin >> n >> k;
long long cur = 4;
while (cur <= n) {
a.push_back(cur);
cur = get_next(cur);
}
long long f = 1, d = 0;
for (int i = 2; i < 1000; ++i) {
if (f >= k) {
d = i - 1;
break;
}
f *= i;
}
if (k == 1) {
d = 0;
}
if (d > n) {
cout << -1 << endl;
} else {
int safe = n - d;
int ans = int(lower_bound(a.begin(), a.end(), (long long)safe) - a.begin());
if (ans < a.size() && a[ans] == safe) {
ans++;
}
perm.resize(d);
used.assign(d, false);
int n = d;
int m = k - 1;
for (int i = 0; i < n; ++i) {
int f = fact(n - i - 1);
int j = m / f;
m -= j * f;
for (int k = 0; k < n; ++k) {
if (!used[k]) {
if (j-- == 0) {
perm[i] = k + 1;
used[k] = true;
break;
}
}
}
}
for (int i = 0; i < n; ++i) {
int x = int(lower_bound(a.begin(), a.end(), perm[i] + safe) - a.begin());
int y = int(lower_bound(a.begin(), a.end(), i + 1 + safe) - a.begin());
if (a[x] == perm[i] + safe && a[y] == i + 1 + safe) {
ans++;
}
}
cout << ans << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
const int MAX_FACT = 14;
int N, K;
long long fact[MAX_FACT];
int countLucky(long long val, int n) {
int sol = 0;
if (val <= n) {
if (val > 0) ++sol;
sol += countLucky(val * 10 + 4, n);
sol += countLucky(val * 10 + 7, n);
}
return sol;
}
bool isLucky(int val) {
do {
int digit = val % 10;
if (digit != 4 && digit != 7) return false;
val /= 10;
} while (val > 0);
return true;
}
int solve() {
int sol = 0;
fact[0] = 1;
for (int i = 1; i < MAX_FACT; ++i) fact[i] = fact[i - 1] * i;
int pos = 0;
for (int i = 0; i < MAX_FACT; ++i)
if (fact[i] >= K) {
pos = i;
break;
}
if (pos < N) sol += countLucky(0, N - pos);
if (pos > N) return -1;
int perm[MAX_FACT];
bool fol[MAX_FACT];
memset(fol, false, sizeof(fol));
for (int i = 1; i <= pos; ++i) {
long long cnt = 0;
for (int j = 1; j <= pos; ++j)
if (!fol[j]) {
++cnt;
if (cnt * fact[pos - i] >= K) {
fol[j] = true;
perm[i] = N - pos + j;
K -= (cnt - 1) * fact[pos - i];
break;
}
}
}
for (int i = 1; i <= pos; ++i)
if (isLucky(N - pos + i) && isLucky(perm[i])) ++sol;
return sol;
}
int main() {
assert(scanf("%d %d", &N, &K) == 2);
printf("%d\n", solve());
}
| ### 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>
const int MAX_FACT = 14;
int N, K;
long long fact[MAX_FACT];
int countLucky(long long val, int n) {
int sol = 0;
if (val <= n) {
if (val > 0) ++sol;
sol += countLucky(val * 10 + 4, n);
sol += countLucky(val * 10 + 7, n);
}
return sol;
}
bool isLucky(int val) {
do {
int digit = val % 10;
if (digit != 4 && digit != 7) return false;
val /= 10;
} while (val > 0);
return true;
}
int solve() {
int sol = 0;
fact[0] = 1;
for (int i = 1; i < MAX_FACT; ++i) fact[i] = fact[i - 1] * i;
int pos = 0;
for (int i = 0; i < MAX_FACT; ++i)
if (fact[i] >= K) {
pos = i;
break;
}
if (pos < N) sol += countLucky(0, N - pos);
if (pos > N) return -1;
int perm[MAX_FACT];
bool fol[MAX_FACT];
memset(fol, false, sizeof(fol));
for (int i = 1; i <= pos; ++i) {
long long cnt = 0;
for (int j = 1; j <= pos; ++j)
if (!fol[j]) {
++cnt;
if (cnt * fact[pos - i] >= K) {
fol[j] = true;
perm[i] = N - pos + j;
K -= (cnt - 1) * fact[pos - i];
break;
}
}
}
for (int i = 1; i <= pos; ++i)
if (isLucky(N - pos + i) && isLucky(perm[i])) ++sol;
return sol;
}
int main() {
assert(scanf("%d %d", &N, &K) == 2);
printf("%d\n", solve());
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long fact[20];
int permutation[20];
vector<int> canUse;
int luckySmaller(int n) {
queue<long long> q;
while (!q.empty()) q.pop();
q.push(0);
int ans = 0;
while (!q.empty()) {
long long f = q.front();
q.pop();
long long a[2] = {f * 10 + 4, f * 10 + 7};
for (int i = 0; i < 2; i++)
if (a[i] < n) {
q.push(a[i]);
ans++;
}
}
return ans;
}
void build(int n, int k, int idx) {
if (n == 0) return;
if (n == 1) {
permutation[idx] = canUse[0];
return;
}
int use = k / fact[n - 1];
permutation[idx] = canUse[use];
swap(canUse[use], canUse[canUse.size() - 1]);
canUse.pop_back();
sort(canUse.begin(), canUse.end());
build(n - 1, k % fact[n - 1], idx + 1);
}
bool isLucky(int n) {
while (n != 0) {
int d = n % 10;
if (d != 4 && d != 7) return false;
n /= 10;
}
return true;
}
int main() {
int n, k;
cin >> n >> k;
int start = 0;
fact[0] = 1;
while (fact[start] < k) {
start++;
fact[start] = fact[start - 1] * start;
if (start > n) {
cout << -1 << endl;
return 0;
}
}
for (int i = n - start + 1; i <= n; i++) canUse.push_back(i);
build(start, k - 1, 0);
int ans = luckySmaller(n - start + 1);
for (int i = 0; i < start; i++) {
int idx = n - start + 1 + i;
if (isLucky(permutation[i]) && isLucky(idx)) 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 fact[20];
int permutation[20];
vector<int> canUse;
int luckySmaller(int n) {
queue<long long> q;
while (!q.empty()) q.pop();
q.push(0);
int ans = 0;
while (!q.empty()) {
long long f = q.front();
q.pop();
long long a[2] = {f * 10 + 4, f * 10 + 7};
for (int i = 0; i < 2; i++)
if (a[i] < n) {
q.push(a[i]);
ans++;
}
}
return ans;
}
void build(int n, int k, int idx) {
if (n == 0) return;
if (n == 1) {
permutation[idx] = canUse[0];
return;
}
int use = k / fact[n - 1];
permutation[idx] = canUse[use];
swap(canUse[use], canUse[canUse.size() - 1]);
canUse.pop_back();
sort(canUse.begin(), canUse.end());
build(n - 1, k % fact[n - 1], idx + 1);
}
bool isLucky(int n) {
while (n != 0) {
int d = n % 10;
if (d != 4 && d != 7) return false;
n /= 10;
}
return true;
}
int main() {
int n, k;
cin >> n >> k;
int start = 0;
fact[0] = 1;
while (fact[start] < k) {
start++;
fact[start] = fact[start - 1] * start;
if (start > n) {
cout << -1 << endl;
return 0;
}
}
for (int i = n - start + 1; i <= n; i++) canUse.push_back(i);
build(start, k - 1, 0);
int ans = luckySmaller(n - start + 1);
for (int i = 0; i < start; i++) {
int idx = n - start + 1 + i;
if (isLucky(permutation[i]) && isLucky(idx)) ans++;
}
cout << ans << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long N = 14, LIM = 5e9;
vector<long long> vec, per;
long long fac[N];
set<long long> s;
long long n, k, ind, ans;
void bt(long long num) {
if (num > LIM) return;
vec.push_back(num);
bt(num * 10 + 4);
bt(num * 10 + 7);
}
bool f(long long n) {
while (n > 0) {
long long r = n % 10;
n /= 10;
if (r != 4 && r != 7) return false;
}
return true;
}
int32_t main() {
bt(4);
bt(7);
vec.push_back(0);
sort(vec.begin(), vec.end());
fac[0] = 1;
for (long long i = 1; i < N; i++) fac[i] = fac[i - 1] * i;
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> k;
for (ind = 0; fac[ind] < k; ind++)
;
if (n < 13 && fac[n] < k) return cout << -1, 0;
long long bu = ind;
for (auto x : vec)
if (x <= n - ind) ans++;
for (long long i = n - ind + 1; i <= n; i++) s.insert(i);
while (k && ind) {
ind--;
long long tmp = k / fac[ind] + (k % fac[ind] > 0);
k -= (tmp - 1) * fac[ind];
for (auto x : s) {
tmp--;
if (tmp == 0) {
per.push_back(x);
s.erase(x);
break;
}
}
}
for (long long i = 0; i < per.size(); i++)
if (f(per[i]) && f(n - bu + 1 + i)) ans++;
return cout << --ans, 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 N = 14, LIM = 5e9;
vector<long long> vec, per;
long long fac[N];
set<long long> s;
long long n, k, ind, ans;
void bt(long long num) {
if (num > LIM) return;
vec.push_back(num);
bt(num * 10 + 4);
bt(num * 10 + 7);
}
bool f(long long n) {
while (n > 0) {
long long r = n % 10;
n /= 10;
if (r != 4 && r != 7) return false;
}
return true;
}
int32_t main() {
bt(4);
bt(7);
vec.push_back(0);
sort(vec.begin(), vec.end());
fac[0] = 1;
for (long long i = 1; i < N; i++) fac[i] = fac[i - 1] * i;
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> k;
for (ind = 0; fac[ind] < k; ind++)
;
if (n < 13 && fac[n] < k) return cout << -1, 0;
long long bu = ind;
for (auto x : vec)
if (x <= n - ind) ans++;
for (long long i = n - ind + 1; i <= n; i++) s.insert(i);
while (k && ind) {
ind--;
long long tmp = k / fac[ind] + (k % fac[ind] > 0);
k -= (tmp - 1) * fac[ind];
for (auto x : s) {
tmp--;
if (tmp == 0) {
per.push_back(x);
s.erase(x);
break;
}
}
}
for (long long i = 0; i < per.size(); i++)
if (f(per[i]) && f(n - bu + 1 + i)) ans++;
return cout << --ans, 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const double dinf = 1e200;
void ext(int c) {}
int ar[30];
bool was[30];
int n;
int k;
long long fact[30];
vector<long long> nums;
void gen(long long v) {
if (v > 1000000000) return;
nums.push_back(v);
gen(v * 10 + 4);
gen(v * 10 + 7);
}
int main() {
fact[0] = 1;
for (int i = 0; i < (20); ++i) fact[i + 1] = fact[i] * (i + 1);
cin >> n >> k;
--k;
if (n <= 13 && k >= fact[n]) {
cout << -1;
return 0;
}
int p = n + 1;
while (k >= fact[n - p + 1]) --p;
for (int a = p; a <= n; ++a) {
for (int v = p;; ++v) {
if (was[v - p]) continue;
if (k < fact[n - a]) {
was[v - p] = true;
ar[a - p] = v;
break;
}
k -= fact[n - a];
}
}
gen(4);
gen(7);
sort(nums.begin(), nums.end());
long long res = lower_bound(nums.begin(), nums.end(), p) - nums.begin();
for (int a = p; a <= n; ++a) {
int i1 = lower_bound(nums.begin(), nums.end(), a) - nums.begin();
int i2 = lower_bound(nums.begin(), nums.end(), ar[a - p]) - nums.begin();
if (i1 == nums.size() || i2 == nums.size() || nums[i1] != a ||
nums[i2] != ar[a - p])
continue;
++res;
}
cout << res;
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 double dinf = 1e200;
void ext(int c) {}
int ar[30];
bool was[30];
int n;
int k;
long long fact[30];
vector<long long> nums;
void gen(long long v) {
if (v > 1000000000) return;
nums.push_back(v);
gen(v * 10 + 4);
gen(v * 10 + 7);
}
int main() {
fact[0] = 1;
for (int i = 0; i < (20); ++i) fact[i + 1] = fact[i] * (i + 1);
cin >> n >> k;
--k;
if (n <= 13 && k >= fact[n]) {
cout << -1;
return 0;
}
int p = n + 1;
while (k >= fact[n - p + 1]) --p;
for (int a = p; a <= n; ++a) {
for (int v = p;; ++v) {
if (was[v - p]) continue;
if (k < fact[n - a]) {
was[v - p] = true;
ar[a - p] = v;
break;
}
k -= fact[n - a];
}
}
gen(4);
gen(7);
sort(nums.begin(), nums.end());
long long res = lower_bound(nums.begin(), nums.end(), p) - nums.begin();
for (int a = p; a <= n; ++a) {
int i1 = lower_bound(nums.begin(), nums.end(), a) - nums.begin();
int i2 = lower_bound(nums.begin(), nums.end(), ar[a - p]) - nums.begin();
if (i1 == nums.size() || i2 == nums.size() || nums[i1] != a ||
nums[i2] != ar[a - p])
continue;
++res;
}
cout << res;
return 0;
}
``` |
#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
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 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 fact[20], n, k;
bool isGood(long long x) {
while (x) {
if (x % 10 != 4 && x % 10 != 7) return 0;
x /= 10;
}
return 1;
}
vector<long long> getkth(long long k, long long n, long long x) {
vector<long long> v(n + 1, 0);
for (long long i = 1; i <= n; ++i) {
long long ok = 1, poz = 1;
while (ok && poz <= n) {
if (v[poz])
poz++;
else {
if (k > fact[n - i])
k -= fact[n - i];
else
break;
poz++;
}
}
v[poz] = x + i - 1;
}
return v;
}
long long getnext(long long x) {
long long sol = x, p10 = 1, ok = 1;
while (x && ok) {
if (x % 10 == 4) {
sol += p10 * 3;
ok = 0;
} else
sol -= p10 * 3;
p10 *= 10;
x /= 10;
}
if (ok) sol += p10 * 4;
return sol;
}
long long howmany(long long n) {
long long x = 4, sol = 0;
while (x <= n) sol++, x = getnext(x);
return sol;
}
int32_t 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 << "\n";
return 0;
}
long long sol = 0;
if (n <= 14) {
vector<long long> v = getkth(k, n, 1);
for (long long i = 1; i <= n; ++i) {
if (isGood(i) && isGood(v[i])) sol++;
}
} else {
sol += howmany(n - 14);
vector<long long> v = getkth(k, 14, n - 13);
for (long long i = 1; i <= 14; ++i) {
if (isGood(i + n - 14) && isGood(v[i])) sol++;
}
}
cout << sol << "\n";
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 fact[20], n, k;
bool isGood(long long x) {
while (x) {
if (x % 10 != 4 && x % 10 != 7) return 0;
x /= 10;
}
return 1;
}
vector<long long> getkth(long long k, long long n, long long x) {
vector<long long> v(n + 1, 0);
for (long long i = 1; i <= n; ++i) {
long long ok = 1, poz = 1;
while (ok && poz <= n) {
if (v[poz])
poz++;
else {
if (k > fact[n - i])
k -= fact[n - i];
else
break;
poz++;
}
}
v[poz] = x + i - 1;
}
return v;
}
long long getnext(long long x) {
long long sol = x, p10 = 1, ok = 1;
while (x && ok) {
if (x % 10 == 4) {
sol += p10 * 3;
ok = 0;
} else
sol -= p10 * 3;
p10 *= 10;
x /= 10;
}
if (ok) sol += p10 * 4;
return sol;
}
long long howmany(long long n) {
long long x = 4, sol = 0;
while (x <= n) sol++, x = getnext(x);
return sol;
}
int32_t 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 << "\n";
return 0;
}
long long sol = 0;
if (n <= 14) {
vector<long long> v = getkth(k, n, 1);
for (long long i = 1; i <= n; ++i) {
if (isGood(i) && isGood(v[i])) sol++;
}
} else {
sol += howmany(n - 14);
vector<long long> v = getkth(k, 14, n - 13);
for (long long i = 1; i <= 14; ++i) {
if (isGood(i + n - 14) && isGood(v[i])) sol++;
}
}
cout << sol << "\n";
return 0;
}
``` |
#include <bits/stdc++.h>
int lucky(int x) {
while (x) {
if (x % 10 != 4 && x % 10 != 7) break;
x /= 10;
}
if (x == 0) {
return 1;
} else {
return 0;
}
}
int fact(int n) {
int x = 1, i;
for (i = 1; i <= n; i++) x *= i;
return x;
}
int main() {
int n, k, p = 0, q = 2, r = 2, sum = 0, i, j;
long long x = 1;
int a[10000], b[100];
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>
int lucky(int x) {
while (x) {
if (x % 10 != 4 && x % 10 != 7) break;
x /= 10;
}
if (x == 0) {
return 1;
} else {
return 0;
}
}
int fact(int n) {
int x = 1, i;
for (i = 1; i <= n; i++) x *= i;
return x;
}
int main() {
int n, k, p = 0, q = 2, r = 2, sum = 0, i, j;
long long x = 1;
int a[10000], b[100];
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;
long long N, K;
long long f[15];
int count(long long x, long long k) {
if (x > k) return 0;
return 1 + count(10 * x + 4, k) + count(10 * x + 7, k);
}
bool check(long long x) {
if (x == 0) return false;
while (x) {
long long r = x % 10;
if (r != 4 && r != 7) return false;
x /= 10;
}
return true;
}
int main() {
cin >> N >> K;
f[1] = 1;
for (int i = 2; i <= 14; i++) f[i] = f[i - 1] * i;
if (N <= 14 && K > f[N]) {
cout << -1 << endl;
return 0;
}
int ans = count(0, max(N - 13, 0ll)) - 1;
vector<int> t;
for (int i = N - 12; i <= N; i++) t.push_back(i);
K--;
for (int i = 13; i > 1 && t.size() > 0; i--) {
int tmp = K / f[i - 1];
if (check(N - i + 1) && check(t[tmp])) ans++;
t.erase(t.begin() + tmp);
K %= f[i - 1];
}
if (check(N) && check(t[0])) ans++;
cout << ans << 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 N, K;
long long f[15];
int count(long long x, long long k) {
if (x > k) return 0;
return 1 + count(10 * x + 4, k) + count(10 * x + 7, k);
}
bool check(long long x) {
if (x == 0) return false;
while (x) {
long long r = x % 10;
if (r != 4 && r != 7) return false;
x /= 10;
}
return true;
}
int main() {
cin >> N >> K;
f[1] = 1;
for (int i = 2; i <= 14; i++) f[i] = f[i - 1] * i;
if (N <= 14 && K > f[N]) {
cout << -1 << endl;
return 0;
}
int ans = count(0, max(N - 13, 0ll)) - 1;
vector<int> t;
for (int i = N - 12; i <= N; i++) t.push_back(i);
K--;
for (int i = 13; i > 1 && t.size() > 0; i--) {
int tmp = K / f[i - 1];
if (check(N - i + 1) && check(t[tmp])) ans++;
t.erase(t.begin() + tmp);
K %= f[i - 1];
}
if (check(N) && check(t[0])) ans++;
cout << ans << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <class T>
string i2s(T x) {
ostringstream o;
o << x;
return o.str();
}
int s2i(string x) {
int r = 0;
istringstream sin(x);
sin >> r;
return r;
}
long long fact[14];
int N, K;
bool isLucky(int x) {
string s = i2s(x);
for (int i = 0; i < s.size(); i++)
if (s[i] != '4' && s[i] != '7') return false;
return true;
}
int countLucky(long long val, int mx) {
if (val > mx) return 0;
int ans = 0;
if (val != 0) ans += 1;
ans += countLucky(val * 10 + 4, mx);
ans += countLucky(val * 10 + 7, mx);
return ans;
}
int solve() {
int i, j, sol = 0;
fact[0] = fact[1] = 1;
for (i = 2; i < 14; i++) fact[i] = i * fact[i - 1];
int pos = 0;
for (i = 0; i < 14; i++)
if (fact[i] >= K) {
pos = i;
break;
}
if (pos > N) return -1;
if (pos < N) sol += countLucky(0, N - pos);
int perm[14];
bool flag[14];
memset(flag, false, sizeof(flag));
for (i = 1; i <= pos; i++) {
long long cnt = 0;
for (j = 1; j <= pos; j++)
if (!flag[j]) {
++cnt;
if (cnt * fact[pos - i] >= K) {
flag[j] = true;
perm[i] = N - pos + j;
K -= (cnt - 1) * fact[pos - i];
break;
}
}
}
for (i = 1; i <= pos; ++i)
if (isLucky(N - pos + i) && isLucky(perm[i])) ++sol;
return sol;
}
int main() {
scanf("%d %d", &N, &K);
cout << solve() << 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;
template <class T>
string i2s(T x) {
ostringstream o;
o << x;
return o.str();
}
int s2i(string x) {
int r = 0;
istringstream sin(x);
sin >> r;
return r;
}
long long fact[14];
int N, K;
bool isLucky(int x) {
string s = i2s(x);
for (int i = 0; i < s.size(); i++)
if (s[i] != '4' && s[i] != '7') return false;
return true;
}
int countLucky(long long val, int mx) {
if (val > mx) return 0;
int ans = 0;
if (val != 0) ans += 1;
ans += countLucky(val * 10 + 4, mx);
ans += countLucky(val * 10 + 7, mx);
return ans;
}
int solve() {
int i, j, sol = 0;
fact[0] = fact[1] = 1;
for (i = 2; i < 14; i++) fact[i] = i * fact[i - 1];
int pos = 0;
for (i = 0; i < 14; i++)
if (fact[i] >= K) {
pos = i;
break;
}
if (pos > N) return -1;
if (pos < N) sol += countLucky(0, N - pos);
int perm[14];
bool flag[14];
memset(flag, false, sizeof(flag));
for (i = 1; i <= pos; i++) {
long long cnt = 0;
for (j = 1; j <= pos; j++)
if (!flag[j]) {
++cnt;
if (cnt * fact[pos - i] >= K) {
flag[j] = true;
perm[i] = N - pos + j;
K -= (cnt - 1) * fact[pos - i];
break;
}
}
}
for (i = 1; i <= pos; ++i)
if (isLucky(N - pos + i) && isLucky(perm[i])) ++sol;
return sol;
}
int main() {
scanf("%d %d", &N, &K);
cout << solve() << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
double PI = 3.141592653589793;
const int mx = 13;
long long cnt[mx + 1];
int val[mx + 1];
bool lck(int n) {
bool out = 1;
while (n != 0) {
if (n % 10 != 4 && n % 10 != 7) out = 0;
n /= 10;
}
return out;
}
const int bmx = 9;
vector<int> proc;
void init() {
for (int i = 1; i < bmx + 1; i++) {
for (int j = 0; j < (1 << i); j++) {
int val = 0;
for (int k = 0; k < i; k++) {
if (j & (1 << k))
val = 10 * val + 7;
else
val = 10 * val + 4;
}
proc.push_back(val);
}
}
}
void solve() {
init();
int n, k;
cin >> n >> k;
k--;
cnt[0] = 1;
for (int i = 1; i < mx + 1; i++) cnt[i] = cnt[i - 1] * i;
if (n <= mx && k + 1 > cnt[n]) {
cout << -1;
return;
}
set<int> done;
for (int i = 0; i < mx + 1; i++) {
long long ccnt = 0, ind = 0;
while (done.count(ind)) ind++;
while (ccnt + cnt[mx - i] <= k) {
ccnt += cnt[mx - i];
ind++;
while (done.count(ind)) ind++;
}
k -= ccnt;
val[i] = ind;
done.insert(ind);
}
int ans = 0;
if (n <= mx + 1) {
for (int i = mx - n + 1; i <= mx; i++) {
int in = i - (mx - n);
int v = val[i] - (mx - n);
if (lck(in) && lck(v)) ans++;
}
} else {
int st = n - mx;
for (auto i : proc) ans += (i < st);
for (int i = 0; i <= mx; i++) {
int in = st + i;
int v = st + (val[i]);
if (lck(in) && lck(v)) ans++;
}
}
cout << ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
for (int c = 1; c < T + 1; c++) {
solve();
}
}
| ### 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;
double PI = 3.141592653589793;
const int mx = 13;
long long cnt[mx + 1];
int val[mx + 1];
bool lck(int n) {
bool out = 1;
while (n != 0) {
if (n % 10 != 4 && n % 10 != 7) out = 0;
n /= 10;
}
return out;
}
const int bmx = 9;
vector<int> proc;
void init() {
for (int i = 1; i < bmx + 1; i++) {
for (int j = 0; j < (1 << i); j++) {
int val = 0;
for (int k = 0; k < i; k++) {
if (j & (1 << k))
val = 10 * val + 7;
else
val = 10 * val + 4;
}
proc.push_back(val);
}
}
}
void solve() {
init();
int n, k;
cin >> n >> k;
k--;
cnt[0] = 1;
for (int i = 1; i < mx + 1; i++) cnt[i] = cnt[i - 1] * i;
if (n <= mx && k + 1 > cnt[n]) {
cout << -1;
return;
}
set<int> done;
for (int i = 0; i < mx + 1; i++) {
long long ccnt = 0, ind = 0;
while (done.count(ind)) ind++;
while (ccnt + cnt[mx - i] <= k) {
ccnt += cnt[mx - i];
ind++;
while (done.count(ind)) ind++;
}
k -= ccnt;
val[i] = ind;
done.insert(ind);
}
int ans = 0;
if (n <= mx + 1) {
for (int i = mx - n + 1; i <= mx; i++) {
int in = i - (mx - n);
int v = val[i] - (mx - n);
if (lck(in) && lck(v)) ans++;
}
} else {
int st = n - mx;
for (auto i : proc) ans += (i < st);
for (int i = 0; i <= mx; i++) {
int in = st + i;
int v = st + (val[i]);
if (lck(in) && lck(v)) ans++;
}
}
cout << ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
for (int c = 1; c < T + 1; c++) {
solve();
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int m = 0;
int nn;
long long n, k;
long long fact[20];
long long a[20000];
int b[20];
bool check[20];
int per[20];
bool found;
int res;
void doit(int, int);
void gen(int);
bool isLucky(int);
int main() {
cin >> n >> k;
fact[0] = fact[1] = 1;
for (int i = 2; i <= 13; i++) fact[i] = i * fact[i - 1];
for (int d = 1; d <= 10; d++) {
for (int i = 1; i <= d; i++) b[i] = 0;
doit(1, d);
}
res = 0;
for (int i = 1; i <= m; i++)
if (a[i] > n - 13)
break;
else
res++;
found = false;
if (n > 13)
nn = 13;
else
nn = n;
for (int i = 1; i <= nn; i++) check[i] = true;
gen(1);
if (!found)
cout << -1 << endl;
else
cout << res << endl;
return 0;
}
void doit(int i, int d) {
if (i > d) {
long long x = 0;
for (int j = 1; j <= d; j++)
if (b[j] == 0)
x = x * 10 + 4;
else
x = x * 10 + 7;
a[++m] = x;
return;
}
for (int j = 0; j <= 1; j++) {
b[i] = j;
doit(i + 1, d);
b[i] = 0;
}
}
void gen(int i) {
if (i > nn) {
found = true;
int x = n - 13;
if (x < 0) x = 0;
for (int j = 1; j <= nn; j++) {
if (isLucky(x + per[j]) && isLucky(x + j)) res++;
}
return;
}
for (int j = 1; j <= nn; j++)
if (check[j]) {
if (k <= fact[nn - i]) {
per[i] = j;
check[j] = false;
gen(i + 1);
if (found) return;
} else
k = k - fact[nn - i];
}
}
bool isLucky(int x) {
while (x > 0) {
if (!(x % 10 == 4 || x % 10 == 7)) return false;
x = x / 10;
}
return true;
}
| ### 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 m = 0;
int nn;
long long n, k;
long long fact[20];
long long a[20000];
int b[20];
bool check[20];
int per[20];
bool found;
int res;
void doit(int, int);
void gen(int);
bool isLucky(int);
int main() {
cin >> n >> k;
fact[0] = fact[1] = 1;
for (int i = 2; i <= 13; i++) fact[i] = i * fact[i - 1];
for (int d = 1; d <= 10; d++) {
for (int i = 1; i <= d; i++) b[i] = 0;
doit(1, d);
}
res = 0;
for (int i = 1; i <= m; i++)
if (a[i] > n - 13)
break;
else
res++;
found = false;
if (n > 13)
nn = 13;
else
nn = n;
for (int i = 1; i <= nn; i++) check[i] = true;
gen(1);
if (!found)
cout << -1 << endl;
else
cout << res << endl;
return 0;
}
void doit(int i, int d) {
if (i > d) {
long long x = 0;
for (int j = 1; j <= d; j++)
if (b[j] == 0)
x = x * 10 + 4;
else
x = x * 10 + 7;
a[++m] = x;
return;
}
for (int j = 0; j <= 1; j++) {
b[i] = j;
doit(i + 1, d);
b[i] = 0;
}
}
void gen(int i) {
if (i > nn) {
found = true;
int x = n - 13;
if (x < 0) x = 0;
for (int j = 1; j <= nn; j++) {
if (isLucky(x + per[j]) && isLucky(x + j)) res++;
}
return;
}
for (int j = 1; j <= nn; j++)
if (check[j]) {
if (k <= fact[nn - i]) {
per[i] = j;
check[j] = false;
gen(i + 1);
if (found) return;
} else
k = k - fact[nn - i];
}
}
bool isLucky(int x) {
while (x > 0) {
if (!(x % 10 == 4 || x % 10 == 7)) return false;
x = x / 10;
}
return true;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1E9 + 5;
const long long LINF = (long long)4E18 + 5;
const long double PI = acos(-1.0);
const long double E = 2.718281828459045L;
template <typename T>
T gcd(T a, T b) {
return (b == 0) ? abs(a) : gcd(b, a % b);
}
template <typename T>
inline T lcm(T a, T b) {
return a / gcd(a, b) * b;
}
template <typename T>
inline T mod(T a, T b) {
return (a % b + b) % b;
}
template <typename T>
inline T sqr(T x) {
return x * x;
}
template <typename T>
inline string toString(const T& x) {
ostringstream os;
os << x;
return os.str();
}
inline long long toInt(const string& s) {
istringstream is(s);
long long x;
is >> x;
return x;
}
inline long double toDouble(const string& s) {
istringstream is(s);
long double x;
is >> x;
return x;
}
inline string toLower(string s) {
for (typeof((s).begin()) it = (s).begin(); it != (s).end(); ++it)
*it = tolower(*it);
return s;
}
inline string toUpper(string s) {
for (typeof((s).begin()) it = (s).begin(); it != (s).end(); ++it)
*it = toupper(*it);
return s;
}
const char DEBUG_PARAM[] = "__LOCAL_TESTING";
const char IN[] = "_.in";
const char OUT[] = "_.out";
inline void init();
inline void run();
int ntest = 0, test;
int main(int argc, char* argv[]) {
if (argc > 1 && strcmp(argv[1], DEBUG_PARAM) == 0) {
freopen(IN, "r", stdin);
}
init();
if (ntest == 0) {
puts("ntest = ?");
return 0;
}
for (test = 1; test <= ntest; test++) {
run();
}
return 0;
}
inline void stop() { ntest = test - 1; }
const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, -1, 1, 0};
const long double EPS = 1E-9;
const long long MODULE = 1000000007LL;
vector<long long> v;
void dfs(long long x) {
if (x > INF * 10LL) {
return;
}
v.push_back(x);
dfs(x * 10 + 4);
dfs(x * 10 + 7);
}
inline void init() {
ntest = 1;
dfs(4LL), dfs(7LL);
sort((v).begin(), (v).end());
}
int n, k;
bool isLucky(int x) {
string s = toString(x);
for (typeof((s).begin()) it = (s).begin(); it != (s).end(); ++it) {
if (*it != '4' && *it != '7') {
return false;
}
}
return true;
}
int find(int x) {
int cnt = 0;
for (typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it) {
if (*it <= x) {
cnt++;
}
}
return cnt;
}
bool check[15];
long long frac[15], tail[15];
inline void run() {
scanf("%d%d\n", &n, &k);
long long t = 1;
int w = 0;
for (int i = (1); i <= (n); i++) {
t = t * i;
if (t >= k) {
w = i;
break;
}
}
if (!w) {
puts("-1");
return;
}
frac[0] = 1;
for (int i = (1); i <= (w); i++) {
frac[i] = frac[i - 1] * i;
}
vector<pair<int, int> > u;
memset((check), (true), sizeof(check));
for (int i = (1); i <= (w); i++) {
int j = 1;
while (1) {
while (!check[j]) {
j++;
}
if (k <= frac[w - i]) {
check[j] = false;
u.push_back(make_pair(n - w + j, n - w + i));
break;
} else {
k -= frac[w - i];
j++;
}
}
}
int ret = 0;
for (typeof((u).begin()) it = (u).begin(); it != (u).end(); ++it) {
if (isLucky(it->first) && isLucky(it->second)) {
ret++;
}
}
int x = n - w;
ret += find(x);
cout << ret << endl;
}
| ### 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 int INF = (int)1E9 + 5;
const long long LINF = (long long)4E18 + 5;
const long double PI = acos(-1.0);
const long double E = 2.718281828459045L;
template <typename T>
T gcd(T a, T b) {
return (b == 0) ? abs(a) : gcd(b, a % b);
}
template <typename T>
inline T lcm(T a, T b) {
return a / gcd(a, b) * b;
}
template <typename T>
inline T mod(T a, T b) {
return (a % b + b) % b;
}
template <typename T>
inline T sqr(T x) {
return x * x;
}
template <typename T>
inline string toString(const T& x) {
ostringstream os;
os << x;
return os.str();
}
inline long long toInt(const string& s) {
istringstream is(s);
long long x;
is >> x;
return x;
}
inline long double toDouble(const string& s) {
istringstream is(s);
long double x;
is >> x;
return x;
}
inline string toLower(string s) {
for (typeof((s).begin()) it = (s).begin(); it != (s).end(); ++it)
*it = tolower(*it);
return s;
}
inline string toUpper(string s) {
for (typeof((s).begin()) it = (s).begin(); it != (s).end(); ++it)
*it = toupper(*it);
return s;
}
const char DEBUG_PARAM[] = "__LOCAL_TESTING";
const char IN[] = "_.in";
const char OUT[] = "_.out";
inline void init();
inline void run();
int ntest = 0, test;
int main(int argc, char* argv[]) {
if (argc > 1 && strcmp(argv[1], DEBUG_PARAM) == 0) {
freopen(IN, "r", stdin);
}
init();
if (ntest == 0) {
puts("ntest = ?");
return 0;
}
for (test = 1; test <= ntest; test++) {
run();
}
return 0;
}
inline void stop() { ntest = test - 1; }
const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, -1, 1, 0};
const long double EPS = 1E-9;
const long long MODULE = 1000000007LL;
vector<long long> v;
void dfs(long long x) {
if (x > INF * 10LL) {
return;
}
v.push_back(x);
dfs(x * 10 + 4);
dfs(x * 10 + 7);
}
inline void init() {
ntest = 1;
dfs(4LL), dfs(7LL);
sort((v).begin(), (v).end());
}
int n, k;
bool isLucky(int x) {
string s = toString(x);
for (typeof((s).begin()) it = (s).begin(); it != (s).end(); ++it) {
if (*it != '4' && *it != '7') {
return false;
}
}
return true;
}
int find(int x) {
int cnt = 0;
for (typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it) {
if (*it <= x) {
cnt++;
}
}
return cnt;
}
bool check[15];
long long frac[15], tail[15];
inline void run() {
scanf("%d%d\n", &n, &k);
long long t = 1;
int w = 0;
for (int i = (1); i <= (n); i++) {
t = t * i;
if (t >= k) {
w = i;
break;
}
}
if (!w) {
puts("-1");
return;
}
frac[0] = 1;
for (int i = (1); i <= (w); i++) {
frac[i] = frac[i - 1] * i;
}
vector<pair<int, int> > u;
memset((check), (true), sizeof(check));
for (int i = (1); i <= (w); i++) {
int j = 1;
while (1) {
while (!check[j]) {
j++;
}
if (k <= frac[w - i]) {
check[j] = false;
u.push_back(make_pair(n - w + j, n - w + i));
break;
} else {
k -= frac[w - i];
j++;
}
}
}
int ret = 0;
for (typeof((u).begin()) it = (u).begin(); it != (u).end(); ++it) {
if (isLucky(it->first) && isLucky(it->second)) {
ret++;
}
}
int x = n - w;
ret += find(x);
cout << ret << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, mod = 1e9 + 7;
vector<int> v;
long long fac[N];
int n, k;
bool check(int x) {
while (x) {
int y = x % 10;
x /= 10;
if (y != 4 && y != 7) return 0;
}
return 1;
}
vector<int> get(int cnt, int num) {
set<int> s;
vector<int> ret;
for (int i = 1; i <= cnt; i++) s.insert(i);
int now = 1;
for (int i = 1; i <= cnt; i++) {
for (int x : s) {
if (now + fac[cnt - i] > k) {
ret.push_back(x);
s.erase(x);
break;
}
now += fac[cnt - i];
}
}
return ret;
}
void go(long long x) {
if (x > mod) return;
v.push_back(x);
x *= 10;
go(x + 7);
go(x + 4);
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
fac[0] = 1;
for (int i = 1; i < N; i++) {
fac[i] = 1ll * fac[i - 1] * i;
if (fac[i] > mod) break;
}
go(4);
go(7);
sort(v.begin(), v.end());
cin >> n >> k;
long long nw = 1;
int cnt;
for (int i = 1; i <= n; i++) {
nw *= i;
cnt = i;
if (nw >= k) break;
}
if (nw < k) return cout << -1, 0;
int x = n - cnt;
int ret = upper_bound(v.begin(), v.end(), x) - v.begin();
vector<int> vec = get(cnt, k);
for (int i = x + 1; i <= n; i++) {
if (check(i) && check(vec[i - x - 1] + x)) ret++;
}
cout << ret;
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 N = 1e5 + 10, mod = 1e9 + 7;
vector<int> v;
long long fac[N];
int n, k;
bool check(int x) {
while (x) {
int y = x % 10;
x /= 10;
if (y != 4 && y != 7) return 0;
}
return 1;
}
vector<int> get(int cnt, int num) {
set<int> s;
vector<int> ret;
for (int i = 1; i <= cnt; i++) s.insert(i);
int now = 1;
for (int i = 1; i <= cnt; i++) {
for (int x : s) {
if (now + fac[cnt - i] > k) {
ret.push_back(x);
s.erase(x);
break;
}
now += fac[cnt - i];
}
}
return ret;
}
void go(long long x) {
if (x > mod) return;
v.push_back(x);
x *= 10;
go(x + 7);
go(x + 4);
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
fac[0] = 1;
for (int i = 1; i < N; i++) {
fac[i] = 1ll * fac[i - 1] * i;
if (fac[i] > mod) break;
}
go(4);
go(7);
sort(v.begin(), v.end());
cin >> n >> k;
long long nw = 1;
int cnt;
for (int i = 1; i <= n; i++) {
nw *= i;
cnt = i;
if (nw >= k) break;
}
if (nw < k) return cout << -1, 0;
int x = n - cnt;
int ret = upper_bound(v.begin(), v.end(), x) - v.begin();
vector<int> vec = get(cnt, k);
for (int i = x + 1; i <= n; i++) {
if (check(i) && check(vec[i - x - 1] + x)) ret++;
}
cout << ret;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long lu[] = {
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};
void pv(vector<char> v) {
cout << "[ ";
for (int i = 0; i < int((v).size()); i++) {
cout << v[i] << ", ";
}
cout << "]" << endl;
}
long long factorial(int n) {
long long res = 1;
while (n) {
res *= n;
--n;
}
return res;
}
vector<int> ret;
void per(vector<int> v, int indx) {
if (indx == 0) {
sort((v).begin(), (v).end());
for (int i = 0; i < int((v).size()); ++i) ret.push_back(v[i]);
return;
}
int ln = int((v).size()) - 1;
vector<long long> rng(ln + 1);
for (int i = 0; i <= ln; ++i) {
rng[i] = i * factorial(ln);
}
for (int i = 0; i <= ln; ++i) {
if (indx >= rng[i] && indx < rng[i + 1]) {
int at = find((v).begin(), (v).end(), v[i]) - v.begin();
int a = v[i];
v.erase(v.begin() + at);
sort((v).begin(), (v).end());
ret.push_back(a);
per(v, indx - rng[i]);
break;
}
}
}
bool isluck(int n) {
while (n) {
if (n % 10 != 7 && n % 10 != 4) return false;
n /= 10;
}
return true;
}
int main() {
long long n, k;
int ct = 0;
cin >> n >> k;
k--;
int siz = sizeof lu / sizeof(long long);
if (n > 15) {
int nn = n - 15;
for (int i = 0; i < siz; ++i) {
if (lu[i] <= nn) ct++;
}
vector<int> vec;
for (int i = nn + 1; i <= n; ++i) {
vec.push_back(i);
}
per(vec, k);
for (int i = 0; i < int((ret).size()); ++i) {
int indx = i + nn + 1;
if (isluck(indx) && isluck(ret[i])) ct++;
}
cout << ct << endl;
return 0;
} else {
if (k + 1 > factorial(n)) {
cout << -1 << endl;
return 0;
}
vector<int> vec;
for (int i = 1; i <= n; ++i) {
vec.push_back(i);
}
per(vec, k);
for (int i = 0; i < int((ret).size()); ++i) {
if (isluck(i + 1) && isluck(ret[i])) ct++;
}
cout << ct << endl;
return 0;
}
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;
long long lu[] = {
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};
void pv(vector<char> v) {
cout << "[ ";
for (int i = 0; i < int((v).size()); i++) {
cout << v[i] << ", ";
}
cout << "]" << endl;
}
long long factorial(int n) {
long long res = 1;
while (n) {
res *= n;
--n;
}
return res;
}
vector<int> ret;
void per(vector<int> v, int indx) {
if (indx == 0) {
sort((v).begin(), (v).end());
for (int i = 0; i < int((v).size()); ++i) ret.push_back(v[i]);
return;
}
int ln = int((v).size()) - 1;
vector<long long> rng(ln + 1);
for (int i = 0; i <= ln; ++i) {
rng[i] = i * factorial(ln);
}
for (int i = 0; i <= ln; ++i) {
if (indx >= rng[i] && indx < rng[i + 1]) {
int at = find((v).begin(), (v).end(), v[i]) - v.begin();
int a = v[i];
v.erase(v.begin() + at);
sort((v).begin(), (v).end());
ret.push_back(a);
per(v, indx - rng[i]);
break;
}
}
}
bool isluck(int n) {
while (n) {
if (n % 10 != 7 && n % 10 != 4) return false;
n /= 10;
}
return true;
}
int main() {
long long n, k;
int ct = 0;
cin >> n >> k;
k--;
int siz = sizeof lu / sizeof(long long);
if (n > 15) {
int nn = n - 15;
for (int i = 0; i < siz; ++i) {
if (lu[i] <= nn) ct++;
}
vector<int> vec;
for (int i = nn + 1; i <= n; ++i) {
vec.push_back(i);
}
per(vec, k);
for (int i = 0; i < int((ret).size()); ++i) {
int indx = i + nn + 1;
if (isluck(indx) && isluck(ret[i])) ct++;
}
cout << ct << endl;
return 0;
} else {
if (k + 1 > factorial(n)) {
cout << -1 << endl;
return 0;
}
vector<int> vec;
for (int i = 1; i <= n; ++i) {
vec.push_back(i);
}
per(vec, k);
for (int i = 0; i < int((ret).size()); ++i) {
if (isluck(i + 1) && isluck(ret[i])) ct++;
}
cout << ct << endl;
return 0;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, k;
int mx[15];
int cal(int ix) {
if (ix == 0) return 0;
long long temp = 1, i;
for (i = 1;; i++) {
temp *= i;
if (temp >= ix) break;
}
int p = i - 1;
if (temp == ix) {
reverse(mx, mx + p + 1);
return 0;
}
int t = temp / i;
t = (ix - 1) / t;
swap(mx[p], mx[p - t]);
sort(mx, mx + p);
reverse(mx, mx + p);
cal(ix - temp / i * t);
return p;
}
bool islucky(int x) {
while (x) {
int t = x % 10;
if (t != 4 && t != 7) return 0;
x /= 10;
}
return 1;
}
int res;
long long np;
void per(long long x) {
if (x >= np) return;
if (x) res++;
per(x * 10 + 4);
per(x * 10 + 7);
}
int main() {
cin >> n >> k;
long long temp = 1;
bool sign = false;
for (int i = 1; i <= n; i++) {
temp *= i;
if (temp >= k) {
sign = true;
break;
}
}
if (!sign) {
cout << "-1" << endl;
return 0;
}
int nn = n;
for (int i = 0; i <= 14; i++) {
mx[i] = nn--;
}
int p = cal(k);
np = n - p;
per(0);
nn = n;
for (int i = 0; i <= p; i++) {
if (islucky(mx[i]) && islucky(nn)) res++;
nn--;
}
cout << res << 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;
int n, k;
int mx[15];
int cal(int ix) {
if (ix == 0) return 0;
long long temp = 1, i;
for (i = 1;; i++) {
temp *= i;
if (temp >= ix) break;
}
int p = i - 1;
if (temp == ix) {
reverse(mx, mx + p + 1);
return 0;
}
int t = temp / i;
t = (ix - 1) / t;
swap(mx[p], mx[p - t]);
sort(mx, mx + p);
reverse(mx, mx + p);
cal(ix - temp / i * t);
return p;
}
bool islucky(int x) {
while (x) {
int t = x % 10;
if (t != 4 && t != 7) return 0;
x /= 10;
}
return 1;
}
int res;
long long np;
void per(long long x) {
if (x >= np) return;
if (x) res++;
per(x * 10 + 4);
per(x * 10 + 7);
}
int main() {
cin >> n >> k;
long long temp = 1;
bool sign = false;
for (int i = 1; i <= n; i++) {
temp *= i;
if (temp >= k) {
sign = true;
break;
}
}
if (!sign) {
cout << "-1" << endl;
return 0;
}
int nn = n;
for (int i = 0; i <= 14; i++) {
mx[i] = nn--;
}
int p = cal(k);
np = n - p;
per(0);
nn = n;
for (int i = 0; i <= p; i++) {
if (islucky(mx[i]) && islucky(nn)) res++;
nn--;
}
cout << res << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
vector<int> gen_perm(int n, int k) {
long long fact[20];
fact[0] = 1;
for (long long i = 1; i < 20; i++) {
fact[i] = i * fact[i - 1];
fact[i] %= mod;
}
bool placed[20] = {0};
vector<int> v;
for (int p = 1; p <= n; p++) {
for (int i = 1; i <= n; i++) {
if (!placed[i]) {
if (k > fact[n - p]) {
k -= fact[n - p];
} else {
placed[i] = 1;
v.push_back(i);
break;
}
}
}
}
for (int i = 1; i <= n; i++) {
if (!placed[i]) {
v.push_back(i);
}
}
return v;
}
bool islucky(int x) {
while (x > 0) {
if (x % 10 != 4 and x % 10 != 7) return 0;
x /= 10;
}
return 1;
}
long long lim;
int ans = 0;
set<int> s;
void dfs(long long v) {
if (v > lim) return;
if (s.count(v)) return;
ans++;
s.insert(v);
dfs(10 * v + 4);
dfs(10 * v + 7);
}
int main() {
int n, k;
cin >> n >> k;
if (n <= 13) {
long long ret = 1;
for (int i = 1; i <= n; i++) {
ret *= (long long)i;
}
if (ret < k) {
cout << -1;
return 0;
}
vector<int> v = gen_perm(n, k);
int ans = 0;
for (int i = 0; i < v.size(); i++) {
if (islucky(i + 1) and islucky(v[i])) ans++;
}
cout << ans;
} else {
vector<int> v = gen_perm(13, k);
lim = n - 13;
dfs(4);
dfs(7);
for (int i = lim + 1; i <= n; i++) {
if (islucky(i) == 1 and islucky(lim + v[i - lim - 1]) == 1) ans++;
}
cout << ans;
}
}
| ### 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 mod = 1000000007;
vector<int> gen_perm(int n, int k) {
long long fact[20];
fact[0] = 1;
for (long long i = 1; i < 20; i++) {
fact[i] = i * fact[i - 1];
fact[i] %= mod;
}
bool placed[20] = {0};
vector<int> v;
for (int p = 1; p <= n; p++) {
for (int i = 1; i <= n; i++) {
if (!placed[i]) {
if (k > fact[n - p]) {
k -= fact[n - p];
} else {
placed[i] = 1;
v.push_back(i);
break;
}
}
}
}
for (int i = 1; i <= n; i++) {
if (!placed[i]) {
v.push_back(i);
}
}
return v;
}
bool islucky(int x) {
while (x > 0) {
if (x % 10 != 4 and x % 10 != 7) return 0;
x /= 10;
}
return 1;
}
long long lim;
int ans = 0;
set<int> s;
void dfs(long long v) {
if (v > lim) return;
if (s.count(v)) return;
ans++;
s.insert(v);
dfs(10 * v + 4);
dfs(10 * v + 7);
}
int main() {
int n, k;
cin >> n >> k;
if (n <= 13) {
long long ret = 1;
for (int i = 1; i <= n; i++) {
ret *= (long long)i;
}
if (ret < k) {
cout << -1;
return 0;
}
vector<int> v = gen_perm(n, k);
int ans = 0;
for (int i = 0; i < v.size(); i++) {
if (islucky(i + 1) and islucky(v[i])) ans++;
}
cout << ans;
} else {
vector<int> v = gen_perm(13, k);
lim = n - 13;
dfs(4);
dfs(7);
for (int i = lim + 1; i <= n; i++) {
if (islucky(i) == 1 and islucky(lim + v[i - lim - 1]) == 1) ans++;
}
cout << ans;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long dx[] = {4LL, 7LL};
vector<long long> lucky;
vector<long long> P;
int main() {
{
queue<int> Q;
Q.push(0);
while (!Q.empty()) {
long long x = Q.front();
for (int i = 0; i < 2; i++) {
long long x1 = x * 10LL + dx[i];
lucky.push_back(x1);
if (x1 <= 1000000000LL) {
Q.push(x1);
}
}
Q.pop();
}
}
for (long long i = 1, p = 1; i <= 14; i++) {
P.push_back(p);
p *= i;
}
int n, k;
cin >> n >> k;
k--;
if (n < 13 && P[n] - 1 < k) {
cout << -1;
return (0);
}
list<int> L;
for (int i = 1;; i++) {
L.push_back(i);
if (P[i] - 1 >= k) break;
}
vector<int> perm;
for (int i = L.size() - 1; i >= 0; i--) {
list<int>::iterator it = L.begin();
while (k >= P[i]) {
k -= P[i];
it++;
}
perm.push_back(*it);
L.erase(it);
}
int res = 0;
for (int i = 0; lucky[i] < n - perm.size() + 1; i++) res++;
for (int i = 0, pos = n - perm.size() + 1; i < perm.size(); i++, pos++) {
int d = perm[i] - (i + 1);
int truenum = pos + d;
bool f1 = false, f2 = false;
for (int i = 0; i < lucky.size(); i++) {
if (truenum == lucky[i]) f1 = true;
if (pos == lucky[i]) f2 = true;
}
if (f1 && f2) res++;
}
cout << res;
exit:
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 dx[] = {4LL, 7LL};
vector<long long> lucky;
vector<long long> P;
int main() {
{
queue<int> Q;
Q.push(0);
while (!Q.empty()) {
long long x = Q.front();
for (int i = 0; i < 2; i++) {
long long x1 = x * 10LL + dx[i];
lucky.push_back(x1);
if (x1 <= 1000000000LL) {
Q.push(x1);
}
}
Q.pop();
}
}
for (long long i = 1, p = 1; i <= 14; i++) {
P.push_back(p);
p *= i;
}
int n, k;
cin >> n >> k;
k--;
if (n < 13 && P[n] - 1 < k) {
cout << -1;
return (0);
}
list<int> L;
for (int i = 1;; i++) {
L.push_back(i);
if (P[i] - 1 >= k) break;
}
vector<int> perm;
for (int i = L.size() - 1; i >= 0; i--) {
list<int>::iterator it = L.begin();
while (k >= P[i]) {
k -= P[i];
it++;
}
perm.push_back(*it);
L.erase(it);
}
int res = 0;
for (int i = 0; lucky[i] < n - perm.size() + 1; i++) res++;
for (int i = 0, pos = n - perm.size() + 1; i < perm.size(); i++, pos++) {
int d = perm[i] - (i + 1);
int truenum = pos + d;
bool f1 = false, f2 = false;
for (int i = 0; i < lucky.size(); i++) {
if (truenum == lucky[i]) f1 = true;
if (pos == lucky[i]) f2 = true;
}
if (f1 && f2) res++;
}
cout << res;
exit:
return (0);
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <class _T>
inline _T sqr(const _T &first) {
return first * first;
}
template <class _T>
inline string tostr(const _T &a) {
ostringstream os("");
os << a;
return os.str();
}
const long double PI = 3.1415926535897932384626433832795L;
const long double EPS = 1e-9;
char TEMPORARY_CHAR;
const int INF = 1e9;
inline void fft(vector<complex<long double> > &a, bool invert) {
int n = (int)a.size();
for (int i = 1, j = 0; i < n; ++i) {
int bit = n >> 1;
for (; j >= bit; bit >>= 1) j -= bit;
j += bit;
if (i < j) swap(a[i], a[j]);
}
for (int len = 2; len <= n; len <<= 1) {
long double ang = 2 * PI / len * (invert ? -1 : 1);
complex<long double> wlen(cos(ang), sin(ang));
for (int i = 0; i < n; i += len) {
complex<long double> w(1);
for (int j = 0; j < len / 2; ++j) {
complex<long double> u = a[i + j], v = a[i + j + len / 2] * w;
a[i + j] = u + v;
a[i + j + len / 2] = u - v;
w *= wlen;
}
}
}
if (invert)
for (int i = 0; i < n; ++i) a[i] /= n;
}
inline void input(int &a) {
a = 0;
while (((TEMPORARY_CHAR = getchar()) > '9' || TEMPORARY_CHAR < '0') &&
(TEMPORARY_CHAR != '-')) {
}
char neg = 0;
if (TEMPORARY_CHAR == '-') {
neg = 1;
TEMPORARY_CHAR = getchar();
}
while (TEMPORARY_CHAR <= '9' && TEMPORARY_CHAR >= '0') {
a = (a << 3) + (a << 1) + TEMPORARY_CHAR - '0';
TEMPORARY_CHAR = getchar();
}
if (neg) a = -a;
}
inline void out(long long a) {
if (!a) putchar('0');
if (a < 0) {
putchar('-');
a = -a;
}
char s[20];
int i;
for (i = 0; a; ++i) {
s[i] = '0' + a % 10;
a /= 10;
}
for (int j = (i)-1; j >= 0; j--) putchar(s[j]);
}
inline int nxt() {
int(ret);
input((ret));
;
return ret;
}
struct lnum {
vector<int> a;
int base;
lnum(int num = 0, int base = 1000000000) : base(base) {
if (!num) a.resize(1);
while (num) {
a.push_back(num % base);
num /= base;
}
}
inline int len() const { return a.size(); }
lnum &operator=(const lnum &l) {
if (this != &l) {
a = l.a;
base = l.base;
}
return *this;
}
inline friend lnum operator+(const lnum &l, const lnum &r) {
lnum ret(0, l.base);
int base = l.base;
int ln = l.len(), rn = r.len();
int n = max(ln, rn);
ret.a.resize(n);
int o = 0;
for (int i = 0; i < n; ++i) {
int s = o;
if (i < ln) s += l.a[i];
if (i < rn) s += r.a[i];
o = s >= base;
if (o) s -= base;
ret.a[i] = s;
}
if (o) ret.a.push_back(1);
return ret;
}
inline friend lnum operator-(const lnum &l, const lnum &r) {
lnum ret(0, l.base);
int base = l.base;
int n = l.len();
int rn = r.len();
ret.a.resize(n);
int o = 0;
for (int i = 0; i < n; ++i) {
int s = l.a[i] - o;
if (i < rn) s -= r.a[i];
o = s < 0;
if (o) s += base;
ret.a[i] = s;
}
if (ret.len() > 1 && !ret.a.back()) ret.a.pop_back();
return ret;
}
inline friend lnum operator*(const lnum &l, const lnum &r) {
lnum ret(0, l.base);
int base = l.base;
if (l.len() * r.len() > 1000000) {
vector<complex<long double> > fa(l.a.begin(), l.a.end()),
fb(r.a.begin(), r.a.end());
int n = 1;
while (n < max(l.len(), r.len())) n <<= 1;
n <<= 1;
fa.resize(n), fb.resize(n);
fft(fa, false), fft(fb, false);
for (int i = 0; i < n; ++i) fa[i] *= fb[i];
fft(fa, true);
ret.a.resize(n);
for (int i = 0; i < n; ++i) ret.a[i] = int(fa[i].real() + 0.5);
int carry = 0;
for (int i = 0; i < n; ++i) {
ret.a[i] += carry;
carry = ret.a[i] / base;
ret.a[i] %= base;
}
} else {
ret.a.resize(l.len() + r.len());
for (int i = 0; i < l.len(); ++i)
for (int j = 0, carry = 0; j < r.len() || carry; ++j) {
long long cur = ret.a[i + j] +
(long long)l.a[i] * (j < r.len() ? r.a[j] : 0) +
carry;
ret.a[i + j] = cur % base;
carry = cur / base;
}
}
while (ret.len() > 1 && !ret.a.back()) ret.a.pop_back();
return ret;
}
inline friend lnum operator/(const lnum &l, const int &r) {
lnum ret(0, l.base);
ret.a.resize(l.len());
int carry = 0;
for (int i = l.len() - 1; i >= 0; --i) {
long long cur = l.a[i] + (long long)carry * l.base;
ret.a[i] = cur / r;
carry = cur % r;
}
while (ret.len() > 1 && ret.a.back() == 0) ret.a.pop_back();
return ret;
}
inline friend bool operator<(const lnum &l, const lnum &r) {
if (l.len() < r.len()) return true;
if (l.len() > r.len()) return false;
int n = l.len();
for (int i = n - 1; i >= 0; --i) {
if (l.a[i] < r.a[i]) return true;
if (l.a[i] > r.a[i]) return false;
}
return false;
}
inline friend bool operator>(const lnum &l, const lnum &r) { return r < l; }
inline friend bool operator==(const lnum &l, const lnum &r) {
if (l.len() != r.len()) return false;
int n = l.len();
for (int i = n - 1; i; --i) {
if (l.a[i] != r.a[i]) return false;
}
return true;
}
inline friend bool operator!=(const lnum &l, const lnum &r) {
return !(l == r);
}
inline void print() {
if (base == 1000000000) {
printf("%d", a.back());
for (int i = a.size() - 2; i >= 0; --i) printf("%09d", a[i]);
} else {
for (int i = a.size() - 1; i >= 0; --i) printf("%d", a[i]);
}
}
};
bool is_happy(int a) {
while (a) {
if (a % 10 != 4 && a % 10 != 7) return false;
a /= 10;
}
return true;
}
int main() {
vector<long long> nums;
for (int l = 1; l <= 10; ++l) {
int n = 1 << l;
for (int i = 0; i < (int)(n); i++) {
long long cur = 0;
for (int j = 0; j < (int)(l); j++)
if ((i >> j) & 1)
cur = 10 * cur + 4;
else
cur = 10 * cur + 7;
nums.push_back(cur);
}
}
sort((nums).begin(), (nums).end());
int(n);
input((n));
;
int(k);
input((k));
;
long long f[14];
f[0] = 1;
int q = 13;
((q) = (n) < (q) ? (n) : (q));
for (int i = 1; i <= q; ++i) f[i] = i * f[i - 1];
if (n <= q && f[n] < k) {
puts("-1");
return 0;
}
--k;
int a[q];
int used[q];
memset((used), 0, sizeof(used));
for (int i = 0; i < q; ++i) {
int pos = k / f[q - i - 1];
for (int j = 0; j < (int)(q); j++) {
if (!used[j]) {
if (!pos) {
a[i] = j;
used[j] = true;
break;
}
--pos;
}
}
k %= f[q - i - 1];
}
int ans = upper_bound((nums).begin(), (nums).end(), n - q) - nums.begin();
for (int j = n - q; j < n; ++j) {
int pos = j + 1;
int val = a[j - (n - q)] + n - q + 1;
if (is_happy(pos) && is_happy(val)) ++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;
template <class _T>
inline _T sqr(const _T &first) {
return first * first;
}
template <class _T>
inline string tostr(const _T &a) {
ostringstream os("");
os << a;
return os.str();
}
const long double PI = 3.1415926535897932384626433832795L;
const long double EPS = 1e-9;
char TEMPORARY_CHAR;
const int INF = 1e9;
inline void fft(vector<complex<long double> > &a, bool invert) {
int n = (int)a.size();
for (int i = 1, j = 0; i < n; ++i) {
int bit = n >> 1;
for (; j >= bit; bit >>= 1) j -= bit;
j += bit;
if (i < j) swap(a[i], a[j]);
}
for (int len = 2; len <= n; len <<= 1) {
long double ang = 2 * PI / len * (invert ? -1 : 1);
complex<long double> wlen(cos(ang), sin(ang));
for (int i = 0; i < n; i += len) {
complex<long double> w(1);
for (int j = 0; j < len / 2; ++j) {
complex<long double> u = a[i + j], v = a[i + j + len / 2] * w;
a[i + j] = u + v;
a[i + j + len / 2] = u - v;
w *= wlen;
}
}
}
if (invert)
for (int i = 0; i < n; ++i) a[i] /= n;
}
inline void input(int &a) {
a = 0;
while (((TEMPORARY_CHAR = getchar()) > '9' || TEMPORARY_CHAR < '0') &&
(TEMPORARY_CHAR != '-')) {
}
char neg = 0;
if (TEMPORARY_CHAR == '-') {
neg = 1;
TEMPORARY_CHAR = getchar();
}
while (TEMPORARY_CHAR <= '9' && TEMPORARY_CHAR >= '0') {
a = (a << 3) + (a << 1) + TEMPORARY_CHAR - '0';
TEMPORARY_CHAR = getchar();
}
if (neg) a = -a;
}
inline void out(long long a) {
if (!a) putchar('0');
if (a < 0) {
putchar('-');
a = -a;
}
char s[20];
int i;
for (i = 0; a; ++i) {
s[i] = '0' + a % 10;
a /= 10;
}
for (int j = (i)-1; j >= 0; j--) putchar(s[j]);
}
inline int nxt() {
int(ret);
input((ret));
;
return ret;
}
struct lnum {
vector<int> a;
int base;
lnum(int num = 0, int base = 1000000000) : base(base) {
if (!num) a.resize(1);
while (num) {
a.push_back(num % base);
num /= base;
}
}
inline int len() const { return a.size(); }
lnum &operator=(const lnum &l) {
if (this != &l) {
a = l.a;
base = l.base;
}
return *this;
}
inline friend lnum operator+(const lnum &l, const lnum &r) {
lnum ret(0, l.base);
int base = l.base;
int ln = l.len(), rn = r.len();
int n = max(ln, rn);
ret.a.resize(n);
int o = 0;
for (int i = 0; i < n; ++i) {
int s = o;
if (i < ln) s += l.a[i];
if (i < rn) s += r.a[i];
o = s >= base;
if (o) s -= base;
ret.a[i] = s;
}
if (o) ret.a.push_back(1);
return ret;
}
inline friend lnum operator-(const lnum &l, const lnum &r) {
lnum ret(0, l.base);
int base = l.base;
int n = l.len();
int rn = r.len();
ret.a.resize(n);
int o = 0;
for (int i = 0; i < n; ++i) {
int s = l.a[i] - o;
if (i < rn) s -= r.a[i];
o = s < 0;
if (o) s += base;
ret.a[i] = s;
}
if (ret.len() > 1 && !ret.a.back()) ret.a.pop_back();
return ret;
}
inline friend lnum operator*(const lnum &l, const lnum &r) {
lnum ret(0, l.base);
int base = l.base;
if (l.len() * r.len() > 1000000) {
vector<complex<long double> > fa(l.a.begin(), l.a.end()),
fb(r.a.begin(), r.a.end());
int n = 1;
while (n < max(l.len(), r.len())) n <<= 1;
n <<= 1;
fa.resize(n), fb.resize(n);
fft(fa, false), fft(fb, false);
for (int i = 0; i < n; ++i) fa[i] *= fb[i];
fft(fa, true);
ret.a.resize(n);
for (int i = 0; i < n; ++i) ret.a[i] = int(fa[i].real() + 0.5);
int carry = 0;
for (int i = 0; i < n; ++i) {
ret.a[i] += carry;
carry = ret.a[i] / base;
ret.a[i] %= base;
}
} else {
ret.a.resize(l.len() + r.len());
for (int i = 0; i < l.len(); ++i)
for (int j = 0, carry = 0; j < r.len() || carry; ++j) {
long long cur = ret.a[i + j] +
(long long)l.a[i] * (j < r.len() ? r.a[j] : 0) +
carry;
ret.a[i + j] = cur % base;
carry = cur / base;
}
}
while (ret.len() > 1 && !ret.a.back()) ret.a.pop_back();
return ret;
}
inline friend lnum operator/(const lnum &l, const int &r) {
lnum ret(0, l.base);
ret.a.resize(l.len());
int carry = 0;
for (int i = l.len() - 1; i >= 0; --i) {
long long cur = l.a[i] + (long long)carry * l.base;
ret.a[i] = cur / r;
carry = cur % r;
}
while (ret.len() > 1 && ret.a.back() == 0) ret.a.pop_back();
return ret;
}
inline friend bool operator<(const lnum &l, const lnum &r) {
if (l.len() < r.len()) return true;
if (l.len() > r.len()) return false;
int n = l.len();
for (int i = n - 1; i >= 0; --i) {
if (l.a[i] < r.a[i]) return true;
if (l.a[i] > r.a[i]) return false;
}
return false;
}
inline friend bool operator>(const lnum &l, const lnum &r) { return r < l; }
inline friend bool operator==(const lnum &l, const lnum &r) {
if (l.len() != r.len()) return false;
int n = l.len();
for (int i = n - 1; i; --i) {
if (l.a[i] != r.a[i]) return false;
}
return true;
}
inline friend bool operator!=(const lnum &l, const lnum &r) {
return !(l == r);
}
inline void print() {
if (base == 1000000000) {
printf("%d", a.back());
for (int i = a.size() - 2; i >= 0; --i) printf("%09d", a[i]);
} else {
for (int i = a.size() - 1; i >= 0; --i) printf("%d", a[i]);
}
}
};
bool is_happy(int a) {
while (a) {
if (a % 10 != 4 && a % 10 != 7) return false;
a /= 10;
}
return true;
}
int main() {
vector<long long> nums;
for (int l = 1; l <= 10; ++l) {
int n = 1 << l;
for (int i = 0; i < (int)(n); i++) {
long long cur = 0;
for (int j = 0; j < (int)(l); j++)
if ((i >> j) & 1)
cur = 10 * cur + 4;
else
cur = 10 * cur + 7;
nums.push_back(cur);
}
}
sort((nums).begin(), (nums).end());
int(n);
input((n));
;
int(k);
input((k));
;
long long f[14];
f[0] = 1;
int q = 13;
((q) = (n) < (q) ? (n) : (q));
for (int i = 1; i <= q; ++i) f[i] = i * f[i - 1];
if (n <= q && f[n] < k) {
puts("-1");
return 0;
}
--k;
int a[q];
int used[q];
memset((used), 0, sizeof(used));
for (int i = 0; i < q; ++i) {
int pos = k / f[q - i - 1];
for (int j = 0; j < (int)(q); j++) {
if (!used[j]) {
if (!pos) {
a[i] = j;
used[j] = true;
break;
}
--pos;
}
}
k %= f[q - i - 1];
}
int ans = upper_bound((nums).begin(), (nums).end(), n - q) - nums.begin();
for (int j = n - q; j < n; ++j) {
int pos = j + 1;
int val = a[j - (n - q)] + n - q + 1;
if (is_happy(pos) && is_happy(val)) ++ans;
}
cout << ans << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
const int N = 100100;
using namespace std;
long long n, k, cnt;
bool used[1000];
void build(long long x, long long mx) {
if (x > mx) return;
if (x) cnt++;
build(x * 10 + 4, mx);
build(x * 10 + 7, mx);
}
bool check(long long x) {
while (x) {
if (x % 10 != 4 && x % 10 != 7) return false;
x /= 10;
}
return true;
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> k;
long long f = 1, g = 1;
for (long long i = 1; i <= n; i++) {
f *= i;
if (f >= k) {
g = i;
break;
}
}
if (f < k) {
cout << -1 << endl;
return 0;
}
vector<long long> v;
for (long long i = g; i >= 1; i--) {
f /= i;
long long s = 0;
for (long long j = 1; j <= g; j++) {
if (used[j])
continue;
else
s++;
if (f * (s - 1) + 1 <= k && k <= f * s) {
k -= f * (s - 1);
used[j] = true;
v.push_back(j);
break;
}
}
}
build(0, n - g);
for (int i = 0; i < v.size(); i++)
if (check(n - g + i + 1) && check(n - g + v[i])) cnt++;
cout << cnt << endl;
}
| ### 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>
const int N = 100100;
using namespace std;
long long n, k, cnt;
bool used[1000];
void build(long long x, long long mx) {
if (x > mx) return;
if (x) cnt++;
build(x * 10 + 4, mx);
build(x * 10 + 7, mx);
}
bool check(long long x) {
while (x) {
if (x % 10 != 4 && x % 10 != 7) return false;
x /= 10;
}
return true;
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> k;
long long f = 1, g = 1;
for (long long i = 1; i <= n; i++) {
f *= i;
if (f >= k) {
g = i;
break;
}
}
if (f < k) {
cout << -1 << endl;
return 0;
}
vector<long long> v;
for (long long i = g; i >= 1; i--) {
f /= i;
long long s = 0;
for (long long j = 1; j <= g; j++) {
if (used[j])
continue;
else
s++;
if (f * (s - 1) + 1 <= k && k <= f * s) {
k -= f * (s - 1);
used[j] = true;
v.push_back(j);
break;
}
}
}
build(0, n - g);
for (int i = 0; i < v.size(); i++)
if (check(n - g + i + 1) && check(n - g + v[i])) cnt++;
cout << cnt << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7, INF = 1e18;
long long n, k;
long long tot(long long count) {
if (count > n) return 0;
return 1 + tot(count * 10 + 7) + tot(count * 10 + 4);
}
long long findit(long long a) {
while (a) {
if (a % 10 != 4 && a % 10 != 7) return 0;
a /= 10;
}
return 1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t = 1;
while (t--) {
cin >> n >> k;
vector<long long> vect(1, 1);
long long i = 0, ans = tot(4) + tot(7);
while (vect[i] < k) {
vect.push_back(vect[i] * (i + 2));
i++;
}
if (vect.size() > n) {
cout << "-1\n";
continue;
}
k--;
vector<int> vis(vect.size(), 0), perm;
long long m = n - vect.size() + 1, count = 0;
while (k) {
i = upper_bound(vect.begin(), vect.end(), k) - vect.begin();
long long j = 0;
while (count < vis.size() - i - 1) {
while (vis[j]) j++;
vis[j] = 1;
count++;
perm.push_back(j);
}
i--;
long long idx = k / vect[i];
k %= vect[i];
while (idx) {
while (vis[j]) j++;
j++;
idx--;
}
while (vis[j]) j++;
vis[j] = 1;
count++;
perm.push_back(j);
}
for (i = 0; i < vis.size(); i++)
if (!vis[i]) {
perm.push_back(i);
}
for (i = 0; i < perm.size(); i++) {
if (findit(perm[i] + m)) {
if (!findit(i + m)) {
ans--;
}
}
}
cout << ans << "\n";
}
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 long long mod = 1e9 + 7, INF = 1e18;
long long n, k;
long long tot(long long count) {
if (count > n) return 0;
return 1 + tot(count * 10 + 7) + tot(count * 10 + 4);
}
long long findit(long long a) {
while (a) {
if (a % 10 != 4 && a % 10 != 7) return 0;
a /= 10;
}
return 1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t = 1;
while (t--) {
cin >> n >> k;
vector<long long> vect(1, 1);
long long i = 0, ans = tot(4) + tot(7);
while (vect[i] < k) {
vect.push_back(vect[i] * (i + 2));
i++;
}
if (vect.size() > n) {
cout << "-1\n";
continue;
}
k--;
vector<int> vis(vect.size(), 0), perm;
long long m = n - vect.size() + 1, count = 0;
while (k) {
i = upper_bound(vect.begin(), vect.end(), k) - vect.begin();
long long j = 0;
while (count < vis.size() - i - 1) {
while (vis[j]) j++;
vis[j] = 1;
count++;
perm.push_back(j);
}
i--;
long long idx = k / vect[i];
k %= vect[i];
while (idx) {
while (vis[j]) j++;
j++;
idx--;
}
while (vis[j]) j++;
vis[j] = 1;
count++;
perm.push_back(j);
}
for (i = 0; i < vis.size(); i++)
if (!vis[i]) {
perm.push_back(i);
}
for (i = 0; i < perm.size(); i++) {
if (findit(perm[i] + m)) {
if (!findit(i + m)) {
ans--;
}
}
}
cout << ans << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lb = 10000000000;
void vrikodara(long long n = 12) {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << setprecision(n) << fixed;
}
long long n, k;
set<long long> s;
vector<long long> fact(14, 1);
void generator(long long num) {
lb--;
if (lb < 0) {
cout << "exit";
exit(0);
};
if (num * 10 + 4 > n) return;
s.insert(num * 10 + 4);
generator(num * 10 + 4);
if (num * 10 + 7 > n) return;
s.insert(num * 10 + 7);
generator(num * 10 + 7);
}
void generate_kth_permutation(vector<long long> &v, long long sz) {
long long K = k;
set<long long> num;
v.clear();
for (long long i = 1; i <= sz; i++) num.insert(i);
while (sz--) {
K -= fact[sz];
for (auto i : num) {
if (K > 0) {
K -= fact[sz];
continue;
}
v.push_back(i);
break;
}
num.erase(v.back());
if (!K) break;
K += fact[sz];
}
vector<long long> vd;
for (auto e : num) vd.push_back(e);
reverse(vd.begin(), vd.end());
for (auto e : vd) v.push_back(e);
}
void solve() {
cin >> n >> k;
bool flag = false;
for (long long a = 1, i = n; i >= 1; i--) {
a *= i;
if (a >= k) {
flag = true;
break;
}
}
if (!flag) {
cout << "-1";
return;
}
generator(0);
long long i = n, p = 1;
while (i >= 1 && p < k) {
p *= (n - i + 1);
i--;
}
long long ans = 0;
for (auto e : s)
if (e <= i) ans++;
long long sz = n - i;
vector<long long> v;
for (long long i = 1, a = 1; i <= 13; i++) {
a *= i;
fact[i] = a;
}
generate_kth_permutation(v, sz);
for (auto &e : v) e += i;
for (long long idx = i + 1; idx <= n; idx++) {
if (s.count(idx) && s.count(v[idx - i - 1])) ans++;
}
cout << ans << '\n';
}
int32_t main() {
vrikodara();
long long t = 1;
while (t--) {
solve();
}
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;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lb = 10000000000;
void vrikodara(long long n = 12) {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << setprecision(n) << fixed;
}
long long n, k;
set<long long> s;
vector<long long> fact(14, 1);
void generator(long long num) {
lb--;
if (lb < 0) {
cout << "exit";
exit(0);
};
if (num * 10 + 4 > n) return;
s.insert(num * 10 + 4);
generator(num * 10 + 4);
if (num * 10 + 7 > n) return;
s.insert(num * 10 + 7);
generator(num * 10 + 7);
}
void generate_kth_permutation(vector<long long> &v, long long sz) {
long long K = k;
set<long long> num;
v.clear();
for (long long i = 1; i <= sz; i++) num.insert(i);
while (sz--) {
K -= fact[sz];
for (auto i : num) {
if (K > 0) {
K -= fact[sz];
continue;
}
v.push_back(i);
break;
}
num.erase(v.back());
if (!K) break;
K += fact[sz];
}
vector<long long> vd;
for (auto e : num) vd.push_back(e);
reverse(vd.begin(), vd.end());
for (auto e : vd) v.push_back(e);
}
void solve() {
cin >> n >> k;
bool flag = false;
for (long long a = 1, i = n; i >= 1; i--) {
a *= i;
if (a >= k) {
flag = true;
break;
}
}
if (!flag) {
cout << "-1";
return;
}
generator(0);
long long i = n, p = 1;
while (i >= 1 && p < k) {
p *= (n - i + 1);
i--;
}
long long ans = 0;
for (auto e : s)
if (e <= i) ans++;
long long sz = n - i;
vector<long long> v;
for (long long i = 1, a = 1; i <= 13; i++) {
a *= i;
fact[i] = a;
}
generate_kth_permutation(v, sz);
for (auto &e : v) e += i;
for (long long idx = i + 1; idx <= n; idx++) {
if (s.count(idx) && s.count(v[idx - i - 1])) ans++;
}
cout << ans << '\n';
}
int32_t main() {
vrikodara();
long long t = 1;
while (t--) {
solve();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long factorial[13], res = 0;
bool OK(int n) {
stringstream ss;
string s;
ss << n;
ss >> s;
ss.clear();
for (int i = 0; i < s.size(); i++) {
if (s[i] != '4' && s[i] != '7') return false;
}
return true;
}
int main() {
int n, k;
cin >> n >> k;
vector<int> permutation;
vector<int> v;
vector<long long> luckys;
luckys.push_back(4);
luckys.push_back(7);
for (int i = 0;; i++) {
if (luckys[i] > 1e9) break;
luckys.push_back(luckys[i] * 10 + 4);
luckys.push_back(luckys[i] * 10 + 7);
}
factorial[0] = 1;
for (int i = 1; i <= 13; i++) {
factorial[i] = factorial[i - 1] * i;
if (i <= n) permutation.push_back(i);
}
if (n < 13) {
if (k > factorial[n]) {
cout << -1;
return 0;
}
}
res = 0;
for (int i = min(n - 1, 12); i >= 1; i--) {
for (int j = 0; j < permutation.size(); j++) {
if (k > factorial[i])
k -= factorial[i];
else {
v.push_back(permutation[j]);
permutation.erase(permutation.begin() + j);
break;
}
}
}
for (int i = 0; i < permutation.size(); i++) v.push_back(permutation[i]);
int nn = n;
if (n > 12) {
for (int i = 0; i < v.size(); i++) {
v[i] = n - (13 - v[i]);
}
}
for (int i = v.size() - 1; i >= 0; i--) {
if (OK(nn) && OK(v[i])) {
res++;
}
nn--;
}
for (int i = 0;; i++) {
if (luckys[i] < n - 12)
res++;
else
break;
}
cout << res;
}
| ### 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 factorial[13], res = 0;
bool OK(int n) {
stringstream ss;
string s;
ss << n;
ss >> s;
ss.clear();
for (int i = 0; i < s.size(); i++) {
if (s[i] != '4' && s[i] != '7') return false;
}
return true;
}
int main() {
int n, k;
cin >> n >> k;
vector<int> permutation;
vector<int> v;
vector<long long> luckys;
luckys.push_back(4);
luckys.push_back(7);
for (int i = 0;; i++) {
if (luckys[i] > 1e9) break;
luckys.push_back(luckys[i] * 10 + 4);
luckys.push_back(luckys[i] * 10 + 7);
}
factorial[0] = 1;
for (int i = 1; i <= 13; i++) {
factorial[i] = factorial[i - 1] * i;
if (i <= n) permutation.push_back(i);
}
if (n < 13) {
if (k > factorial[n]) {
cout << -1;
return 0;
}
}
res = 0;
for (int i = min(n - 1, 12); i >= 1; i--) {
for (int j = 0; j < permutation.size(); j++) {
if (k > factorial[i])
k -= factorial[i];
else {
v.push_back(permutation[j]);
permutation.erase(permutation.begin() + j);
break;
}
}
}
for (int i = 0; i < permutation.size(); i++) v.push_back(permutation[i]);
int nn = n;
if (n > 12) {
for (int i = 0; i < v.size(); i++) {
v[i] = n - (13 - v[i]);
}
}
for (int i = v.size() - 1; i >= 0; i--) {
if (OK(nn) && OK(v[i])) {
res++;
}
nn--;
}
for (int i = 0;; i++) {
if (luckys[i] < n - 12)
res++;
else
break;
}
cout << res;
}
``` |
#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;
if (K > F) {
cout << -1 << endl;
return 0;
}
}
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
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;
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;
if (K > F) {
cout << -1 << endl;
return 0;
}
}
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 double pi = acos(-1.0);
const double eps = 1E-7;
const int inf = int(1e9) + 7;
int a[200000];
int tot;
void dfs(long long x, int th) {
if (x > 0) a[tot++] = x;
if (th > 0) {
dfs(x * 10 + 4, th - 1);
dfs(x * 10 + 7, th - 1);
}
}
long long jie(int x) {
long long res = 1;
for (int(i) = (1); (i) < (x + 1); ++(i)) res *= i;
return res;
}
int ans[30];
void get_permutation(int n, int k) {
bool mark[20];
memset((mark), 0, sizeof((mark)));
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= n; j++)
if (!mark[j]) {
if (jie(i - 1) >= k) {
mark[j] = true;
ans[i] = j;
break;
} else {
k -= jie(i - 1);
}
}
}
reverse(ans + 1, ans + n + 1);
}
bool isLuck(int x) {
while (x) {
int y = x % 10;
if (y != 4 && y != 7) return false;
x /= 10;
}
return true;
}
int main() {
dfs(0, 9);
sort(a, a + tot);
int n, k;
cin >> n >> k;
int base = 1;
while (base <= n && jie(base) < k) base++;
if (base > n) {
cout << "-1" << endl;
return 0;
}
int num = 0;
for (int(i) = 0; (i) < (tot); ++(i))
if (a[i] <= n - base) num = i + 1;
get_permutation(base, k);
for (int i = 1; i <= base; i++) {
if (isLuck(i + n - base) && isLuck(ans[i] + n - base)) num++;
}
cout << num << 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;
const double pi = acos(-1.0);
const double eps = 1E-7;
const int inf = int(1e9) + 7;
int a[200000];
int tot;
void dfs(long long x, int th) {
if (x > 0) a[tot++] = x;
if (th > 0) {
dfs(x * 10 + 4, th - 1);
dfs(x * 10 + 7, th - 1);
}
}
long long jie(int x) {
long long res = 1;
for (int(i) = (1); (i) < (x + 1); ++(i)) res *= i;
return res;
}
int ans[30];
void get_permutation(int n, int k) {
bool mark[20];
memset((mark), 0, sizeof((mark)));
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= n; j++)
if (!mark[j]) {
if (jie(i - 1) >= k) {
mark[j] = true;
ans[i] = j;
break;
} else {
k -= jie(i - 1);
}
}
}
reverse(ans + 1, ans + n + 1);
}
bool isLuck(int x) {
while (x) {
int y = x % 10;
if (y != 4 && y != 7) return false;
x /= 10;
}
return true;
}
int main() {
dfs(0, 9);
sort(a, a + tot);
int n, k;
cin >> n >> k;
int base = 1;
while (base <= n && jie(base) < k) base++;
if (base > n) {
cout << "-1" << endl;
return 0;
}
int num = 0;
for (int(i) = 0; (i) < (tot); ++(i))
if (a[i] <= n - base) num = i + 1;
get_permutation(base, k);
for (int i = 1; i <= base; i++) {
if (isLuck(i + n - base) && isLuck(ans[i] + n - base)) num++;
}
cout << num << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long getLuckyNumberNoGreater(long long mx) {
vector<long long> nums;
if (mx >= 4) nums.push_back(4);
if (mx >= 7) nums.push_back(7);
for (int i = 0; i < nums.size() - 1 + 1; ++i) {
auto x = nums[i];
if (x * 10 + 4 <= mx) nums.push_back(x * 10 + 4);
if (x * 10 + 7 <= mx) nums.push_back(x * 10 + 7);
}
return nums.size();
}
bool isLucky(long long x) {
if (x == 0) return false;
while (x > 0) {
if (x % 10 != 4 && x % 10 != 7) return false;
x /= 10;
}
return true;
}
int main() {
ios_base::sync_with_stdio(false);
long long n;
cin >> n;
;
long long k;
cin >> k;
;
int digits = 1;
long long givePermutations = 1;
while (givePermutations < k) {
digits++;
givePermutations *= digits;
}
if (digits > n) {
cout << -1;
return 0;
}
auto digitsBefore = n - digits;
auto res = getLuckyNumberNoGreater(digitsBefore);
auto startFrom = digitsBefore + 1;
vector<long long> remainingNumbers;
for (int i = startFrom; i < n + 1; ++i) remainingNumbers.push_back(i);
k--;
givePermutations /= digits;
for (int i = 1; i < digits + 1; ++i) {
auto i2 = k / givePermutations;
k %= givePermutations;
if (isLucky(remainingNumbers[i2]) && isLucky(startFrom + i - 1)) res++;
remainingNumbers.erase(remainingNumbers.begin() + i2);
if (i < digits) givePermutations /= (digits - i);
}
cout << res;
}
| ### 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;
long long getLuckyNumberNoGreater(long long mx) {
vector<long long> nums;
if (mx >= 4) nums.push_back(4);
if (mx >= 7) nums.push_back(7);
for (int i = 0; i < nums.size() - 1 + 1; ++i) {
auto x = nums[i];
if (x * 10 + 4 <= mx) nums.push_back(x * 10 + 4);
if (x * 10 + 7 <= mx) nums.push_back(x * 10 + 7);
}
return nums.size();
}
bool isLucky(long long x) {
if (x == 0) return false;
while (x > 0) {
if (x % 10 != 4 && x % 10 != 7) return false;
x /= 10;
}
return true;
}
int main() {
ios_base::sync_with_stdio(false);
long long n;
cin >> n;
;
long long k;
cin >> k;
;
int digits = 1;
long long givePermutations = 1;
while (givePermutations < k) {
digits++;
givePermutations *= digits;
}
if (digits > n) {
cout << -1;
return 0;
}
auto digitsBefore = n - digits;
auto res = getLuckyNumberNoGreater(digitsBefore);
auto startFrom = digitsBefore + 1;
vector<long long> remainingNumbers;
for (int i = startFrom; i < n + 1; ++i) remainingNumbers.push_back(i);
k--;
givePermutations /= digits;
for (int i = 1; i < digits + 1; ++i) {
auto i2 = k / givePermutations;
k %= givePermutations;
if (isLucky(remainingNumbers[i2]) && isLucky(startFrom + i - 1)) res++;
remainingNumbers.erase(remainingNumbers.begin() + i2);
if (i < digits) givePermutations /= (digits - i);
}
cout << res;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int logfac(long long k) {
long long fac = 1;
int ans = 0;
while (fac < k) {
ans++;
fac *= ans;
}
return ans;
}
int a[100];
long long fact[30];
bool mark[100];
void make_permutation(int now, int n, int k) {
if (now == n) return;
int i;
for (i = 1; i <= n; i++)
if (!mark[i]) break;
while (fact[n - now - 1] < k) {
k -= fact[n - now - 1];
i++;
while (mark[i]) i++;
}
a[now] = i;
mark[i] = true;
make_permutation(now + 1, n, k);
}
bool lucky(long long n) {
while (n != 0) {
if (n % 10 != 4 && n % 10 != 7) return false;
n /= 10;
}
return true;
}
void addLuckeys(long long now, long long Max, int &ans) {
if (now > Max) return;
if (now != 0) ans++;
addLuckeys(now * 10 + 4, Max, ans);
addLuckeys(now * 10 + 7, Max, ans);
}
int main() {
int n, k;
cin >> n >> k;
int changable = logfac(k);
if (changable > n) {
cout << -1 << endl;
return 0;
}
fact[0] = 1;
for (int i = 1; fact[i - 1] < 1e9; i++) fact[i] = fact[i - 1] * i;
make_permutation(0, changable, k);
int ans = 0;
for (int i = 0; i < changable; i++) {
if (lucky(n - changable + 1 + i) && lucky(a[i] + n - changable)) ans++;
}
addLuckeys(0, n - changable, 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;
int logfac(long long k) {
long long fac = 1;
int ans = 0;
while (fac < k) {
ans++;
fac *= ans;
}
return ans;
}
int a[100];
long long fact[30];
bool mark[100];
void make_permutation(int now, int n, int k) {
if (now == n) return;
int i;
for (i = 1; i <= n; i++)
if (!mark[i]) break;
while (fact[n - now - 1] < k) {
k -= fact[n - now - 1];
i++;
while (mark[i]) i++;
}
a[now] = i;
mark[i] = true;
make_permutation(now + 1, n, k);
}
bool lucky(long long n) {
while (n != 0) {
if (n % 10 != 4 && n % 10 != 7) return false;
n /= 10;
}
return true;
}
void addLuckeys(long long now, long long Max, int &ans) {
if (now > Max) return;
if (now != 0) ans++;
addLuckeys(now * 10 + 4, Max, ans);
addLuckeys(now * 10 + 7, Max, ans);
}
int main() {
int n, k;
cin >> n >> k;
int changable = logfac(k);
if (changable > n) {
cout << -1 << endl;
return 0;
}
fact[0] = 1;
for (int i = 1; fact[i - 1] < 1e9; i++) fact[i] = fact[i - 1] * i;
make_permutation(0, changable, k);
int ans = 0;
for (int i = 0; i < changable; i++) {
if (lucky(n - changable + 1 + i) && lucky(a[i] + n - changable)) ans++;
}
addLuckeys(0, n - changable, ans);
cout << ans << endl;
return 0;
}
``` |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.