Division of P1050 numbers in Luogu

Keywords: Java

Division of P1050 numbers in Luogu

Code:

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

public class Main {
    private static int[][] direct={{0,1},{1,0},{-1,0},{0,-1}};
    private static AWriter writer = new AWriter(System.out);
    private static AReader reader = new AReader(System.in);
    private static long n;
    private static double INFINITY=Double.POSITIVE_INFINITY;
    private static int dp[][];
    public static void main(String[] args) throws IOException {
        int n=reader.nextInt();
        int k=reader.nextInt();
        dp=new int[n+1][k+1];
        for (int i=1;i<=n;i++) {dp[i][1]=1;dp[i][0]=1;}
        for (int x=2;x<=k;x++) {dp[1][x]=0;dp[0][x]=0;}
        for(int i=2;i<=n;i++)
            for(int j=2;j<=k;j++) {
                if (i>j) dp[i][j]=dp[i-1][j-1]+dp[i-j][j];
                else dp[i][j]=dp[i-1][j-1];
            }
        writer.println(dp[n][k]);
        writer.close();
        reader.close();
    }
}

class AReader implements Closeable {
    private BufferedReader reader;
    private StringTokenizer tokenizer;

    public AReader(InputStream inputStream) {
        reader = new BufferedReader(new InputStreamReader(inputStream));
        tokenizer = new StringTokenizer("");
    }

    private String innerNextLine() {
        try {
            return reader.readLine();
        } catch (IOException ex) {
            return null;
        }
    }

    public boolean hasNext() {
        while (!tokenizer.hasMoreTokens()) {
            String nextLine = innerNextLine();
            if (nextLine == null) {
                return false;
            }
            tokenizer = new StringTokenizer(nextLine);
        }
        return true;
    }

    public String nextLine() {
        tokenizer = new StringTokenizer("");
        return innerNextLine();
    }

    public String next() {
        hasNext();
        return tokenizer.nextToken();
    }

    public int nextInt() {
        return Integer.valueOf(next());
    }

    public long nextLong() {
        return Long.valueOf(next());
    }

    public double nextDouble() {
        return Double.valueOf(next());
    }

    public BigInteger nextBigInteger() {
        return new BigInteger(next());
    }

    @Override
    public void close() throws IOException {
        reader.close();
    }
}

// Fast writer for ACM By Azure99
class AWriter implements Closeable {
    private BufferedWriter writer;

    public AWriter(OutputStream outputStream) {
        writer = new BufferedWriter(new OutputStreamWriter(outputStream));
    }

    public void print(Object object) throws IOException {
        writer.write(object.toString());
    }

    public void println(Object object) throws IOException {
        writer.write(object.toString());
        writer.write("\n");
    }

    @Override
    public void close() throws IOException {
        writer.close();
    }
}

thinking

I have been stuck for a day and can't read the solution. I feel that I have a little more understanding of dp.

We can describe the problem as putting a ball in a box

For the number of classes, it can be divided into two cases

1. For all boxes containing only one ball

2. For all boxes that do not contain only one ball

In contrast, these two cases can form our state transfer equation.

In the first case, it can be transferred to

k-1 box contains n-1 balls

In the second case, it can be transferred to

Put x-k ball into n boxes

List state transition equations
dp[n][k]=dp[n−1][k−1]+dp[n−k][k] dp[n][k]=dp[n-1][k-1]+dp[n-k][k] dp[n][k]=dp[n−1][k−1]+dp[n−k][k]

Posted by michaelmuller on Thu, 31 Oct 2019 00:50:22 -0700