URAL 1036. Lucky Tickets(dp+大数)

18 篇文章 0 订阅

题目很水,就是得用到大数,用Java过的啊、、

就是给你n,s。判断前面n个数和后面n个数的和为s/2的情况有多少种。注意s为奇数输出0.

1036. Lucky Tickets

Time limit: 2.0 second
Memory limit: 64 MB
You are given a number 1 ≤  N ≤ 50. Every ticket has its 2 N-digit number. We call a ticket lucky, if the sum of its first  N digits is equal to the sum of its last  N digits. You are also given the sum of ALL digits in the number. Your task is to count an amount of lucky numbers, having the specified sum of ALL digits.

Input

Two space-separated numbers:  N and  S. Here  S is the sum of all digits. Assume that 0 ≤  S ≤ 1000.

Output

The amount of lucky tickets.

Sample

input output
2 2
4

Hint

The tickets are 0101, 0110, 1001, 1010 in the example above
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
	public static void main(String agrs[]) {
		Scanner cin = new Scanner(System.in);
		BigInteger dp[][] = new BigInteger[101][1001];
		int i, j, k;
		for (i = 0; i <= 100; i++) {
			for (j = 0; j <= 1000; j++)
				dp[i][j] = BigInteger.valueOf(0);
		}
		dp[0][0] = BigInteger.valueOf(1);
		for (i = 1; i <= 51; i++) {
			for (j = 0; j <= i * 10; j++) {
				for (k = 0; k <= 9; k++) {
					if (dp[i - 1][j].compareTo(BigInteger.valueOf(0)) != 0) {
						dp[i][j + k] = dp[i][j + k].add(dp[i - 1][j]);
					}
				}
			}
		}
		int n, s;
		while (cin.hasNext()) {
			n = cin.nextInt();
			s = cin.nextInt();
			if (s % 2 == 1)
				System.out.println("0");
			else
				System.out.println(dp[n][s / 2].multiply(dp[n][s / 2]));
		}
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值