답안 #283005

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
283005 2020-08-25T08:32:47 Z AKaan37 Palindrome-Free Numbers (BOI13_numbers) C++17
100 / 100
6 ms 640 KB
//Bismillahirrahmanirrahim
//█▀█─█──█──█▀█─█─█
//█▄█─█──█──█▄█─█▄█
//█─█─█▄─█▄─█─█─█─█


#include <bits/stdc++.h>

using namespace std;

typedef long long lo;
typedef pair< lo,lo > PII;

#define fi first
#define se second
#define mp make_pair
#define endl "\n"
#define pb push_back
#define int long long
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FOR for(int i=1;i<=n;i++)
#define mid ((start+end)/2)
#define ort ((bas+son)/2)

const lo inf = 2000000000000000000;
const lo KOK = 100000;
const lo LOG = 30;
const lo li = 500005;
const lo mod = 1000000007;

int n,m,b[li],a[li],k,flag,t,dp[30][6][15][15],aman;
int cev;
string s;
vector<int> v,vv;

inline int add(int x,int y){
	if(x+y>=inf){aman=1;return x+y-inf;}
	return x+y;
}

inline int f(int sira,int al,int last2,int last1){
	int cevv=0;
	if(sira==(int)v.size())return 1;
	if(~dp[sira][al][last2][last1])return dp[sira][al][last2][last1];
	for(int i=0;i<=9;i++){
		if(al==0 && i>v[sira])break;
		if(i==last2 || i==last1)continue;
		if(last1==10 && last2==10 && i==0){
			int yes=0;
			if(i<v[sira])yes=1;
			cevv=add(cevv,f(sira+1,max(yes,al),10,10));
		}
		else{
			int yes=0;
			if(i<v[sira])yes=1;
			cevv=add(cevv,f(sira+1,max(yes,al),last1,i));
		}
	}
	return dp[sira][al][last2][last1]=cevv;
}

inline int f1(int sira,int al,int last2,int last1){
	int cevv=0;
	//~ cout<<sira<<" : : "<<al<<" : : "<<last2<<" : : "<<last1<<endl;
	if(sira==(int)vv.size())return 1;
	if(~dp[sira][al][last2][last1])return dp[sira][al][last2][last1];
	for(int i=0;i<=9;i++){
		if(al==0 && i>vv[sira])continue;
		if(i==last2 || i==last1)continue;
		//~ if(sira==0 && i==0)continue;
		if(last1==10 && last2==10 && i==0){
			int yes=0;
			if(i<vv[sira])yes=1;
			cevv=add(cevv,f1(sira+1,max(yes,al),10,10));
		}
		else{
			int yes=0;
			if(i<vv[sira])yes=1;
			cevv=add(cevv,f1(sira+1,max(yes,al),last1,i));
		}
	}
	return dp[sira][al][last2][last1]=cevv;
}

main(void){
	scanf("%lld %lld",&n,&m);
	if(m-n<=1000000){
		for(int i=n;i<=m;i++){
			int last1=-1,last2=-1;
			flag=0;
			int ii=i;
			while(ii){
				if(ii%10==last2 || ii%10==last1)flag=1;
				last2=last1;
				last1=ii%10;
				ii/=10;
			}
			if(flag==0)cev++;
		}
		printf("%lld\n",cev);
		return 0;
	}
	n--;
	if(n==-1)flag=1;
	while(n){
		v.pb(n%10);
		n/=10;
	}
	while(m){
		vv.pb(m%10);
		m/=10;
	}
	//~ v=vv;
	reverse(v.begin(),v.end());
	reverse(vv.begin(),vv.end());
	//~ for(int i=0;i<(int)vv.size();i++)printf("%lld",vv[i]);
	//~ printf("\n");
	//~ for(int i=0;i<(int)v.size();i++)printf("%lld",v[i]);
	//~ printf("\n");
	memset(dp,-1,sizeof(dp));
	//~ if(n==-1)
	cev=f1(0,0,10,10);
	memset(dp,-1,sizeof(dp));
	if(flag)cev=0;
	//~ cout<<cev<<endl;8
	printf("%lld\n",cev-f(0,0,10,10));
	//~ cout<<aman<<endl;
	return 0;
}

Compilation message

numbers.cpp:85:10: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   85 | main(void){
      |          ^
numbers.cpp: In function 'int main()':
numbers.cpp:86:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   86 |  scanf("%lld %lld",&n,&m);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 6 ms 384 KB Output is correct
4 Correct 3 ms 512 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 6 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 640 KB Output is correct
2 Correct 1 ms 640 KB Output is correct
3 Correct 1 ms 640 KB Output is correct
4 Correct 1 ms 640 KB Output is correct
5 Correct 1 ms 640 KB Output is correct
6 Correct 1 ms 640 KB Output is correct
7 Correct 1 ms 640 KB Output is correct
8 Correct 1 ms 640 KB Output is correct
9 Correct 1 ms 640 KB Output is correct
10 Correct 1 ms 640 KB Output is correct
11 Correct 1 ms 640 KB Output is correct
12 Correct 1 ms 640 KB Output is correct
13 Correct 1 ms 640 KB Output is correct
14 Correct 1 ms 640 KB Output is correct
15 Correct 1 ms 640 KB Output is correct
16 Correct 1 ms 640 KB Output is correct
17 Correct 1 ms 640 KB Output is correct
18 Correct 1 ms 640 KB Output is correct
19 Correct 1 ms 640 KB Output is correct
20 Correct 1 ms 640 KB Output is correct
21 Correct 1 ms 640 KB Output is correct
22 Correct 1 ms 640 KB Output is correct
23 Correct 1 ms 640 KB Output is correct
24 Correct 1 ms 640 KB Output is correct
25 Correct 1 ms 640 KB Output is correct
26 Correct 1 ms 640 KB Output is correct
27 Correct 1 ms 640 KB Output is correct
28 Correct 1 ms 640 KB Output is correct
29 Correct 1 ms 640 KB Output is correct
30 Correct 1 ms 640 KB Output is correct
31 Correct 1 ms 640 KB Output is correct
32 Correct 1 ms 640 KB Output is correct
33 Correct 1 ms 640 KB Output is correct
34 Correct 1 ms 640 KB Output is correct
35 Correct 1 ms 616 KB Output is correct
36 Correct 1 ms 640 KB Output is correct
37 Correct 1 ms 640 KB Output is correct
38 Correct 1 ms 640 KB Output is correct
39 Correct 1 ms 640 KB Output is correct
40 Correct 1 ms 640 KB Output is correct
41 Correct 1 ms 640 KB Output is correct
42 Correct 1 ms 640 KB Output is correct
43 Correct 1 ms 640 KB Output is correct
44 Correct 1 ms 640 KB Output is correct
45 Correct 1 ms 640 KB Output is correct