-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathNY10E.cs
68 lines (61 loc) · 2.34 KB
/
NY10E.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
// https://www.spoj.com/problems/NY10E/ #dynamic-programming-2d
// Calculates how many non-decreasing digit strings exist for the given length.
public static class NY10E
{
private static long[,] _endingDigitCounts = new long[65, 10];
// Note that for any non-decreasing digit string of length n, it's prefixed
// by a non-decreasing digit string of length n - 1. We need to figure out a way
// to build up the non-decreasing digit strings of length n using the digit
// strings of length n - 1.
// For strings of length 1, obviously there are 9 non-decreasing digit strings,
// one for each digit. For strings of length 2, consider the ending digit 0. It
// can only be appended to 0. The ending digit 1 can be appended to 0 or 1. The
// ending digit 3 can be appened to 0, 1, or 2.
// To create digit strings of length n, the ending digit d can be appened to
// strings of length n - 1 with ending digits 0 through d. If we know the
// ending digit counts for n - 1, we can figure out the ending digit counts for
// n, and summing the ending digit counts gives us the total string count.
static NY10E()
{
for (int digit = 0; digit <= 9; ++digit)
{
_endingDigitCounts[1, digit] = 1;
}
for (int stringLength = 2; stringLength <= 64; ++stringLength)
{
for (int digit = 0; digit <= 9; ++digit)
{
for (int d = 0; d <= digit; ++d)
{
_endingDigitCounts[stringLength, digit]
+= _endingDigitCounts[stringLength - 1, d];
}
}
}
}
public static long Solve(int stringLength)
{
long stringCount = 0;
for (int digit = 0; digit <= 9; ++digit)
{
stringCount += _endingDigitCounts[stringLength, digit];
}
return stringCount;
}
}
public static class Program
{
private static void Main()
{
int remainingTestCases = int.Parse(Console.ReadLine());
while (remainingTestCases-- > 0)
{
string[] line = Console.ReadLine().Split();
int testNumber = int.Parse(line[0]);
int stringLength = int.Parse(line[1]);
Console.WriteLine(
$"{testNumber} {NY10E.Solve(stringLength)}");
}
}
}