結果

問題 No.3204 Permuted Integer
ユーザー zawakasu
提出日時 2025-07-18 21:27:22
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 82 ms / 2,000 ms
コード長 1,696 bytes
コンパイル時間 1,293 ms
コンパイル使用メモリ 128,712 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2025-07-18 23:35:14
合計ジャッジ時間 3,791 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <cassert>
#include <vector>
#include <algorithm>
#include <utility>
#include <numeric>
#include <tuple>
#include <ranges>
namespace ranges = std::ranges;
namespace views = std::views;
// #include "Src/Number/IntegerDivision.hpp"
// #include "Src/Utility/BinarySearch.hpp"
// #include "Src/Sequence/CompressedSequence.hpp"
// #include "Src/Sequence/RunLengthEncoding.hpp"
// #include "Src/Algebra/Group/AdditiveGroup.hpp"
// #include "Src/DataStructure/FenwickTree/FenwickTree.hpp"
// #include "Src/DataStructure/SegmentTree/SegmentTree.hpp"
// #include "Src/DataStructure/DisjointSetUnion/DisjointSetUnion.hpp"
// using namespace zawa;
// #include "atcoder/modint"
// using mint = atcoder::modint998244353;
#include <array>
#include <map>
using item = std::array<int, 10>;
item f(int n) {
    item res;
    res.fill(0);
    while (n) {
        res[n % 10]++;
        n /= 10;
    }
    return res;
}
int main() {
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    std::ios::sync_with_stdio(false);
    std::map<item, int> map;
    for (int i = 1 ; i * i <= 1000000000 ; i++) {
        const item v = f(i * i);
        auto it = map.find(v);
        if (it == map.end()) map[v] = i * i;
        else it->second = std::min(it->second, i * i);
    }
    int T;
    std::cin >> T;
    while (T--) {
        int N;
        std::cin >> N;
        item v = f(N);
        const int INF = (int)2e9;
        int ans = INF;
        while (v[0] >= 0) {
            const auto it = map.find(v);
            if (it != map.end()) ans = std::min(ans, it->second);
            v[0]--;
        }
        std::cout << (ans == INF ? -1 : ans) << '\n';
    }
}
0