The Inclusion and Exclusion Principle of ac Number Theory and Dichotomous Enumeration--The kth Coprime Number

k-th coprime number

Time Limit: 1000  ms | Memory Limit: 65535  KB
Difficulty: 4
describe
The gcd of a and b of two numbers is 1, that is, a and b are relatively prime. Now give you a number m. Do you know the kth number that is relatively prime to it? Numbers that are relatively prime to m are arranged in ascending order.
enter
输入m ,k (1<=m<=1000000;1<=k<=100000000)
output
Output the kth number.
sample input
10 1
10 2
10 3
Sample output
1
3

7

#include<iostream>
#include<vector>
using namespace std;
vector<int> yinzi;
int m,k;
void fetch(int mm)
{
    for(int i=2;i*i<=m;i++)
    {
        if(mm%i==0)
        {
            yinzi.push_back(i);
            while(mm%i==0)
                mm/=i;
        }
    }
    if(mm>1)//If you are a prime number then add yourself
        yinzi.push_back(mm);
}
int cnt(int top)
{
  int sum=top;
  int t[1000];
  int g=0;
  t[g++]=1;//Number of odd or even factors
  for(int i=0;i<yinzi.size();i++)
  {
      int tt=g;
      for(int j=0;j<g;j++)
      {
          t[tt++]=(-1*yinzi[i]*t[j]);//even prime factors are positive, odd prime factors are negative
          sum+=top/t[tt-1];//Calculate how many factors between [1,n] contain factor b[t]
      }
      g=tt;
  }
  return sum;
}
//Dichotomous enumeration answer mid, calculate how many numbers in [1, mid] are relatively prime to m, and compare the result with k
int Binary_search()//It is equivalent to a binary search, find the mid, if the coprime number with m in the interval from 1 to mid is greater than or equal to k, then the interval moves down to approach the kth, if the and in the interval from 1 to mid The co-prime number of m is less than k, indicating that the interval is small, then the interval is moved up to approach the k-th co-prime number
{
    int le=0;
    long long ri=1000000000;//Because it is 10 digits, so I define a long long

    while(le<=ri)
    {
        int mid=(le+ri)/2;
        if(cnt(mid)>=k)
        {
            ri=mid-1;
        }
        else
            le=mid+1;
    }
    return le;//Because each calculation of mid is rounded down, so return le
}
intmain()
{
    while(cin>>m>>k)
    {
        yinzi.clear();
        fetch(m);
        cout<<Binary_search()<<endl;
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326483780&siteId=291194637