結果

問題 No.603 hel__world (2)
ユーザー 37zigen37zigen
提出日時 2017-12-21 22:34:03
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,150 ms / 3,000 ms
コード長 2,631 bytes
コンパイル時間 2,544 ms
コンパイル使用メモリ 83,196 KB
実行使用メモリ 156,316 KB
最終ジャッジ日時 2023-08-22 21:00:16
合計ジャッジ時間 15,924 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 176 ms
78,564 KB
testcase_01 AC 180 ms
78,776 KB
testcase_02 AC 181 ms
78,848 KB
testcase_03 AC 179 ms
78,580 KB
testcase_04 AC 176 ms
78,548 KB
testcase_05 AC 177 ms
78,336 KB
testcase_06 AC 177 ms
78,288 KB
testcase_07 AC 178 ms
78,616 KB
testcase_08 AC 177 ms
78,516 KB
testcase_09 AC 176 ms
78,628 KB
testcase_10 AC 177 ms
78,296 KB
testcase_11 AC 603 ms
99,660 KB
testcase_12 AC 461 ms
94,892 KB
testcase_13 AC 493 ms
99,480 KB
testcase_14 AC 178 ms
78,512 KB
testcase_15 AC 182 ms
78,772 KB
testcase_16 AC 180 ms
78,304 KB
testcase_17 AC 183 ms
78,484 KB
testcase_18 AC 183 ms
78,512 KB
testcase_19 AC 182 ms
78,576 KB
testcase_20 AC 358 ms
91,520 KB
testcase_21 AC 358 ms
91,440 KB
testcase_22 AC 182 ms
78,548 KB
testcase_23 AC 180 ms
78,512 KB
testcase_24 AC 181 ms
78,480 KB
testcase_25 AC 998 ms
113,952 KB
testcase_26 AC 1,031 ms
114,516 KB
testcase_27 AC 1,030 ms
114,364 KB
testcase_28 AC 1,150 ms
156,316 KB
testcase_29 AC 353 ms
91,240 KB
testcase_30 AC 357 ms
91,768 KB
testcase_31 AC 178 ms
78,532 KB
testcase_32 AC 344 ms
91,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Scanner;

class Main {

	class State implements Comparable<State> {
		long a, b;
		int id;

		public State(long a_, long b_, int id_) {
			a = a_;
			b = b_;
			id = id_;
		}

		void inc() {
			long x = a - 1;
			long y = a - b;
			a = a + 1;
			b = a - y;
		}

		@Override
		public int compareTo(State o) {
			return -Long.compare(a * o.b, o.a * b);
		}

	}

	final long MOD = 1_000_000 + 3;
	long[] fac = new long[(int) MOD];
	long[] invfac = new long[(int) MOD];
	long[] inv = new long[(int) MOD];
	{
		fac[0] = invfac[0] = fac[1] = invfac[1] = inv[1] = 1;
		for (int i = 2; i < MOD; ++i) {
			fac[i] = i * fac[i - 1] % MOD;
			inv[i] = MOD - inv[(int) MOD % i] * (MOD / i) % MOD;
			invfac[i] = inv[i] * invfac[i - 1] % MOD;
		}
	}

	long comb(long n, long k) {
		long ret = 1;
		while (n > 0 || k > 0) {
			if (n % MOD < k % MOD)
				return 0;
			ret = ret * comb((int) (n % MOD), (int) (k % MOD)) % MOD;
			n /= MOD;
			k /= MOD;
		}
		return ret;
	}

	long comb(int n, int k) {
		return fac[n] * invfac[k] % MOD * invfac[n - k] % MOD;
	}

	long f(ArrayList<Long> list, long s) {
		long res = s;
		long all = 0;
		for (long v : list) {
			all += v;
		}
		if (all == s)
			return 1;
		if (all > s)
			return 0;
		if (list.size() == 0)
			return 1;
		PriorityQueue<State> pq = new PriorityQueue<>();
		long[] cnt = new long[list.size()];
		for (int i = 0; i < list.size(); ++i) {
			long y = (list.get(i) * s / all) - 1;
			y = Math.max(y, list.get(i));
			res -= y;
			pq.add(new State(y + 1, y + 1 - list.get(i), i));
			cnt[i] = y;
		}
		while (res > 0) {
			State state = pq.poll();
			cnt[state.id]++;
			--res;
			state.inc();
			pq.add(state);
		}
		long ret = 1;
		for (int i = 0; i < cnt.length; ++i) {
			ret = ret * comb(cnt[i], list.get(i)) % MOD;
		}
		return ret;
	}

	void run() {
		Scanner sc = new Scanner(System.in);
		long[] s = new long[26];
		for (int i = 0; i < 26; ++i) {
			s[i] = sc.nextLong();
		}
		String T = sc.next();
		ArrayList<Long>[] list = new ArrayList[26];
		for (int i = 0; i < list.length; ++i)
			list[i] = new ArrayList();
		for (int i = 0; i < T.length(); ++i) {
			long c = 1;
			while (i + 1 < T.length() && T.charAt(i) == T.charAt(i + 1)) {
				++c;
				++i;
			}
			list[T.charAt(i) - 'a'].add(c);
		}
		long ret = 1;
		for (int i = 0; i < 26; ++i) {
			ret = ret * f(list[i], s[i]) % MOD;
		}
		System.out.println(ret);
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

	public static void main(String[] args) {
		new Main().run();
	}

}
0