Dertouzos
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2097 Accepted Submission(s): 634
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
題意:找2~n-1中有幾個數的最大因子是d
思路:一個數 d*x 的最大因子是 d 那麼 x 必是素數,且 x 小於等於 d 的最大素因子 (比賽時看樣例100 13,d恰好是素數, 只想着找到有多少個小於等於d的素數就可以了 ,沒有考慮到d不是素數時d本身就有因子,不可能是最大的因子了,如:42 d爲14時,42的最大因子是21 而不是14 )
code:
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<stack>
#include<math.h>
#include<algorithm>
using namespace std;
int n,d,k=0;
int vis[1000005],prime[1000005];
void get_prime()
{
k=0;
vis[0]=vis[1]=1;
prime[0]=prime[1]=0;
for(int i=2; i<=1000000; i++)
{
if(vis[i]==0)
{
prime[k++]=i;
for(int j=i+i; j<=1000000; j+=i)
{
vis[j]=1;
}
}
}
}
int main()
{
int T;
get_prime();
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&d);
int ans=0;
for(int i=0;i<k;i++)
{
if(prime[i]>d||prime[i]*d>=n)
break;
if(d%prime[i]==0)
{
ans++;
break;
}
ans++;
}
printf("%d\n",ans);
}
}