BestCoder Round #84

1004 Dertouzos

思路:求最小质因子,质数个数

官方题解

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

d比较大的时候,​(n1)/d​​⌋比较小, 暴力枚举x即可. 当d比较小的时候, 可以直接预处理出答案. 阈值设置到10^6​​10^7​​都可以过.

ranklist 1的代码

 1 // #pragma comment(linker, "/STACK:102c000000,102c000000")
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <sstream>
 6 #include <string>
 7 #include <algorithm>
 8 #include <list>
 9 #include <map>
10 #include <vector>
11 #include <queue>
12 #include <stack>
13 #include <cmath>
14 #include <cstdlib>
15 // #include <conio.h>
16 using namespace std;
17 #define clc(a,b) memset(a,b,sizeof(a))
18 #define inf 0x3f3f3f3f
19 #define lson l,mid,rt<<1
20 #define rson mid+1,r,rt<<1|1
21 const int N = 2e5+10;
22 const int M = 1e6+10;
23 const int MOD = 1e9+7;
24 #define LL long long
25 #define mi() (l+r)>>1
26 double const pi = acos(-1);
27 const double eps = 1e-8;
28 void fre() {
29     freopen("in.txt","r",stdin);
30 }
31 // inline int r() {
32 //     int x=0,f=1;char ch=getchar();
33 //     while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();}
34 //     while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f;
35 // }
36 
37 bool np[M];
38 int pri[M],g[M];
39 int cnt;
40 void init(){
41     for(int i = 2; i < M; ++i) {
42         if(!np[i]) {
43             pri[cnt++] = i;
44             g[i] = i;
45         }
46         for(int j = 0; j < cnt && i * pri[j] < M; ++j) {
47             np[i*pri[j]] = 1;
48             g[i*pri[j]] = pri[j];
49             if(i % pri[j] == 0) break;
50         }
51     }
52 }
53 
54 int calc(int x){
55     int ans= upper_bound(pri, pri + cnt, x) - pri;
56     return ans;
57 }
58 int main(){
59     // fre();
60     init();
61     int T;
62     scanf("%d",&T);
63     while(T--){
64         int n,d;
65         scanf("%d%d",&n,&d);
66         n--;
67         if(n<=2*d){
68             printf("0
");
69             continue;
70         }
71         int ans=0;
72         if(d>=M){
73             int t=(n)/d;
74             int f=-1;
75             for(int i=0;pri[i]<=t;i++){
76                 if(d%pri[i]==0){
77                     f=pri[i];
78                     break;
79                 }
80             }
81             if(f==-1) ans=calc(t);
82             else ans=calc(f);
83         }
84         else{
85             int f=min(g[d],(n)/d);
86             ans=calc(f);
87         }
88         printf("%d
",ans);
89     }
90     return 0;
91 }
原文地址:https://www.cnblogs.com/ITUPC/p/5700951.html