QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#293794 | #7749. A Simple MST Problem | ucup-team139 | TL | 966ms | 133764kb | C++23 | 2.0kb | 2023-12-29 19:16:55 | 2023-12-29 19:16:56 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e6+5;
bool pr[MAXN];
int omega[MAXN];
vector<int> divi[MAXN];
void solve(int t){
int l,r;
cin>>l>>r;
auto reduce = [](int x,int y)->int{
while(__gcd(x,y)>1){
x/=__gcd(x,y);
}
return x;
};
auto cost = [&](int x,int y)->int{
int gcd= __gcd(x,y);
x = reduce(x,gcd);
y = reduce(y,gcd);
return omega[gcd]+omega[x]+omega[y];
};
vector<int> best(r+1);
for(int i=1;i<=r;i++){
best[i]=-1;
for(int j=(l+i-1)/i*i;j<=r;j+=i){
if(best[i]==-1 || omega[reduce(best[i],j)]>omega[reduce(j,i)]){
best[i]=j;
}
}
}
vector<array<int,3>> v;
for(int i=l;i<=r;i++){
for(auto x : divi[i]){
if(best[x]!=-1)v.push_back({i,best[x],cost(i,best[x])});
}
}
int ans=0;
vector<int> ds(r+1);
for(int i=l;i<=r;i++){
ds[i]=i;
}
auto f = [&](auto &f,int x)->int{
if(ds[x]!=x)return ds[x]=f(f,ds[x]);
return x;
};
auto add = [&](int x,int y)->bool{
int a = f(f,x), b = f(f,y);
if(a==b)return false;
ds[a]=b;
return true;
};
sort(v.begin(),v.end(),[](auto x,auto y){return x[2]<y[2];});
for(auto [i,j,cost] : v){
if(add(i,j)){
//cout<<i<<" "<<j<<" "<<cost<<"\n";
ans+=cost;
}
}
cout<<ans<<"\n";
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
memset(pr,true,sizeof pr);
for(int i=2;i<MAXN;i++){
if(pr[i]){
for(int j=i;j<MAXN;j+=i){
omega[j]++;
pr[j]=false;
}
}
divi[i].push_back(1);
for(int j=i;j<MAXN;j+=i)divi[j].push_back(i);
}
int t=1;
cin>>t;
for(int i=1;i<=t;i++)solve(i);
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 699ms
memory: 119268kb
input:
5 1 1 4 5 1 4 1 9 19 810
output:
0 2 3 9 1812
result:
ok 5 lines
Test #2:
score: 0
Accepted
time: 966ms
memory: 133764kb
input:
2 27 30 183704 252609
output:
8 223092
result:
ok 2 lines
Test #3:
score: 0
Accepted
time: 964ms
memory: 132604kb
input:
1 183704 252609
output:
223092
result:
ok single line: '223092'
Test #4:
score: -100
Time Limit Exceeded
input:
2 639898 942309 30927 34660