20261019 模拟赛总结
要记得关同步流
T1(P17161)
题目大意
定义集合
现在在
过程
一眼题
首先肯定需要快速确定
欧拉筛可以求出对应数的最小值因子,所有可以在
写的时候脑子抽了,没有哈希表来存储集合,时间复杂度
但是
考场代码
:::info[考场代码]
include <bits/stdc++.h>
define multicase() int t;cin>>t;while(t--)
using namespace std; using ll = long long; using pii = pair<ll,ll>;
const ll kMaxN = 1e7 + 7;
void fast_io(){ ios::sync_with_stdio(0); cin.tie(0) ,cout.tie(0); }
ll pz[kMaxN]; vector<ll> pri; bitset<kMaxN> bt;
void init(ll n){
for(int i = 2 ; i <= n ; i++){
if(!bt[i]) pri.push_back(i) , pz[i] = i;
for(auto j : pri){
if(i j > n) break;
bt[i j] = 1;
pz[i * j] = j;
if(i % j == 0) break;
}
}
}
void debug(ll x){ // cout<<x; // for(int i = 1 ; i <= x ; i++) cout<<pz[i]<<" "; // cout<<"\n"; while(pz[x] != x){ // cout<<pz[x]; x /= pz[x]; } cout<<x<<" "; // cout<<pz[x]; }
ll x , y; ll res = 1;
void solve(){ // st.clear(); // vis.reset(); // visx.reset() , visy.reset(); set<ll> stx , sty; cin>>x>>y; res = 1; while(pz[x] != x){ // cout<<pz[x]<<" "; if(stx.find(pz[x]) == stx.end()) stx.insert(pz[x]) , res = pz[x]; x /= pz[x]; } // cout<<pz[x]<<" - "<<res<<"\n"; if(stx.find(pz[x]) == stx.end()) stx.insert(pz[x]) , res = pz[x]; while(pz[y] != y){ bool flag = 0; if(stx.find(pz[y]) != stx.end()) flag = 1; if(sty.find(pz[y]) == sty.end()){ if(flag) res /= pz[y]; else res = pz[y]; sty.insert(pz[y]); } y /= pz[y]; } bool flag = 0; if(stx.find(pz[y]) != stx.end()) flag = 1; if(sty.find(pz[y]) == sty.end()){ if(flag) res /= pz[y]; else res = pz[y]; sty.insert(pz[y]); } cout<<res; }
int main(){ fast_io(); // freopen("factor.in" , "r" , stdin); // freopen("factor.out" , "w" , stdout); init(kMaxN - 7); // debug(12); multicase(){ solve(); cout<<"\n"; } return 0; } :::
带预处理的还是不蒸最优解了
总结
没有被
多做点莫反还是有用的,没有被埃氏筛的 (四只log)
题目大意搞错了是数论对称差
T2(CF1385G)
其实我忘记了网络流求最大权匹配
题目大意
给定一个
过程
首先这个东西很好建图。先判最简单的无解情况,然后同数连边,要求所有边都是跨越行的,很明显可以用二分图最大权匹配做,但是我忘记了
把同行的必须操作相反,设一个为
对于两种操作,取大小最大的,点位任意,这里选根。没必要按秩合并,
考场代码
:::info[考场代码]
#include <bits/stdc++.h>
#define multicase() int t;cin>>t;while(t--)
using namespace std;
using ll = long long;
using pii = pair<ll,ll>;
const ll kMaxN = 2e5 + 7;
ll fa[kMaxN << 1];
pii HASH2[kMaxN][2];
ll HASH[kMaxN];
ll cnt0[kMaxN << 1] , cnt1[kMaxN << 1];
ll rt0[kMaxN] , rt1[kMaxN];
void init_fa(ll n){
for(int i = 1 ; i <= n * 2 ; i++) fa[i] = i;
for(int i = 1 ; i <= n ; i++) cnt1[i] = cnt0[i] = rt0[i] = rt1[i] = HASH[i] = 0;
for(int i = 1 ; i <= n ; i++) HASH2[i][0] = HASH2[i][1] = {0,0};
}
ll find(ll x){
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
void merge(ll u , ll v){
u = find(u) , v = find(v);
fa[u] = v;
}
ll n;
// ll tmap[2][kMaxN];
void solve(){
cin>>n;
init_fa(n);
for(int i = 1 ; i <= 2 ; i++){
for(int j = 1 , x ; j <= n ; j++){
cin>>x;
if(HASH[x] < 2) HASH2[x][HASH[x]] = {i , j};
HASH[x]++;
}
}
// cout<<"dsa";
for(int i = 1 ; i <= n ; i++) if(HASH[i] < 2) return void(cout<<-1);
// cout<<"dd";
for(int i = 1 ; i <= n ; i++){
pii a , b;
a = HASH2[i][0] , b = HASH2[i][1];
if(a.first == b.first){
merge(a.second , b.second + n);
merge(a.second + n , b.second);
}
if(a.first != b.first){
merge(a.second , b.second);
merge(a.second + n , b.second + n);
}
}
for(int i = 1 ; i <= n ; i++){
rt0[i] = find(i) , rt1[i] = find(i + n);
if(rt0[i] == rt1[i]) return void(cout<<-1);
}
for(int i = 1 ; i <= n ; i++){
ll g = min(rt0[i] , rt1[i]);
if(g == rt0[i]) cnt0[g]++;
else cnt1[g]++;
}
vector<ll> ans;
for(int i = 1 ; i <= n ; i++){
ll g = min(rt0[i] , rt1[i]);
if(cnt0[g] < cnt1[g]){
if(rt0[i] == g) ans.push_back(i);
}else{
if(rt1[i] == g) ans.push_back(i);
}
}
cout<<ans.size()<<"\n";
for(auto v : ans) cout<<v<<" ";
}
int main(){
// freopen("columns.in" , "r" , stdin);
// freopen("columns.out" , "w" , stdout);
multicase(){
solve();
cout<<"\n";
}
return 0;
}
::: 按秩合并是谁发明的,感觉路径压缩就可以了,再不行启发式合并一下差不多了。怎么所有人都在用按秩合并,大小比深度好统计多了吧
总结
并查集的题训练的少了,好吧,好像是图论这一块都写少了,不然我直接网络流跑过去
T3(P15482)
加粗字体题