Dertouzos
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2152 Accepted Submission(s): 659
Total Submission(s): 2152 Accepted Submission(s): 659
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
题解:先定义一个数,若d是n所有因子中最大的一个,则d为n的最大约数;
给你n和d,求1到n中(不包括n)最大约数为d的个数……
1
2
1
0
0
0
0
0
4
题解:先定义一个数,若d是n所有因子中最大的一个,则d为n的最大约数;
给你n和d,求1到n中(不包括n)最大约数为d的个数……
#include<cstdio>
#include<cmath>
#include<string>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<queue>
#include<stack>
#include<ctime>
#define INF 50000
using namespace std;
int x[INF],prime[INF],p=0; //虽然n,d,的范围为10的9次方,但要取平方根,所以50000的素数就够了
void Isprime() // 筛法打表,存素数……
{
memset(x,1,sizeof(x));
x[0]=x[1]=0;
for(int i=2;i<INF;i++)
{
if(x[i])
{
prime[p++]=i;
for(int j=2*i;j<INF;j+=i)
x[j]=0;
}
}
}
int sove(int n,int d)
{
int ans=0;
for(int i=0;i<p;i++)
{
if(d*prime[i]>=n)break; // 超出范围,break 掉
ans++;
if(d%prime[i]==0)break; // d 是合数,如果不break,最大约数就不是d了,如d=20,n=100,当prime=2时
// d*prime=40,40的最大约数为20,但当prime=3时,d*prime=60
// 而60的最大约数为30……
}
return ans;
}
int main()
{
Isprime();
int n,d,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&d);
int a=sove(n,d);
printf("%d\n",a);
}
}