zoukankan      html  css  js  c++  java
  • 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

    inputoutput
    1
    
    10

    题意:求1到109中各位数字之和为s的数有多少个;

    思路1:背包思路;

     1 #include<iostream>
     2 #include<cstdio>
     3 
     4 
     5 using namespace std;
     6 
     7 int dp[2][82]={0};
     8 
     9 
    10 int main()
    11 {
    12 //    freopen("1.txt","r",stdin);
    13     int s;
    14     cin>>s;
    15     int i,j,k;
    16     dp[0][0]=1;
    17     dp[1][0]=1;
    18     for(i=1;i<10;i++)
    19     {
    20         for(j=1;j<=(i*9)&&j<=s;j++)
    21         {
    22             dp[i%2][j]=dp[1-i%2][j];
    23             for(k=1;k<10&&k<=j;k++)
    24                 dp[i%2][j]+=dp[1-i%2][j-k];
    25         }
    26     }
    27     dp[1][1]++;
    28     cout<<dp[1][s]<<endl;
    29     return 0;
    30 }
    View Code

    思路2:递归的深搜;

     1 #include<iostream>
     2 #include<cstdio>
     3 
     4 using namespace std;
     5 
     6 
     7 int ks(int s,int k)
     8 {
     9     if(k==1)
    10     {
    11         if(s>9)
    12             return 0;
    13         return 1;
    14     }
    15     if(s==0)return 1;
    16     int sum=0;
    17     int i;
    18     for(i=0;i<10&&i<=s;i++)
    19     {
    20         sum+=ks(s-i,k-1);
    21     }
    22     return sum;
    23 }
    24 
    25 
    26 int main()
    27 {
    28 //    freopen("1.txt","r",stdin);
    29     int s;
    30     cin>>s;
    31     if(s==1)
    32     {
    33         cout<<ks(s,10)<<endl;
    34         return 0;
    35     }
    36     cout<<ks(s,9)<<endl;
    37     return 0;
    38 }
    View Code
  • 相关阅读:
    怎样创建动态扩容数组?
    怎样终止一个线程?
    模式识别
    基于Linux根据僵尸网络病毒平台《比尔盖茨》
    web.xml在&lt;init-param&gt;一个错误
    DTD学习笔记
    Java排序算法(四):Shell排序
    VC6迁移到VS2008几个问题——良好的代码,从我做起,从现在开始。
    Maven 建 Struts2 基本实现 CURD Controller
    Andy&#39;s First Dictionary
  • 原文地址:https://www.cnblogs.com/zhangchengbing/p/3307824.html
Copyright © 2011-2022 走看看