Acm VF

VF

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 2
描述
Vasya is the beginning mathematician. He decided to make an important contribution to the science and to become famous all over the world. But how can he do that if the most interesting facts such as Pythagor’s theorem are already proved? Correct! He is to think out something his own, original. So he thought out the Theory of Vasya’s Functions. Vasya’s Functions (VF) are rather simple: the value of the  N th VF in the point  S is an amount of integers from 1 to  N that have the sum of digits  S. You seem to be great programmers, so Vasya gave you a task to find the milliard VF value (i.e. the VF with  N = 10 9) because Vasya himself won’t cope with the task. Can you solve the problem?
输入
There are multiple test cases.
Integer S (1 ≤ S ≤ 81).
输出
The milliard VF value in the point S.
样例输入
1
样例输出
10
来源
USU Junior Championship March'2005
上传者

李文鑫


题意:1~1000000000之间,各位数字之和等于给定s的数的个数。

每行给出一个数s(1 ≤ s ≤ 81),求出1~10^9内各位数之和与s相等的数的个数。


思路:可以将本问题若干子问题,即给出一个数s,分别求出 i(1<=i<=10)位数满足各位数和等于s的情况,创建数组data[i][j],i=10,1<=j<=81(给出的最大s值为81,即每一位上面的数都为9)。

当s=1时,总个数为10。每位数上面分别为1时。

当s!=1时,初始化数组data[i][j];,i对应的是i(1<=i<=10)位数,j对应的可能给出的s。所以我们只需要将数组填满,最后的个数sum+=data[i][s]个。

例如我们给出的s等于5:

1、只有一位数的时候只有1种,5

2、有两位数的时候有5种,14,23,32,41,50

3、有三位数的时候有15种

4、有四位数的时候有.....

.........

10、有十位数的时候有:495种

所以总个数为sum= 1 + 5+.....+495种

那么为题来了,我们怎么才能知道data[i][j]对应的次数呢。

例如:我们把问题缩小化,我们想知道data[4][5]的次数,即1-10^4内各位数之和与5相等的数的个数.

第一步:求得千位数为1,2,3,4,5时满足的情况

1、当千位数为1时,百位数为4(即1-10^3内各位数之和与4相等的数的个数),百位数为4满足的情况(当百位数为1时,十位数为3即1-10^3内各位数之和与3相等的数的个数)........

2、当千位数为2时,百位数为3(即1-10^3内各位数之和与3相等的数的个数),百位数为3满足的情况(当百位数为1时,十位数为2即1-10^3内各位数之和与2相等的数的个数)........

.......

最后总结得:data[i][j] += data[i][k](1<=i<=10)  (1 <= k <=9*i)     ,


import java.io.BufferedInputStream;
import java.util.Scanner;

public class _269_2_AC_DP {
	public static void main(String[] args) {

		Scanner scan = new Scanner(new BufferedInputStream(System.in));
		int testData, sum = 0;
		int[][] data = new int[10][82];
		for (int i = 1; i < 10; ++i) {
			data[1][i] = 1;
		}
		for (int i = 1; i < 10; ++i) {
			for (int j = 1; j <= i * 9; ++j) {
				for (int k = 0; k <= 9 && k <= j; ++k) {
					data[i][j] += data[i - 1][j - k];
				}
			}
		}
		while (scan.hasNext()) {
			testData = scan.nextInt();
			if (testData == 1) {
				System.out.println(10);
			} else {
				for (int i = 1; i < 10; ++i) {
					sum += data[i][testData];
				}
				System.out.println(sum);
				sum = 0;
			}
		}

	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值