p2257 YY的GCD

传送门

题目

给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对

T = 10000 ; N, M <= 10000000

分析

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
int mu[10000010],f[10000010];
long long pre[10000010];
int prime[10000010],cnt,is[10000010];
inline int read(){
      int x=0;char s=getchar();
      while(!isdigit(s))s=getchar();
      while(isdigit(s))x=(x<<3)+(x<<1)+(s-'0'),s=getchar();
      return x;
}
inline void init(){
      int i,j,k;
      mu[1]=1;
      for(i=2;i<=10000000;i++){
          if(!is[i]){
          prime[++cnt]=i;
          mu[i]=-1;
        }
        for(j=1;j<=cnt,i*prime[j]<=10000000;j++){
          is[i*prime[j]]=1;
          if(i%prime[j]==0){
              mu[i*prime[j]]=0;
              break;
          }
          mu[i*prime[j]]=-mu[i];
        }
      }
      for(j=1;j<=cnt;j++)
        for(i=1;i*prime[j]<=10000000;i++)
           f[i*prime[j]]+=mu[i];
      for(i=1;i<=10000000;i++)
        pre[i]=pre[i-1]+f[i];
}
int main()
{     int n,m,i,j,le,ri,t;
      scanf("%d",&t);
      init();
      while(t--){
        n=read(),m=read();
        if(n>m)swap(n,m);
        long long ans=0;
        for(le=1;le<=n;le=ri+1){
          ri=min(n/(n/le),m/(m/le));
          ans+=(long long)(n/le)*(m/le)*(pre[ri]-pre[le-1]);
        }
        printf("%lld\n",ans);
      }
      return 0;
}
posted @ 2018-06-30 08:21  水题收割者  阅读(168)  评论(0编辑  收藏  举报