小朋友的球

题目链接

题目描述

@发源于 小朋友最近特别喜欢球。有一天他脑子抽了,从口袋里拿出了 $N$ 个不同的球,想把它们放到 $M$ 个相同的盒子里,并且要求每个盒子中至少要有一个球,他好奇有几种放法,于是尝试编程实现,但由于他天天不好好学习,只会上 B 站看游泳教练,于是他向你求助。

输入格式

多组数据,每行两个数 $N,M$。

输出格式

每组数据一行,表示方案数。

样例 #1

样例输入 #1

1
2
4 2
1 1

样例输出 #1

1
2
7
1

提示

  • 对于 $20%$ 的数据,满足 $N,M \leq 10$;
  • 对于 $100%$ 的数据,满足 $1 \leq N,M \leq 100$,一个测试文件最多有 $10$ 组测试数据。

题解

斯特灵数

BigInteger

因为实在不想写C++高精,或者说我已经不用写C++高精了,所以选择java或者python去解决问题更明智一些。
简单记录一下java中BigInteger的使用方法。
在 Java 中,BigInteger 是不可变的类,这意味着一旦创建了一个 BigInteger 对象,它的值就不能被改变。
BigInteger 类是 Java 中用来表示任意精度整数的类。由于它可以处理比 long 类型更大范围的整数,因此在需要处理大整数计算时非常有用。以下是一些 BigInteger 类的常见用法:

  1. 创建 BigInteger 对象:可以通过构造函数或静态工厂方法来创建 BigInteger 对象。

    1
    2
    BigInteger num1 = new BigInteger("12345678901234567890");
    BigInteger num2 = BigInteger.valueOf(98765432109876543210L);
  2. 常用方法

    • 加法:add(BigInteger val)
    • 减法:subtract(BigInteger val)
    • 乘法:multiply(BigInteger val)
    • 除法:divide(BigInteger val)
    • 取余:remainder(BigInteger val)
    • 绝对值:abs()
    • 比较:compareTo(BigInteger val)
    • 转换为 long 类型:longValue()
    • 转换为字符串:toString()
  3. 与原始数据类型的转换

    1
    2
    3
    int intValue = num1.intValue();
    long longValue = num1.longValue();
    double doubleValue = num1.doubleValue();
  4. 比较两个 BigInteger

    1
    2
    3
    4
    5
    6
    7
    if (num1.compareTo(num2) > 0) {
    // num1 大于 num2
    } else if (num1.compareTo(num2) < 0) {
    // num1 小于 num2
    } else {
    // num1 等于 num2
    }
  5. BigInteger 的不可变性:BigInteger 对象的值是不可变的,因此任何数学运算都会产生一个新的 BigInteger 对象。所以,进行数学运算后,原始的 BigInteger 对象不会改变。

  6. BigInteger 的位运算:BigInteger 类也支持位运算方法,比如 and()or()xor()shiftLeft()shiftRight() 等方法。

以上是一些常见的 BigInteger 类的用法,可以根据具体的需求选择合适的方法来操作 BigInteger 对象。

AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
final static int MAX = 200;
final static BigInteger[][] S = new BigInteger[MAX][MAX];
private static void initS(){
for(int i=1;i<MAX;i++){
S[i][i]=BigInteger.ONE;
S[i][1]=BigInteger.ONE;
for(int j=2;j<i;j++){
S[i][j]=S[i-1][j-1].add(S[i-1][j].multiply(BigInteger.valueOf(j)));
}
}
}
public static void main(String[] args) {
initS();
Scanner sc = new Scanner(System.in);
while(sc.hasNextInt()){
int n = sc.nextInt();
int m = sc.nextInt();
if(n<m) System.out.println(0);
else System.out.println(S[n][m]);
}
sc.close();
}
}