結果

問題 No.137 貯金箱の焦り
ユーザー 👑 hos.lyrichos.lyric
提出日時 2015-01-05 05:33:14
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 991 ms / 5,000 ms
コード長 733 bytes
コンパイル時間 864 ms
コンパイル使用メモリ 102,784 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-12 01:51:28
合計ジャッジ時間 6,543 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.stdio, std.string;

immutable long MO = 1_234_567_891;

void add(ref long t, in long f) {
	if ((t += f) >= MO) {
		t -= MO;
	}
}

void main() {
	string[] tokens;
	tokens = readln.chomp.split(" ");
	const N = tokens[0].to!int;
	const M = tokens[1].to!long;
	tokens = readln.chomp.split(" ");
	auto A = new int[N];
	foreach (i; 0 .. N) {
		A[i] = tokens[i].to!int;
	}
	
	const ASum = A.reduce!((a, b) => a + b);
	auto dp = new long[ASum * 2];
	dp[0] = 1;
	foreach (j; 0 .. 64) {
		foreach (i; 0 .. N) {
			foreach_reverse (x; A[i] .. ASum * 2) {
				add(dp[x], dp[x - A[i]]);
			}
		}
		foreach (x; 0 .. ASum) {
			dp[x] = dp[x << 1 | ((M >> j) & 1)];
		}
		dp[ASum .. $] = 0;
	}
	writeln(dp[0]);
}
0