1 条题解

  • 0
    @ 2024-9-27 21:47:37

    @李文瀚:我应该就是代码第 293429 \sim 34 行计算 oo 的逻辑写错了,我重新修改了一下求解 oo 的逻辑就通过了。

    完整代码:

    #include <bits/stdc++.h>
    using namespace std;
    #define int long long
    
    int X, a[22], f[22][2][44][12][2], b[22];
    bool vis[22][2][44][12][2];
    
    // other = 10 没有除了 X 以外的其它数字
    // other = 11 有至少两个除了 X 以外的其它数字
    // 0 <= other <= 9 除了 X 以外的其它数字都是 other 
    int dfs(int p, int zero, int c, int other, bool limit) {
    	if (p < 0) {
    		if (c > 0 + 20 || c == 0 + 20 && other > X)
    			return 1;
    		else
    			return 0;
    	}
    	int &u = f[p][zero][c][other][limit];
    	if (vis[p][zero][c][other][limit])
    		return u;
    	u = 0;
    	vis[p][zero][c][other][limit] = true;
    	int up = limit ? a[p] : 9;
    	for (int i = 0; i <= up; i++) {
    		int cc;
    		if (zero && !i) cc = c;
    		else cc = (i == X) ? c + 1 : c - 1;
    		int oo = other;
    		if (i == X) ;
    		else if (zero && !i) ;
    		else if (i != X) {
    			if (oo == 10) oo = i;
    			else if (oo != i) oo = 11;
    		}
    		b[p] = i;
    		u += dfs(p-1, zero && !i, cc, oo, limit && i == up);
    	}
    	return u;
    }
    
    int _cal(int num) {
    	memset(vis, 0, sizeof vis);
    	memset(f, 0, sizeof f);
    	int p = 0;
    	for (; num; a[p++] = num % 10, num /= 10);
    	return dfs(p-1, true, 20, 10, true);
    }
    
    int cal(int num) {
    	int res = 0;
    	for (X = 0; X <= 9; X++) {
    		res += _cal(num);
    	}
    	return res;
    }
    
    signed main() {
    	int l, r;
    	cin >> l >> r;
    	cout << cal(r) - cal(l-1) << endl;
    	return 0;
    }
    
    • 1

    信息

    ID
    43
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    (无)
    递交数
    4
    已通过
    1
    上传者