51752023-04-20 17:32:49SleepyOverlordSzivárványszámokcpp17Accepted 45/453ms4668 KiB
#include <vector>
#include <string> 
#include <set> 
#include <map> 
#include <unordered_set>
#include <unordered_map>
#include <queue> 
#include <bitset> 
#include <stack>
#include <list>

#include <numeric> 
#include <algorithm> 
#include <random>
#include <chrono>

#include <cstdio>
#include <fstream>
#include <iostream> 
#include <sstream> 
#include <iomanip>
#include <climits>

#include <cctype>
#include <cmath> 
#include <ctime>
#include <cassert>

using namespace std;

#define ULL unsigned long long
#define LL long long
#define PII pair <int, int>
#define VB vector <bool>
#define VI vector <int>
#define VLL vector <LL>
#define VD vector <double>
#define VS vector <string>
#define VPII vector <pair <int, int> >
#define VVI vector < VI >
#define VVB vector < VB >
#define SI set < int >
#define USI unordered_set <int>
#define MII map <int, int>
#define UMII unordered_map <int, int>
#define MS multiset
#define US unordered_set
#define UM unordered_map
#define UMS unordered_multiset
#define UMM unordered_multimap

#define FORN(i, n) for(int i = 0; i < (n); ++i)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)

#define SZ size()
#define BG begin() 
#define EN end() 
#define CL clear()
#define X first
#define Y second
#define RS resize
#define PB push_back
#define MP make_pair
#define ALL(x) x.begin(), x.end()
#define INS insert
#define ER erase
#define CNT count

//dp[i][j][0/1] - hany szivarvanyszam van, melynek i. szamjegye j es 
//monoton novekszik / legalabb egyszer csokkent szigoruan
vector < vector < vector <ULL> > > dp;

string s;
int n;

int main()
{
	cin >> s;
	n = s.SZ;
	s = "#" + s;
	dp.RS(n + 1, vector < vector <ULL> > (10, vector <ULL> (2)));
	FOR(j, 0, 9) dp[1][j][0] = 1;
	
	FOR(i, 1, n)
		FOR(j, 0, 9)
		{
			FOR(d, 0, j) dp[i][j][0] += dp[i - 1][d][0];
			FOR(d, j, 9) dp[i][j][1] += dp[i - 1][d][1];
			FOR(d, j + 1, 9) dp[i][j][1] += dp[i - 1][d][0];
		}

	ULL sum = 0;
	int digit = s[1] - '0';
	VI a = {0, digit};
	FOR(d, 0, digit - 1) sum += dp[n][d][0] + dp[n][d][1];
	FOR(i, 2, n)
	{
		digit = s[i] - '0';
		a.PB(digit);

		FOR(d, 0, digit - 1)
		{
			a[i] = d;
			bool inc = false, dec = false, ok = true;
			FOR(j, 2, i)
				if (a[j - 1] < a[j]) 
					if (dec) 
					{
						ok = false;
						break;
					}
					else inc = true;
				else
					if (a[j - 1] > a[j]) dec = true;
			if (!ok) continue;
			sum += dp[n + 1 - i][d][0];
			if (!dec) sum += dp[n + 1 - i][d][1];
		}

		a[i] = digit;
	}
	cout << sum;

	return 0;
}
SubtaskSumTestVerdictTimeMemory
base45/45
1Accepted0/03ms1816 KiB
2Accepted0/03ms2060 KiB
3Accepted0/03ms2304 KiB
4Accepted1/12ms2352 KiB
5Accepted1/13ms2600 KiB
6Accepted1/13ms2708 KiB
7Accepted1/13ms2944 KiB
8Accepted1/13ms3128 KiB
9Accepted1/13ms3188 KiB
10Accepted1/13ms3320 KiB
11Accepted1/13ms3436 KiB
12Accepted1/12ms3432 KiB
13Accepted2/23ms3440 KiB
14Accepted2/22ms3444 KiB
15Accepted2/23ms3552 KiB
16Accepted2/23ms3796 KiB
17Accepted1/13ms4008 KiB
18Accepted2/23ms4116 KiB
19Accepted2/23ms4184 KiB
20Accepted2/23ms4204 KiB
21Accepted3/33ms4464 KiB
22Accepted2/23ms4500 KiB
23Accepted2/23ms4448 KiB
24Accepted2/23ms4408 KiB
25Accepted2/23ms4548 KiB
26Accepted2/23ms4556 KiB
27Accepted2/23ms4456 KiB
28Accepted2/23ms4668 KiB
29Accepted2/23ms4460 KiB
30Accepted2/23ms4608 KiB