题解 ARC171D【Rolling Hash】

UPD:对 \(s_i\) 的定义进行了一些修改,使得结论的得出更加自然。感谢 @233L 对本题解提出的修改建议!

来补题了。昨天赛时想法是对的,代码写错了,没调过太可惜了。

显然 \(P > n\) 时必定有解。

设后缀 \([i,n]\) 的哈希值为 \(s_i\),则区间 \([l,r]\) 的哈希值为 \(B^{r-n}(s_l-s_{r+1})\bmod P\),即 \([l,r]\) 的哈希值不为 \(0\) 的充要条件是 \(s_l\ne s_{r+1}\)。建立一个点的编号为 \(1\sim n+1\) 的图,对于每个输入的区间 \([l,r]\),连接一条边 \((l,r+1)\),问题转化为是否可以用 \(P\) 种颜色将所有点染色,使得每条边两侧颜色不同。

\(f_S\) 表示将点集 \(S\) 的导出子图按以上规则染色,需要的最少颜色数。设 \(g_S\) 表示点集 \(S\) 是否为独立集。有转移:

\[f_S= \begin{cases} 0,&S=\varnothing\\ \min\limits_{T\subseteq S\land g(T)}\{f_{S\setminus T}+1\},&S\ne\varnothing \end{cases} \]

答案即为 \([f_U\le P]\),其中 \(U\) 表示节点的全集。

时间复杂度 \(O(3^n)\)

// Problem: D - Rolling Hash
// Contest: AtCoder - AtCoder Regular Contest 171
// URL: https://atcoder.jp/contests/arc171/tasks/arc171_d
// Memory Limit: 1024 MB
// Time Limit: 2000 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(x > y) 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 N = 17;

int P, B, n, m, G[N][N], f[1 << N], g[1 << N];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> P >> B >> n >> m;
    rep(i, 1, m) {
    	int l, r;
    	cin >> l >> r;
    	G[l - 1][r] = G[r][l - 1] = 1;
    }
    if(P > n) {cout << "Yes" << endl; return 0;}
    const int U = (1 << (n + 1)) - 1;
    rep(S, 0, U) {
    	g[S] = 1;
    	rep(u, 0, n) {
    		if((S >> u) & 1) {
    			rep(v, u + 1, n) {
    				if((S >> v) & 1) {
    					if(G[u][v]) {
    						g[S] = 0;
    					}
    				}
    			}
    		}
    	}
    }
    memset(f, 0x3f, sizeof(f));
    f[0] = 0;
    rep(S, 1, U) {
    	for(int T = S; T; T = (T - 1) & S) {
    		if(g[T]) {
    			chkmin(f[S], f[S ^ T] + 1);
    		}
    	}
    }
    cout << (f[U] <= P ? "Yes" : "No") << endl;
    return 0;
}
posted @ 2024-02-05 16:36  rui_er  阅读(38)  评论(0编辑  收藏  举报