Ural_1353. milliard Vasya's function (DP)

Source: Internet
Author: User
/* Question: calculate the number of the sum of numbers in the range [1 to 10 ^ 9;
Idea: Define DP [I] [J] (I = 1 to 9, j = 1 to 81), which indicates the sum of the numbers with the bits of I and the sum of the bits of J.
Number. DP [I] [J] = (I-1 bits complement 0) + (J-K {k | 1 <= k <= 9 }). Therefore
The moving equation is
DP [I] [J] = DP [I-1] [J] + sum (DP [I-1] [J-1], DP [I-1] [J-2],..., DP [I-1] [j-9]);

PS: note that S = 1 is 10 instead of 9, because 10 ^ 9 is also included in S = 1.

My code :*/

# Include <iostream>
# Include <fstream>
# Include <cstring>

Using namespace STD;

Int DP [15] [85];

Int main (){
// Fstream CIN ("data. In ");

Int I, J, K;
Memset (DP, 0, sizeof (DP ));
For (I = 1; I <= 9; I ++ ){
DP [1] [I] = 1;
}
For (I = 2; I <10; I ++ ){
For (j = 1; j <= 81; j ++ ){
DP [I] [J] = DP [I-1] [J];
For (k = 1; k <= 9 & J-k> = 0; k ++ ){
DP [I] [J] + = DP [I-1] [J-K];
}
}
}
Int S, ans;
While (CIN> S ){
If (S = 1) {cout <"10 \ n"; continue ;}
For (ANS = 0, I = 1; I <10; I ++ ){
Ans + = DP [I] [s];
}
Cout <ans <Endl;
}
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.