nowcoder 2020 多校 第三场

nowcoder 2020 多校 第三场 E Two Matchings 题意 可以简化题意为,每个点有个权重$w$,两个点$i,j$相连的代价$abs(w_i-w_j)$,找两个没有重叠的匹配使得代价最小 Solution 其实就是在将数字用长度为偶数的环连起来,求最小代价。进一步可以发现,环用长度为4或6,长度更长的环可以被分解达到更小的代价 LL n; LL a[MAX]; LL dp[MAX][2]; LL four(const vector<LL> &a){ return 2LL * (a[3]-a[0]); } LL six(const vector<LL> &a){ return 2LL * (a[5]-a[0]); } int main(){ #ifdef DEBUG freopen("in", "r", stdin); #endif int T; scanf("%d", &T); while (T--) { cin >> n; for(int i = 0; i < n; i++) cin >> a[i]; sort(a, a+n); for(LL i = 0; i <= n; i++ ) dp[i][0] = dp[i][1] = __64inf; dp[3][0] = 2LL * (a[3]-a[0]); dp[5][0] = 2LL * (a[5]-a[0]); dp[7][0] = 2LL * (a[7]-a[4] + a[3] - a[0]); for(LL i = 9; i < n; i+=2){ dp[i][0] = min(dp[i-4][0], dp[i-4][1]) + four(vector<LL>({a[i-3], a[i-2], a[i-1], a[i]})); dp[i][1] = min(dp[i-6][0], dp[i-6][1]) + six(vector<LL>({a[i-5], a[i-4], a[i-3], a[i-2], a[i-1], a[i]})); } printf("%d\n", min(dp[n-1][0], dp[n-1][1])); } } F Fraction Construction Problem 题意 ...

📝 August 11, 2020&nbsp;·&nbsp;⌛ 3 min

nowcoder 2020 多校 第五场

nowcoder 2020 多校 第五场 https://ac.nowcoder.com/acm/contest/5670 B Graph tire树,最小生成树 题意: 给一个带有边权的树,可以删除或添加边,但要保证: 图联通 环上边权异或为0 Solution 不管怎么操作,两点路径上边权的异或值是固定的,于是问题就转化成一个最小生成树问题。每次选出不联通的点集$S_1$, $S_2$, 将他们联通的代价是 $$ \min\limits_{u\in S_1 v\in S_2} {\mathord{dis}(u,v)} $$ 可以通过tire树实现这个过程,就是tire树合并子节点,复杂度为$O(n\log n)$ #include <cstdio> #include <stack> #include <set> #include <cmath> #include <map> #include <time.h> #include <vector> #include <iostream> #include <string> #include <cstring> #include <algorithm> #include <memory.h> #include <cstdlib> #include <queue> #include <iomanip> #include <bitset> #include <unordered_map> #include <assert.h> #define P pair<int, int> #define LL long long #define LD long double #define PLL pair<LL, LL> #define mset(a, b) memset(a, b, sizeof(a)) #define rep(i, a, b) for (int i = a; i < b; i++) #define PI acos(-1.0) #define random(x) rand() % x #define debug(x) cout << #x << " " << x << "\n" using namespace std; const int inf = 0x3f3f3f3f; const LL __64inf = 0x3f3f3f3f3f3f3f3f; #ifdef DEBUG const int MAX = 1e3 + 50; #else const int MAX = 3e6 + 50; #endif const int mod = 10007; LL pow2[100]; vector<P> G[MAX]; int dis[MAX]; struct Tire{ int nex[MAX][2]; int siz[MAX]; int tot ; LL ans; void init(){ ans = 0; siz[0] = 0; mset(nex[0], 0); tot = 1; } void add(int x, int d) { int rt = 0; for(int i = d; i >= 0; i--){ int e = x & pow2[i] ? 1 : 0; if(!nex[rt][e]) { mset(nex[tot], 0); siz[tot] = 0; nex[rt][e] = tot ++; } rt = nex[rt][e]; siz[rt] ++; } } LL query(int val, int u, int d) { int rt = u; LL res = val; for(int i = d; i >= 0; i--){ if(res & pow2[i]) { if(nex[rt][1]) { res ^= pow2[i]; rt = nex[rt][1]; } else rt = nex[rt][0]; } else { if(nex[rt][0]) { rt = nex[rt][0]; } else { res ^= pow2[i]; rt = nex[rt][1]; } } } return res; } LL merge(int l, int r, int val, int d, int row_d){ if(!nex[l][0] and !nex[l][1]) { return query(val, r, row_d); } LL res = inf; if(nex[l][0]) res = min(res, merge(nex[l][0], r, val, d-1, row_d)); if(nex[l][1]) res = min(res, merge(nex[l][1], r, val^pow2[d], d-1, row_d)); return res; } void dfs(int u, int val, int d) { if(nex[u][0]) dfs(nex[u][0], val, d-1); if(nex[u][1]) dfs(nex[u][1], val ^ pow2[d], d-1); if(!nex[u][0] or !nex[u][1]) return; int l = nex[u][0], r = nex[u][1]; if(siz[l] > siz[r]) swap(l, r); ans += 1LL * merge(l, r, 0, d-1, d-1) + pow2[d]; } }tire; void dfs(int u, int p, int d){ dis[u] = d; for(int i = 0; i < G[u].size(); i++){ int v = G[u][i].first; if(v == p) continue; dfs(v, u, d^G[u][i].second); } } int main(){ #ifdef DEBUG freopen64("in", "r", stdin); #endif pow2[0] = 1; for(int i = 1; i < 32; i++) pow2[i] = pow2[i-1] << 1; int n; scanf("%d", &n); for(int i = 1; i < n; i++) { int u, v, c; scanf("%d%d%d", &u, &v, &c); u++, v++; G[u].emplace_back(v, c); G[v].emplace_back(u, c); } dfs(1, -1, 0); tire.init(); for(int i = 1; i <= n; i++) tire.add(dis[i], 29); tire.dfs(0, 0, 29); printf("%lld\n", tire.ans); return 0; } D Drop Voicing 最长上升子序列 ...

📝 August 7, 2020&nbsp;·&nbsp;⌛ 4 min

POJ 3376

POJ 3376 tire + manacher 题意 http://poj.org/problem?id=3376 给$n$个串,两两连接,一共$n^2$种,求其中有多少是回文, 字符串的长度和小于$2e6$ Solution 在考虑第$i$个串$t$和多少个串$s$拼接是回文时,计算$s+t$是回文的数量,将所有数量累加就是最终的回文数。 首先考虑什么情况下,两个串拼接会是一个回文串。 case 1: s=reverse(t) $$ \begin{cases} s = a_0\dots a_{n-1} \newline t = a_{n-1}\dots a_0 \end{cases} $$ case 2 $$ \begin{cases} \begin{matrix} &\mathrm{palindrome} \newline s = a_0\dots a_i &\overbrace{a_{i+1}\dots a_{n-1}} \end{matrix} \newline \newline t = a_i\dots a_0 \end{cases} $$ 以及对称的情况 $$ \begin{cases} s = a_{n-1}\dots a_i \newline \begin{matrix} \ \ \ \ \ \mathrm{palindrome} &\newline t = \overbrace{a_0\dots a_{i-1}} &a_i\dots a_{n-1} \end{matrix} \end{cases} $$ ...

📝 August 5, 2020&nbsp;·&nbsp;⌛ 4 min

nowcoder 2020 多校 第二场

nowcoder 2020 多校 第二场 https://ac.nowcoder.com/acm/contest/5667 A All with Pairs 题意 给$n$个串,定义$f(s,t)$为$s$前缀和$t$后缀最长的长度,求$\sum_i\sum_j f(s_i, s_j)^2$ Solution 先把每个串的后缀hash的值存下来,对每个串$s_i$,求$\sum_j f(s_i, s_j)^2$。对于$s_i$的每个前缀都可以查询hash求出 对应多少后缀,但是有可能一对$s_i, s_j$会有多个贡献,因此要用next数组去重 const unsigned long long base = 131; // vector<string> str; string str[MAX]; unordered_map<unsigned long long, int> mp; int nex[MAX]; int cnt[MAX]; void get_hash(const string &s){ unsigned long long res = 0; unsigned long long p = 1; for(int i = s.size()-1; i >= 0; i--){ // unsigned long long x = s[i] - 'a'+1; res += (s[i]-'a'+1) * p; p *= base; mp[res]++; } } void get_next(const string &t){ nex[0] = -1; int k = -1; for(int i = 1; i < t.size(); i++){ while(k > -1 and t[k+1] != t[i]) k = nex[k]; if(t[k+1] == t[i]) k++; nex[i] = k; } } int main(){ #ifdef DEBUG freopen("in", "r", stdin); #endif int n; scanf("%d", &n); // cin >> n; for(int i = 0; i < n; i++){ // string s; cin >> str[i]; // str.push_back(s); get_hash(str[i]); } LL ans = 0; for(int i = 0; i < n; i++){ unsigned long long cur_has = 0 ; for(int j = 0; j < str[i].size(); j++){ cur_has = cur_has * base + (str[i][j] - 'a' + 1); // cnt[j] = mp[cur_has]; auto it = mp.find(cur_has); if(it != mp.end()) cnt[j] = it->second; else cnt[j] = 0; } get_next(str[i]); for(int j = 1; j < str[i].size(); j++){ if(nex[j] >= 0) cnt[nex[j]] -= cnt[j]; } for(LL j = 0; j < str[i].size(); j++){ ans = (ans + 1LL * cnt[j] * (j+1LL) % mod * (j+1LL) % mod) % mod; } } printf("%lld\n", ans); // cout << ans << '\n'; } B Boundary 题意 ...

📝 July 14, 2020&nbsp;·&nbsp;⌛ 6 min

AtCoder-Sum of gcd of Tuples

AtCoder-Sum of gcd of Tuples (Hard) 题意 https://atcoder.jp/contests/abc162/tasks/abc162_e 求$\sum\gcd(a_1,a_2,\cdots,a_n)$,其中$a_i\in[1,K]$ Solution 直接计算肯定是不好计算的,可以考虑按$gcd$的值进行分类,问题就转化为一个计数问题 $\displaystyle \gcd(a,b)=d\Rightarrow\gcd(\frac{a}{d},\frac{b}{d})=1$ $\displaystyle {\gcd(a_1,\cdots, a_n)=d的数量}={\gcd(\frac{a_1}{d},\cdots,\frac{a_n}{d})=1的数量}$ 那么, $$ Ans=\sum_{d=1}^{K}dF(\lfloor\frac{K}{d}\rfloor, N) $$ 其中$F(K,N)$表示$\gcd(a_1,\cdots,a_N)=1$的个数,$a_i\in[1,K]$ 可以用容斥算出$\displaystyle F(K,N)=K^N-\sum_{i=1}^{K}F(\lfloor\frac{K}{i}\rfloor)$ #include <cstdio> #include <stack> #include <set> #include <cmath> #include <map> #include <time.h> #include <vector> #include <iostream> #include <string> #include <cstring> #include <algorithm> #include <memory.h> #include <cstdlib> #include <queue> #include <iomanip> // #include <unordered_map> #define P pair<int, int> #define LL long long #define LD long double #define PLL pair<LL, LL> #define mset(a, b) memset(a, b, sizeof(a)) #define rep(i, a, b) for (int i = a; i < b; i++) #define PI acos(-1.0) #define random(x) rand() % x #define debug(x) cout << #x << " " << x << "\n" using namespace std; const int inf = 0x3f3f3f3f; const LL __64inf = 0x3f3f3f3f3f3f3f3f; #ifdef DEBUG const int MAX = 1e6 + 50; #else const int MAX = 1e6 + 50; #endif const int mod = 1e9 + 7; LL N,K; LL f[MAX]; LL fact[MAX]; inline LL add(LL x, LL y){ LL res = x + y; return res >= mod ? res - mod : res; } inline LL qpow(LL x, LL n){ LL res = 1; while (n) { if(n &1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } LL F(LL K, LL N){ if(f[K]) return f[K]; LL &res =f[K]; if(K==1){ return res = 1; } // res = qpow(K, N); res = fact[K]; for(LL i = 2, j; i <= K; i=j+1){ j = K/(K/i); // res = add(res, mod-F(K/i, N)); LL tmp = (j-i+1LL) * F(K/i, N) % mod; res = add(res, mod-tmp); } return res; } int main(){ #ifdef DEBUG freopen("in", "r", stdin); #endif scanf("%lld%lld", &N, &K); for(LL i = 1; i <= K; i++) fact[i] = qpow(i, N); LL ans = 0; f[1] = 1LL; for(LL k= 1; k <= K; k++){ F(k, N); } for(LL i = 1, j; i <= K; i++){ ans += f[K/i] % mod * i % mod; if(ans >= mod) ans -= mod; } printf("%lld\n", ans); }

📝 April 25, 2020&nbsp;·&nbsp;⌛ 2 min