Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static class SumFinder
- {
- static Dictionary<(int, int), long> record = new Dictionary<(int, int), long>();
- public static long CountSums(int n)
- {
- long ret = 0;
- for (int iLength = 1; iLength <= n; iLength++)
- {
- ret += CountSums(iLength, n);
- }
- return ret;
- }
- public static long CountSums(int length, int n)
- {
- if (n <= 0 || length < (n + 8) / 9)
- {
- return 0;
- }
- if (length == 1)
- {
- return 0 < n && n < 10 ? 1 : 0;
- }
- if (record.ContainsKey((length, n)))
- {
- return record[(length, n)];
- }
- long total = 0;
- for (int iLastDigit = 1; iLastDigit < 10; iLastDigit++)
- {
- total += CountSums(length - 1, n - iLastDigit);
- }
- record[(length, n)] = total;
- return total;
- }
- }
Add Comment
Please, Sign In to add comment