malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int INF = 2147483647;   int nxt[4][100001] = {0};   bool ok[4];   void getnext(int m, char s2[], int t) {     int j = 0;     for (int i = 2; i <= m; i++) {       while (j != 0 && s2[j + 1] != s2[i]) j = nxt[t][j];       if (s2[j + 1] == s2[i]) j++;       nxt[t][i] = j;     }     return;   }   int KMP(int n, int m, char s1[], char s2[], int t) {     int j = 0, cnt = 0;     for (int i = 1; i <= n; i++) {       while (j != 0 && s2[j + 1] != s1[i]) j = nxt[t][j];       if (s2[j + 1] == s1[i]) j++;       if (j == m) {         return -1;         j = nxt[t][j];       }     }     return j;   }   char s[4][100001];   int len[4];   int K[4][4];   int main() {     scanf( %s%s%s , s[1] + 1, s[2] + 1, s[3] + 1);     len[1] = strlen(s[1] + 1);     len[2] = strlen(s[2] + 1);     len[3] = strlen(s[3] + 1);     for (int i = 1; i <= 3; i++) {       getnext(len[i], s[i], i);       for (int j = 1; j <= 3; j++) {         if (i == j) continue;         K[j][i] = KMP(len[j], len[i], s[j], s[i], i);       }     }     int ans = INF;     for (int i = 1; i <= 3; i++) {       for (int j = 1; j <= 3; j++) {         for (int k = 1; k <= 3; k++) {           if (i == j || j == k || i == k) continue;           int sum = len[i] + len[j] + len[k] - K[i][j] - K[j][k];           if (K[i][j] >= 0 && K[j][k] >= 0)             ans = min(ans, sum);           else {             if (K[i][j] < 0 && K[i][k] < 0)               ans = min(ans, len[i]);             else if (K[i][j] < 0)               ans = min(ans, sum + K[i][j] + K[j][k] - len[j] - K[i][k]);             if (K[j][k] < 0) ans = min(ans, sum + K[j][k] - len[k]);           }         }       }     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void file(string s) {     freopen((s +  .in ).c_str(),  r , stdin);     freopen((s +  .out ).c_str(),  w , stdout);   }   template <typename Tp>   void read(Tp &x) {     long long fh = 1;     char c = getchar();     x = 0;     while (c >  9  || c <  0 ) {       if (c ==  - ) {         fh = -1;       }       c = getchar();     }     while (c >=  0  && c <=  9 ) {       x = (x << 1) + (x << 3) + (c & 15);       c = getchar();     }     x *= fh;   }   long long n, m, T;   vector<long long> ft[1000005];   long long pos[1000005];   namespace FTree {   long long dfnl[1000005], dfnr[1000005], tot;   struct BIT {     long long b[1000005];     void add(long long x, long long v) {       for (; x <= tot; x += (x & (-x))) b[x] += v;     }     long long ask(long long x) {       long long ret = 0;       for (; x; x -= (x & (-x))) ret += b[x];       return ret;     }   } Tr;   void dfs(long long x) {     dfnl[x] = ++tot;     for (unsigned i = 0; i < ft[x].size(); ++i) {       long long y = ft[x][i];       dfs(y);     }     dfnr[x] = tot;   }   void prep() {     dfs(0);     for (long long i = 1; i <= n; ++i) {       Tr.add(dfnl[pos[i]], 1);       Tr.add(dfnr[pos[i]] + 1, -1);     }   }   void upd(long long x, long long v) {     Tr.add(dfnl[x], v);     Tr.add(dfnr[x] + 1, -v);   }   long long qry(long long x) { return Tr.ask(dfnl[x]); }   }  // namespace FTree   namespace ACAM {   signed ch[1000005][26], fail[1000005], tot;   long long ins(const char *str) {     long long p = 0, nn = strlen(str), dir;     for (long long i = 0; i < nn; ++i) {       dir = str[i] -  a ;       if (!ch[p][dir]) ch[p][dir] = ++tot;       p = ch[p][dir];     }     return p;   }   void build() {     queue<long long> q;     for (long long i = 0; i < 26; ++i)       if (ch[0][i]) q.push(ch[0][i]);     while (!q.empty()) {       long long x = q.front();       q.pop();       for (long long i = 0; i < 26; ++i) {         if (ch[x][i]) {           fail[ch[x][i]] = ch[fail[x]][i];           q.push(ch[x][i]);         } else {           ch[x][i] = ch[fail[x]][i];         }       }     }     for (long long i = 1; i <= tot; ++i) ft[fail[i]].push_back(i);     FTree::prep();   }   void mtch(const char *str) {     long long p = 0, nn = strlen(str), ans = 0;     for (long long i = 0; i < nn; ++i) {       p = ch[p][str[i] -  a ];       ans += FTree::qry(p);     }     printf( %lld n , ans);   }   }  // namespace ACAM   char sstr[1000005];   bool vis[1000005];   signed main() {     read(T);     read(n);     for (long long i = 1; i <= n; ++i) {       scanf( %s , sstr);       pos[i] = ACAM::ins(sstr);       vis[i] = 1;     }     ACAM::build();     char op;     for (long long i = 1, x; i <= T; ++i) {       scanf(  n%c , &op);       if (op ==  ? ) {         scanf( %s , sstr);         ACAM::mtch(sstr);       }       if (op ==  + ) {         read(x);         if (!vis[x]) {           FTree::upd(pos[x], 1), vis[x] = 1;         }       }       if (op ==  - ) {         read(x);         if (vis[x]) {           FTree::upd(pos[x], -1), vis[x] = 0;         }       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};   vector<int> next(vector<int>& a, int p, int k) {     vector<int> result;     for (long long mul = 1; mul <= (long long)k; mul *= p) {       for (int i = 0; i < a.size(); ++i) {         long long newN = (long long)a[i] * mul;         if (newN <= k) {           result.push_back((int)newN);         }       }     }     return result;   }   int main() {     ios_base::sync_with_stdio(false);     int k;     cin >> k;     int treshold = 2 * k * k;     vector<int> p;     vector<int> a;     a.push_back(1);     int need = (k / 2);     if (k % 2 == 1) need++;     p.push_back(prime[0]);     int ppp = 1;     for (int i = 0; i < p.size(); ++i) {       int old = a.size();       a = next(a, p[i], 2 * k * k);       if (a.size() - old >= need && a.size() >= k)         break;       else         p.push_back(prime[ppp++]);     }     vector<int> result;     vector<int> h(p.size());     int i = a.size() - 1;     for (int ch = (int)p.size() - 1; ch >= 0; ch--) {       for (; h[ch] < need && i >= 0; --i) {         if (a[i] % p[ch] == 0) {           result.push_back(a[i]);           for (int pp = 0; pp < p.size(); ++pp) {             if (a[i] % p[pp] == 0) ++h[pp];           }           a[i] = 31;         }       }     }     while (result.size() < k) {       if (i < 0) i = (a.size() - 1);       if (a[i] != 31) {         result.push_back(a[i]);         a[i] = 31;       }       i--;     }     for (int i = 0; i < result.size(); ++i) cout << result[i] <<    ;     return 0;   }
#include <bits/stdc++.h>   int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }   using namespace ::std;   const int maxn = 5300;   const int inf = 1e9 + 800;   const int mod = 1e9 + 7;   bool ro[maxn][maxn];   bool cu[maxn][maxn];   bool mosro[maxn];   bool moscu[maxn];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     for (int i = 0; i < n; i++) {       string s;       cin >> s;       for (int j = 0; j < n; j += 4) {         char c = s[j / 4];         if ( A  <= c && c <=  Z ) {           c -=  A ;           c += 10;         } else {           c -=  0 ;         }         ro[i][j] = ((c >> 3) & 1);         ro[i][j + 1] = ((c >> 2) & 1);         ro[i][j + 2] = ((c >> 1) & 1);         ro[i][j + 3] = ((c >> 0) & 1);       }     }     for (int i = 0; i < n; i++) {       for (int j = n - 1; j >= 0; j--) {         cu[i][j] = ro[j][i];       }     }     for (int i = 0; i + 1 < n; i++) {       mosro[i] = 1;       moscu[i] = 1;       for (int j = 0; j < n; j++) {         if (ro[i][j] != ro[i + 1][j]) {           mosro[i] = 0;         }         if (cu[i][j] != cu[i + 1][j]) {           moscu[i] = 0;         }       }     }     for (int x = n; x >= 1; x--) {       if (n % x == 0) {         bool good = 1;         for (int j = 0; j + 1 < n; j++) {           if (j % x != x - 1 && (mosro[j] == 0 || moscu[j] == 0)) {             good = 0;             break;           }         }         if (good) {           cout << x;           return 0;         }       }     }   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   int p[100010];   const int md = 1000000009;   int an = 1;   int sz[100010];   void make_set(int v) {     p[v] = v;     sz[v] = 1;   }   int find_set(int v) {     if (p[v] == v) return v;     return p[v] = find_set(p[v]);   }   void union_sets(int a, int b) {     a = find_set(a);     b = find_set(b);     if (a != b) {       if (sz[a] < sz[b]) swap(a, b);       p[b] = a;       sz[a] += sz[b];     } else       an = (an * 2) % md;   }   int main() {     scanf( %d %d , &n, &m);     for (int i = 1; i <= n; i++) make_set(i);     for (int i = 0; i < m; i++) {       int a, b;       scanf( %d %d , &a, &b);       union_sets(a, b);       printf( %d n , an - 1);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int dp[100][100];   struct point {    public:     long long x, y;     point(long long a, long long b) {       x = a;       y = b;     }     point() {}     void setPoint(long long a, long long b) {       x = a;       y = b;     }     double dist(point p) { return sqrt(pow(x - p.x, 2.0) + pow(y - p.y, 2.0)); }     long long dot(point A, point B) {       point OA(A.x - x, A.y - y), OB(B.x - x, B.y - y);       return (OA.x * OB.x + OA.y * OB.y);     }     long long cross(point A, point B) {       point OA(A.x - x, A.y - y), OB(B.x - x, B.y - y);       return (OA.x * OB.y - OA.y * OB.x);     }     bool operator<(const point &p) const {       return x < p.x || (x == p.x && y < p.y);     }   };   int main() {     long long ans = 0;     int X, Y;     string str;     cin >> str;     Y = str.size();     cin >> X;     int val;     vector<pair<int, int>> a;     for (int i = 0; i < (X); i++) {       cin >> val;       a.push_back(make_pair(val - 1, 1));       a.push_back(make_pair(Y - val, -1));     }     sort((a).begin(), (a).end());     int start = 0, open = 0, stop = 0, dist, last = 1;     for (int i = 0; i <= a.size() / 2; i++) {       stop = a[i].first;       dist = stop;       if (last != a[i].second) {         dist = stop - start + 1;         dist = start + dist / 2;         last = a[i].second;       }       for (int j = (start); j < (dist); j++) {         if (open > 0) {           if (open % 2) {             str[j] ^= str[Y - 1 - j] ^= str[j] ^= str[Y - 1 - j];           }         }       }       start = stop;       open += a[i].second;     }     cout << str << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   string s;   int n;   int main() {     cin >> n;     cin >> s;     for (int i = 0; i < n; i++) {       if (i + 2 < n && s[i] ==  o  && s[i + 1] ==  g  && s[i + 2] ==  o ) {         while (i + 2 < n && s[i + 1] ==  g  && s[i + 2] ==  o ) i += 2;         cout <<  *** ;       } else         cout << s[i];     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     cin.tie(NULL);     cout.tie(NULL);     ios_base::sync_with_stdio(false);     int n;     cin >> n;     vector<string> arr;     for (int i = 0; i < n; i++) {       string x;       cin >> x;       arr.push_back(x);     }     int chocolate = 0;     for (int i = 0; i < n; i++) {       int temp = 0;       temp = count(arr[i].begin(), arr[i].end(),  C );       chocolate += (temp * (temp - 1)) / 2;     }     for (int i = 0; i < n; i++) {       int temp = 0;       for (int j = 0; j < n; j++) {         if (arr[j][i] ==  C ) {           temp += 1;         }       }       chocolate += (temp * (temp - 1)) / 2;     }     cout << chocolate << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<pair<pair<int, int>, int> > a;   int main() {     int L, b, f, n, m, cmd, x;     cin >> L >> b >> f;     cin >> n;     for (int cs = (1); cs <= (n); ++cs) {       sort((a).begin(), (a).end());       cin >> cmd >> x;       if (cmd == 1) {         if ((a.empty() && x <= L) || (!a.empty() && x + f <= a[0].first.first)) {           cout << 0 << endl;           a.push_back(make_pair(make_pair(0, x), cs));           goto C;         }         m = (int)(a).size();         for (int i = 0; i < (m - 1); ++i)           if (a[i + 1].first.first - a[i].first.second >= b + f + x) {             cout << a[i].first.second + b << endl;             a.push_back(make_pair(                 make_pair(a[i].first.second + b, a[i].first.second + b + x), cs));             goto C;           }         if (m && L - a.back().first.second >= b + x) {           cout << a.back().first.second + b << endl;           a.push_back(make_pair(               make_pair(a.back().first.second + b, a.back().first.second + b + x),               cs));           goto C;         }         cout << -1 << endl;       } else {         for (__typeof((a).begin()) i = ((a).begin()); i != (a).end(); ++i)           if (i->second == x) {             swap(*i, a.back());             a.pop_back();             goto C;           }       }     C:;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const int maxn = 2e6 + 7;   char str[maxn];   int ans[10][10], used[10];   int mp[10][10][10][10];   struct NODE {     int num, step;   };   int bfs(int numa, int numb, int a, int b) {     queue<NODE> que;     memset(used, 0, sizeof(used));     que.push((NODE){numa, 0});     while (!que.empty()) {       NODE now = que.front();       que.pop();       int x = (now.num + a) % 10;       int y = (now.num + b) % 10;       if (x == numb || y == numb) return now.step;       if (!used[x]) {         used[x] = 1;         que.push((NODE){x, now.step + 1});       }       if (!used[y]) {         used[y] = 1;         que.push((NODE){y, now.step + 1});       }     }     return -1;   }   int main() {     for (auto i = 0; i < 10; i++) {       for (auto j = i; j < 10; j++) {         for (auto numa = 0; numa < 10; numa++) {           for (auto numb = 0; numb < 10; numb++) {             int Insert = bfs(numa, numb, i, j);             if (Insert == -1) {               mp[i][j][numa][numb] = mp[j][i][numa][numb] = -1;               continue;             } else               mp[i][j][numa][numb] = mp[j][i][numa][numb] = Insert;           }         }       }     }     scanf( %s , str);     int len = strlen(str);     for (auto i = 0; i < 10; i++) {       for (auto j = i; j < 10; j++) {         for (auto p = 1; p < len; p++) {           int numa = str[p - 1] -  0 ;           int numb = str[p] -  0 ;           int Insert = mp[i][j][numa][numb];           if (Insert == -1) {             ans[i][j] = ans[j][i] = -1;             break;           } else {             ans[i][j] += Insert;             if (i != j) ans[j][i] += Insert;           }         }       }     }     for (auto i = 0; i < 10; i++) {       for (auto j = 0; j < 10; j++) {         printf( %d  , ans[i][j]);       }       puts(  );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string st;     cin >> st;     for (int i = 0; i < st.size(); i++) {       if (st[i] ==  0  || st[i] ==  8 ) {         cout <<  YES n ;         cout << st[i] << endl;         return 0;       }       for (int j = i + 1; j < st.size(); j++) {         int num = st[i] -  0 ;         num *= 10;         num += (st[j] -  0 );         if (num % 8 == 0) {           cout <<  YES n ;           cout << num << endl;           return 0;         }         for (int k = j + 1; k < st.size(); k++) {           num = st[i] -  0 ;           num *= 10;           num += (st[j] -  0 );           num *= 10;           num += (st[k] -  0 );           if (num % 8 == 0) {             cout <<  YES n ;             cout << num << endl;             return 0;           }         }       }     }     cout <<  NO n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   map<char, long long> mp;   map<pair<char, long long>, long long> cnt;   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     for (char c =  a ; c <=  z ; c++) cin >> mp[c];     string s;     cin >> s;     long long n = s.length();     long long sum = mp[s[0]];     cnt[{s[0], mp[s[0]]}]++;     long long ans = 0;     for (long long i = 1; i < n; i++) {       ans += cnt[{s[i], sum}];       sum += mp[s[i]];       cnt[{s[i], sum}]++;     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     map<int, int> mp;     long long ans = 0, mx = 1e6;     for (int i = 0; i < n; i++) {       int first;       scanf( %d , &first);       int second = 1;       long long yy = 1;       for (int j = 2; j * j <= first; j++)         if (first % j == 0) {           int cnt = 0;           while (first % j == 0) cnt++, first /= j;           cnt %= k;           if (cnt) {             for (int t = 0; t < cnt; t++) second *= j;             for (int t = cnt; t < k; t++) yy = min(mx, yy * j);           }         }       if (first > 1) {         int cnt = 1, j = first;         for (int t = 0; t < cnt; t++) second *= j;         for (int t = cnt; t < k; t++) yy = min(mx, yy * j);       }       if (mp.count(yy)) ans += mp[yy];       mp[second]++;     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   int R, C;   int ER, EC;   int SR, SC;   char field[1010][1010];   int dist[1010][1010];   int Q[1010 * 1010], Qb, Qe;   int dr[4] = {-1, +1, 0, 0};   int dc[4] = {0, 0, -1, +1};   int main(void) {     scanf( %d %d , &R, &C);     for (int r = 0; r < R; ++r) {       scanf( %s , field[r]);       for (int c = 0; c < C; ++c) {         if (field[r][c] ==  E ) {           ER = r;           EC = c;         }         if (field[r][c] ==  S ) {           SR = r;           SC = c;         }       }     }     memset(dist, -1, sizeof dist);     Q[0] = ER * 10000 + EC;     Qe = 1;     dist[ER][EC] = 0;     while (Qb != Qe) {       int r = Q[Qb] / 10000;       int c = Q[Qb] % 10000;       Qb += 1;       for (int d = 0; d < 4; ++d) {         int rr = r + dr[d];         int cc = c + dc[d];         if (rr < 0 || cc < 0 || rr >= R || cc >= C) continue;         if (field[rr][cc] ==  T ) continue;         if (dist[rr][cc] != -1) continue;         dist[rr][cc] = dist[r][c] + 1;         Q[Qe++] = rr * 10000 + cc;       }     }     int battles = 0;     int mydist = dist[SR][SC];     for (int r = 0; r < R; ++r) {       for (int c = 0; c < C; ++c) {         if (!isdigit(field[r][c])) continue;         if (dist[r][c] == -1 || dist[r][c] > mydist) continue;         int cnt = field[r][c] -  0 ;         battles += cnt;       }     }     printf( %d n , battles);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, M;   vector<long long> cards;   map<int, vector<int>> m;   int sodd = 1, seven = 2;   int oddbalance;   int nextOdd() {     for (int i = sodd;; i += 2) {       if (m[i].empty()) {         sodd = i + 2;         if (i > M) {           cout << -1 << endl;           exit(0);         }         return i;       }     }   }   int nextEven() {     for (int i = seven;; i += 2) {       if (m[i].empty()) {         seven = i + 2;         if (i > M) {           cout << -1 << endl;           exit(0);         }         return i;       }     }   }   int cnt;   int main() {     cin >> n >> M;     cards.resize(n);     for (int i = 0; i < n; i++) {       cin >> cards[i];       if (cards[i] % 2)         oddbalance++;       else         oddbalance--;       m[cards[i]].push_back(i);     }     for (auto p : m) {       while (p.second.size() > 1) {         cnt++;         if (p.first % 2 == 1)           oddbalance--;         else           oddbalance++;         int pos = p.second.back();         p.second.pop_back();         int val;         if (oddbalance > 0) {           val = nextEven();           oddbalance--;         } else {           val = nextOdd();           oddbalance++;         }         m[val].push_back(pos);         cards[pos] = val;       }     }     for (int i = 0; i < n && oddbalance; i++) {       if (oddbalance > 0 && cards[i] % 2 == 1) {         cnt++;         cards[i] = nextEven();         oddbalance -= 2;       } else if (oddbalance < 0 && cards[i] % 2 == 0) {         cnt++;         cards[i] = nextOdd();         oddbalance += 2;       }     }     if (oddbalance != 0) {       cout << -1 << endl;       exit(0);     }     cout << cnt << endl;     for (int c : cards) {       cout << c <<    ;     }     cout << endl;   }
#include <bits/stdc++.h>   using namespace std;   #pragma GCC optimize  -O3    bool prime(long long int n) {     if (n == 1) return false;     if (n == 2 || n == 3) return true;     long long int k = sqrt(n) + 1;     for (long long int i = 2; i < k; i++) {       if (n % i == 0) return false;     }     return true;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int x;     cin >> x;     long long int b[x];     for (long long int i = 0; i < x; i++) {       cin >> b[i];     }     long long int y;     cin >> y;     long long int g[y];     for (long long int i = 0; i < y; i++) {       cin >> g[i];     }     sort(b, b + x);     sort(g, g + y);     long long int ans = 0;     for (long long int i = 0; i < x; i++) {       for (long long int j = 0; j < y; j++) {         long long int val = abs(b[i] - g[j]);         if (val <= 1) {           g[j] = 99999;           ans++;           break;           ;         }       }     }     cout << ans;     cout << endl;     ;     ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, s, inp;   int c[10];   int best = -1;   int b3, b4, b5;   struct bezout {     long long d, a, b;   };   bezout extgcd(long long x, long long y) {     if (y == 0) return (bezout){x, 1, 0};     bezout s = extgcd(y, x % y);     return (bezout){s.d, s.b, s.a - x / y * s.b};   }   void check(int k3, int k4, int k5) {     int a = k3 * c[3], b = k4 * c[4], z = k5 * c[5];     if (a + b + z != s) return;     if (k3 < 0 || k3 > k4 || k4 > k5 || k3 > k5) return;     int ct = abs(a - b) + abs(z - b);     if (best == -1 || best > ct) {       best = ct;       b3 = k3;       b4 = k4;       b5 = k5;     }   }   int main() {     scanf( %d %d , &n, &s);     for (int i = 0; i < n; i++) {       scanf( %d , &inp);       c[inp]++;     }     for (int k4 = 0; c[4] * k4 <= s; k4++) {       int C = s - c[4] * k4;       bezout t = extgcd(c[3], c[5]);       if (C % t.d != 0) continue;       long long k3 = t.a * C / t.d;       long long k5 = t.b * C / t.d;       int lcm = c[3] * c[5] / t.d;       int inc3 = lcm / c[3], inc5 = lcm / c[5];       if (k3 >= 0) {         int mod = k3 / inc3;         k3 = k3 % inc3;         k5 = k5 + mod * inc5;       }       if (k3 < 0) {         int mod = -k3 / inc3;         if ((-k3) % inc3 != 0) mod++;         k3 = k3 + inc3 * mod;         k5 = k5 - inc5 * mod;       }       check(k3, k4, k5);       int st = 0, ed = s;       while (st < ed) {         int m = (st + ed + 1) / 2;         int nk5 = k5 - m * inc5;         if (k3 + m * inc3 <= k4 && nk5 >= k4 && c[5] * nk5 >= c[4] * k4)           st = m;         else           ed = m - 1;       }       int maxk3 = k3 + inc3 * st;       int maxk5 = k5 - inc5 * st;       check(maxk3, k4, maxk5);       check(maxk3 + inc3, k4, maxk5 - inc5);     }     if (best == -1)       printf( %d n , best);     else       printf( %d %d %d n , b3, b4, b5);   }
#include <bits/stdc++.h>   using namespace std;   int res;   int a[100], ins[100];   int IsLucky(int x) {     for (; x; x /= 10)       if (x % 10 != 4 && x % 10 != 7) return 0;     return 1;   }   void Dfs(int now, int lim) {     res++;     if ((long long)now * 10 + 4 <= lim) Dfs(now * 10 + 4, lim);     if ((long long)now * 10 + 7 <= lim) Dfs(now * 10 + 7, lim);   }   int main() {     int n, m, k;     scanf( %d%d , &n, &m);     long long cnt = 1;     for (int i = 1;; i++) {       cnt *= i;       if (cnt >= m) {         k = i;         break;       }     }     if (k > n) {       puts( -1 );       return 0;     }     memset(ins, 0, sizeof(ins));     for (int i = 0; i < k; i++) {       cnt = 1;       for (int j = 1; j < k - i; j++) cnt *= j;       int ret = (m - 1) / cnt;       m -= ret * cnt;       for (int j = 0; j < k; j++)         if (!ins[j]) {           if (ret == 0) {             ins[j] = 1;             a[i] = j;             break;           } else             ret--;         }     }     res = 0;     for (int i = 0; i < k; i++)       if (IsLucky(n - k + 1 + i) && IsLucky(n - k + 1 + a[i])) res++;     if (4 <= n - k) Dfs(4, n - k);     if (7 <= n - k) Dfs(7, n - k);     printf( %d n , res);     return 0;   }