SMU Autumn 2024 Team Round 3(The 2024 ICPC Latin America Championship)
SMU Autumn 2024 Team Round 3(The 2024 ICPC Latin America Championship)
D. DiviDuelo
思路
队友写得,分类讨论,不过他上了点黑科技,但是貌似 \(\sqrt{n}\) 也可以处理。
代码
#include<bits/stdc++.h> #define ll long long #define int long long const int mod= 998244353; #define PII pair<ll,int> #define PIII pair<int,PII> #define INF 0x3f3f3f3f #define double long double using ull = std::uint64_t; #define endl '\n' #pragma GCC optimize(2) using namespace std; namespace Factor { /*Montgomery Multiplt Template*/ ull modmul(ull a, ull b, ull M) { ll ret = a * b - M * ull(1.L / M * a * b); return ret + M * (ret < 0) - M * (ret >= (ll) M); } ull modpow(ull b, ull e, ull mod) { ull ans = 1; for (; e; b = modmul(b, b, mod), e /= 2) if (e & 1) ans = modmul(ans, b, mod); return ans; } bool isPrime(ull n) { if (n < 2 || n % 6 % 4 != 1) return (n | 1) == 3; std::vector<ull> A = {2, 325, 9375, 28178, 450775, 9780504, 1795265022}; ull s = __builtin_ctzll(n - 1), d = n >> s; for (ull a: A) { // ^ count trailing zeroes ull p = modpow(a % n, d, n), i = s; while (p != 1 && p != n - 1 && a % n && i--) p = modmul(p, p, n); if (p != n - 1 && i != s) return 0; } return 1; } ull pollard(ull n) { auto f = [n](ull x, ull k) { return modmul(x, x, n) + 1; }; ull x = 0, y = 0, t = 30, prd = 2, i = 1, q; while (t++ % 40 || std::gcd(prd, n) == 1) { if (x == y) x = ++i, y = f(x, i); if ((q = modmul(prd, std::max(x, y) - std::min(x, y), n))) prd = q; x = f(x, i), y = f(f(y, i), i); } return std::gcd(prd, n); } std::vector<ull> factor(ull n) { if (n == 1) return {}; if (isPrime(n)) return {n}; ull x = pollard(n); auto l = factor(x), r = factor(n / x); l.insert(l.end(), r.begin(), r.end()); return l; } } int siz; vector<ull>fac; int num[1003]; int res=0; set<int>st; int n; void solve() { cin >> n; fac = Factor::factor(n); int w = n; sort(fac.begin(), fac.end()); fac.erase(unique(fac.begin(), fac.end()), fac.end()); siz = fac.size(); for (int i = 0; i < siz; ++i) { num[i] = 0; while (w % fac[i] == 0) { num[i]++; w /= fac[i]; } } if(n==1){ cout<<"N\n"; return; } if(fac.size()==1){ if(num[0]%2==1){ cout<<"Y\n"; } else{ cout<<"N\n"; } } else if(fac.size()==2){ if(num[0]== num[1] and num[0]==1){ cout<<"Y\n"; } else{ cout<<"N\n"; } } else{ cout<<"N\n"; } } int32_t main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int T = 1; // cin >> T; while (T--) { solve(); } return 0; }
E. Expanding STACKS!
题意
给你两个栈,然后给出 \(n\) 个数字的入栈出栈记录,问你能否判断每个数字进入的是哪个栈,如果不能则输出 *
。
思路
由题意可知,题目不要求我们输出每个数字进栈的先后顺序,只要我们输出哪些数字在哪个栈内即可。
把 \(n\) 个数字分成两个集合,其就是两个栈里的数字集合,那么我们要根据什么将这些数字分成两个集合呢?观察数字的入栈出栈记录,对于+1,+2,-1,-2
这种情况,我们可以手动模拟一下,\(2\) 是比 \(1\) 后入栈的,但是 \(1\) 却比 \(2\) 先出栈,这说明什么?说明 \(1\) 和 \(2\) 不是进的同一个栈!假设 \(1\) 和 \(2\) 进入的是同一个栈,那么根据栈的性质,\(2\) 一定比 \(1\) 先出栈,否则就是不合法情况。
由此,我们可以得出一条关系,那就是+a,+b,+c,+d,-a...
这样的记录中,在 \([l_{+a},r_{-a}]\) 的这个区间里,一旦有新的数进来但是却没有再出去过,那么就可以确定,这些数和 \(a\) 是不在同一个集合内的。
由以上结论我们可以处理出所有的数字间的一个排斥关系,那就是谁和谁肯定不在同一个集合里,根据这个关系然后将所有点分成两个部分,就是很典型的二分图染色,此外种类并查集也可以处理这种题型,不过种类并查集最后需要将合并得到的多个联通块再次合并成两个集合(产生多个联通块的原因就是有的数字进栈后马上就出栈了,这个数字就是属于‘中立’的那种,不与任何数字排斥,放在哪个集合都可以),我的做法是维护其中一个集合,如果当前点所排斥的点在我维护的集合中,那就说明它要放在另一个集合。
(种类并查集)代码
#include <bits/stdc++.h> using namespace std; using i64 = long long; struct UFS { int sz; vector<int> rank, p; UFS(int n) { init(n); } void link(int x, int y) { if (x == y) return; if (rank[x] > rank[y]) p[y] = x; else p[x] = y; if (rank[x] == rank[y]) rank[y]++; } void init(int n) { sz = n; rank.resize(n + 1); p.resize(n + 1); for (int i = 0; i <= sz; i++) { p[i] = i; rank[i] = 0; } } int find(int x) { return x == p[x] ? x : (p[x] = find(p[x])); } void unin(int x, int y) { link(find(x), find(y)); } void compress() { for (int i = 0; i < sz; i++) find(i); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> a(2 * n + 1), l(n + 1, 2 * n), r(n + 1); for (int i = 1; i <= 2 * n; i ++) { cin >> a[i]; l[abs(a[i])] = min(l[abs(a[i])], i); r[abs(a[i])] = max(r[abs(a[i])], i); } vector<pair<int, int>> e; for (int i = 1; i <= 2 * n; i ++) { if (a[i] < 0) continue; vector<int> cnt(n + 1); for (int j = l[a[i]] + 1; j < r[a[i]]; j ++) { if (a[j] > 0) cnt[a[j]] = 1; if (a[j] < 0 && cnt[-a[j]]) cnt[-a[j]] = 0; } for (int j = 1; j <= n; j ++) { if (cnt[j]) { e.emplace_back(a[i], j); } } } UFS d(2 * n); for (auto [u, v] : e) { if (d.find(u) == d.find(v) || d.find(u + n) == d.find(v + n)) { cout << "*\n"; return 0; } d.unin(u + n, v); d.unin(u, v + n); } vector<vector<int>> w(2 * n + 1); set<int> x; for (auto [u, v] : e) { u = d.find(u), v = d.find(v); w[u].emplace_back(v); w[v].emplace_back(u); } string ans = "", p = "SG"; for (int i = 1; i <= n; i ++) { int t = d.find(i), f = 1; for (auto v : w[t]) { if (x.count(v)) f = 0; } ans += p[f]; if (f) { x.insert(t); } } cout << ans << '\n'; return 0; }
队友写得,可以做个参考。
(二分图染色)代码
#include<bits/stdc++.h> using namespace std; #define int long long #define ll long long const int N=1e3+3; #define PII pair<ll,ll> #define endl '\n' #define mem(a, b) memset(a, b, sizeof(a)) int tt[N], g[N][N], n; bool used[N]; bool flag=1; void dfs(int u,int t) { used[u] = true; if(!tt[u]){ tt[u]=t; }else{ if(tt[u]!=t){ flag=0; return; } } for (int v = 1; v <= n; v++) { if (g[u][v]) { if(tt[v]&&tt[v]==tt[u]){ flag=0; break; } if(!used[v]){ dfs(v,t^3); } } } } int a[N*2],b[N],c[N]; int d[N]; void solve() { int ans = 0; cin>>n; for(int i=1;i<=2*n;i++){ cin>>a[i]; if(a[i]>0)b[a[i]]=i; else c[-a[i]]=i; } for(int i=1;i<=n;i++){ for(int j=b[i]+1;j<=c[i]-1;j++){ if(a[j]>0){ d[a[j]]++; }else{ d[-a[j]]--; } } for(int j=1;j<=n;j++){ if(d[j]>0){ g[j][i]=1; g[i][j]=1; } d[j]=0; } } for(int i=1;i<=n;i++){ if(!used[i]){ dfs(i,1); } } if(flag){ for(int i=1;i<=n;i++){ if(tt[i]==1){ cout<<'G'; }else{ cout<<'S'; } }cout<<endl; }else{ cout<<'*'; } } signed main(){ ios::sync_with_stdio(false),cin.tie(0); int t=1; // cin>>t; while(t--){ solve(); } }
F. Fair Distribution
题意
给你 \(n\) 对二元组 \((G,R)\),由二元组可以得到一个值 \(x=G+k\times R(k>0)\) 你需要将这 \(n\) 对二元组分成两组,然后给每组中的二元组都确定一个值 \(x\) ,使得两组的所有 \(x\) 之和相等,更具体地,将 \(n\) 组二元组分成两个集合 \(S_1,S_2\),问 \(\sum\limits_{i=1}^{|S_1|}G_i+k_i\times R_i=\sum\limits_{j=1}^{|S_2|}G_j+k_j\times R_j\) 是否存在。
思路
前置知识:
裴蜀定理
设 a,b 是不全为零的整数,对任意整数 x,y,满足 gcd(a,b)|ax+by,且存在整数 x,y,使得 ax+by=gcd(a,b).
先只看单独一个集合 \(S_1\),因为 \(G\) 是一个定值,所以可以写成 \((\sum\limits_{i=1}^{|S_1|} G_i) +k_1\times R_1+k_2\times R_2+\dots\),由裴蜀定理可得, 即存在 \(k_1,k_2,\dots\),使得 \(k_1\times R_1+k_2\times R_2+\dots =\gcd(R_1,R_2,\dots)|X\),设 \(\gcd(R_1,R_2,\dots)=g_{|S_1|}\),所以集合 \(S_1\) 的总和等于 \(G_{|S_1|}+X=G_{|S_1|}+g_{|S_1|}\times k_{|S_1|}\),同理,集合 \(S_2\) 的总和等于 \(G_{|S_2|}+X=G_{|S_2|}+g_{|S_2|}\times k_{|S_2|}\),即有:
又由裴蜀定理可知,满足 \(\gcd(g_{|S_1|},g_{|S_2|})|(|G_{|S_1|}-G_{|S_2|}|)\),而 \(\gcd(g_{|S_1|},g_{|S_2|})\) 其实就是 \(\gcd(R_1,R_2,\dots,R_n)=g\),所以 \(|G_{|S_1|}-G_{|S_2|}|\) 一定能被 \(g\) 整除,即,只要我们找到一种分配方式,使得两个集合中的 \(G\) 总和相减的绝对值是 \(g\) 的倍数即可。
而对于 \(G_i\) 能凑成哪些数,直接枚举子集显然是不可取的做法,诶,取出一些数判断其能凑成的数,那不就是01背包吗!但是,这里的 \(1\le n\le 2\times 10^5\),直接做背包是 \(O(n\times W)\),也是难以接受的。
不过,观察到 \(0\le \sum\limits_{i=1}^nG_i\le 2\times 10^5\),那么说明,不同种类的数最多只有 \(\sqrt{2\times 10^5}\) 个,那么我们可以用多重背包的做法去枚举 \(G_i\) 能凑成的数即可,这样复杂度就是 \(O(\sqrt W \times W\times log(\sqrt W))\) 相比朴素01背包可以接受 。
最后就是枚举 \(i\) ,判断两个集合的差值是否是 \(g\) 的整数倍即可,但是有个注意的点,如果存在 \(G_i=0\),那么可以从 \(0\) 枚举到 $ G$,否则是 \(1\sim G -1\ (G=\sum\limits_{i=1}^nG_i)\)。
代码
#include <bits/stdc++.h> using namespace std; using i64 = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; int g = 0, s = 0; const int N = 2e5 + 10; set<int> object; vector<int> num(N); for (int i = 1; i <= n; i ++) { int G, R; cin >> G >> R; s += G; g = gcd(R, g); object.insert(G); num[G] ++; } if (n == 1) { cout << "N\n"; return 0; } vector<int> dp(s + 1); dp[0] = 1; for (auto &i : object) { int has = num[i]; for (int k = 1; k <= has; k <<= 1) { has -= k; i64 w = k * i; for (int j = s; j >= w; j --) dp[j] |= dp[j - w]; } if (has) { i64 w = has * i; for (int j = s; j >= w; j --) dp[j] |= dp[j - w]; } } i64 ans = 0, f = object.count(0); f ^= 1; for (int i = f; i <= s - f; i ++) { if (dp[i]) { if (abs(s - 2 * i) % g == 0) { cout << "Y\n"; return 0; } } } cout << "N\n"; return 0; }
G. Greek Casino
思路
有点久远忘了,可以看看队友写得blog。
https://www.luogu.com/article/wa3tz7wp
代码
#include <bits/stdc++.h> using namespace std; using i64 = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; int sum = 0; vector<int> a(n + 1); for (int i = 1; i <= n; i ++) { cin >> a[i]; sum += a[i]; } vector<double> p(n + 1); for (int i = 1; i <= n; i ++) { p[i] = a[i] * 1.0 / sum; } vector g(n + 1, vector<int>()); for (int i = 1; i <= n; i ++) { for (int j = i; j <= n; j += i) { g[j].emplace_back(i); } } vector<double> dp(n + 1); for (int i = n; i >= 1; i --) { double s1 = 0.0, s2 = 0.0; for (auto &u : g[i]) { s1 += p[u]; } for (int j = 2 * i, k = 2; j <= n; j += i, k ++) { for (auto &u : g[i]) { i64 x = 1ll * u * k; if (lcm(x, i) == j) { dp[i] += p[x] * (dp[j] + 1); s2 += p[x]; } } } dp[i] += 1.0 - s2; dp[i] /= (1 - s1); } cout << fixed << setprecision(10) << dp[1] - 1 << '\n'; return 0; }
K. KMOP
思路
设 \(dp_{i,j}\) 表示第 \(i\) 个字符串取前 \(j\) 个字符能形成的答案最大值。
好像也可以贪心,不过还是喜欢DP。
代码
#include <bits/stdc++.h> using namespace std; using i64 = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; string t = "AEIOUY"; vector<string> s(n); for (auto &i : s) { cin >> i; if (i.size() > 3) i = i.substr(0, 3); for (auto &j : i) { if (~t.find(j)) j = '1'; else j = '0'; } } const int inf = INT_MAX >> 1; vector<array<int, 3>> dp(n, {inf, inf, inf}); for (int i = 0, cnt = 0; i < s[0].size(); i ++) { if (s[0][i] == '0') cnt ++; else cnt = 0; dp[0][cnt] = min(dp[0][cnt], i + 1); } for (int x = 1; x < n; x ++) { auto c = s[x]; int cnt = 0; for (int i = 0; i < c.size(); i ++) { if (c[i] == '0') cnt ++; else cnt = 0; if (!cnt) { int zero = 0, idx = 0; while (idx < i && c[idx] == '0') idx++, zero ++; for (int j = 0; j < 3 - zero; j ++) { dp[x][cnt] = min(dp[x - 1][j] + i + 1, dp[x][cnt]); } } else { for (int j = 0; j + cnt < 3; j ++) { dp[x][cnt + j] = min(dp[x - 1][j] + i + 1, dp[x][cnt + j]); } } } } int ans = inf; for (int i = 0; i < 3; i ++) { ans = min(ans, dp[n - 1][i]); } if (ans == inf) cout << "*\n"; else cout << ans << '\n'; return 0; }
L. LED Matrix
思路
模拟。
代码
#include <bits/stdc++.h> using namespace std; #define int long long #define double long double #define LL __int128 #define PII pair<int,int> #define PIII pair<int,PII> #define endl '\n' #define ll unsigned long long #define Pll pair<ll,ll> const ll P = 131, N = 1e6 + 3, mod1 = 1610612741, mod2 = 1e9 + 7; int a[N]; int b[N]; void solve() { int n, l, r; cin >> n >> l >> r; for(int i=1;i<=n;i++){ int f1=0; for(int j=1;j<=l;j++){ char x; cin>>x; if(x=='-'){ f1=1; } } int f2=0; for(int j=1;j<=r;j++){ char x; cin>>x; if(x=='*'){ f2=1; } } a[i]=f1; b[i]=f2; } int ans=1; for(int i=1;i<=n;i++){ if(a[i]&&b[i]){ ans=0; } } if(ans){ cout<<"Y\n"; }else{ cout<<"N\n"; } } signed main() { ios::sync_with_stdio(false); cin.tie(0); int t = 1; // cin >> t; while (t--) { solve(); } return 0; }
本文作者:Ke_scholar
本文链接:https://www.cnblogs.com/Kescholar/p/18430354
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步