QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#290073#4624. Shortest Path in GCD GraphyspmAC ✓247ms97712kbC++201.6kb2023-12-24 12:29:572023-12-24 12:29:57

Judging History

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

  • [2023-12-24 12:29:57]
  • 评测
  • 测评结果:AC
  • 用时:247ms
  • 内存:97712kb
  • [2023-12-24 12:29:57]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
template<typename T>inline void ckmax(T &x,T y){x=x<y?y:x;}
template<typename T>inline void ckmin(T &x,T y){x=x>y?y:x;}
template<typename T=int>inline T read(){
    T res=0,f=1; char k;
    while(!isdigit(k=getchar())) if(k=='-') f=-1;
    while(isdigit(k)) res=res*10+k-'0',k=getchar();
    return res*f;
}
template<typename T>inline void print(T x,bool fl=1){
    if(x<0) putchar('-'),x=-x; 
    if(x>=10) print(x/10,0);
    putchar(x%10+'0');
    if(fl) putchar('\n');
}
int n,q;
const int N=1e7+10;
int pri[N],d[N],cnt;
int pp[20],vv[1<<12];
bool isc[N];
signed main(){
    for(int i=2;i<=1e7;++i){
        if(!isc[i]) pri[++cnt]=i,d[i]=i;
        for(int j=1;i<=1e7/pri[j]&&j<=cnt;++j){
            d[i*pri[j]]=pri[j];
            isc[i*pri[j]]=1;
            if(i%pri[j]==0) break;
        }
    }
    n=read(); q=read();
    for(int eee=1;eee<=q;++eee){
        int u=read(),v=read();
        if(gcd(u,v)==1){
            puts("1 1");
            continue;
        }
        int ans=n;
        set<int> pr;
        int tmp=u;
        while(tmp!=1) pr.insert(d[tmp]),tmp/=d[tmp];
        tmp=v;
        while(tmp!=1) pr.insert(d[tmp]),tmp/=d[tmp];
        int cnt=0;
        for(auto v:pr) pp[cnt++]=v;
        vv[0]=1;
        for(int s=1;s<(1<<cnt);++s){
            int lb=s&(-s);
            vv[s]=vv[s^lb]*pp[__builtin_ctz(lb)];
            if(__builtin_popcount(s)&1) ans-=n/vv[s];
            else ans+=n/vv[s];
        }
        if(gcd(u,v)==2) ++ans;
        printf("2 %d\n",ans);
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 247ms
memory: 97712kb

input:

10000000 50000
6063825 1688077
9748275 1717197
5067284 9994733
8895125 6988260
9446181 5378152
9460161 1794337
3393611 9297610
7525386 5806968
9608609 2225468
9808741 2596273
606784 8423027
2361246 4662937
4385583 1195254
8440346 4819524
4256844 679638
8959165 3868431
9063859 4480534
9568261 1389414...

output:

1 1
2 5240548
1 1
2 2666605
1 1
1 1
1 1
2 2903766
1 1
1 1
1 1
1 1
2 3216618
2 3333325
2 3311989
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
2 2637091
1 1
1 1
1 1
2 3589273
1 1
2 2178004
1 1
1 1
1 1
2 3075313
2 4571237
2 3392155
2 3999993
1 1
2 3144296
1 1
1 1
2 2840582
1 1
2 5121187
2 2461856
1 1
1 1
1 1
1 1
1 ...

result:

ok 50000 lines