您的位置:首页 > 其它

BestCoder Round #84 1004 hdu 5750 数论

2016-07-23 22:09 267 查看
链接:戳这里

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

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: