ural 1353. Milliard Vasya's Function

点击打开链接

1353. Milliard Vasya‘s Function

Time limit: 1.0 second

Memory limit: 64 MB

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 Nth VF
in the point S is an amount of integers from 1 to N that have the sum of digitsS. You seem to be great programmers, so Vasya gave you a task to find the milliard VF value (i.e. the VF with N = 109)
because Vasya himself won’t cope with the task. Can you solve the problem?

Input

Integer S (1 ≤ S ≤ 81).

Output

The milliard VF value in the point S.

Sample

input output
1
10

Problem Author: Denis Musin

Problem Source: USU Junior Championship March‘2005

在1~10^9这些数中,找出各个位数和为s的个数。

dp[i][j]表示位数为i的时候,各个位数之和为j的个数,那么它是由两部分组成,一部分是dp[i-1][j],因为可以在它的低位补0,另一部分是dp[i-1][j-k](1<=k<=9),因为dp[i-1][j-k+k]就是dp[i][j]要求的。

感觉智商被碾压~~

//0.031	206 KB
#include<stdio.h>
#include<string.h>
using namespace std;
int dp[10][107];
int main()
{
    int n,j;
    memset(dp,0,sizeof(dp));
    for(int i=1;i<=9;i++)dp[1][i]++;//从最高位往下找,最高位只能使1~9
    for(int i=2;i<=9;i++)
        for(int j=1;j<=81;j++)
        {
            dp[i][j]=dp[i-1][j];//第一部分
            for(int k=1;k<=9;k++)//第二部分
                if(j-k>0)dp[i][j]+=dp[i-1][j-k];
        }
    while(scanf("%d",&n)!=EOF)
    {
        int sum=0;
        if(n==1)printf("10\n");
        else
        {
            for(int i=1;i<=9;i++)//将各个位数符合条件的加起来
                sum+=dp[i][n];
            printf("%d\n",sum);
        }
    }
    return 0;
}

ural 1353. Milliard Vasya's Function

时间: 2024-10-10 13:34:50

ural 1353. Milliard Vasya's Function的相关文章

Ural 1353 Milliard Vasya&#39;s Function(DP)

题目地址:Ural 1353 定义dp[i][j],表示当前位数为i位时,各位数和为j的个数. 对于第i位数来说,总可以看成在前i-1位后面加上一个0~9,所以状态转移方程就很容易出来了: dp[i][j]=dp[i][j]+dp[i][j-1]+dp[i][j-2]+.......+dp[i][j-9]: 最后统计即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <

递推DP URAL 1353 Milliard Vasya&#39;s Function

题目传送门 1 /* 2 题意:1~1e9的数字里,各个位数数字相加和为s的个数 3 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 4 状态转移方程:dp[i][j] += dp[i-1][j-k],为了不出现负数 5 改为:dp[i][j+k] += dp[i-1][j] 6 */ 7 #include <cstdio> 8 #include <cstring> 9 #include <cmath> 10 #include <algorithm

URAL 1353 Milliard Vasya&#39;s Function DP

题目:click here 分析:dp[i][j] 表示i位数字,当前数字和为j的个数.dp[i][j] = dp[i-1][j] + dp[i-1][j-k] 前面表示在i-1位数后面加零,后面表示在前一位前面加k这个数.(注意k的取值范围). 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int s; 5 int dp[11][83]; 6 int cnt[83]; 7 void pre() { 8 memset( dp, 0,

Ural 1353 Milliard Vasya&amp;#39;s Function(DP)

题目地址:Ural 1353 定义dp[i][j].表示当前位数为i位时,各位数和为j的个数. 对于第i位数来说.总能够看成在前i-1位后面加上一个0~9.所以状态转移方程就非常easy出来了: dp[i][j]=dp[i][j]+dp[i][j-1]+dp[i][j-2]+.......+dp[i][j-9]: 最后统计就可以. 代码例如以下: #include <iostream> #include <cstdio> #include <string> #inclu

timus.1353. Milliard Vasya&#39;s Function 动态规划

题目传送门 题目大意: 输入一个1到81(9*9)的数字s,求在1到109有多少个数字的数位和等于s. 解题思路: 我们很容易知道一位数字等于s的个数.要使一个i位的数字数位和等于s,可以通过一个i-1位的数字后面加上0~9(如果s<9就是0~s).于是很容易得出方程 dp[i][j]=dp[i-1][j-0]+dp[i-2][j-1]+……+dp[i-9][j-9];其中i表示i位的数字,j表示s.dp[i][j]表示一个i位数字数位和为j的方案数. #include <iostream&g

URAL1353——DP——Milliard Vasya&#39;s Function

Description 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?

URAL1353---Milliard Vasya&#39;s Function(简单数位dp)

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

CodeForces - 837E - Vasya&#39;s Function | Educational Codeforces Round 26

/* CodeForces - 837E - Vasya's Function [ 数论 ] | Educational Codeforces Round 26 题意: f(a, 0) = 0; f(a, b) = 1 + f(a, b-gcd(a, b)); 求 f(a, b) , a,b <= 1e12 分析: b 每次减 gcd(a, b) 等价于 b/gcd(a,b) 每次减 1 减到什么时候呢,就是 b/gcd(a,b)-k 后 不与 a 互质 可先将 a 质因数分解,b能除就除,不能

Educational Codeforces Round 26 E - Vasya&#39;s Function

数论题还是好恶心啊. 题目大意:给你两个不超过1e12的数 x,y,定义一个f ( x, y ) 如果y==0 返回 0 否则返回1+ f ( x , y - gcd( x , y ) ); 思路:我们设gcd ( x , y) 为G,那么 设 x  = A*G,y = B*G,我们考虑减去多少个G时x y 的gcd会改变,我们设减去 k个G的时候 x和y 的gcd为改变,即 A*G 和 ( B - k ) * G 的 gcd 改变了,什么情况下会改变呢,就是A 和( B -  k )的gcd