답안 #363291

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
363291 2021-02-05T12:37:08 Z sofapuden Palindrome-Free Numbers (BOI13_numbers) C++14
100 / 100
1 ms 492 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll fin(ll x){
	if(x == -1)return 0;
	ll dp[11][10][20];
	memset(dp,0,sizeof dp);
	string s = to_string(x);
	if(s.size() == 1)return x+1;
	for(int i = 0; i < s[0]-'0'; ++i){
		dp[10][i][0]++;
	}
	bool ok = 1;
	for(int i = 1; i <(int)s.size(); ++i){
		for(int j = 0; j < 10; ++j){
			for(int k = 0; k < 10; ++k){
				for(int x = 0; x < 10; ++x){
					if((j == x || k == x) && j != k)continue;
					dp[k][x][i]+=dp[j][k][i-1];
				}
			}
		}
		for(int j = 0; j < 10; ++j){
			for(int k = 0; k < 10; ++k){
				if(j == 0){
					dp[10][k][i]+=dp[10][j][i-1];
				}
				else{
					if(j != k)dp[j][k][i]+=dp[10][j][i-1];
				}
			}
		}
		if(ok){
			if(s[i] == s[i-1] || (i > 1 && s[i] == s[i-2]))ok=0;
			for(int j = 0; j < s[i]-'0'; ++j){
				if(j == s[i-1]-'0' || (i > 1 && j == s[i-2]-'0'))continue;
				dp[s[i-1]-'0'][j][i]++;
			}

		}
	}
	ll res = 0;
	for(int i = 0; i < 11; ++i){
		for(int j = 0; j < 10; ++j){
			res+=dp[i][j][s.size()-1];
		}
	}
	if(!ok ||( s[s.size()-1] == s[s.size()-2] || (s.size()>2 && s[s.size()-1] == s[s.size()-3]))){
		res--;
	}
	return res+1;
}

int main(){
	ll a, b; cin >> a >> b;
	cout << fin(b)-fin(a-1) << '\n';
}

# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 1 ms 364 KB Output is correct
37 Correct 1 ms 364 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 1 ms 364 KB Output is correct
40 Correct 1 ms 492 KB Output is correct
41 Correct 1 ms 384 KB Output is correct
42 Correct 1 ms 364 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 1 ms 364 KB Output is correct
45 Correct 1 ms 364 KB Output is correct