QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#321528#7749. A Simple MST ProblemEndlineRE 660ms159024kbC++143.0kb2024-02-04 21:50:022024-02-04 21:50:02

Judging History

你现在查看的是最新测评结果

  • [2024-02-04 21:50:02]
  • 评测
  • 测评结果:RE
  • 用时:660ms
  • 内存:159024kb
  • [2024-02-04 21:50:02]
  • 提交

answer

/*
 * Author: Endline
 * Time: 2024/02/04 20:58:43
 * File: Math-D.cpp
 */
#include<bits/stdc++.h>
#define debug(...) fprintf(stderr,__VA_ARGS__)
using namespace std;
using ll=long long;
const int MAXN=1000002;
const int mod=998244353;
template<typename _Tp>inline void Add(_Tp&x,ll y){x=x+y>=mod?x+y-mod:x+y;}
template<typename _Tp>inline void Mul(_Tp&x,ll y){x=x*y>=mod?x*y%mod:x*y;}
int T,l,r,pcnt,ecnt;
bool vis[MAXN],flg[MAXN];
int pri[MAXN],fa[MAXN],cnt[MAXN],val[MAXN],pre[MAXN],nxt[MAXN],mind[MAXN],las[MAXN];
vector<int>divor[MAXN];
struct edge
{
    int u,v,w;
}e[MAXN];
inline int getf(int x){return x==fa[x]?x:fa[x]=getf(fa[x]);}
inline void prework(int n)
{
    val[1]=1;
    for(int i=2;i<=n;i++)
    {
        val[i]=1;
        if(!vis[i])pri[++pcnt]=i;
        for(int j=1;j<=pcnt&&pri[j]*i<=n;j++)
        {
            vis[pri[j]*i]=true;
            if(i%pri[j]==0)break;
        }
    }
    for(int i=1;i<=n;i++)
        for(int j=1;j*i<=n;j++)
            divor[i*j].push_back(i);
    for(int i=1;i<=pcnt;i++)
        for(int j=1;pri[i]*j<=n;j++)
        {
            cnt[pri[i]*j]++,val[pri[i]*j]*=pri[i];
            if(!mind[pri[i]*j])mind[pri[i]*j]=pri[i];
        }
    for(int i=1;i<=n;i++)
        las[i]=0;
    for(int i=1;i<=n;i++)
    {
        for(int j:divor[i])
            pre[i]=max(pre[i],las[j]);
        las[val[i]]=i;
    }
    for(int i=1;i<=n;i++)las[i]=n+1;
    for(int i=n;i>=1;i--)
    {
        nxt[i]=n+1;
        for(int j:divor[i])
            nxt[i]=min(nxt[i],las[j]);
        las[val[i]]=i;
    }
    return;
}
inline bitset<MAXN>calc(int x)
{
    bitset<MAXN>ans;
    for(int i=1;i<=pcnt&&pri[i]*pri[i]<=x;i++)
    {
        if(x%pri[i]==0)
        {
            ans.set(pri[i]);
            while(x%pri[i]==0)x/=pri[i];
        }
    }
    if(x>1)ans.set(x);
    return ans;
}
inline int Kruskal()
{
    int ans=0;
    sort(e+1,e+ecnt+1,[](edge x,edge y){return x.w<y.w;});
    for(int i=l;i<=r;i++)fa[i]=i;
    for(int i=1;i<=ecnt;i++)
    {
        if(getf(e[i].u)==getf(e[i].v))continue;
        fa[getf(e[i].u)]=getf(e[i].v);
        ans+=e[i].w;
    }
    return ans;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    prework(1000000);
    cin>>T;
    while(T--)
    {
        cin>>l>>r;
        int pl=lower_bound(pri+1,pri+pcnt+1,l)-pri,pr=lower_bound(pri+1,pri+pcnt+1,r)-pri;
        if(pl==pr)
        {
            ecnt=0;
            for(int i=l;i<=r;i++)
                for(int j=i+1;j<=r;j++)
                    e[++ecnt]={i,j,(int)(calc(i)|calc(j)).count()};
        }
        else
        {
            ecnt=0;
            int pos=0;
            for(int i=l;i<=r;i++)if(!vis[i]&&!pos)pos=i;
            for(int i=l;i<=r;i++)
            {
                if(pre[i]>=l)e[++ecnt]={i,pre[i],cnt[i]};
                if(nxt[i]<=r)e[++ecnt]={i,nxt[i],cnt[i]};
                if(i!=pos)e[++ecnt]={i,pos,cnt[i]+1};
            }
        }
        printf("%d\n",Kruskal());
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 618ms
memory: 145188kb

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: 646ms
memory: 150804kb

input:

2
27 30
183704 252609

output:

8
223092

result:

ok 2 lines

Test #3:

score: 0
Accepted
time: 597ms
memory: 150820kb

input:

1
183704 252609

output:

223092

result:

ok single line: '223092'

Test #4:

score: 0
Accepted
time: 636ms
memory: 158316kb

input:

2
639898 942309
30927 34660

output:

983228
11512

result:

ok 2 lines

Test #5:

score: 0
Accepted
time: 660ms
memory: 159024kb

input:

3
21731 33468
46192 370315
1712 3753

output:

34608
948775
5299

result:

ok 3 lines

Test #6:

score: 0
Accepted
time: 645ms
memory: 150408kb

input:

4
15237 67700
10918 86104
98287 116980
17432 23592

output:

148811
210927
60893
18687

result:

ok 4 lines

Test #7:

score: -100
Runtime Error

input:

5
5594 70302
19202 69588
5634 7877
59821 469300
97439 261121

output:


result: