ZOJ - 2865 A very easy task

本文介绍了一种使用Java大数处理大规模二项式定理计算的方法,通过递归和记忆化技术优化计算过程,实现高效求解。代码中详细展示了初始化组合数数组、递归求解函数及主函数流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文章学习于 acdreamers

二项式定理:

{ (x+y) }^{ n }={ C }_{ n }^{ 0 }{ x }^{ n }{ y }^{ 0 }+{ C }_{ n }^{ 1 }{ x }^{ n-1 }{ y }^{ 1 }+{ C }_{ n }^{ 2 }{ x }^{ n-2 }{ y }^{ 2 }+--+{ C }_{ n }^{ n-1 }{ x }^{ 1 }{ y }^{ n-1 }+{ C }_{ n }^{ n }{ x }^{ 0 }{ y }^{ n }

 

这个题因为n太大了,所以用java大数写

 

{ (n+1) }^{ k+1 }-{ n }^{ k+1 }={ C }_{ k+1 }^{ 1 }{ n }^{ k }+{ C }_{ k+1 }^{ 2 }{ n }^{ k-1 }+--+{ C }_{ k+1 }^{ k }n+1\\ n\quad =\quad \{ 1,\quad 2,\quad 3,\quad 4...\} \\ { (n+1) }^{ k+1 }-1={ C }_{ k+1 }^{ 1 }\sum _{ i=1 }^{ n }{ { i }^{ k } } +{ C }_{ k+1 }^{ 2 }\sum _{ i=1 }^{ n }{ { i }^{ k-1 } } +--+{ C }_{ k+1 }^{ k }\sum _{ i=1 }^{ n }{ i } +n\\ \sum _{ i=1 }^{ n }{ { i }^{ k } } =\frac { 1 }{ K+1 } [{ (n+1) }^{ k+1 }-({ C }_{ k+1 }^{ 2 }\sum _{ i=1 }^{ n }{ { i }^{ k-1 } } +--+{ C }_{ k+1 }^{ k }\sum _{ i=1 }^{ n }{ i } +n+1)]\\ S(n,k)=\sum _{ i=1 }^{ n }{ { i }^{ k } } \\ S(n,k)=\frac { 1 }{ K+1 } [{ (n+1) }^{ k+1 }-({ C }_{ k+1 }^{ 2 }S(n,k-1)+---+{ C }_{ k+1 }^{ k }S(n,1)+n+1)]\\ S(n,1)=\frac { n(n+1) }{ 2 }

在递归的时候 记忆化一下

import java.math.*;
import java.util.*;
public class Main {
    public static final int N = 105;
    public static final BigInteger FLAG = (BigInteger.ZERO).subtract(BigInteger.ONE); // -1
    public static BigInteger[][] C = new BigInteger[N][N];
    public static BigInteger[] ans = new BigInteger[N];

    public static void Init(){
        for(int i = 0; i < N; i++){
            C[i][0] = C[i][i] = BigInteger.ONE;
            if(i == 0){
                continue;
            }
            for(int j = 1; j < i; j++){
                C[i][j] = C[i-1][j].add(C[i-1][j-1]);
            }
        }
    }
    public static BigInteger slove(BigInteger n, int k){
        if(ans[k].compareTo(FLAG) != 0){
            return ans[k];
        }
        if(k == 1){
            ans[k] = ((n.add(BigInteger.ONE)).multiply(n)).divide(BigInteger.valueOf(2));
            return ans[k];
        }
        BigInteger temp = BigInteger.ONE;
        for(int i = 0; i < k + 1; i++){
            temp = temp.multiply(n.add(BigInteger.ONE));
        }
        temp = temp.subtract(n.add(BigInteger.ONE));
        BigInteger sum = BigInteger.ZERO;
        for(int i = 1; i < k; i++){
            BigInteger t = C[k+1][i+1].multiply(slove(n, k - i));
            sum = sum.add(t);
        }
        ans[k] = (temp.subtract(sum)).divide(BigInteger.valueOf(k+1));
        return ans[k];
    }
    public static void main(String[] args) {
        Init();
        Scanner cin = new Scanner(System.in);
        while (cin.hasNext()){
            BigInteger n = cin.nextBigInteger();
            int k = cin.nextInt();
            for(int i = 0; i < N; i++){
                ans[i] = FLAG;
            }
            System.out.println(slove(n, k));
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值