牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs

链接:https://www.nowcoder.com/acm/contest/141/H
来源:牛客网

题目描述

Eddy has solved lots of problem involving calculating the number of coprime pairs within some range. This problem can be solved with inclusion-exclusion method. Eddy has implemented it lots of times. Someday, when he encounters another coprime pairs problem, he comes up with diff-prime pairs problem. diff-prime pairs problem is that given N, you need to find the number of pairs (i, j), where and are both prime and i ,j ≤ N. gcd(i, j) is the greatest common divisor of i and j. Prime is an integer greater than 1 and has only 2 positive divisors.

Eddy tried to solve it with inclusion-exclusion method but failed. Please help Eddy to solve this problem.

Note that pair (i1, j1) and pair (i2, j2) are considered different if i1 ≠ i2 or j1 ≠ j2.

输入描述:

Input has only one line containing a positive integer N.

1 ≤ N ≤ 107

输出描述:

Output one line containing a non-negative integer indicating the number of diff-prime pairs (i,j) where i, j ≤ N

示例1

输入

复制

3

输出

复制

2

示例2

输入

复制

5

输出

复制

6
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<set>
#include <cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstring>
#include<map>
//第一题
using namespace std;
bool f[10000100];
int b[1000000];
 
int biao(int n)
{
  f[0]=0;
  f[1]=0;
  f[2]=true;
  int e=0;
 
  for(int i=2;i<=sqrt(n )+1;i++)
  {
    if(f[i])
    {
      b[e++]=i;
      for(int j=i+i;j<n+3;j+=i)
      {
        f[j]=false;
      }
    }
  }
 
 
  for(int i=sqrt(n )+2;i<=n;i++)
  {
    if(f[i])
    {
      b[e++]=i;
    }
  }
//  for(int i=0;i<20;i++)
//  cout<<b[i]<<endl;
//  cout<<"qwer"<<endl;
 
  return e;
}
 
int main()
{
  int n;
  scanf("%d",&n );
 
  for(int i=0;i<=n;i++)
  f[i]=true;
 
  int i,j,k,t;
  int e=biao(n);
  long long u=0;
  //map<int,int>mp;
  long long mx=0;
  for(int gcd=1; gcd <=n ;gcd++)
  {
      u=0;
      for(int i=0;b[i]*gcd<=n&&i<e;i++)
      {
 
              u++;
      }
      u=u*(u-1);
      mx+=u;
}
    printf("%lld\n",mx);
    return 0;
  }

猜你喜欢

转载自blog.csdn.net/henu111/article/details/81223444
今日推荐