天天看点

HDU 5750 Dertouzos

Problem Description

n, excluding 

n itself. For example, 1, 2, and 3 are positive proper divisors of 6, but 6 itself is not.

Peter has two positive integers 

n and 

d. He would like to know the number of integers below 

n whose maximum positive proper divisor is 

d.

Input

(1≤T≤106), indicating the number of test cases. For each test case:

The first line contains two integers 

n and 

(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

4

可以知道,答案一定是从2到d的最小的素数因子中素数的个数。

然后就是如何统计了,由于测试数据非常多,根据数据范围使用不同的方案。

#include<set>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 0x7FFFFFFF;
int T, n, m, p[N], f[N], t;

void init()
{
    t = 0;
    rep(i, 2, N - 1)
    {
        if (!f[i]) p[t++] = i;
        for (int j = 0; j < t&&p[j] * i < N; j++)
        {
            f[p[j] * i] = 1;
            if (i%p[j] == 0) break;
        }
    }
}

int main()
{
    init();
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d%d", &n, &m);
        int ans = 0, flag = 0;
        rep(i, 0, t - 1)
        {
            if (p[i] > m || 1LL * p[i] * m >= n) break;
            ans++;
            if (m % p[i] == 0) break;
            if (p[i] * p[i] > m) { flag = 1; break; }
        }
        if (!flag) printf("%d\n", ans);
        else 
        {
            int q = 0, h = t - 1;
            while (q <= h)
            {
                int mid = q + h >> 1;
                if (p[mid] > m || 1LL * p[mid] * m >= n) h = mid - 1; else q = mid + 1;
            }
            printf("%d\n", q);
        }
    }
    return 0;
}