結果
問題 | No.603 hel__world (2) |
ユーザー | 37zigen |
提出日時 | 2017-12-21 22:34:03 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 1,179 ms / 3,000 ms |
コード長 | 2,631 bytes |
コンパイル時間 | 2,903 ms |
コンパイル使用メモリ | 82,440 KB |
実行使用メモリ | 154,352 KB |
最終ジャッジ日時 | 2024-12-17 22:48:40 |
合計ジャッジ時間 | 16,590 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
ソースコード
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(); } }