关闭

UVa 10458 - Cricket Ranking (容斥)

标签: icpc
266人阅读 评论(0) 收藏 举报
分类:

题意

让几个区间内的数的和为K,问方法数。

思路

组合数学P169。

代码

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

public class Main {

    static BigInteger C(long n, long m) {
        if (n < m) return BigInteger.ZERO;
        m = Math.min(m, n-m);
        BigInteger cur_ans = BigInteger.ONE;
        for (int i = 0; i < m; i++) cur_ans = cur_ans.multiply(BigInteger.valueOf(n-i)).divide(BigInteger.valueOf(i+1));
        return cur_ans;
    }

    static int[] x, y;
    static int n, m;

    static void Solve() {
        BigInteger ans = BigInteger.ZERO;
        BigInteger add = BigInteger.ZERO, del = BigInteger.ZERO;
        for (int i = 0; i < (1<<n); i++) {
            long cnt = 0, cur_m = m;
            for (int j = 0; j < n; j++) {
                if (((1<<j)&i) != 0) {
                    cnt++;
                    cur_m -= y[j]+1;
                }
            }
            if (cur_m < 0) continue;
            if (cnt%2 == 0) add = add.add(C(cur_m+n-1, cur_m));
            else del = del.add(C(cur_m+n-1, cur_m));
        }
        System.out.println(add.subtract(del));
    }

    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            x = new int[100]; y = new int[100];
            n = in.nextInt(); m = in.nextInt();
            for (int i = 0; i < n; i++) {
                x[i] = in.nextInt();
                m -= x[i];
                y[i] = in.nextInt();
                y[i] -= x[i];
            }
            if (m < 0) System.out.println(0);
            else if (m == 0) System.out.println(1);
            else Solve();
        }
    }
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:265620次
    • 积分:7165
    • 等级:
    • 排名:第3134名
    • 原创:432篇
    • 转载:7篇
    • 译文:1篇
    • 评论:74条