Dertouzos
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 853 Accepted Submission(s): 262
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=最小质因子*最大约数,那么假设m为n的最小质因数,我们得到的数除以这个最大约数d一定会<=m
比如题目给16 5的话,m=16/5=3 那么最后的结果肯定包含1~m的质数*d
不过还需要考虑另一个情况,就是题目一开始给的d不是n的最大约数
比如25 8的话有,只有16是满足条件的。 因为24的最大约数是12不是8了。 大于24的就更不用考虑了,能够整除8最大约数肯定不会是8了
所以这里还有个条件,我们取出的质数还需要<=ma(d)
其中ma(d)表示的是d的最小质因子,如果d是质数那么为他本身。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 40010
long long np[N],prime[N];
int cnt;
void init()
{
memset(np,0,sizeof(np));
cnt=0;
for(long long i=2; i<N; i++)
if(!np[i])
{
prime[cnt++]=i;
for(long long j=i*i; j<N; j+=i)
np[j]=1;
}
}
long long solve(long long n,long long d)
{
long long sum=0;
for(int i=0;i<cnt;i++)
{
if(d*prime[i]>=n) break;
sum++;
if(d%prime[i]==0) break;
}
return sum;
}
int main()
{
int T,t;
long long n,d;
init();
scanf("%d",&T);
while(T--)
{
scanf("%lld %lld",&n,&d);
long long ans=solve(n,d);
printf("%lld\n",ans);
}
return 0;
}