hdu 5750 数论

                                                   Dertouzos

A positive proper divisor is a positive divisor of a number nn, excluding nn itself. For example, 1, 2, and 3 are positive proper divisors of 6, but 6 itself is not. 

Peter has two positive integers nn and dd. He would like to know the number of integers below nn whose maximum positive proper divisor is dd.

Input

There are multiple test cases. The first line of input contains an integer TT (1≤T≤106)(1≤T≤106), indicating the number of test cases. For each test case: 

The first line contains two integers nn and dd (2≤n,d≤109)(2≤n,d≤109).

Output

For each test case, output an integer denoting the answer. 

Sample Input

9
10 2
10 3
10 4
10 5
10 6
10 7
10 8
10 9
100 13

Sample Output

1
2
1
0
0
0
0
0
4

题意:存在多少个小于n的数,是的其最大的因子为d;

通过最后一组样例发现 13*4,13*6;是不行的,所以就是要找有多少小于d的质数是的d*x< n;

但是假如 d是合数 如26,在这选到13就不能继续往下选了,下一个素数17能组成的因子可以是 17*2;明显比26大;

所以质数只能选到d能整除的数;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1e7+5;
const int MOD=998244353;
long long n,d;
vector<int> v;
bool vis[maxn];
int p[maxn];
void Prime()
{
    for(int i=2;i<maxn;++i)
    {
        if(!vis[i]) v.push_back(i);
        for(int j=0;j<v.size()&&v[j]*i<maxn;++j)
        {
            vis[i * v[j]] = 1;
            if(i % v[j] == 0) break;
        }
    }
    for(int i=2;i<maxn;++i)
    {
        if(!vis[i]) p[i]=1;
        p[i]+=p[i-1];
    }
}
int main()
{
    int fuck;
    cin>>fuck;
    Prime();
    while(fuck--)
    {
        int ans=0;
        scanf("%lld%lld",&n,&d);
        for(int i=0;i<v.size();++i)
        {
            if(d*v[i]>=n) break;
            ++ans;
            if(d%v[i]==0) break;
        }
        cout<<ans<<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/codetypeman/article/details/81841318