简单组合数学(一)

本文围绕组合数学基础应用展开,涵盖穿搭方案计数、刷题统计中的排列组合建模、墙壁划线的分隔问题、[HNOI2008]越狱这一经典容斥原理题目,以及前缀平方和序列的构造与计数。所有问题均体现组合计数、容斥原理、递推与模运算等核心算法思想,适用于算法竞赛与编程实践。

一、穿搭大挑战

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        while (T-- > 0) {
            long a = sc.nextLong();
            long b = sc.nextLong();
            long c = sc.nextLong();
            long ans = a*b + a*c + b*c;
            System.out.println(ans);
        }
    }
}

运行结果

二、刷题统计

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        while (T-- > 0) {
            long n = sc.nextLong();
            long a = sc.nextLong();
            long b = sc.nextLong();
            long c = sc.nextLong();
            long d = sc.nextLong();
            long x = (a + b + c - d - n) / 2;
            System.out.println(x);
        }
    }
}

运行结果

三、墙壁划线

import java.util.Scanner;

public class Main {
    public static long gcd(long a, long b) {
        while (b != 0) {
            long t = a % b;
            a = b;
            b = t;
        }
        return a;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long a = sc.nextLong();
        long b = sc.nextLong();
        long x = sc.nextLong();
        long y = sc.nextLong();
        long g = gcd(a, b);
        long single = a + b - g + 1;
        long ans;
        if (a % 2 == 1 && b % 2 == 1) {
            ans = 2 * single;
        } else {
            ans = 2 * single - 1;
        }
        System.out.println(ans);
    }
}

运行结果

四、[HNOI2008]越狱

import java.util.Scanner;

public class Main {
    static final long P = 100003L;

    // 快速幂:a^b mod modu
    static long pow(long a, long b, long modu) {
        long res = 1;
        a %= modu;
        while (b > 0) {
            if (b % 2 == 1) {
                res = res * a % modu;
            }
            a = a * a % modu;
            b /= 2;
        }
        return res;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long M = sc.nextLong();
        long N = sc.nextLong();

        long mMod = M % P;
        long m1Mod = (M - 1) % P;
        if (m1Mod < 0) m1Mod += P;

        long total = pow(mMod, N, P);
        long safe = mMod * pow(m1Mod, N - 1, P) % P;

        long ans = (total - safe + P) % P; // 加P防止负数
        System.out.println(ans);
    }
}

运行结果

五、前缀平方和序列

import java.util.Scanner;

public class Main {
    static final int MOD = 1000000007;
    static final int MAX = 1000; // t最大1000

    static long[] fac = new long[MAX + 1];
    static long[] invfac = new long[MAX + 1];

    static long pow(long a, long b) {
        long res = 1;
        while (b > 0) {
            if (b % 2 == 1) res = res * a % MOD;
            a = a * a % MOD;
            b >>= 1;
        }
        return res;
    }

    static void pre() {
        fac[0] = 1;
        for (int i = 1; i <= MAX; i++) {
            fac[i] = fac[i - 1] * i % MOD;
        }
        invfac[MAX] = pow(fac[MAX], MOD - 2);
        for (int i = MAX - 1; i >= 0; i--) {
            invfac[i] = invfac[i + 1] * (i + 1) % MOD;
        }
    }

    static long C(int a, int b) {
        if (a < 0 || b < 0 || a < b) return 0;
        return fac[a] * invfac[b] % MOD * invfac[a - b] % MOD;
    }

    public static void main(String[] args) {
        pre();
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int x = sc.nextInt();
        int t = (int) Math.sqrt(x);
        long ans = C(t, n);
        System.out.println(ans);
    }
}

运行结果

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值