Editorial for Bất lặp


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này

Code mẫu của ladpro98

#include <bits/stdc++.h>
const long long oo = 9999999999ll;
using namespace std;
long long a, n; long long res;
bool was[10];

void Back(int i, long long now) {
    if (i > n) {
        if (now > a) res = min(res, now);
        return;
    }
    for(int j=1; j<=9; j++)
    if (!was[j]) {
        was[j] = true;
        Back(i+1, 10 * now + j);
        was[j] = false;
    }
}

int main()
{
    while (scanf("%lld\n", &a) == 1) {
        ostringstream st; st << a; n = st.str().length();
        res = oo; Back(1, 0);
        if (res == oo) {
            if (n < 9)
                for(int i=1; i<=n+1; i++) printf("%d", i);
            else printf("0");
        }
        else
            printf("%lld", res);
        printf("\n");
    }
    return 0;
}

Code mẫu của RR

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <complex>
#include <iostream>
#include <algorithm>

#include <ctime>
#include <deque>
#include <bitset>
#include <cctype>
#include <utility>
#include <cassert>

#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)

#define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
#define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }
using namespace std;

int n;
bool has[11];

bool check(int n) {
    memset(has, 0, sizeof has);
    has[0] = true;
    while (n) {
        int t = n % 10;
        if (has[t]) return false;
        has[t] = true;
        n /= 10;
    }
    return true;
}

void solve() {
    if (n < 9) {
        cout << n+1 << endl;
        return ;
    }
    FOR(res,n+1,1000000000) {
        if (check(res)) {
            cout << res << endl;
            return ;
        }
    }
    cout << 0 << endl;
}

int main() {
    ios :: sync_with_stdio(false);
    while (cin >> n) {
        solve();
    }
}

Code mẫu của skyvn97

#include<bits/stdc++.h>
#define FOR(i,a,b) for (int i=(a),_b=(b);i<=_b;i=i+1)
using namespace std;
set<int> num;
bool ex[20];
void backtrack(int val) {
    FOR(i,1,9) if (!ex[i]) {
        ex[i]=true;
        num.insert(val*10+i);
        backtrack(val*10+i);
        ex[i]=false;
    }
}
int answer(int x) {
    __typeof(num.begin()) it=num.upper_bound(x);
    if (it==num.end()) return (0); else return (*it);
}
int main(void) {
    backtrack(0);
    int x;
    while (scanf("%d",&x)==1) printf("%d\n",answer(x));
}

Comments

Please read the guidelines before commenting.


There are no comments at the moment.