結果

問題 No.995 タピオカオイシクナーレ
ユーザー ks2mks2m
提出日時 2020-02-21 22:45:40
言語 Java21
(openjdk 21)
結果
AC  
実行時間 186 ms / 2,000 ms
コード長 2,006 bytes
コンパイル時間 2,141 ms
コンパイル使用メモリ 76,784 KB
実行使用メモリ 43,960 KB
最終ジャッジ日時 2024-04-17 08:27:58
合計ジャッジ時間 5,821 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
37,292 KB
testcase_01 AC 53 ms
36,980 KB
testcase_02 AC 53 ms
36,952 KB
testcase_03 AC 53 ms
37,160 KB
testcase_04 AC 53 ms
37,244 KB
testcase_05 AC 54 ms
36,992 KB
testcase_06 AC 53 ms
37,160 KB
testcase_07 AC 53 ms
37,240 KB
testcase_08 AC 54 ms
37,296 KB
testcase_09 AC 53 ms
37,176 KB
testcase_10 AC 53 ms
37,196 KB
testcase_11 AC 57 ms
36,880 KB
testcase_12 AC 57 ms
37,132 KB
testcase_13 AC 56 ms
36,892 KB
testcase_14 AC 54 ms
36,984 KB
testcase_15 AC 56 ms
36,904 KB
testcase_16 AC 171 ms
43,656 KB
testcase_17 AC 174 ms
43,676 KB
testcase_18 AC 181 ms
43,776 KB
testcase_19 AC 179 ms
43,880 KB
testcase_20 AC 180 ms
43,768 KB
testcase_21 AC 180 ms
43,932 KB
testcase_22 AC 177 ms
43,960 KB
testcase_23 AC 182 ms
43,700 KB
testcase_24 AC 183 ms
43,684 KB
testcase_25 AC 186 ms
43,508 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigInteger;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		int n = Integer.parseInt(sa[0]);
		int m = Integer.parseInt(sa[1]);
		long k = Long.parseLong(sa[2]);
		int p = Integer.parseInt(sa[3]);
		int q = Integer.parseInt(sa[4]);
		int[] b = new int[n];
		for (int i = 0; i < b.length; i++) {
			b[i] = Integer.parseInt(br.readLine());
		}
		br.close();

		int mod = 1000000007;
		long sum1 = 0;
		for (int i = 0; i < m; i++) {
			sum1 += b[i];
		}
		sum1 %= mod;
		long sum2 = 0;
		for (int i = m; i < n; i++) {
			sum2 += b[i];
		}
		sum2 %= mod;

		long[][] dp = new long[2][50];
		dp[0][0] = q - p;
		dp[1][0] = p;
		long[] pow = new long[50];
		pow[0] = q;
		for (int i = 1; i < 50; i++) {
			dp[0][i] = dp[0][i - 1] * dp[0][i - 1] + dp[1][i - 1] * dp[1][i - 1];
			dp[0][i] %= mod;
			dp[1][i] = dp[0][i - 1] * dp[1][i - 1] + dp[1][i - 1] * dp[0][i - 1];
			dp[1][i] %= mod;
			pow[i] = pow[i - 1] * pow[i - 1] % mod;
		}

		long stay = 0;
		long not = 0;
		long total = 0;
		if (k == 0) {
			stay = q - p;
			not = p;
			total = q;
		} else {
			long k2 = k;
			boolean first = true;
			for (int i = 48; i >= 0; i--) {
				long d = 1L << i;
				long e = k2 / d;
				k2 %= d;
				if (e > 0 && first) {
					stay = dp[0][i];
					not = dp[1][i];
					total = pow[i];
					first = false;
					continue;
				}
				if (e > 0) {
					long bk = stay;
					stay = stay * dp[0][i] + not * dp[1][i];
					stay %= mod;
					not = bk * dp[1][i] + not * dp[0][i];
					not %= mod;
					total = total * pow[i];
					total %= mod;
				}
			}
		}

		long t2 = BigInteger.valueOf(total).modInverse(BigInteger.valueOf(mod)).longValue();
		long ans = stay * sum1 % mod + (total - stay + mod) % mod * sum2 % mod;
		ans %= mod;
		ans = ans * t2 % mod;
		System.out.println(ans);
	}
}
0