题解 [ARC184B] 123 Set

个人认为思维难点相同的三倍经验:P3226 [HNOI2012] 集合选数TFSETS - Triple-Free Sets。区别在于状压 DP 的方法。

我们称不包含质因子 23 的数为 2,3-Free 的。

对于 [1,n] 内每个 2,3-Free 的整数 u,可以列出以下的矩阵:

[2030u2031u203qu2130u2131u213qu2p30u2p31u2p3qu]

我们只保留矩阵中不超过 n 的元素,得到一个杨表。

例如,对于 n=12,u=1 的情况,对应的杨表如下:

[13926412]

先考虑对于一个杨表,如何求出最少的操作次数,使得这个杨表中的所有数都在集合中。

容易发现,一次操作等价于选择一个数,把它、它右边、它下边共三个数覆盖,上述问题转化为最少选择多少个数,能覆盖整个杨表。

考虑轮廓线 DP。设 fi,j,S 表示现在考虑到第 i 行第 j 列的格子(下标从 0 开始),轮廓线每个元素是否被覆盖的状态为 S 的最小操作次数。轮廓线的定义如下图所示。

初始状态 f0,0,0=0,f0,0,S=+ (S0)。对于每个格子 (i,j) 和状态 S,若 S 中表明 (i,j) 已被覆盖,则可以进行一次操作,或者不进行任何操作;若未被覆盖,则必须进行一次操作。容易写出转移方程,可以将 i,j 两维滚掉。

因此,对一个杨表求答案的时间复杂度 O(log2(nu)log3(nu)×2log3(nu))=O((nu)log32log2(nu))=O((nu)0.64log2(nu))

显然,以每个 2,3-Freeu 作为杨表左上角,列出的元素互不相同。因此,答案即为每个 2,3-Freeu 作为杨表左上角的答案之和。

显然不能对于每个 2,3-Freeu 都求一遍答案。动用一下智慧,我们发现有大量的 u 对应的杨表形状是一样的,答案自然也是一样的。我们懒得考虑具体有多少种杨表形状,但是至少 nu 相等的所有 u 的杨表形状肯定一样。记忆化一下,如果 nu 与上一个杨表相同就不重复求答案,否则再跑轮廓线 DP 即可。

不会分析时间复杂度,但一看就不太大,不到 5 秒就跑完了。

另外,官方题解的 DP 部分好像跟我不一样,官方题解似乎用了子集卷积,我并没有特别理解。而我使用轮廓线 DP 可以直接规避子集卷积的知识,做到了相同的复杂度。

代码:

// Problem: B - 123 Set
// Contest: AtCoder - AtCoder Regular Contest 184
// URL: https://atcoder.jp/contests/arc184/tasks/arc184_b
// Memory Limit: 1024 MB
// Time Limit: 8000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//By: OIer rui_er
#include <bits/stdc++.h>
#define rep(x, y, z) for(int x = (y); x <= (z); ++x)
#define per(x, y, z) for(int x = (y); x >= (z); --x)
#define debug(format...) fprintf(stderr, format)
#define fileIO(s) do {freopen(s".in", "r", stdin); freopen(s".out", "w", stdout);} while(false)
#define endl '\n'
using namespace std;
typedef long long ll;

mt19937 rnd(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
int randint(int L, int R) {
    uniform_int_distribution<int> dist(L, R);
    return dist(rnd);
}

template<typename T> void chkmin(T& x, T y) {if(y < x) x = y;}
template<typename T> void chkmax(T& x, T y) {if(x < y) x = y;}

template<int mod>
inline unsigned int down(unsigned int x) {
	return x >= mod ? x - mod : x;
}

template<int mod>
struct Modint {
	unsigned int x;
	Modint() = default;
	Modint(unsigned int x) : x(x) {}
	friend istream& operator>>(istream& in, Modint& a) {return in >> a.x;}
	friend ostream& operator<<(ostream& out, Modint a) {return out << a.x;}
	friend Modint operator+(Modint a, Modint b) {return down<mod>(a.x + b.x);}
	friend Modint operator-(Modint a, Modint b) {return down<mod>(a.x - b.x + mod);}
	friend Modint operator*(Modint a, Modint b) {return 1ULL * a.x * b.x % mod;}
	friend Modint operator/(Modint a, Modint b) {return a * ~b;}
	friend Modint operator^(Modint a, int b) {Modint ans = 1; for(; b; b >>= 1, a *= a) if(b & 1) ans *= a; return ans;}
	friend Modint operator~(Modint a) {return a ^ (mod - 2);}
	friend Modint operator-(Modint a) {return down<mod>(mod - a.x);}
	friend Modint& operator+=(Modint& a, Modint b) {return a = a + b;}
	friend Modint& operator-=(Modint& a, Modint b) {return a = a - b;}
	friend Modint& operator*=(Modint& a, Modint b) {return a = a * b;}
	friend Modint& operator/=(Modint& a, Modint b) {return a = a / b;}
	friend Modint& operator^=(Modint& a, int b) {return a = a ^ b;}
	friend Modint& operator++(Modint& a) {return a += 1;}
	friend Modint operator++(Modint& a, int) {Modint x = a; a += 1; return x;}
	friend Modint& operator--(Modint& a) {return a -= 1;}
	friend Modint operator--(Modint& a, int) {Modint x = a; a -= 1; return x;}
	friend bool operator==(Modint a, Modint b) {return a.x == b.x;}
	friend bool operator!=(Modint a, Modint b) {return !(a == b);}
};

const int LOG2 = 30, LOG3 = 19, N = 1000000000, inf = 0x3f3f3f3f;

int n, btm, lim[LOG2], f[1 << LOG3], g[1 << LOG3], lst, mem, ans;
ll a[LOG2][LOG3];

void init(int x) {
    btm = 0;
    memset(lim, 0, sizeof(lim));
    a[0][0] = x;
    lim[0] = 0;
    rep(j, 1, LOG3 - 1) {
        a[0][j] = a[0][j - 1] * 3;
        if(a[0][j] > n) break;
        lim[0] = j;
    }
    rep(i, 1, LOG2 - 1) {
        a[i][0] = a[i - 1][0] * 2;
        if(a[i][0] > n) break;
        lim[i] = 0;
        ++btm;
        rep(j, 1, LOG3 - 1) {
            a[i][j] = a[i][j - 1] * 3;
            if(a[i][j] > n) break;
            lim[i] = j;
        }
    }
}

int solve() {
    rep(S, 0, (1 << LOG3) - 1) f[S] = +inf;
    f[0] = 0;
    rep(i, 0, btm) {
        rep(j, 0, lim[i]) {
            int U = (1 << (lim[i] + 1)) - 1;
            rep(S, 0, U) g[S] = +inf;
            if(j < lim[i]) {
                rep(S, 0, U) {
                    if((S >> j) & 1) {
                        chkmin(g[S ^ (1 << j)], f[S]);
                        chkmin(g[S | (1 << j) | (1 << (j + 1))], f[S] + 1);
                    }
                    else chkmin(g[S | (1 << j) | (1 << (j + 1))], f[S] + 1);
                }
            }
            else {
                rep(S, 0, U) {
                    if((S >> j) & 1) {
                        chkmin(g[S ^ (1 << j)], f[S]);
                        chkmin(g[S | (1 << j)], f[S] + 1);
                    }
                    else chkmin(g[S | (1 << j)], f[S] + 1);
                }
            }
            rep(S, 0, U) f[S] = g[S];
        }
    }
    return *min_element(f, f + (1 << (lim[btm] + 1)));
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n;
    rep(i, 1, n) {
        if(i % 2 != 0 && i % 3 != 0) {
            if(n / i != lst) {
                lst = n / i;
                init(i);
                ans += (mem = solve());
            }
            else ans += mem;
        }
    }
    cout << ans << endl;
    return 0;
}
posted @   rui_er  阅读(56)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示