結果

問題 No.3204 Permuted Integer
ユーザー tenten
提出日時 2025-08-07 13:56:07
言語 Java
(openjdk 23)
結果
TLE  
実行時間 -
コード長 4,733 bytes
コンパイル時間 3,533 ms
コンパイル使用メモリ 82,232 KB
実行使用メモリ 55,492 KB
最終ジャッジ日時 2025-08-07 13:56:17
合計ジャッジ時間 8,122 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other TLE * 1 -- * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
import java.math.*;

public class Main {
    static long[] bases = new long[11];
    static List<Set<Long>> enables = new ArrayList<>();
    static final int MAX = 1000000000;
    public static void main(String[] args) throws Exception {
        FastScanner sc = new FastScanner();
        int t = sc.nextInt();
        for (int i = 0; i <= 10; i++) {
            enables.add(new HashSet<>());
        }
        bases[0] = 1;
        for (int i = 1; i <= 10; i++) {
            bases[i] = bases[i - 1] * 10;
        }
        for (long i = 0; i * i <= MAX; i++) {
            long x = i * i;
            for (int j = 0; j <= 10; j++) {
                enables.get(j).add(x % bases[j]);
            }
        }
        StringBuilder sb = new StringBuilder();
        while (t-- > 0) {
            int[] counts = new int[10];
            int times = 0;
            for (char c : sc.next().toCharArray()) {
                counts[c - '0']++;
                times++;
            }
            long ans = check(0, counts, 0, times);
            sb.append(ans == Long.MAX_VALUE ? -1 : ans).append("\n");
        }
        System.out.print(sb);
    }
    
    static long check(int idx, int[] counts, long value, int times) {
        if (!enables.get(idx).contains(value)) {
            return Long.MAX_VALUE;
        }
        if (idx == times) {
            return isSqrt(value) ? value : Long.MAX_VALUE;
        }
        long ans = Long.MAX_VALUE;
        for (int i = 0; i < 10; i++) {
            if (counts[i] == 0) {
                continue;
            }
            counts[i]--;
            ans = Math.min(ans, check(idx + 1, counts, value + i * bases[idx], times));
            counts[i]++;
        }
        return ans;
    }
    
    static boolean isSqrt(long x) {
        long left = 0;
        long right = Integer.MAX_VALUE - 10;
        while (right - left > 1) {
            long m = (left + right) / 2;
            if (m * m <= x) {
                left = m;
            } else {
                right = m;
            }
        }
        return left * left == x;
    }
}

class FastScanner {
    private DataInputStream din;
    private byte[] buffer;
    private int bufferPointer, bytesRead;
    private static final int BUFFER_SIZE = 1 << 16; // 65,536 バイト

    public FastScanner() {
        din = new DataInputStream(System.in);
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    private byte read() {
        try {
            if (bufferPointer == bytesRead) {
                bytesRead = din.read(buffer, 0, BUFFER_SIZE);
                if (bytesRead == -1) return -1;
                bufferPointer = 0;
            }
            return buffer[bufferPointer++];
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public String next() {
        byte b = read();
        // 空白文字を読み飛ばす
        while (isSpaceChar(b)) {
            b = read();
        }
        StringBuilder sb = new StringBuilder();
        while (!isSpaceChar(b)) {
            sb.append((char) b);
            b = read();
        }
        return sb.toString();
    }

    public int nextInt() {
        int ret = 0;
        byte c = read();
        while (isSpaceChar(c)) c = read();
        boolean neg = (c == '-');
        if (neg) c = read();
        do {
            ret = ret * 10 + c - '0';
            c = read();
        } while (!isSpaceChar(c));
        return neg ? -ret : ret;
    }

    public long nextLong() {
        long ret = 0;
        byte c = read();
        while (isSpaceChar(c)) c = read();
        boolean neg = (c == '-');
        if (neg) c = read();
        do {
            ret = ret * 10L + c - '0';
            c = read();
        } while (!isSpaceChar(c));
        return neg ? -ret : ret;
    }

    public double nextDouble() {
        double ret = 0, div = 1;
        byte c = read();
        while (isSpaceChar(c)) c = read();
        boolean neg = (c == '-');
        if (neg) c = read();
        do {
            ret = ret * 10 + c - '0';
            c = read();
        } while (!isSpaceChar(c) && c != '.');
        if (c == '.') {
            while (!isSpaceChar(c = read())) {
                ret += (c - '0') / (div *= 10);
            }
        }
        return neg ? -ret : ret;
    }

    private boolean isSpaceChar(byte c) {
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    public void close() {
        try {
            if (din != null) din.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}


0