結果

問題 No.995 タピオカオイシクナーレ
ユーザー ks2mks2m
提出日時 2020-02-21 22:31:29
言語 Java21
(openjdk 21)
結果
AC  
実行時間 178 ms / 2,000 ms
コード長 1,909 bytes
コンパイル時間 2,230 ms
コンパイル使用メモリ 77,292 KB
実行使用メモリ 44,224 KB
最終ジャッジ日時 2024-04-17 08:09:56
合計ジャッジ時間 5,755 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
37,144 KB
testcase_01 AC 52 ms
37,360 KB
testcase_02 AC 51 ms
37,328 KB
testcase_03 AC 51 ms
37,220 KB
testcase_04 AC 51 ms
37,440 KB
testcase_05 AC 51 ms
37,432 KB
testcase_06 AC 51 ms
37,352 KB
testcase_07 AC 51 ms
37,188 KB
testcase_08 AC 52 ms
37,444 KB
testcase_09 AC 53 ms
37,444 KB
testcase_10 AC 50 ms
37,404 KB
testcase_11 AC 54 ms
37,352 KB
testcase_12 AC 54 ms
37,312 KB
testcase_13 AC 55 ms
37,264 KB
testcase_14 AC 53 ms
37,400 KB
testcase_15 AC 54 ms
37,420 KB
testcase_16 AC 178 ms
44,012 KB
testcase_17 AC 161 ms
43,772 KB
testcase_18 AC 178 ms
44,132 KB
testcase_19 AC 173 ms
43,884 KB
testcase_20 AC 160 ms
43,768 KB
testcase_21 AC 171 ms
43,812 KB
testcase_22 AC 176 ms
44,224 KB
testcase_23 AC 176 ms
44,052 KB
testcase_24 AC 178 ms
44,128 KB
testcase_25 AC 173 ms
44,132 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;
		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