Problem:
C: カニサル暗号
問題
えびちゃんは、ある非負整数
D
を「カニサル暗号」で暗号化して得られた文字列
C
を与えられました。この暗号は、非負整数を
10
進法で表したときの各数字をある（元と異なるとは限らない）決められた数字で置き換えるものです。異なる数字が同じ数字に置き換えられたり、同じ数字が出現位置によって異なる数字に書き換えられることはありません。
たとえば、この暗号化方式によって
2646
が
0545
になることはありますが、
3456
が
1333
になったり
1333
が
3456
になることはありません。
いま、えびちゃんは
D
を
10^9+7
で割った余りが
M
になることを教えてもらいました。このとき、
D
として考えられるものを一つ出力してください。複数考えられる場合はどれを出力しても構いません。ただし、
D
の先頭には余分な
0
はついていないものとします。
入力形式
M
C
制約
0\leq M < 10^9+7
1\leq |C| \leq 10^5
出力形式
D
として考えられる非負整数を一行に出力してください。存在しない場合は
-1
を出力してください。
入力例1
2
1000000007
出力例1
1000000009
今回の暗号化方式は、
0
を
0
に、
1
を
1
に、
9
を
7
に置き換えるものでした。
入力例2
3
1000000007
出力例2
-1
入力例3
1
01
出力例3
-1
D
の先頭に余分な
0
がついていることはありません。
入力例4
45
1000000023
出力例4
6000000087
1000000052
や
2000000059
なども条件を満たすので、それを出力してもかまいません。
入力例5
0
940578326285963740
出力例5
123456789864197523

Submitted solutions:

code_snippet: #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <chrono>
#include <climits>
#include <cmath>
#include <deque>
#include <filesystem>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <mutex>
#include <numeric>
#include <optional>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <variant>
#include <vector>

int main() {
	constexpr int MOD{ 1'000'000'007 };
	int m; std::cin >> m;
	std::string digits; std::cin >> digits;

	std::vector<long long int> multiplier(10, 0);
	long long int digit{ 1 };
	auto reversed{ digits }; std::reverse(reversed.begin(), reversed.end());
	for (const auto c : reversed) {
		multiplier[c - '0'] = (multiplier[c - '0'] + digit) % MOD;
		digit = digit * 10 % MOD;
	}
	std::vector<int> permutation(10); std::iota(permutation.begin(), permutation.end(), 0);
	bool found{ false };
	do {
		if (digits.size() > 1 && permutation[digits.front() - '0'] == 0) continue;
		long long int sum{ 0 };
		for (auto i = 0; i < 10; ++i) {
			sum = (sum + permutation[i] * multiplier[i]) % MOD;
		}
		if (m == sum) {
			found = true;
		}
	} while (!found && std::next_permutation(permutation.begin(), permutation.end()));
	if (found) {
		std::string result;
		std::transform(digits.begin(), digits.end(), std::back_inserter(result), [&](const auto i) {return permutation[i - '0'] + '0'; });
		std::cout << result << '\n';
	}
	else {
		std::cout << "-1\n";
	}
}
accuracy: 1
time_ms: 60
memory_kb: 3640
final_rank: 3

code_snippet: #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <chrono>
#include <climits>
#include <cmath>
#include <deque>
#include <filesystem>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <mutex>
#include <numeric>
#include <optional>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <variant>
#include <vector>

int main() {
	constexpr int MOD{ 1'000'000'007 };
	int m; std::cin >> m;
	std::string digits; std::cin >> digits;

	std::vector<long long int> multiplier(10, 0);
	long long int digit{ 1 };
	auto reversed{ digits }; std::reverse(reversed.begin(), reversed.end());
	for (const auto c : reversed) {
		multiplier[c - '0'] = (multiplier[c - '0'] + digit) % MOD;
		digit = digit * 10 % MOD;
	}
	std::vector<int> permutation(10); std::iota(permutation.begin(), permutation.end(), 0);
	bool found{ false };
	do {
		if (permutation[digits.front() - '0'] == 0) continue;
		long long int sum{ 0 };
		for (auto i = 0; i < 10; ++i) {
			sum = (sum + permutation[i] * multiplier[i]) % MOD;
		}
		if (m == sum) {
			found = true;
		}
	} while (!found && std::next_permutation(permutation.begin(), permutation.end()));
	if (found) {
		std::string result;
		std::transform(digits.begin(), digits.end(), std::back_inserter(result), [&](const auto i) {return permutation[i - '0'] + '0'; });
		std::cout << result << '\n';
	}
	else {
		std::cout << "-1\n";
	}
}
accuracy: 0.8870967741935484
time_ms: 60
memory_kb: 3644
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
using P = pair<ll,ll>;
#define rep(i,n) for (int i = 0; i < (n); ++i)
template<typename T,typename U>inline bool chmax(T&a,U b){return a<b?a=b,1:0;}
template<typename T,typename U>inline bool chmin(T&a,U b){return a>b?a=b,1:0;}

#include <cassert>
#include <numeric>
#include <type_traits>

#ifdef _MSC_VER
#include <intrin.h>
#endif


#include <utility>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

constexpr long long safe_mod(long long x, long long m) {
    x %= m;
    if (x < 0) x += m;
    return x;
}

struct barrett {
    unsigned int _m;
    unsigned long long im;

    explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}

    unsigned int umod() const { return _m; }

    unsigned int mul(unsigned int a, unsigned int b) const {

        unsigned long long z = a;
        z *= b;
#ifdef _MSC_VER
        unsigned long long x;
        _umul128(z, im, &x);
#else
        unsigned long long x =
            (unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
        unsigned int v = (unsigned int)(z - x * _m);
        if (_m <= v) v += _m;
        return v;
    }
};

constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
    if (m == 1) return 0;
    unsigned int _m = (unsigned int)(m);
    unsigned long long r = 1;
    unsigned long long y = safe_mod(x, m);
    while (n) {
        if (n & 1) r = (r * y) % _m;
        y = (y * y) % _m;
        n >>= 1;
    }
    return r;
}

constexpr bool is_prime_constexpr(int n) {
    if (n <= 1) return false;
    if (n == 2 || n == 7 || n == 61) return true;
    if (n % 2 == 0) return false;
    long long d = n - 1;
    while (d % 2 == 0) d /= 2;
    constexpr long long bases[3] = {2, 7, 61};
    for (long long a : bases) {
        long long t = d;
        long long y = pow_mod_constexpr(a, t, n);
        while (t != n - 1 && y != 1 && y != n - 1) {
            y = y * y % n;
            t <<= 1;
        }
        if (y != n - 1 && t % 2 == 0) {
            return false;
        }
    }
    return true;
}
template <int n> constexpr bool is_prime = is_prime_constexpr(n);

constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
    a = safe_mod(a, b);
    if (a == 0) return {b, 0};

    long long s = b, t = a;
    long long m0 = 0, m1 = 1;

    while (t) {
        long long u = s / t;
        s -= t * u;
        m0 -= m1 * u;  // |m1 * u| <= |m1| * s <= b


        auto tmp = s;
        s = t;
        t = tmp;
        tmp = m0;
        m0 = m1;
        m1 = tmp;
    }
    if (m0 < 0) m0 += b / s;
    return {s, m0};
}

constexpr int primitive_root_constexpr(int m) {
    if (m == 2) return 1;
    if (m == 167772161) return 3;
    if (m == 469762049) return 3;
    if (m == 754974721) return 11;
    if (m == 998244353) return 3;
    int divs[20] = {};
    divs[0] = 2;
    int cnt = 1;
    int x = (m - 1) / 2;
    while (x % 2 == 0) x /= 2;
    for (int i = 3; (long long)(i)*i <= x; i += 2) {
        if (x % i == 0) {
            divs[cnt++] = i;
            while (x % i == 0) {
                x /= i;
            }
        }
    }
    if (x > 1) {
        divs[cnt++] = x;
    }
    for (int g = 2;; g++) {
        bool ok = true;
        for (int i = 0; i < cnt; i++) {
            if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
                ok = false;
                break;
            }
        }
        if (ok) return g;
    }
}
template <int m> constexpr int primitive_root = primitive_root_constexpr(m);

unsigned long long floor_sum_unsigned(unsigned long long n,
                                      unsigned long long m,
                                      unsigned long long a,
                                      unsigned long long b) {
    unsigned long long ans = 0;
    while (true) {
        if (a >= m) {
            ans += n * (n - 1) / 2 * (a / m);
            a %= m;
        }
        if (b >= m) {
            ans += n * (b / m);
            b %= m;
        }

        unsigned long long y_max = a * n + b;
        if (y_max < m) break;
        n = (unsigned long long)(y_max / m);
        b = (unsigned long long)(y_max % m);
        std::swap(m, a);
    }
    return ans;
}

}  // namespace internal

}  // namespace atcoder


#include <cassert>
#include <numeric>
#include <type_traits>

namespace atcoder {

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value ||
                                  std::is_same<T, __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int128 =
    typename std::conditional<std::is_same<T, __uint128_t>::value ||
                                  std::is_same<T, unsigned __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using make_unsigned_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value,
                              __uint128_t,
                              unsigned __int128>;

template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
                                                  is_signed_int128<T>::value ||
                                                  is_unsigned_int128<T>::value,
                                              std::true_type,
                                              std::false_type>::type;

template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
                                                 std::is_signed<T>::value) ||
                                                    is_signed_int128<T>::value,
                                                std::true_type,
                                                std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<(is_integral<T>::value &&
                               std::is_unsigned<T>::value) ||
                                  is_unsigned_int128<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
    is_signed_int128<T>::value,
    make_unsigned_int128<T>,
    typename std::conditional<std::is_signed<T>::value,
                              std::make_unsigned<T>,
                              std::common_type<T>>::type>::type;

#else

template <class T> using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
    typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<is_integral<T>::value &&
                                  std::is_unsigned<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
                                              std::make_unsigned<T>,
                                              std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

}  // namespace internal

}  // namespace atcoder


namespace atcoder {

namespace internal {

struct modint_base {};
struct static_modint_base : modint_base {};

template <class T> using is_modint = std::is_base_of<modint_base, T>;
template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;

}  // namespace internal

template <int m, std::enable_if_t<(1 <= m)>* = nullptr>
struct static_modint : internal::static_modint_base {
    using mint = static_modint;

  public:
    static constexpr int mod() { return m; }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    static_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    static_modint(T v) {
        long long x = (long long)(v % (long long)(umod()));
        if (x < 0) x += umod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    static_modint(T v) {
        _v = (unsigned int)(v % umod());
    }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v -= rhs._v;
        if (_v >= umod()) _v += umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        unsigned long long z = _v;
        z *= rhs._v;
        _v = (unsigned int)(z % umod());
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        if (prime) {
            assert(_v);
            return pow(umod() - 2);
        } else {
            auto eg = internal::inv_gcd(_v, m);
            assert(eg.first == 1);
            return eg.second;
        }
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static constexpr unsigned int umod() { return m; }
    static constexpr bool prime = internal::is_prime<m>;
};

template <int id> struct dynamic_modint : internal::modint_base {
    using mint = dynamic_modint;

  public:
    static int mod() { return (int)(bt.umod()); }
    static void set_mod(int m) {
        assert(1 <= m);
        bt = internal::barrett(m);
    }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    dynamic_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        long long x = (long long)(v % (long long)(mod()));
        if (x < 0) x += mod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        _v = (unsigned int)(v % mod());
    }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v += mod() - rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        _v = bt.mul(_v, rhs._v);
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        auto eg = internal::inv_gcd(_v, mod());
        assert(eg.first == 1);
        return eg.second;
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static internal::barrett bt;
    static unsigned int umod() { return bt.umod(); }
};
template <int id> internal::barrett dynamic_modint<id>::bt(998244353);

using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;
using modint = dynamic_modint<-1>;

namespace internal {

template <class T>
using is_static_modint = std::is_base_of<internal::static_modint_base, T>;

template <class T>
using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;

template <class> struct is_dynamic_modint : public std::false_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};

template <class T>
using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;

}  // namespace internal

}  // namespace atcoder

using namespace atcoder;
using mint = modint1000000007;

int main(){
    int m;cin>>m;
    string c;cin>>c;
    vector<int> p(10);
    iota(p.begin(),p.end(),0);
    vector<mint> a(10);
    rep(i,10)for(char&j:c){
        a[i]*=10;
        if(i+'0'==j)a[i]+=1;
    }
    do{
        mint now=0;
        rep(i,10)now+=a[i]*p[i];
        if(now==m && (p[c[0]-'0'] || c.size()==1)){
            for(char i:c)cout<<p[i-'0'];
            cout<<endl;
            return 0;
        }
    } while (next_permutation(p.begin(),p.end()));
    cout<<-1<<endl;
}
accuracy: 1
time_ms: 80
memory_kb: 3552
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
using P = pair<ll,ll>;
#define rep(i,n) for (int i = 0; i < (n); ++i)
template<typename T,typename U>inline bool chmax(T&a,U b){return a<b?a=b,1:0;}
template<typename T,typename U>inline bool chmin(T&a,U b){return a>b?a=b,1:0;}

#include <cassert>
#include <numeric>
#include <type_traits>

#ifdef _MSC_VER
#include <intrin.h>
#endif


#include <utility>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

constexpr long long safe_mod(long long x, long long m) {
    x %= m;
    if (x < 0) x += m;
    return x;
}

struct barrett {
    unsigned int _m;
    unsigned long long im;

    explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}

    unsigned int umod() const { return _m; }

    unsigned int mul(unsigned int a, unsigned int b) const {

        unsigned long long z = a;
        z *= b;
#ifdef _MSC_VER
        unsigned long long x;
        _umul128(z, im, &x);
#else
        unsigned long long x =
            (unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
        unsigned int v = (unsigned int)(z - x * _m);
        if (_m <= v) v += _m;
        return v;
    }
};

constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
    if (m == 1) return 0;
    unsigned int _m = (unsigned int)(m);
    unsigned long long r = 1;
    unsigned long long y = safe_mod(x, m);
    while (n) {
        if (n & 1) r = (r * y) % _m;
        y = (y * y) % _m;
        n >>= 1;
    }
    return r;
}

constexpr bool is_prime_constexpr(int n) {
    if (n <= 1) return false;
    if (n == 2 || n == 7 || n == 61) return true;
    if (n % 2 == 0) return false;
    long long d = n - 1;
    while (d % 2 == 0) d /= 2;
    constexpr long long bases[3] = {2, 7, 61};
    for (long long a : bases) {
        long long t = d;
        long long y = pow_mod_constexpr(a, t, n);
        while (t != n - 1 && y != 1 && y != n - 1) {
            y = y * y % n;
            t <<= 1;
        }
        if (y != n - 1 && t % 2 == 0) {
            return false;
        }
    }
    return true;
}
template <int n> constexpr bool is_prime = is_prime_constexpr(n);

constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
    a = safe_mod(a, b);
    if (a == 0) return {b, 0};

    long long s = b, t = a;
    long long m0 = 0, m1 = 1;

    while (t) {
        long long u = s / t;
        s -= t * u;
        m0 -= m1 * u;  // |m1 * u| <= |m1| * s <= b


        auto tmp = s;
        s = t;
        t = tmp;
        tmp = m0;
        m0 = m1;
        m1 = tmp;
    }
    if (m0 < 0) m0 += b / s;
    return {s, m0};
}

constexpr int primitive_root_constexpr(int m) {
    if (m == 2) return 1;
    if (m == 167772161) return 3;
    if (m == 469762049) return 3;
    if (m == 754974721) return 11;
    if (m == 998244353) return 3;
    int divs[20] = {};
    divs[0] = 2;
    int cnt = 1;
    int x = (m - 1) / 2;
    while (x % 2 == 0) x /= 2;
    for (int i = 3; (long long)(i)*i <= x; i += 2) {
        if (x % i == 0) {
            divs[cnt++] = i;
            while (x % i == 0) {
                x /= i;
            }
        }
    }
    if (x > 1) {
        divs[cnt++] = x;
    }
    for (int g = 2;; g++) {
        bool ok = true;
        for (int i = 0; i < cnt; i++) {
            if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
                ok = false;
                break;
            }
        }
        if (ok) return g;
    }
}
template <int m> constexpr int primitive_root = primitive_root_constexpr(m);

unsigned long long floor_sum_unsigned(unsigned long long n,
                                      unsigned long long m,
                                      unsigned long long a,
                                      unsigned long long b) {
    unsigned long long ans = 0;
    while (true) {
        if (a >= m) {
            ans += n * (n - 1) / 2 * (a / m);
            a %= m;
        }
        if (b >= m) {
            ans += n * (b / m);
            b %= m;
        }

        unsigned long long y_max = a * n + b;
        if (y_max < m) break;
        n = (unsigned long long)(y_max / m);
        b = (unsigned long long)(y_max % m);
        std::swap(m, a);
    }
    return ans;
}

}  // namespace internal

}  // namespace atcoder


#include <cassert>
#include <numeric>
#include <type_traits>

namespace atcoder {

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value ||
                                  std::is_same<T, __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int128 =
    typename std::conditional<std::is_same<T, __uint128_t>::value ||
                                  std::is_same<T, unsigned __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using make_unsigned_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value,
                              __uint128_t,
                              unsigned __int128>;

template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
                                                  is_signed_int128<T>::value ||
                                                  is_unsigned_int128<T>::value,
                                              std::true_type,
                                              std::false_type>::type;

template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
                                                 std::is_signed<T>::value) ||
                                                    is_signed_int128<T>::value,
                                                std::true_type,
                                                std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<(is_integral<T>::value &&
                               std::is_unsigned<T>::value) ||
                                  is_unsigned_int128<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
    is_signed_int128<T>::value,
    make_unsigned_int128<T>,
    typename std::conditional<std::is_signed<T>::value,
                              std::make_unsigned<T>,
                              std::common_type<T>>::type>::type;

#else

template <class T> using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
    typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<is_integral<T>::value &&
                                  std::is_unsigned<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
                                              std::make_unsigned<T>,
                                              std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

}  // namespace internal

}  // namespace atcoder


namespace atcoder {

namespace internal {

struct modint_base {};
struct static_modint_base : modint_base {};

template <class T> using is_modint = std::is_base_of<modint_base, T>;
template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;

}  // namespace internal

template <int m, std::enable_if_t<(1 <= m)>* = nullptr>
struct static_modint : internal::static_modint_base {
    using mint = static_modint;

  public:
    static constexpr int mod() { return m; }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    static_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    static_modint(T v) {
        long long x = (long long)(v % (long long)(umod()));
        if (x < 0) x += umod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    static_modint(T v) {
        _v = (unsigned int)(v % umod());
    }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v -= rhs._v;
        if (_v >= umod()) _v += umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        unsigned long long z = _v;
        z *= rhs._v;
        _v = (unsigned int)(z % umod());
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        if (prime) {
            assert(_v);
            return pow(umod() - 2);
        } else {
            auto eg = internal::inv_gcd(_v, m);
            assert(eg.first == 1);
            return eg.second;
        }
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static constexpr unsigned int umod() { return m; }
    static constexpr bool prime = internal::is_prime<m>;
};

template <int id> struct dynamic_modint : internal::modint_base {
    using mint = dynamic_modint;

  public:
    static int mod() { return (int)(bt.umod()); }
    static void set_mod(int m) {
        assert(1 <= m);
        bt = internal::barrett(m);
    }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    dynamic_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        long long x = (long long)(v % (long long)(mod()));
        if (x < 0) x += mod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        _v = (unsigned int)(v % mod());
    }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v += mod() - rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        _v = bt.mul(_v, rhs._v);
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        auto eg = internal::inv_gcd(_v, mod());
        assert(eg.first == 1);
        return eg.second;
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static internal::barrett bt;
    static unsigned int umod() { return bt.umod(); }
};
template <int id> internal::barrett dynamic_modint<id>::bt(998244353);

using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;
using modint = dynamic_modint<-1>;

namespace internal {

template <class T>
using is_static_modint = std::is_base_of<internal::static_modint_base, T>;

template <class T>
using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;

template <class> struct is_dynamic_modint : public std::false_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};

template <class T>
using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;

}  // namespace internal

}  // namespace atcoder

using namespace atcoder;
using mint = modint1000000007;

int main(){
    int m;cin>>m;
    string c;cin>>c;
    vector<int> p(10);
    iota(p.begin(),p.end(),0);
    vector<mint> a(10);
    rep(i,10)for(char&j:c){
        a[i]*=10;
        if(i+'0'==j)a[i]+=1;
    }
    do{
        mint now=0;
        rep(i,10)now+=a[i]*p[i];
        if(now==m && p[c[0]-'0']){
            for(char i:c)cout<<p[i-'0'];
            cout<<endl;
            return 0;
        }
    } while (next_permutation(p.begin(),p.end()));
    cout<<-1<<endl;
}
accuracy: 0.8870967741935484
time_ms: 80
memory_kb: 3556
final_rank: 14

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

#define int long long

using ll = long long;
using P = pair<int,int>;
const int mod=1000000007;
const int inf = (1<<30);

const int dy[4] = {-1,0,0,1};
const int dx[4] = {0,-1,-1,0};

signed main() {
    int m;cin>>m;
    string s;cin>>s;
    vector<int> has[10];
    for(int i = 0; (int)s.size() > i; i++){
        has[s[i]-'0'].push_back(i);    
    }
    vector<vector<long long>> tab(s.size(), vector<long long>(10));
    for(int i = 0; (int)s.size() > i; i++){
        for(int j = 0; 9 >= j; j++){
           if(!i){
              tab[i][j] = j;    
           }else{
              tab[i][j] = (tab[i-1][j]*10)%mod;
           }
        }    
    }
    vector<vector<int>> A(10, vector<int>(10));
    for(int i = 0; 9 >= i; i++){// from
        for(int j = 0; 9 >= j; j++){ // to
             for(int k = 0; (int)has[i].size() > k; k++){
                A[i][j] = (A[i][j] + tab[s.size()-has[i][k]-1][j])%mod;
             }
         }    
    }
    vector<int> B;
    for(int i = 0; 10 > i; i++){
       B.push_back(i);
    }
    do{
       long long nw = 0;
       for(int i = 0; 10 > i; i++){
           nw = (nw + A[i][B[i]])%mod;
       }
       if(nw == m){
            if(B[s[0]-'0'] == 0 && s.size() != 1)continue;
            for(int i = 0; s.size() > i; i++){
                cout << B[s[i]-'0']; 
            }
            cout << endl;
            return 0;
       }
    }while(next_permutation(B.begin(), B.end()));
    
    cout << -1 << endl;


}
accuracy: 1
time_ms: 80
memory_kb: 15760
final_rank: 8

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

#define int long long

using ll = long long;
using P = pair<int,int>;
const int mod=1000000007;
const int inf = (1<<30);

const int dy[4] = {-1,0,0,1};
const int dx[4] = {0,-1,-1,0};

signed main() {
    int m;cin>>m;
    string s;cin>>s;
    if(s == "0" && m == 0){
        cout << 0 << endl;
        return 0;
    }
    vector<int> has[10];
    for(int i = 0; (int)s.size() > i; i++){
        has[s[i]-'0'].push_back(i);    
    }
    vector<vector<long long>> tab(s.size(), vector<long long>(10));
    for(int i = 0; (int)s.size() > i; i++){
        for(int j = 0; 9 >= j; j++){
           if(!i){
              tab[i][j] = j;    
           }else{
              tab[i][j] = (tab[i-1][j]*10)%mod;
           }
        }    
    }
    vector<vector<int>> A(10, vector<int>(10));
    for(int i = 0; 9 >= i; i++){// from
        for(int j = 0; 9 >= j; j++){ // to
             for(int k = 0; (int)has[i].size() > k; k++){
                A[i][j] = (A[i][j] + tab[s.size()-has[i][k]-1][j])%mod;
             }
         }    
    }
    vector<int> B;
    for(int i = 0; 10 > i; i++){
       B.push_back(i);
    }
    do{
       long long nw = 0;
       for(int i = 0; 10 > i; i++){
           nw = (nw + A[i][B[i]])%mod;
       }
       if(nw == m){
            if(B[s[0]-'0'] == 0)continue;
            for(int i = 0; s.size() > i; i++){
                cout << B[s[i]-'0']; 
            }
            cout << endl;
            return 0;
       }
    }while(next_permutation(B.begin(), B.end()));
    
    cout << -1 << endl;


}
accuracy: 0.9032258064516129
time_ms: 70
memory_kb: 15612
final_rank: 9

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

#define int long long

using ll = long long;
using P = pair<int,int>;
const int mod=1000000007;
const int inf = (1<<30);

const int dy[4] = {-1,0,0,1};
const int dx[4] = {0,-1,-1,0};

signed main() {
    int m;cin>>m;
    string s;cin>>s;
    vector<int> has[10];
    for(int i = 0; (int)s.size() > i; i++){
        has[s[i]-'0'].push_back(i);    
    }
    vector<vector<long long>> tab(s.size(), vector<long long>(10));
    for(int i = 0; (int)s.size() > i; i++){
        for(int j = 0; 9 >= j; j++){
           if(!i){
              tab[i][j] = j;    
           }else{
              tab[i][j] = (tab[i-1][j]*10)%mod;
           }
        }    
    }
    vector<vector<int>> A(10, vector<int>(10));
    for(int i = 0; 9 >= i; i++){// from
        for(int j = 0; 9 >= j; j++){ // to
             for(int k = 0; (int)has[i].size() > k; k++){
                A[i][j] = (A[i][j] + tab[s.size()-has[i][k]-1][j])%mod;
             }
         }    
    }
    vector<int> B;
    for(int i = 0; 10 > i; i++){
       B.push_back(i);
    }
    do{
       long long nw = 0;
       for(int i = 0; 10 > i; i++){
           nw = (nw + A[i][B[i]])%mod;
       }
       if(nw == m){
            if(B[s[0]-'0'] == 0)continue;
            for(int i = 0; s.size() > i; i++){
                cout << B[s[i]-'0']; 
            }
            cout << endl;
            return 0;
       }
    }while(next_permutation(B.begin(), B.end()));
    
    cout << -1 << endl;


}
accuracy: 0.8870967741935484
time_ms: 70
memory_kb: 15796
final_rank: 16

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;
using ll = long long;
using P = pair<int,int>;
const int mod=1000000007;
const int inf = (1<<30);

const int dy[4] = {-1,0,0,1};
const int dx[4] = {0,-1,-1,0};

int main() {
    int m;cin>>m;
    string s;cin>>s;
    vector<int> has[10];
    for(int i = 0; (int)s.size() > i; i++){
        has[s[i]-'0'].push_back(i);    
    }
    vector<vector<long long>> tab(s.size(), vector<long long>(10));
    for(int i = 0; (int)s.size() > i; i++){
        for(int j = 0; 9 >= j; j++){
           if(!i){
              tab[i][j] = j;    
           }else{
              tab[i][j] = (tab[i-1][j]*10)%mod;
           }
        }    
    }
    vector<vector<int>> A(10, vector<int>(10));
    for(int i = 0; 9 >= i; i++){// from
        for(int j = 0; 9 >= j; j++){ // to
             for(int k = 0; (int)has[i].size() > k; k++){
                A[i][j] = (A[i][j] + tab[s.size()-has[i][k]-1][j])%mod;
             }
         }    
    }
    vector<int> B;
    for(int i = 0; 10 > i; i++){
       B.push_back(i);
    }
    do{
       long long nw = 0;
       for(int i = 0; 10 > i; i++){
           nw = (nw + A[i][B[i]])%mod;
       }
       if(nw == m){
            if(B[s[0]-'0'] == 0)continue;
            for(int i = 0; s.size() > i; i++){
                cout << B[s[i]-'0']; 
            }
            cout << endl;
            return 0;
       }
    }while(next_permutation(B.begin(), B.end()));
    
    cout << -1 << endl;


}
accuracy: 0.8870967741935484
time_ms: 70
memory_kb: 15380
final_rank: 15

code_snippet: #include<bits/stdc++.h>

using namespace std;

string f(string s){
  reverse(s.begin(),s.end());
  while(s.size() >= 2 and s.back()=='0')s.erase(s.end()-1);
  reverse(s.begin(),s.end());
  return s;
}

using ll = long long;

const ll MOD = 1e9+7;

string func(){
  ll m;
  cin >> m;
  string c;
  cin >> c;
  vector<ll> ks(10,0);
  for(int i=0;i<c.size();++i){
    for(auto &i:ks)i = i * 10 % MOD;
    int v = c[i] - '0';
    ks[v] = (ks[v] + 1) % MOD;
  }
  int dontzero = -1;
  if(c.size() >= 2)dontzero = c[0] - '0';

  vector<int> xs(10);
  for(int i=0;i<10;++i)xs[i] = i;
  do{
    if(dontzero >= 0 and xs[dontzero] == 0)continue;
    ll sum = 0;
    for(int i=0;i<10;++i){
      sum = sum + (ks[i] * xs[i]);
      sum %= MOD;
    }
    if(sum == m){
      for(auto &i:c){
        char c = xs[i-'0'] + '0';
        i = c;
      }
      return c;
    }
  }while(next_permutation(xs.begin(),xs.end()));
  return "-1";
}

int main(){
  cout << func() << endl;
}
accuracy: 1
time_ms: 60
memory_kb: 3572
final_rank: 2

code_snippet: #include<bits/stdc++.h>

using namespace std;

string f(string s){
  reverse(s.begin(),s.end());
  while(s.size() >= 2 and s.back()=='0')s.erase(s.end()-1);
  reverse(s.begin(),s.end());
  return s;
}

using ll = long long;

const ll MOD = 1e9+7;

string func(){
  ll m;
  cin >> m;
  string c;
  cin >> c;
  vector<ll> ks(10,0);
  for(int i=0;i<c.size();++i){
    for(auto &i:ks)i = i * 10 % MOD;
    int v = c[i] - '0';
    ks[v] = (ks[v] + 1) % MOD;
  }
  if(c.size() >= 2 and c[0]=='0')return "-1";
  int dontzero = -1;
  if(c.size() >= 2)dontzero = c[0] - '0';

  vector<int> xs(10);
  for(int i=0;i<10;++i)xs[i] = i;
  do{
    if(dontzero >= 0 and xs[dontzero] == 0)continue;
    ll sum = 0;
    for(int i=0;i<10;++i){
      sum = sum + (ks[i] * xs[i]);
      sum %= MOD;
    }
    if(sum == m){
      for(auto &i:c){
        char c = xs[i-'0'] + '0';
        i = c;
      }
      return c;
    }
  }while(next_permutation(xs.begin(),xs.end()));
  return "-1";
}

int main(){
  cout << func() << endl;
}
accuracy: 0.43548387096774194
time_ms: 60
memory_kb: 3468
final_rank: 17

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;

const ll mod = 1000000007;

int main(){    
    ll M; cin >> M;
    string C; cin >> C;

    if (M == 0 and (int)C.size() == 1) {
        cout << 0 << endl;
        return 0;
    }

    array<ll, 10> buc; 
    for (int i = 0 ; i < 10 ; i++) buc[i] = 0;
    ll p10 = 1;
    for (int i = (int)C.size() - 1 ; i >= 0 ; i--) {
        int now = C[i] - '0';
        buc.at(now) = (buc[now] + p10) % mod;
        p10 = (p10 * 10) % mod;
    }
    
    vector<int> idx(10, 0);
    iota(idx.begin(), idx.end(), 0);

    do {

        if (idx[C[0] - '0'] == 0) continue;

        ll val = 0LL; 
        for (int i = 0 ; i < 10 ; i++) {
            assert(0 <= idx[i] and idx[i] <= 9);
            val = (val + ((ll)idx.at(i) * buc[i])) % mod;
        }
        if (val == M) {
            string ans = C;
            for (auto& c : ans) c = idx.at(c - '0') + '0';
            assert(ans[0] != '0');
            for (auto& c : ans) assert('0' <= c and c <= '9');
            cout << ans << endl;
            return 0;
        }


    } while (next_permutation(idx.begin(), idx.end())) ;

    cout << -1 << endl;
}
accuracy: 1
time_ms: 60
memory_kb: 3656
final_rank: 4

code_snippet: #include<bits/stdc++.h>

using namespace std;

string f(string s){
  reverse(s.begin(),s.end());
  while(s.size() >= 2 and s.back()=='0')s.erase(s.end()-1);
  reverse(s.begin(),s.end());
  return s;
}

using ll = long long;

const ll MOD = 1e9+7;

string func(){
  ll m;
  cin >> m;
  string c;
  cin >> c;
  vector<ll> ks(10,0);
  for(int i=0;i<c.size();++i){
    for(auto &i:ks)i = i * 10 % MOD;
    int v = c[i] - '0';
    ks[v] = (ks[v] + 1) % MOD;
  }
  if(c.size() >= 2 and c[0]=='0')return "-1";

  vector<int> xs(10);
  iota(xs.begin(),xs.end(),0);
  do{
    ll sum = 0;
    for(int i=0;i<10;++i){
      sum = sum + (ks[i] * xs[i]);
      sum %= MOD;
    }
    if(sum == m){
      string d = c;
      for(auto &i:d){
        char d = xs[i-'0'] + '0';
        i = d;
      }
      d = f(d);
      if(d.size() == c.size()){
        return d;
      }
    }
  }while(next_permutation(xs.begin(),xs.end()));
  return "-1";
}

int main(){
  cout << func() << endl;
}
accuracy: 0.43548387096774194
time_ms: 60
memory_kb: 3552
final_rank: 19

code_snippet: #include<bits/stdc++.h>

using namespace std;

string f(string s){
  reverse(s.begin(),s.end());
  while(s.size() >= 2 and s.back()=='0')s.erase(s.end()-1);
  reverse(s.begin(),s.end());
  return s;
}

using ll = long long;

const ll MOD = 1e9+7;

string func(){
  ll m;
  cin >> m;
  string c;
  cin >> c;
  vector<ll> ks(10,0);
  for(int i=0;i<c.size();++i){
    for(auto &i:ks)i = i * 10 % MOD;
    int v = c[i] - '0';
    ks[v] = (ks[v] + 1) % MOD;
  }
  if(c.size() >= 2 and c[0]=='0')return "-1";

  vector<int> xs(10);
  iota(xs.begin(),xs.end(),0);
  do{
    ll sum = 0;
    for(int i=0;i<10;++i){
      sum = sum + (ks[i] * xs[i]);
      sum %= MOD;
    }
    if(sum == m){
      for(auto &i:c){
        char d = xs[i-'0'] + '0';
        i = d;
      }
      return c;
    }
  }while(next_permutation(xs.begin(),xs.end()));
  return "-1";
}

int main(){
  cout << f(func()) << endl;
}
accuracy: 0.43548387096774194
time_ms: 60
memory_kb: 3504
final_rank: 18

code_snippet: #include<bits/stdc++.h>

using namespace std;

string f(string s){
  reverse(s.begin(),s.end());
  while(s.size() >= 2 and s.back()=='0')s.erase(s.end()-1);
  reverse(s.begin(),s.end());
  return s;
}

using ll = long long;

const ll MOD = 1e9+7;

string func(){
  ll m;
  cin >> m;
  string c;
  cin >> c;
  vector<ll> ks(10,0);
  for(int i=0;i<c.size();++i){
    for(auto &i:ks)i = i * 10 % MOD;
    int v = c[i] - '0';
    ks[v] = (ks[v] + 1) % MOD;
  }
  if(c[0]=='0')return "-1";

  vector<int> xs(10);
  iota(xs.begin(),xs.end(),0);
  do{
    ll sum = 0;
    for(int i=0;i<10;++i){
      sum = sum + (ks[i] * xs[i]);
      sum %= MOD;
    }
    if(sum == m){
      for(auto &i:c){
        char d = xs[i-'0'] + '0';
        i = d;
      }
      return c;
    }
  }while(next_permutation(xs.begin(),xs.end()));
  return "-1";
}

int main(){
  cout << f(func()) << endl;
}
accuracy: 0.43548387096774194
time_ms: 60
memory_kb: 3568
final_rank: 20

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;

const ll mod = 1000000007;

int main(){    
    ll M; cin >> M;
    string C; cin >> C;

    array<ll, 10> buc; 
    for (int i = 0 ; i < 10 ; i++) buc[i] = 0;
    ll p10 = 1;
    for (int i = (int)C.size() - 1 ; i >= 0 ; i--) {
        int now = C[i] - '0';
        buc[now] = (buc[now] + p10) % mod;
        p10 = (p10 * 10) % mod;
    }
    
    vector<int> idx(10, 0);
    iota(idx.begin(), idx.end(), 0);

    do {

        if (idx[C[0] - '0'] == 0) continue;

        ll val = 0LL; 
        for (int i = 0 ; i < 10 ; i++) {
            val = (val + (idx[i] * buc[i])) % mod;
        }
        if (val == M) {
            string ans = C;
            for (auto& c : ans) c = idx[c - '0'] + '0';
            cout << ans << endl;
            return 0;
        }


    } while (next_permutation(idx.begin(), idx.end())) ;

    cout << -1 << endl;
}
accuracy: 0.8870967741935484
time_ms: 60
memory_kb: 3656
final_rank: 13

code_snippet: #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=2167167167167167167;
const int INF=2100000000;
const ll mod=1e9+7;
#define rep(i,a) for (ll i=0;i<a;i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}
template<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}
template<class T> T min(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;}
template<class T> T max(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;}



void solve();
// oddloop
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t=1;
	//cin>>t;
	rep(i,t) solve();
}

void solve(){
	ll M;
	cin>>M;
	string S;
	cin>>S;
	int N=S.size();
	if(M==0&&N==1){
		cout<<"0\n";
		return;
	}
	vector<ll> base(10);
	ll tmp=1;
	for(int i=N-1;i>=0;i--){
		base[S[i]-'0']+=tmp;
		tmp=(tmp*10ll)%mod;
	}
	for(auto &x:base) x%=mod;
	vector<int> order(10);
	rep(i,10) order[i]=i;
	do{
		tmp=0;
		rep(i,10){
			tmp=(tmp+base[i]*(ll)(order[i]))%mod;
		}
		if(tmp==M&&order[S[0]-'0']!=0){
			rep(i,N){
				cout<<order[S[i]-'0'];
			}
			cout<<"\n";
			return;
		}
	}while(next_permutation(all(order)));
	cout<<"-1\n";
}
accuracy: 1
time_ms: 70
memory_kb: 3568
final_rank: 5

code_snippet: #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=2167167167167167167;
const int INF=2100000000;
const ll mod=1e9+7;
#define rep(i,a) for (ll i=0;i<a;i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}
template<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}
template<class T> T min(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;}
template<class T> T max(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;}



void solve();
// oddloop
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t=1;
	//cin>>t;
	rep(i,t) solve();
}

void solve(){
	ll M;
	cin>>M;
	string S;
	cin>>S;
	int N=S.size();
	vector<ll> base(10);
	ll tmp=1;
	for(int i=N-1;i>=0;i--){
		base[S[i]-'0']+=tmp;
		tmp=(tmp*10ll)%mod;
	}
	for(auto &x:base) x%=mod;
	vector<int> order(10);
	rep(i,10) order[i]=i;
	do{
		tmp=0;
		rep(i,10){
			tmp=(tmp+base[i]*(ll)(order[i]))%mod;
		}
		if(tmp==M&&order[S[0]-'0']!=0){
			rep(i,N){
				cout<<order[S[i]-'0'];
			}
			cout<<"\n";
			return;
		}
	}while(next_permutation(all(order)));
	cout<<"-1\n";
}
accuracy: 0.8870967741935484
time_ms: 60
memory_kb: 3500
final_rank: 10

code_snippet: #include <bits/stdc++.h>
// #include <atcoder/modint>

using namespace std;
// using namespace atcoder;
// using mint = modint998244353;

using namespace std;
int main() {
    long long M;
    string C;
    cin >> M >> C;
    string Crev = C;
    reverse(Crev.begin(), Crev.end());
    vector<long long> mod_units(10);
    constexpr long long MOD = 1000000007;

    long long mul = 1;
    for (char c : Crev) {
        int digit = c - '0';
        mod_units[digit] += mul;
        mod_units[digit] %= MOD;
        mul = (mul * 10) % MOD;
    }

    vector<int> mapping(10);
    for (int i=0; i<10; i++) mapping[i] = i;

    int head = C[0] - '0';

    do {
        // 0-heading check
        if (mapping[head] == 0 && C.size() > 1) continue;
        long long mod = 0;
        for (int i = 0; i < 10; i++) {
            mod += (mapping[i] * mod_units[i]) % MOD;
            mod %= MOD;
        }
        if (mod != M) continue;

        for (char c : C) {
            int digit = c - '0';
            cout << mapping[digit];
        }
        cout << endl;
        return 0;

    } while (next_permutation(mapping.begin(), mapping.end()));

    cout << -1 << endl;

    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 3620
final_rank: 7

code_snippet: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    long long M;
    cin>>M;
    string C;
    cin>>C;

    long long MOD = 1000000007;
    vector<long long> C2(10);
    for (int i=0; i<10; i++)
    {
        long long x = 0;
        for (char c: C)
        {
            x = x*10%MOD;
            if (c=='0'+i)
                x = (x+1)%MOD;
        }
        C2[i] = x;
    }

    vector<int> P(10);
    for (int i=0; i<10; i++)
        P[i] = i;
    do
    {
        long long m = 0;
        for (int i=0; i<10; i++)
            m = (m+C2[i]*P[i])%MOD;
        if (m==M && (P[C[0]-'0']!=0 || C.size()==1))
        {
            for (char c: C)
                cout<<char(P[c-'0']+'0');
            cout<<endl;
            return 0;
        }
    }
    while (next_permutation(P.begin(), P.end()));

    cout<<-1<<endl;
}
accuracy: 1
time_ms: 60
memory_kb: 3552
final_rank: 1

code_snippet: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    long long M;
    cin>>M;
    string C;
    cin>>C;

    long long MOD = 1000000007;
    vector<long long> C2(10);
    for (int i=0; i<10; i++)
    {
        long long x = 0;
        for (char c: C)
        {
            x = x*10%MOD;
            if (c=='0'+i)
                x = (x+1)%MOD;
        }
        C2[i] = x;
    }

    vector<int> P(10);
    for (int i=0; i<10; i++)
        P[i] = i;
    do
    {
        long long m = 0;
        for (int i=0; i<10; i++)
            m = (m+C2[i]*P[i])%MOD;
        if (m==M && P[C[0]-'0']!=0)
        {
            for (char c: C)
                cout<<char(P[c-'0']+'0');
            cout<<endl;
            return 0;
        }
    }
    while (next_permutation(P.begin(), P.end()));

    cout<<-1<<endl;
}
accuracy: 0.8870967741935484
time_ms: 60
memory_kb: 3556
final_rank: 11