67532023-12-18 19:09:43111Szivárványszámokcpp17Accepted 45/453ms3824 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define double long double

#define pii pair<int, int>

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifdef CB
	freopen("be2.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);
#endif

	int a[80][10] = {};
	int b[80][10] = {};
	a[0][0] = 1;
	b[0][0] = 1;
	for (int i = 1; i < 80; i++) {
		for (int j = 0; j < 10; j++) {
			a[i][j] += a[i - 1][j];
			for (int k = 0; k < j; k++) {
				a[i][j] += a[i - 1][k];
				b[i][j] += a[i - 1][k];
			}
		}
	}
	
	string S;
	cin >> S;
	int N = S.size();
	S += '1';
	for (char& c : S) {
		c -= '0';
	}
	
	int ans = 0;
	
	// last max is i
	for (int i = 0; i < N && (i == 0 || S[i] >= S[i - 1]); i++) {
		// top is i
		// S[i + 1] = j
		for (int j = 0; j < S[i] && j < S[i + 1]; j++) {
			ans += a[N - (i + 1)][j];
		}
		if (S[i + 1] < S[i]) {
			// last max is actually j
			for (int j = i + 1; j < N && S[j] <= S[j - 1]; j++) {
				for (int k = 0; k <= S[j] && k < S[j + 1]; k++) {
					ans += a[N - (j + 1)][k];
				}
			}
		}
		// S[i + 1] = j
		for (int j = S[i]; j < S[i + 1]; j++) {
			// top is i + 1
			ans += b[N - (i + 1)][j];
			// top is k
			for (int k = i + 2; k < N; k++) {
				// S[k] = l
				for (int l = j; l < 10; l++) {
					ans += a[k - (i + 1)][l - j] * b[N - k][l];
				}
			}
		}
	}

	// S[0] is already not max
	// S[0] is i
	for (int i = 0; i < S[0]; i++) {
		// top is 0
		ans += b[N][i];
		// top is j
		for (int j = 1; j < N; j++) {
			// S[j] = k
			for (int k = i; k < 10; k++) {
				ans += a[j][k - i] * b[N - j][k];
			}
		}
	}
	
	ans++;
	
	int j = 0;
	while (j < N && (j == 0 || S[j] >= S[j - 1])) j++;
	while (j < N && (j == 0 || S[j] <= S[j - 1])) j++;
	
	ans -= j == N;
	
	cout << ans << '\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
base45/45
1Accepted0/03ms1840 KiB
2Accepted0/03ms2028 KiB
3Accepted0/03ms2248 KiB
4Accepted1/13ms2456 KiB
5Accepted1/13ms2676 KiB
6Accepted1/13ms2892 KiB
7Accepted1/13ms3088 KiB
8Accepted1/13ms3192 KiB
9Accepted1/13ms3404 KiB
10Accepted1/13ms3492 KiB
11Accepted1/13ms3488 KiB
12Accepted1/13ms3492 KiB
13Accepted2/23ms3484 KiB
14Accepted2/23ms3492 KiB
15Accepted2/23ms3488 KiB
16Accepted2/23ms3612 KiB
17Accepted1/13ms3692 KiB
18Accepted2/23ms3792 KiB
19Accepted2/23ms3784 KiB
20Accepted2/23ms3800 KiB
21Accepted3/33ms3804 KiB
22Accepted2/23ms3804 KiB
23Accepted2/23ms3804 KiB
24Accepted2/23ms3800 KiB
25Accepted2/23ms3800 KiB
26Accepted2/23ms3804 KiB
27Accepted2/23ms3808 KiB
28Accepted2/23ms3820 KiB
29Accepted2/23ms3824 KiB
30Accepted2/23ms3812 KiB