ZZUOJ-1196-Monotonic (subject D, DP, the seventh ACM College Student Program Design Competition of Zhengzhou University)
1196: Monotonic number Time Limit: 2 Sec Memory Limit: 128 MB
Submit: 35 Solved: 9
[Submit] [Status] [Web Board] Description
For a positive integer x, if each digit of x is not greater than the number on the right side of x, then x is an incremental number, for example, 112,4557, 18899,111.
Similarly, if each digit of x is no less than the number on the Right of x, x is called a descending number, for example, 986, 6331, and 77311,111.
The increasing and decreasing numbers are collectively called monotonic numbers. (111 is both an increasing number and a decreasing number, so 111 must be a monotonic number)
Input
Multiple Input groups exist.
Enter n for each group. (N <= 100)
Output
For n in each group of input data, the number of monotonic numbers with an output value smaller than 10 n.
Sample Input610Sample output12954257032hint
Source
The seventh ACM College Student Program Design Competition of Zhengzhou University
Idea: DP
AC code:
# Include
# Include
# Include
# Include # define LL long longusing namespace std;/* dpi [n] [m] indicates adding the number m (0 .... 9) The number of monotonically incrementing dpd [n] [m] indicates the n-digit number m (0 .... 9) The number of monotonically decreasing components */LL dpi [105] [10]; LL dpd [105] [10]; void init () {for (int I = 1; I <10; I ++) dpi [1] [I] = dpd [1] [I] = 1; for (int I = 2; I <= 100; I ++) {for (int j = 1; j <10; j ++) {for (int k = j; k <10; k ++) dpi [I] [j] + = dpi [I-1] [k]; for (int k = j; k> = 1; k --) dpd [I] [j] + = dpd [I-1] [k]; dpd [I] [j] + = 1; // Add ,..., 90,100,200 ,..., 900 ,... and so on }}int main () {init (); int n; while (cin> n) {LL ans = 0; for (int j = 1; j <= n; j ++) {for (int I = 0; I <10; I ++) ans + = dpi [j] [I] + dpd [j] [I]; ans-= 9; // subtract the number of duplicates of increment and decrease numbers, for example, 11,22 ,..., numbers like 99} printf ("% lld \ n", ans); // mom, % I64d cannot be used here on oj, WA once} return 0 ;}