hdu 5750 Dertouzos(数论)

来源:互联网 发布:企业淘宝怎么注册 编辑:程序博客网 时间:2024/05/18 02:01

Dertouzos

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.

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).

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

Source
BestCoder Round #84

题意:给定两个数n和d,问在[1,n)之间总共有多少个数的最大因子为d

分析:要使数x的最大因子为d,很容易想到这个数x=k*d,k为素数并且k<d,x<n
但是当k和d有公共的因子(不含1)或者k大于d的最小素因子时,那么这时最大的因子就不一定是d了,
比如说当n=150 d=22时 ,这时只有44符合条件,我么可以看到22的素因子为2和11,如果令k=5的话,那么x=110,但是它的最大因子为(5*11=)55,并不符合条件

所以k必须要小于等于d的最小素因子

思路:通过枚举素数,找出在[1,n)之间有多少个素数满足条件,即为结果

代码:

#include<stdio.h>#define maxn 1000000+10int p[maxn/10],c[maxn];void is_Prim(){    int tot=0,i,j;    c[0]=1,c[1]=1;    for(i=2;i<=maxn;i++)    {        if(!c[i])            p[tot++]=i;        for(j=0;j<tot&&i*p[j]<maxn;j++)        {            c[i*p[j]]=1;            if(!(i%p[j]))                break;        }    }}int main(){    int t;    is_Prim();    scanf("%d",&t);    while(t--)    {        int n,m,i;        scanf("%d%d",&n,&m);        for(i=0;;i++)        {            if(p[i]*m>=n)                break;            if(m%p[i]==0)            {                i++;                break;            }        }        printf("%d\n",i);    }    return 0;}
1 0