답안 #806758

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
806758 2023-08-04T09:35:59 Z CESxRhino Palindrome-Free Numbers (BOI13_numbers) C++14
100 / 100
1 ms 372 KB
#include <bits/stdc++.h>
#define ii pair<int,int>
#define f first
#define s second
#define pb push_back
#define ll long long
#define endl '\n'
using namespace std;
const ll MAXN = 1e18;
ll mod = 1e9 + 7;
ll a[100005];
ll dp[20][2][11][11][2];
string s;
ll Calc(int pos,bool check,int digit1,int digit2,bool start)
{
	if(pos == s.size())
	{
		return 1;
	}
	ll ans = 0;
	if(dp[pos][check][digit1 + 1][digit2 + 1][start] != -1)
	{
		return dp[pos][check][digit1 + 1][digit2 + 1][start];
	}
	ll maxdigit = 0;
	if(check)
	{
		maxdigit = s[pos] - '0';
	}
	else
	{
		maxdigit = 9;
	}
	if(!start)
	{
		for(ll i = 0;i <= maxdigit;i++)
		{
			bool newcheck = check & (i == maxdigit);
			ll newdigit1 = i;
			ll newdigit2 = digit1;
			if(i == 0)
			{
				newdigit1 = -1;
			}
			ll newstart = 0;
			if(i != 0)
			{
				start = 1;
			}
			else
			{
				start = 0;
			}
			ans += Calc(pos + 1,newcheck,newdigit1,newdigit2,start);
		}
	}
	else
	{
		for(ll i = 0;i <= maxdigit;i++)
		{	
			if(i == digit1 or i == digit2)
			{
				continue;
			}
			bool newcheck = check & (i == maxdigit);
			ll newdigit1 = i;
			ll newdigit2 = digit1;
			ans += Calc(pos + 1,newcheck,newdigit1,newdigit2,start);
		}
	}
	return dp[pos][check][digit1 + 1][digit2 + 1][start] = ans;
}
ll ans(ll a)
{
	if(a < 0)
	{
		return 0;
	}
	memset(dp,-1,sizeof(dp));
	s = to_string(a);
	return Calc(0,1,-1,-1,0);
}
void AcSolution()
{
	ll a,b;
	cin >> a >> b;
	cout << ans(b) - ans(a - 1) << endl;
}
signed main()
{
//	freopen("BDIGIT.inp", "r",stdin);
//    freopen("BDIGIT.out", "w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t = 1;
//	cin >> t;
	while(t--)
	{
	 	AcSolution();
	}
}


Compilation message

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