有什么区别
查看原帖
有什么区别
487539
LCat90楼主2023/9/21 15:14

看了其他讨论没看懂。

70:

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 20;
int l, r, a[N], dp[N][N][N][2][2][2]; // 前 2 个数,是否有 3 个数,8,4
// 0 表示没有,1 表示有 
int dfs(int pos, int lst1, int lst2, bool f1, bool f2, bool f3, bool limit) {
	if(pos == 0) return (f1 and !(f2 and f3));
	if(!limit and dp[pos][lst1][lst2][f1][f2][f3] != -1) return dp[pos][lst1][lst2][f1][f2][f3];
//	if(f2 and f3) return 0;
	int up = (limit ? a[pos] : 9), ans = 0;
	for(int i = 0;i <= up; ++i)
		ans += dfs(pos - 1, lst2, i, f1 or (lst1 == lst2 and lst1 == i), f2 or i == 8, f3 or i == 4, limit and i == up);
	if(!limit) dp[pos][lst1][lst2][f1][f2][f3] = ans;
	return ans;
}
int solve(int x) {	
	memset(dp, -1, sizeof dp);
	int cnt = 0;
	while(x) a[++cnt] = x % 10, x /= 10;
	return dfs(cnt, -1, -1, 0, 0, 0, 1); 
}
signed main() {
	cin >> l >> r;
	cout << solve(r) - solve(l - 1);
	return 0;	
}

100:

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 20;
int l, r, a[N], cnt, dp[N][N][N][2][2][2]; // 前 2 个数,是否有 3 个数,8,4
// 0 表示没有,1 表示有 
int dfs(int pos, int lst1, int lst2, bool f1, bool f2, bool f3, bool limit) {
	if(pos == 0) return (f1 and !(f2 and f3));
	if(!limit and dp[pos][lst1][lst2][f1][f2][f3] != -1) return dp[pos][lst1][lst2][f1][f2][f3];
//	if(f2 and f3) return 0;
	int up = (limit ? a[pos] : 9), ans = 0;
	for(int i = (pos == cnt ? 1 : 0);i <= up; ++i)
		ans += dfs(pos - 1, lst2, i, f1 or (lst1 == lst2 and lst1 == i), f2 or i == 8, f3 or i == 4, limit and i == up);
	if(!limit) dp[pos][lst1][lst2][f1][f2][f3] = ans;
	return ans;
}
int solve(int x) {	
	if(x < 1e10) return 0;
	memset(dp, -1, sizeof dp);
	cnt = 0;
	while(x) a[++cnt] = x % 10, x /= 10;
	return dfs(cnt, -1, -1, 0, 0, 0, 1); 
}
signed main() {
	cin >> l >> r;
	cout << solve(r) - solve(l - 1);
	return 0;	
}
2023/9/21 15:14
加载中...