HDU 5750 Dertouzos

Dertouzos

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

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

思路:

题目要求的是,在小于n的范围内,找出所有最大除数为d的数。假设满足该条件的一个数y,那么y=x*d,x为y的最小质因子。因为x已经无法再分解为2

个大于1的数 x=a*b,使得  y=a*(b*d)。 接下来再看,另slove(n)表示n的最小质因子。如果d是非质数 那么d=slove(d)*k,y=x*k*slove(d)

并且x*k<=d,由此可得x<=solve(d),因为若x>solve(d),那么x*k>d,d就不是y的最大除数了;如果d是质数,那么solve(d)=d,也满足以上结

论。

同时题目还限定范围小于n,那么可以得出x<=(n-1)/d。

综上所述,取2种情况的并集,那么就是 x<=min(solve(d),(n-1)/d)并且x是质数。并且由于(n-1)/d,那么x最大不会超过sqrt(n),所以先把

sqrt(1e9)内的素数先筛选 用数组存起来,然后再暴力枚举x就可以了。

 1 #include <iostream>
 2 #include <queue>
 3 #include <stack>
 4 #include <cstdio>
 5 #include <vector>
 6 #include <map>
 7 #include <set>
 8 #include <bitset>
 9 #include <algorithm>
10 #include <cmath>
11 #include <cstring>
12 #include <cstdlib>
13 #include <string>
14 #include <sstream>
15 #define lson l,m,rt*2
16 #define rson m+1,r,rt*2+1
17 #define mod 1000000007
18 #define INF 1000000006
19 using namespace std;
20 typedef long long LL;
21 int n,d,k,p;
22 vector<LL> Q;
23 bool vis[1000000+5];
24 void init()
25 {
26     for(LL i=2;i<=1000000+5;i++)
27     {
28         if(!vis[i])
29         {
30             Q.push_back(i);
31             for(LL j=i*i;j<=1000000+5;j+=i)
32             {
33                 vis[j]=true;
34             }
35         }
36     }
37 }
38 int main()
39 {
40 #ifdef Local
41     freopen("data.txt","r",stdin);
42 #endif
43     int T,i,j,h,r,sum=0,m,ans,flag,q,mid,l;
44     cin>>T;
45     init();
46     while(T--)
47     {
48         ans=flag=0;
49         scanf("%d%d",&n,&d);
50         n=(n-1)/d;
51         for(i=0;Q[i]<=n&&i<Q.size();i++)
52         {
53             ans++;
54             if((d%Q[i])==0)break;
55         }
56         cout<<ans<<endl;
57     }
58 }

时间: 2024-08-07 17:01:25

HDU 5750 Dertouzos的相关文章

hdu 5750 Dertouzos 素数

Dertouzos Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1861    Accepted Submission(s): 584 Problem Description A positive proper divisor is a positive divisor of a number n, excluding n its

HDU 5750 Dertouzos 简单数学

感悟:这又是zimpha巨出的一场题,然后04成功fst(也就是这题) 实际上还是too young,要努力增加姿势, 分析:直接枚举这些数不好枚举,换一个角度,枚举x*d,也就是d的另一个乘数是多少 显然  x<=min(d,(n-1)/d),x还得是质数,最后发现x必须小于d的最小因子 然后预处理10w以内的素数,然后每次先得到k=min(d,(n-1)/d),然后看d最小因子是否小于k 这题的关键就在找d的最小因子,我是暴力找的(然后碰上全是大素数就t了) 实际上当d是大素数的时候,k很小

【HDU 5750】Dertouzos(数学)

题目给定n和d,都是10的9次方以内,求1到n里面有几个数最大因数是d?1000000组数据.解:求出d的满足p[i]*d<n的最小质因数是第几个质数.即为答案. #include<cstdio> #define N 100002 int t,n,d,pr[N],p[N],num; int main(){ for(int i=2;i<N;i++)if(!pr[i]){ for(int j=i+i;j<N;j+=i) pr[j]=1; p[++num]=i; } scanf(&

hud 5750 Dertouzos

Dertouzos Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1415    Accepted Submission(s): 443 Problem Description A positive proper divisor is a positive divisor of a number n, excluding n its

hdu 5750(数论)

Dertouzos Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 891    Accepted Submission(s): 274 Problem Description A positive proper divisor is a positive divisor of a number n, excluding n itse

hdu 5750

AC: #include <cstdio> #include <algorithm> #include <cstring> #define maxn 100005 using namespace std; bool check[maxn]; int pr[maxn / 5], mn[maxn]; int tt[maxn]; int tot; int T; void euler() { memset(check,0,sizeof(check)); tot=0; //x=1

HDU 6203 ping ping ping [LCA,贪心,DFS序,BIT(树状数组)]

题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=6203] 题意 :给出一棵树,如果(a,b)路径上有坏点,那么(a,b)之间不联通,给出一些不联通的点对,然后判断最少有多少个坏点. 题解 :求每个点对的LCA,然后根据LCA的深度排序.从LCA最深的点对开始,如果a或者b点已经有点被标记了,那么continue,否者标记(a,b)LCA的子树每个顶点加1. #include<Bits/stdc++.h> using namespace std;

HDU 5542 The Battle of Chibi dp+树状数组

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:给你n个数,求其中上升子序列长度为m的个数 可以考虑用dp[i][j]表示以a[i]结尾的长度为j的上升子序列有多少 裸的dp是o(n2m) 所以需要优化 我们可以发现dp的第3维是找比它小的数,那么就可以用树状数组来找 这样就可以降低复杂度 #include<iostream> #include<cstdio> #include<cstring> #include

hdu 1207 汉诺塔II (DP+递推)

汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4529    Accepted Submission(s): 2231 Problem Description 经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往