BestCoder Round #84 1004 hdu 5750 数论



链接:戳这里


Dertouzos

Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

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
 

思路:

找出d的第一个素数p,然后统计比p还小的素数*d<n的贡献

这里的剪枝是:d的第一个素数肯定小于等于n/d


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 50000
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int prime[50005],vis[50005];
int sum[50005];
ll n,d;
struct node{
    int v,num;
}s[50005];
int main(){
    int cnt=0;
    for(int i=2;i<=MAX;i++){
        if(!vis[i]){
            prime[cnt++]=i;
        }
        for(int j=0;j<cnt;j++){
            if(i*prime[j]>MAX) break;
            vis[i*prime[j]]=1;
            if(i%prime[j]==0) break;
        }
    }
    int tot=0;
    for(int i=2;i<=MAX;i++){
        if(vis[i]) continue;
        s[++tot].v=i;
        s[tot].num=s[tot-1].num+1;
    }
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%I64d%I64d",&n,&d);
        if(n<=d){
            cout<<0<<endl;
            continue;
        }
        int f=-1;
        for(int i=0;prime[i]<=n/d;i++){
            if(d%prime[i]==0){
                f=prime[i];
                break;
            }
        }
        if(f==-1) f=d;
        int l=1,r=tot,mid,x=-1;
        while(l<=r){
            mid=(l+r)/2;
            if(s[mid].v<=f){
                l=mid+1;
                x=mid;
            } else r=mid-1;
        }
        if(x==-1){
            cout<<0<<endl;
            continue;
        }
        l=1;r=x;
        int ans=-1;
        while(l<=r){
            mid=(l+r)/2;
            ll tmp=1LL*s[mid].v*d;
            if(tmp<n){
                l=mid+1;
                ans=mid;
            } else r=mid-1;
        }
        if(ans==-1) {
            cout<<0<<endl;
            continue;
        }
        printf("%d\n",s[ans].num);
    }
    return 0;
}
/*
1
1000000000 239641
573

*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值