답안 #499455

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
499455 2021-12-28T12:52:29 Z s_samchenko Palindrome-Free Numbers (BOI13_numbers) C++17
100 / 100
1 ms 340 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC target ("avx2")
#define ll long long
#define ff first
#define ss second
#define int ll
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
#define pii pair <int, int>
#define pdd pair <double, double>
#define vi vector <int>
using namespace std;
using namespace __gnu_pbds;
template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
const ll inf = 1e12;
const ll mod = 998244353;
const int N = 2e5+100;

int dp[10][10][20][2];
string st;

int calc(int first, int second, int len, bool all){
	if (len >= st.size()) return 1;
	if (dp[first][second][len][all] != -1) return dp[first][second][len][all];
	int help = 0;
	if (all){
		for (int n = 0; n <= 9; ++n)
			if (n != first && n != second) help += calc(second, n, len + 1, 1);
	}
	else{
		int limit = st[len] - '0';
		for (int n = 0; n < limit; ++n){
			if (n != first && n != second) help += calc(second, n, len + 1, 1);
		}
		if (limit != first && limit != second) help += calc(second, limit, len + 1, 0);
	}
	return dp[first][second][len][all] = help;
}

int get(int n){
	if (n < 0) return 0;
	memset(dp, -1, sizeof(dp));
	st = to_string(n);
	int first = st[0] - '0', res = 1;
	for (int i = 1; i <= first; ++i){
		res += calc(i, i, 1, i != first);
	}
	for (int k = 2; k <= st.size(); ++k){
		for (int i = 1; i < 10; ++i) res += calc(i, i, k, 1);
	}
	return res;
}

void solve(){
	int l, r;
	cin >> l >> r;
	cout << get(r) - get(l-1);
}

signed main(){
	ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	int tt = 1;
	//cin >> tt;

	while (tt--){
		solve();
		cout << '\n';
	}
}

Compilation message

numbers.cpp: In function 'long long int calc(long long int, long long int, long long int, bool)':
numbers.cpp:28:10: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |  if (len >= st.size()) return 1;
      |      ~~~~^~~~~~~~~~~~
numbers.cpp: In function 'long long int get(long long int)':
numbers.cpp:53:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |  for (int k = 2; k <= st.size(); ++k){
      |                  ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 300 KB Output is correct
2 Correct 0 ms 308 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 0 ms 296 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 316 KB Output is correct
16 Correct 1 ms 308 KB Output is correct
17 Correct 0 ms 320 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 1 ms 312 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 312 KB Output is correct
4 Correct 1 ms 300 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 312 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 316 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 316 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 316 KB Output is correct
26 Correct 1 ms 292 KB Output is correct
27 Correct 1 ms 316 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 316 KB Output is correct
30 Correct 1 ms 292 KB Output is correct
31 Correct 1 ms 308 KB Output is correct
32 Correct 1 ms 296 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 1 ms 332 KB Output is correct
37 Correct 1 ms 316 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 0 ms 332 KB Output is correct
40 Correct 0 ms 332 KB Output is correct
41 Correct 0 ms 332 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 296 KB Output is correct
44 Correct 1 ms 320 KB Output is correct
45 Correct 1 ms 332 KB Output is correct