結果

問題 No.435 占い(Extra)
ユーザー 37zigen37zigen
提出日時 2016-12-02 18:57:54
言語 Java21
(openjdk 21)
結果
MLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,737 bytes
コンパイル時間 3,157 ms
コンパイル使用メモリ 78,628 KB
実行使用メモリ 77,576 KB
最終ジャッジ日時 2024-04-17 02:55:37
合計ジャッジ時間 13,146 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 MLE -
testcase_01 MLE -
testcase_02 MLE -
testcase_03 MLE -
testcase_04 MLE -
testcase_05 MLE -
testcase_06 MLE -
testcase_07 MLE -
testcase_08 MLE -
testcase_09 MLE -
testcase_10 MLE -
testcase_11 MLE -
testcase_12 MLE -
testcase_13 MLE -
testcase_14 MLE -
testcase_15 MLE -
testcase_16 MLE -
testcase_17 MLE -
testcase_18 MLE -
testcase_19 MLE -
testcase_20 MLE -
testcase_21 MLE -
testcase_22 MLE -
testcase_23 MLE -
testcase_24 MLE -
testcase_25 MLE -
testcase_26 MLE -
testcase_27 MLE -
testcase_28 MLE -
testcase_29 MLE -
testcase_30 MLE -
testcase_31 MLE -
testcase_32 MLE -
testcase_33 MLE -
testcase_34 MLE -
testcase_35 MLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Iterator;

public class Q435 {
	static final int[] inv = new int[] { 0, 1, 5, 0, 7, 2, 0, 4, 8 };

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		PrintWriter pw = new PrintWriter(System.out);
		int T = sc.nextInt();
		while (T-- > 0) {
			int n = Integer.parseInt(sc.next());
			int x = Integer.parseInt(sc.next());
			int a = Integer.parseInt(sc.next());
			int b = Integer.parseInt(sc.next());
			int m = Integer.parseInt(sc.next());
			int reducedCombi = 1;
			int count3 = 0;
			int ans = 0;
			boolean f = true;
			int cur = x;
			for (int i = 0; i < n; ++i) {
				int combi = reducedCombi;
				if (i > 0) {
					cur = next(n, cur, a, b, m);
					reducedCombi = reducedCombi * inv[reduce3(i) % 9] * reduce3(n - 1 - i + 1) % 9;
					count3 = count3 - count3(i) + count3(n - 1 - i + 1);
					if (count3 >= 2) {
						combi = 0;
					} else if (count3 == 1) {
						combi = 3 * reducedCombi % 9;
					} else if (count3 == 0) {
						combi = reducedCombi % 9;
					} else if (count3 < 0) {
						throw new AssertionError();
					}
				}

				ans = (ans + combi * (cur % 10)) % 9;
				if (f)
					if (cur % 10 > 0)
						f = false;
			}
			if (f)
				pw.println(0);
			else if (!f && ans == 0)
				pw.println(9);
			else
				pw.println(ans);
		}
		pw.close();
		sc.close();
	}

	static int count3(int n) {
		int c = 0;
		while (n > 0 && n % 3 == 0) {
			n /= 3;
			++c;
		}
		return c;
	}

	static int reduce3(int n) {
		while (n > 0 && n % 3 == 0)
			n /= 3;
		return n;
	}

	static int next(int n, int cur, int a, int b, int m) {
		return ((cur ^ a) + b) % m;
	}

	@SuppressWarnings("unused")
	private static class Scanner {
		BufferedReader br;
		Iterator<String> it;

		Scanner(InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}

		String next() throws RuntimeException {
			try {
				if (it == null || !it.hasNext()) {
					it = Arrays.asList(br.readLine().split(" ")).iterator();
				}
				return it.next();
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}

		int nextInt() throws RuntimeException {
			return Integer.parseInt(next());
		}

		long nextLong() throws RuntimeException {
			return Long.parseLong(next());
		}

		float nextFloat() throws RuntimeException {
			return Float.parseFloat(next());
		}

		double nextDouble() throws RuntimeException {
			return Double.parseDouble(next());
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
				// throw new IllegalStateException();
			}
		}
	}

}
0