HDU 5750 Dertouzos(数论)

227 篇文章 0 订阅
97 篇文章 0 订阅

传送门
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
题目大意:

xnpositiveproperdivisor,x|n1x<n,1,2,36positiveproperdivisor,6.Peternd.n,positiveproperdivisord

解题思路:

随便推导下, 令 y=xd , 如果 d y的maximum positive proper divisor, 显然要求 x y的最小质因子. 令 mp(n) 表示 n 的最小质因子, 那么就有xmp(d), 同时有y &lt; n, 那么 xn1d . 于是就是计算有多少个素数 x 满足xmin{mp(d),n1d}.

d 比较大的时候, n1d比较小, 暴力枚举 x 即可. 当d比较小的时候, 可以直接预处理出答案。预处理的时候采用素数筛,先将每个数的最小素因子保存在一个数组中,处理的时候采用二分来操作减少时间,要不然很容易超时,当时我们就是超时了,掉分了。。。

My Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
const int MAXN = 5e4+5;
typedef long long LL;
bool prime[MAXN];
int p[MAXN], ret[MAXN];
int k;
void isprime()
{
    k = 0;
    memset(prime, 0, sizeof(prime));
    memset(ret, 0, sizeof(ret));
    for(LL i=2; i<MAXN; i++)
    {
        if(!prime[i])
        {
            p[k++] = i;
            ret[i] = i;
            for(LL j=i+i; j<MAXN; j+=i)
            {
                prime[j] = 1;
                if(!ret[j])
                    ret[j] = i;
            }
        }
    }
}
int main()
{
    isprime();
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n, d;
        scanf("%d%d",&n,&d);
        int tmp = (n-1)/d;
        if(n >= MAXN)
        {
            int ok = 0;
            for(int i=0; p[i]<=tmp; i++)
            {
                if(d%p[i]==0)
                {
                    ok = 1;
                    int ans = upper_bound(p,p+k,p[i])-p;
                    printf("%d\n",ans);
                    break;
                }
            }
            if(!ok)
            {
                int ans = upper_bound(p,p+k,tmp)-p;
                printf("%d\n",ans);
            }
            continue;
        }
        int Min = min(tmp,ret[d]);
        int ans = upper_bound(p,p+k,Min)-p;
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值