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
参考博客:
点击打开链接 这里的思路说的很明确。从他的博客里学到了很多
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<string>
#include<iostream>
#include<algorithm>
#include<utility>
#include<iomanip>
#include<time.h>
typedef long long ll;
const double Pi = acos(-1.0);
const int N = 1e6+10, M = 1e3+20, mod = 1e9+7, inf = 2e9+10;
const double e=2.718281828459 ;
const double esp=1e-9;
using namespace std;
int t;
bool flag[N];
int prime[N];
int num=0;
void Prim()
{
memset(flag,true,sizeof(flag));
for(int i=2; i<100000; i++)
if(flag[i])
{
prime[num++]=i;
for(int j=2*i; j<100000; j+=i)
flag[j]=false;
}
}
int factor(int d,int n)
{
int sum=0;
for(int i=0; i<num; i++)
{
if(d*prime[i]>=n) break;
if(d>=prime[i])
sum++;
if(d%prime[i]==0) break;//如果d被prime[i]整除,
//这说明d中含有素数且小于d,所以再往下都不会出现最大因子是d,
//而是(d/prime[i](这个prime[i]整除d))*prime[i]
}
return sum;
}
int main()
{
Prim();
while(~scanf("%d",&t))
{
for(int i=0; i<t; i++)
{
int n,d;
scanf("%d%d",&n,&d);
int g=factor(d,n);
printf("%d\n",g);
}
}
return 0;
}