結果

問題 No.147 試験監督(2)
ユーザー GrenacheGrenache
提出日時 2016-07-30 13:19:45
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,186 ms / 2,000 ms
コード長 4,057 bytes
コンパイル時間 3,976 ms
コンパイル使用メモリ 78,608 KB
実行使用メモリ 65,584 KB
最終ジャッジ日時 2024-04-24 13:55:06
合計ジャッジ時間 11,316 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,168 ms
55,272 KB
testcase_01 AC 1,186 ms
65,584 KB
testcase_02 AC 1,175 ms
56,812 KB
testcase_03 AC 85 ms
38,124 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


public class Main_yukicoder147 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Printer pr = new Printer(System.out);

//        final int MOD = 1_000_000_007;

        long[][] cnt = new long[4][63];
        cnt[0][0] = 1;
        cnt[1][0] = 0;
        cnt[2][0] = 0;
        cnt[3][0] = 1;
        for (int i = 1; i < 63; i++) {
        	cnt[0][i] = (cnt[0][i - 1] * cnt[0][i - 1] % MOD + cnt[0][i - 1] * cnt[2][i - 1] % MOD + cnt[1][i - 1] * cnt[0][i - 1] % MOD) % MOD;
        	cnt[1][i] = (cnt[0][i - 1] * cnt[1][i - 1] % MOD + cnt[0][i - 1] * cnt[3][i - 1] % MOD + cnt[1][i - 1] * cnt[1][i - 1] % MOD) % MOD;
        	cnt[2][i] = (cnt[2][i - 1] * cnt[0][i - 1] % MOD + cnt[2][i - 1] * cnt[2][i - 1] % MOD + cnt[3][i - 1] * cnt[0][i - 1] % MOD) % MOD;
        	cnt[3][i] = (cnt[2][i - 1] * cnt[1][i - 1] % MOD + cnt[2][i - 1] * cnt[3][i - 1] % MOD + cnt[3][i - 1] * cnt[1][i - 1] % MOD) % MOD;
        }

        int n = sc.nextInt();

        long sum = 1;
        for (int i = 0; i < n ; i++) {
        	long c = sc.nextLong();
        	char[] d = sc.next().toCharArray();

            long[] ret = null;
            for (int j = 0; j < 63; j++) {
            	if ((c & 0x1L << j) != 0) {
            		if (ret == null) {
            			ret = new long[4];
            			ret[0] = cnt[0][j];
            			ret[1] = cnt[1][j];
            			ret[2] = cnt[2][j];
            			ret[3] = cnt[3][j];
            		} else {
            			long[] tmp = new long[4];
            			tmp[0] = (cnt[0][j] * ret[0] % MOD + cnt[0][j] * ret[2] % MOD + cnt[1][j] * ret[0] % MOD) % MOD;
            			tmp[1] = (cnt[0][j] * ret[1] % MOD + cnt[0][j] * ret[3] % MOD + cnt[1][j] * ret[1] % MOD) % MOD;
            			tmp[2] = (cnt[2][j] * ret[0] % MOD + cnt[2][j] * ret[2] % MOD + cnt[3][j] * ret[0] % MOD) % MOD;
            			tmp[3] = (cnt[2][j] * ret[1] % MOD + cnt[2][j] * ret[3] % MOD + cnt[3][j] * ret[1] % MOD) % MOD;
            			ret = tmp;
            		}
            	}
            }

            long cp = (ret[0] + ret[1] + ret[2] + ret[3]) % MOD;
        	if (cp != 0) {
        		long mod = 0;
        		for (char e : d) {
        			mod = mod * 10 % (MOD - 1);
        			mod = mod + e - '0';
        		}

        		sum = (sum * modPow(cp, mod)) % MOD;
        	} else {
        		sum = 0;
        	}
        }

        pr.println(sum);

        pr.close();
        sc.close();
    }

    private static int MOD = 1_000_000_007;

	private static long modPow(long a, long n) {
		long loop = n;
		long ret = 1;
		long x = a;
		while (loop > 0) {
			if (loop % 2 == 1) {
				ret = ret * x % MOD;
			}
			x = x * x % MOD;
			loop /= 2;
		}

		return ret;
	}

	@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();
					it = Arrays.asList(br.readLine().split("\\p{javaWhitespace}+")).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();
			}
		}
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0