AtCoder Beginner Contest 030

Submission #1358539

Source codeソースコード

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

public class Main {
    private static boolean debug = false;
    private static boolean elapsed = false;

    private static PrintWriter _out = new PrintWriter(System.out);
    private static PrintWriter _err = new PrintWriter(System.err);

    private void solve(Scanner sc) {
        int N = sc.nextInt();
        int a = sc.nextInt();
        BigInteger k = new BigInteger(sc.next());
        int[] b = new int[N];
        for (int i = 0; i < N; ++i) {
            b[i] = sc.nextInt();
        }

        int aa = a;
        int[] cnt = new int[N];
        do {
            ++cnt[aa - 1];
            aa = b[aa - 1];
        } while (cnt[aa - 1] < 2);

        int cnt1 = 0;
        int cnt2 = 0;
        for (int i = 0; i < N; ++i) {
            if (cnt[i] == 1) {
                ++cnt1;
            } else if (cnt[i] == 2) {
                ++cnt2;
            }
        }

        k = k.subtract(BigInteger.valueOf(cnt1)).remainder(BigInteger.valueOf(cnt2));

        int kk = k.intValue() + cnt1;
        aa = a;
        while (kk > 0) {
            aa = b[aa - 1];
            --kk;
        }

        _out.println(aa);
    }
    private static BigInteger C(long n, long r) {
        BigInteger res = BigInteger.ONE;
        for (long i = n; i > n - r; --i) {
            res = res.multiply(BigInteger.valueOf(i));
        }
        for (long i = r; i > 1; --i) {
            res = res.divide(BigInteger.valueOf(i));
        }
        return res;
    }
    private static BigInteger P(long n, long r) {
        BigInteger res = BigInteger.ONE;
        for (long i = n; i > n - r; --i) {
            res = res.multiply(BigInteger.valueOf(i));
        }
        return res;
    }
    /*
     * 10^10 > Integer.MAX_VALUE = 2147483647 > 10^9
     * 10^19 > Long.MAX_VALUE = 9223372036854775807L > 10^18
     */
    public static void main(String[] args) {
        long S = System.currentTimeMillis();

        Scanner sc = new Scanner(System.in);
        new Main().solve(sc);
        _out.flush();

        long G = System.currentTimeMillis();
        if (elapsed) {
            _err.println((G - S) + "ms");
        }
        _err.flush();
    }
}

Submission

Task問題 D - へんてこ辞書
User nameユーザ名 HHeLiBeX
Created time投稿日時
Language言語 Java8 (OpenJDK 1.8.0)
Status状態 AC
Score得点 100
Source lengthソースコード長 2296 Byte
File nameファイル名
Exec time実行時間 657 ms
Memory usageメモリ使用量 51464 KB

Test case

Set

Set name Score得点 / Max score Cases
Sample - subtask0_sample_01.txt,subtask0_sample_03.txt
Subtask1 50 / 50 subtask0_0.txt,subtask0_1.txt,subtask0_2.txt,subtask0_3.txt,subtask0_4.txt,subtask0_5.txt,subtask0_6.txt,subtask0_7.txt,subtask0_8.txt,subtask0_9.txt,subtask0_sample_01.txt,subtask0_sample_03.txt
All 50 / 50 subtask0_0.txt,subtask0_1.txt,subtask0_2.txt,subtask0_3.txt,subtask0_4.txt,subtask0_5.txt,subtask0_6.txt,subtask0_7.txt,subtask0_8.txt,subtask0_9.txt,subtask0_sample_01.txt,subtask0_sample_03.txt,subtask1_0.txt,subtask1_1.txt,subtask1_10.txt,subtask1_11.txt,subtask1_2.txt,subtask1_3.txt,subtask1_4.txt,subtask1_5.txt,subtask1_6.txt,subtask1_7.txt,subtask1_8.txt,subtask1_9.txt,subtask1_sample_02.txt

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
subtask0_0.txt AC 405 ms 49828 KB
subtask0_1.txt AC 367 ms 48608 KB
subtask0_2.txt AC 374 ms 49276 KB
subtask0_3.txt AC 355 ms 46656 KB
subtask0_4.txt AC 422 ms 48544 KB
subtask0_5.txt AC 367 ms 47388 KB
subtask0_6.txt AC 370 ms 48580 KB
subtask0_7.txt AC 390 ms 48788 KB
subtask0_8.txt AC 354 ms 45700 KB
subtask0_9.txt AC 389 ms 50812 KB
subtask0_sample_01.txt AC 99 ms 18900 KB
subtask0_sample_03.txt AC 97 ms 19796 KB
subtask1_0.txt AC 521 ms 41996 KB
subtask1_1.txt AC 566 ms 45784 KB
subtask1_10.txt AC 657 ms 49336 KB
subtask1_11.txt AC 97 ms 21840 KB
subtask1_2.txt AC 445 ms 46976 KB
subtask1_3.txt AC 420 ms 46972 KB
subtask1_4.txt AC 494 ms 45356 KB
subtask1_5.txt AC 500 ms 50564 KB
subtask1_6.txt AC 598 ms 45684 KB
subtask1_7.txt AC 546 ms 49500 KB
subtask1_8.txt AC 499 ms 51464 KB
subtask1_9.txt AC 558 ms 50716 KB
subtask1_sample_02.txt AC 94 ms 18772 KB