Dertouzos
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 966 Accepted Submission(s): 295
Problem Description
A positive proper divisor is a positive divisor of a number
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 .
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
There are multiple test cases. The first line of input contains an integer
T
(1≤T≤106)
, indicating the number of test cases. For each test case:
The first line contains two integers n and d (2≤n,d≤109) .
The first line contains two integers n and d (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的整数中,满足他们的positive proper divisor 恰好是d,假设整数m<n满足条件,那么h = m / d必为质数,且h小于等于d的最小质因子.那么设k = min((--n)/d, d),设d的最小质因子为p, k = min(k, p)接下来求小于等于k的整数中有多少个质数即可.
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <map>
#include <cmath>
#define maxn 100005
#define INF 1000000000
using namespace std;
typedef long long ll;
int k[maxn], cnt[maxn];
vector<int> v;
void Init(){
for(ll i = 2; i * i < maxn; i++){
if(k[i] == 0){
for(ll j = i * i; j < maxn; j += i){
k[j] = 1;
}
}
}
}
int main(){
// freopen("in.txt", "r", stdin);
Init();
for(int i = 2; i < maxn; i++){
cnt[i] = cnt[i-1];
if(k[i] == 0){
cnt[i]++;
v.push_back(i);
}
}
int t;
scanf("%d", &t);
while(t--){
int n, d;
scanf("%d%d", &n, &d);
n--;
int h = n / d;
h = min(h, d);
for(int i = 0; i < v.size() && v[i] <= h && (ll)v[i] * v[i] <= d; i++){
if(d % v[i] == 0){
h = min(h, v[i]);
break;
}
}
printf("%d\n", cnt[h]);
}
return 0;
}