Untitled

 avatar
user_9000366
plain_text
2 months ago
1.0 kB
3
Indexable
#include <bits/stdc++.h>
using namespace std;

#define ll          long long

void Init() {
    ios_base::sync_with_stdio(false),
            cin.tie(nullptr),
            cout.tie(nullptr);
}
/*
1869 0
1968 1
1689 2
6891 3
8691 4
8916 5
8196 6
*/
void solve() {
    string s;
    cin >> s;
    int mod7 = 0;
    vector<int> freq(10, 0);
    for (int i = 0; i < s.size(); ++i) {
        freq[s[i] - '0']++;
    }
    freq[1]--, freq[6]--, freq[8]--, freq[9]--;
    for (int i = 1; i < 10; ++i) {
        while (freq[i]--) {
            cout << i;
            mod7 = (mod7 * 10 + i) % 7;
        }
    }
    string t = "1689";
    do {
        int mod = mod7;
        for (char i: t) {
            mod = (mod * 10 + (i - '0')) % 7;
        }
        if (mod == 0) {
            cout << t;
            break;
        }
    } while (next_permutation(t.begin(), t.end()));
    while (freq[0]--)cout << '0';
}

int main() {
    Init();
    solve();
    return 0;
}
Editor is loading...
Leave a Comment