結果

問題 No.412 花火大会
ユーザー GrenacheGrenache
提出日時 2016-08-13 01:00:45
言語 Java21
(openjdk 21)
結果
AC  
実行時間 56 ms / 2,000 ms
コード長 5,170 bytes
コンパイル時間 5,363 ms
コンパイル使用メモリ 75,128 KB
実行使用メモリ 50,640 KB
最終ジャッジ日時 2023-08-07 12:38:35
合計ジャッジ時間 6,927 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
50,532 KB
testcase_01 AC 55 ms
50,356 KB
testcase_02 AC 54 ms
50,176 KB
testcase_03 AC 54 ms
50,360 KB
testcase_04 AC 54 ms
50,164 KB
testcase_05 AC 54 ms
50,236 KB
testcase_06 AC 55 ms
50,196 KB
testcase_07 AC 55 ms
50,356 KB
testcase_08 AC 54 ms
50,640 KB
testcase_09 AC 54 ms
50,200 KB
testcase_10 AC 54 ms
50,344 KB
testcase_11 AC 54 ms
50,332 KB
testcase_12 AC 55 ms
50,300 KB
testcase_13 AC 54 ms
50,292 KB
testcase_14 AC 54 ms
50,196 KB
testcase_15 AC 55 ms
50,200 KB
testcase_16 AC 55 ms
50,468 KB
testcase_17 AC 55 ms
50,228 KB
testcase_18 AC 54 ms
50,236 KB
testcase_19 AC 55 ms
50,224 KB
testcase_20 AC 55 ms
50,188 KB
testcase_21 AC 56 ms
50,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


public class Main_yukicoder412 {

    public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);
    	Printer pr = new Printer(System.out);

    	PC pc = new PC(30, 1_000_000_007);

    	int[] bcd = new int[3];
    	for (int i = 0; i < 3; i++) {
    		bcd[i] = sc.nextInt();
    	}
    	Arrays.sort(bcd);

    	int n = sc.nextInt();

    	int[] e = new int[n];
    	for (int i = 0; i < n; i++) {
    		e[i] = sc.nextInt();
    	}
    	Arrays.sort(e);

    	int[] ret = new int[3];
    	for (int i = 0; i < 3; i++) {
    		for (int j = 0; j < n; j++) {
    			if (e[j] >= bcd[i]) {
    				ret[i] = n - j;
    				break;
    			}
    		}
    	}

    	ret[0] -= ret[1];
    	ret[1] -= ret[2];

    	int res = 0;
    	for (int i = 1; i <= ret[2]; i++) {
    		int tmp1 = pc.C(ret[2], i);
    		if (i >= 3) {
    			tmp1 *= 0x1 << (n - ret[2]);
    			res += tmp1;
    			continue;
    		}
    		for (int j = 0; j <= ret[1]; j++) {
    			int tmp2 = tmp1 * pc.C(ret[1], j);
    			if (i + j >= 3) {
    				tmp2 *= 0x1 << (n - ret[2] - ret[1]);
    				res += tmp2;
    				continue;
    			}
    			if (i + j == 2) {
    				tmp2 *= (0x1 << ret[0]) - 1;
    				tmp2 *= (0x1 << (n - ret[0] - ret[1] - ret[2]));
    				res += tmp2;
    			}
    		}
    	}

    	pr.println(res);

        pr.close();
        sc.close();
    }

	@SuppressWarnings("unused")
    private static class PC {
    	// MOD must be prime number.
    	int MOD;
    	// fact[i] : i! % MOD
    	long[] fact;
    	// ifact[i] : 1/i! % MOD
    	long[] ifact;

    	PC(int size, int MOD) {
    		// O(size)
    		// n=sizeまでのnCrを求める。
    		// nHrはn+r-1Crになってしまうので注意

    		this.MOD = MOD;

    		fact = new long[size + 1];
    		fact[0] = 1;
    		for (int i = 1; i <= size; i++) {
    			fact[i] = fact[i - 1] * i % MOD;
    		}

    		ifact = new long[size + 1];

    		int loop = MOD - 2;
    		long x = fact[size];
    		ifact[size] = 1;
    		while (loop > 0) {
    			if (loop % 2 == 1) {
    				ifact[size] = ifact[size] * x % MOD;
    			}
    			x = x * x % MOD;
    			loop /= 2;
    		}

    		for (int i = size - 1; i >= 0; i--) {
    			ifact[i] = ifact[i + 1] * (i + 1) % MOD;
    		}

    	}

    	// 組合せの数
    	int C(int n, int r) {
    		if (r > n) {
    			return 0;
    		}

    		return (int)(((fact[n] * ifact[n - r]) % MOD) * ifact[r] % MOD);
    	}

    	// 順列
    	int P(int n, int r) {
    		if (r > n) {
    			return 0;
    		}

    		return (int)((fact[n] * ifact[n -r]) % MOD);
    	}

    	// 重複組み合わせ
    	// 異なるn種のものから重複を許してr個を選ぶ場合の数
    	// 0個の種類もあり得る
    	int H(int n, int r) {
    		if (n == 0 && r == 0) {
    			return 1;
    		}

    		return C(n + r - 1, r);
    	}

    	// 組合せの数(nが大きいとき)
    	//   O(r)で求めることができる。rはsizeの大きさまで
    	int C2(long n, int r) {
    		long ret = ifact[r];
    		for (int i = 1; i <= r; i++) {
    			long tmp = (n - r + i) % MOD;
    			ret = (ret * tmp) % MOD;
    		}

    		return (int)ret;
    	}

    	// 第2種スターリング数
    	// n人をちょうどr個のグループに分ける(グループの区別はなし)
    	// グループの区別をする場合はr!S(n,r)。全射の場合の数と同義
    	// O(r log n)
    	int S(long n, int r) {
    		//全射の場合の数を包除原理を使って求めて、1/r!をかける。
    		long ret = 0;
    		for (int i = 1; i <= r; i++) {
    			long tmp = (r - i) % 2 == 0 ? 1 : -1;
    			tmp *= pow(i, n) * C(r, i) % MOD;
    			ret = (ret + tmp + MOD) % MOD;
    		}
    		ret = ret * ifact[r] % MOD;

    		return (int)ret;
    	}

    	long pow(int a, long n) {
    		long loop = n;
    		long ret = 1;
    		long x = a;
    		while (loop > 0) {
    			if (loop % 2 == 1) {
    				ret = ret * x % MOD;
    			}
    			x = x * x % MOD;
    			loop /= 2;
    		}

    		return ret;
    	}
    }

	@SuppressWarnings("unused")
	private static class Scanner {
		BufferedReader br;
		Iterator<String> it;

		Scanner (InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}

		String next() throws RuntimeException  {
			try {
				if (it == null || !it.hasNext()) {
//					it = Arrays.asList(br.readLine().split(" ")).iterator();
					it = Arrays.asList(br.readLine().split("\\p{javaWhitespace}+")).iterator();
				}
				return it.next();
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}

		int nextInt() throws RuntimeException {
			return Integer.parseInt(next());
		}

		long nextLong() throws RuntimeException {
			return Long.parseLong(next());
		}

		float nextFloat() throws RuntimeException {
			return Float.parseFloat(next());
		}

		double nextDouble() throws RuntimeException {
			return Double.parseDouble(next());
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
//				throw new IllegalStateException();
			}
		}
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0