首页 > 其他分享 >题解 [ARC184B] 123 Set

题解 [ARC184B] 123 Set

时间:2024-09-23 23:16:46浏览次数:1  
标签:Set return int 题解 ARC184B 杨表 operator Modint friend

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

我们称不包含质因子 \(2\) 和 \(3\) 的数为 \(2,3\texttt{-Free}\) 的。

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

\[\begin{bmatrix} 2^03^0u & 2^03^1u & \cdots & 2^03^qu & \cdots \\ 2^13^0u & 2^13^1u & \cdots & 2^13^qu & \cdots \\ \vdots & \vdots & \ddots & \vdots & \ddots \\ 2^p3^0u & 2^p3^1u & \cdots & 2^p3^qu & \cdots \\ \vdots & \vdots & \ddots & \vdots & \ddots \\ \end{bmatrix} \]

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

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

\[\begin{bmatrix} 1 & 3 & 9 \\ 2 & 6 \\ 4 & 12 \\ \end{bmatrix} \]

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

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

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

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

因此,对一个杨表求答案的时间复杂度 \(O(\log_2(\frac{n}{u})\log_3(\frac{n}{u})\times2^{\log_3(\frac{n}{u})})=O((\frac{n}{u})^{\log_32}\log^2(\frac{n}{u}))=O((\frac{n}{u})^{0.64}\log^2(\frac{n}{u}))\)。

显然,以每个 \(2,3\texttt{-Free}\) 的 \(u\) 作为杨表左上角,列出的元素互不相同。因此,答案即为每个 \(2,3\texttt{-Free}\) 的 \(u\) 作为杨表左上角的答案之和。

显然不能对于每个 \(2,3\texttt{-Free}\) 的 \(u\) 都求一遍答案。动用一下智慧,我们发现有大量的 \(u\) 对应的杨表形状是一样的,答案自然也是一样的。我们懒得考虑具体有多少种杨表形状,但是至少 \(\lfloor\frac{n}{u}\rfloor\) 相等的所有 \(u\) 的杨表形状肯定一样。记忆化一下,如果 \(\lfloor\frac{n}{u}\rfloor\) 与上一个杨表相同就不重复求答案,否则再跑轮廓线 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;
}

标签:Set,return,int,题解,ARC184B,杨表,operator,Modint,friend
From: https://www.cnblogs.com/ruierqwq/p/18428143/arc184b

相关文章

  • NEERC2013题解
    B.BonusCards简单dp一下,记\(f_{ij}\)为前i次有j次分给第一类的概率。最后再算上我在第一类被选上的概率即可。constintN=3005;#defineintlonglongintn,a,b;doublef[N][N],g[N][N];signedmain(void){#ifdefONLINE_JUDGE freopen("bonus.in","r",stdin......
  • 题解:CF888G Xor-MST
    题解:CF888GXor-MST题目大意:给定\(n\)个点的点权,任意两点间边权是点权的异或和。求这张完全图的MST的权值。思路:Boruvka+Trie树+按位贪心。关键就在于如何求出Boruvka中的best数组。考虑对点权建trie树,对于节点\(i\)本轮的连边,就是找“和它最相似”的那......
  • SP1825 FTOUR2 - Free tour II 题解
    题目传送门前置知识点分治|树状数组解法维护点对信息,考虑点分治。本题比luoguP4149[IOI2011]Race多了个前缀查询\(\max\)。套个支持单点修改、区间查询\(\max\)的数据结构即可。直接线段树维护区间\(\max\)貌似会TLE,换成树状数组维护前缀\(\max\)即可。注......
  • LGP3183 题解
    原题链接:P3183[HAOI2016]食物链。难度:Easy。根据定义,食物链是一个DAG,所以可以进行拓扑排序。食物链也就转化成了:图中从一个入度为\(0\)的点到一个出度为\(0\)的点的路径。那么只需要拓扑排序求出所有起点到每个点的路径条数,然后累加出度为\(0\)的点的值即可。需要注......
  • LGP1901 题解
    原题链接:P1901发射站难度:Easy。注意到"最近的且比它高",容易想到用单调栈维护每个能量发射站左右第一个比它高的,最后统计答案即可。具体的令f[i][0/1]表示能量发射站\(i\)右边/左边第一个\(h_x>h_i\)的位置\(x\)。用单调栈从左向右扫一遍,得到f[i][0]。用单调栈从右......
  • [题解] ICPC网络预选赛 2024 第二场 E Escape (含题目翻译)
    [题解]ICPC网络预选赛2024第二场EEscape(含题目翻译)tag:图论、BFS、最短路题干为原文DeepL翻译题目描述Sneaker在一个巨大的迷宫中醒来,现在他想逃离这个迷宫。通过迷宫中每个房间的地图,Sneaker了解了迷宫的结构。迷宫由......
  • 使用SBP打AssetBundle时脚本引用丢失
    1)使用SBP打AssetBundle时脚本引用丢失2)在UE5.3中连接Power节点为何10的3次幂等于10093)如何在Widget中倾斜一张纹理贴图4)如何在打开关卡蓝图时更改游戏模式这是第401篇UWA技术知识分享的推送,精选了UWA社区的热门话题,涵盖了UWA问答、社区帖子等技术知识点,助力大家更全面地掌握和......
  • 【题解】Solution Set - NOIP2024集训Day36 dp 优化 + 状态设计
    【题解】SolutionSet-NOIP2024集训Day36dp优化+状态设计https://www.becoder.com.cn/contest/5550最后一题较难。「NOIP2023」天天爱打卡考虑dp。\(f_{i,j}\):前\(i\)天,到第\(i\)天为止连续打卡\(j\)天。有转移:\[f_{i,0}=\max(f_{i,j})\\f_{i,j}=\max(f_{i......
  • Codeforces Round 972(Div.2)题解
    CodeforcesRound972(Div.2)题解A.SimplePalindrome贪心贪心,尽可能元素数量平均,并且相同字母放在一起。#include<bits/stdc++.h>usingnamespacestd;#definefffirst#definesssecond#definepbpush_back#defineall(u)u.begin(),u.end()#defineendl'\n'#de......
  • Codeforces Round 973 (Div.2) A-E题解
    CodeforcesRound973(Div.2)A-E题解比赛传送门A.Zhan'sBlender数学显然答案为\(\lceil\frac{n}{min(x,y)}\rceil\)。#include<bits/stdc++.h>usingnamespacestd;#definefffirst#definesssecond#definepbpush_back#defineall(u)u.begin(),u.en......