HDU 5750 Dertouzos

Dertouzos

Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1643    Accepted Submission(s): 514


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   (1T106) , indicating the number of test cases. For each test case:

The first line contains two integers  n  and  d   (2n,d109) .
 

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
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5751  5749  5748  5746  5745 


官方题解:

随便推导下, 令y=xdy=xd, 如果ddyy的maximum positive proper divisor, 显然要求xxyy的最小质因子. 令mp(n)mp(n)表示nn的最小质因子, 那么就有x \le mp(d)xmp(d), 同时有y < ny<n, 那么x \le \lfloor \frac{n-1}{d} \rfloorxdn1. 于是就是计算有多少个素数xx满足x \le \min{mp(d), \lfloor \frac{n-1}{d} \rfloor}xmin{mp(d),dn1}.

dd比较大的时候, \lfloor \frac{n-1}{d} \rfloordn1比较小, 暴力枚举xx即可. 当dd比较小的时候, 可以直接预处理出答案. 阈值设置到10^6 \sim 10^7106107都可以过.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int prime[(int)1e5+5];
void getP(int MAXN)
{
    memset(prime,0,sizeof(prime));
    for(int i=2; i<=MAXN; i++)
    {
        if(!prime[i])prime[++prime[0]]=i;
        for(int j=1; j<=prime[0]&&prime[j]<=MAXN/i; j++)
        {
            prime[prime[j]*i]=1;
            if(i%prime[j]==0) break;
        }
    }
}
int main()
{
    getP((int)1e5+5);
    int t;
    scanf("%d",&t);
    while(t--){
        int n,d;
        scanf("%d%d",&n,&d);
        int ans=0;
        for(int i=1;i<=prime[0];i++){
            if(prime[i]*d>=n) break;//枚举质因子,若质因子×d>=上限说明已统计完毕
            ++ans;
            if(d%prime[i]==0) break;//考虑到maximum positive proper divisor也是合数的情况
        }
        printf("%d\n",ans);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值