历届试题 公式求值
时间限制:1.0s 内存限制:256.0MB
- 锦囊1
- 组合公式。
问题描述
输入n, m, k,输出下面公式的值。
其中C_n^m是组合数,表示在n个人的集合中选出m个人组成一个集合的方案数。组合数的计算公式如下。输入格式
输入的第一行包含一个整数n;第二行包含一个整数m,第三行包含一个整数k。
输出格式
计算上面公式的值,由于答案非常大,请输出这个值除以999101的余数。
样例输入
3
1
3样例输出
162
样例输入
20
10
10样例输出
359316
数据规模和约定
对于10%的数据,n≤10,k≤3;
对于20%的数据,n≤20,k≤3;
对于30%的数据,n≤1000,k≤5;
对于40%的数据,n≤10^7,k≤10;
对于60%的数据,n≤10^15,k ≤100;
对于70%的数据,n≤10^100,k≤200;
对于80%的数据,n≤10^500,k ≤500;
对于100%的数据,n在十进制下不超过1000位,即1≤n<10^1000,1≤k≤1000,同时0≤m≤n,k≤n。提示
999101是一个质数;
当n位数比较多时,绝大多数情况下答案都是0,但评测的时候会选取一些答案不是0的数据;
参考链接:https://blog.csdn.net/u010836847/article/details/21166725?utm_source=copy
import java.math.BigInteger;
import java.util.Scanner;
public class L_20END {
/***
* @author 林梵
*/
public static BigInteger lucas(BigInteger n,BigInteger m,BigInteger p){
if(m.equals(BigInteger.ZERO)) return BigInteger.ONE;
return BigInteger.valueOf(f(n.mod(p).longValue(),m.mod(p).longValue())).multiply(lucas(n.divide(p),m.divide(p),p)).mod(p);
}
public static long f(long n,long m){
if(m>n) return 1;
if(n==m|| m==0) return 1;
if(m>n-m) m=n-m;
long tmpi=1,tmpn=1,s1=1,s2=1,ans=1;
for (int i = 1; i<=m; i++) {
tmpi=i;
tmpn=n-i+1;
s1=s1*tmpi%999101;
s2=s2*tmpn%999101;
}
ans = s2*pow1(s1,999099)%999101;
return ans%999101;
}
public static long pow1(long x,long n) {
if(x==1) return 1;
if (n==0)
return 1;
else {
while ((n & 1)==0) {
n>>=1;
x=(x *x)%999101;
}
}
long result = x%999101;
n>>=1;
while (n!=0) {
x=(x *x)%999101;;
if ((n & 1)!=0)
result =result*x%999101;
n>>=1;
}
return result;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
BigInteger n = new BigInteger(sc.nextLine());
BigInteger m = new BigInteger(sc.nextLine());
int k = Integer.parseInt(sc.nextLine());
long start = System.currentTimeMillis();
BigInteger md = new BigInteger("999101");
long Cnm=lucas(n, m,md).longValue()%999101;
long sum = 0;
if(Cnm!=0){
int[][] a = new int[k][k];
int h = 1;
for (int i = 0; i < k; i++) {
for (int j = 0; j < k; j++) {
if (j >= h)
a[i][j] =0;
else {
if (j == 0 || j == h - 1)
a[i][j] = 1;
else {
a[i][j] = (a[i - 1][j - 1]*(h - j)+a[i - 1][j])%999101;
}
}
}
h++;
}
long m1 = 1,n1 =1;
long x=n.subtract(new BigInteger(k+"")).mod(md.subtract(BigInteger.ONE)).longValue();
long n3 = pow1(2,x);
for (int i = k - 1; i >= 0; i--) {
n1=n3*pow1(2,i)%999101;
m1 = m1*(n.subtract(new BigInteger((k - 1 - i) + "")).mod(md).longValue())%999101;
sum = (sum+m1*a[k - 1][i]*n1)%999101;
}
sum = sum*Cnm%999101;
}
System.out.println(sum);
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}