URAL 1036 Lucky Tickets

URAL_1036

    用f[i][j]表示递推到第i位时数字和为j的方案数,最后用下乘法原理f[N][S/2]*f[N][S/2]就是最后结果。

    由于结果比较大,所以需要高精度。

import java.math.BigInteger;

import java.util.Scanner;



public class Main {

    static int N, S;

    static BigInteger[][] f = new BigInteger[60][510];

    public static void main(String[] args) {

        Scanner cin = new Scanner(System.in);

        while(cin.hasNext())

        {

            N = cin.nextInt();

            S = cin.nextInt();

            if(S % 2 == 1)

                System.out.println("0");

            else

                solve();

        }

    }

    static void solve()

    {

        int i, j, k;

        BigInteger sum;

        S /= 2;

        for(i = 1; i <= S; i ++)

            f[0][i] = new BigInteger("0");

        f[0][0] = new BigInteger("1");

        for(i = 1; i <= N; i ++)

        {

            sum = new BigInteger("0");

            k = 0;

            for(j = 0; j <= S; j ++)

            {

                if(j - k > 9)

                {

                    sum = sum.add(f[i - 1][k].negate());

                    ++ k;

                }

                sum = sum.add(f[i - 1][j]);

                f[i][j] = sum;

            }

        }

        System.out.println(f[N][S].multiply(f[N][S]));

    }

}

你可能感兴趣的:(c)