题目传送门
一句话:给定一个正整数 n n n ,对于所有不超过 n n n的正整数,找到包含约数最多的一个数。如果有多个这样的数,那么回答最小的那个。
25 p s 25ps 25ps
纯暴力
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34#include<iostream> #include<cstdio> #include<cmath> #define ll long long using namespace std; ll n,c; ll num,maxnum=2; int main() { scanf("%lld",&n); for(ll i=1;i<=n;i++){ if(i%2==0){//偶数约数一定多 num=2; for(ll j=2;j<=sqrt(i);j++){ if(i%j==0){ if(j==i/j){ num++; } else if(j<i/j){ num+=2; } else break;//当j>i/j时,在上一步已被记录,所以直接break } } if(num>maxnum) maxnum=num,c=i; } } if(n==1) c=1;// 特判 printf("%lld",c); return 0; }
42 p s 42ps 42ps
暴力 + + + 打表
将
1
0
3
10^3
103 到
1
0
6
10^6
106 用暴力挂出来,直接输出即可。这样得分高
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38#include<iostream> #include<cstdio> #include<cmath> #define ll long long using namespace std; ll n,c; ll num,maxnum=2; int main() { scanf("%lld",&n); if(n>1000&&n<=1e6){ cout<<720720<<endl; return 0; } for(ll i=1;i<=n;i++){ if(i%2==0){ num=2; for(ll j=2;j<=sqrt(i);j++){ if(i%j==0){ if(j==i/j){ num++; } else if(j<i/j){ num+=2; } else break; } } if(num>maxnum) maxnum=num,c=i; } } if(n==1) c=1; printf("%lld",c); return 0; }
100 p s 100ps 100ps
数学知识:一个合数的约数个数等于它所有的 质因数 的指数加一再相乘。
举个例子,若合数 x x x = = = a 1 b 1 a_1^{b_1} a1b1 + + + a 2 b 2 a_2^{b_2} a2b2 + + + . . . ... ... + + + a n b n a_n^{b_n} anbn,则合数 x x x的约数个数为 ( b 1 + 1 ) (b_1+1) (b1+1) ∗ * ∗ ( b 2 + 1 ) (b_2+1) (b2+1) ∗ * ∗ . . . ... ... ∗ * ∗ ( b n + 1 ) (b_n+1) (bn+1)。
知道这个就比较好做了,建一个 p r i m e [ ] prime[] prime[] 数组来存 1 1 1 至 100 100 100 的素数 ,然后 d f s dfs dfs 随时更新最多约数和拥有最多约数的数,并且把当时搜到的数 x x x 的素数的倍数 y y y(有许多) 一起算出来,只需将 x x x ∗ * ∗ ( ( ( x 约 数 个 数 约数个数 约数个数+ 1 ) 1) 1)即可。对于任意一个合数 y y y ,它的约数是多个的,每遇到一个约数 x x x ,就改变 y y y 的约数个数,当搜到 y y y 时,也就完成了,直接返回即可。
M y My My C o d e Code Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #define ll long long using namespace std; inline ll read() { ll s=0,w=1; char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-') w=-1; ch=getchar(); } while(ch>='0'&&ch<='9'){ s=s*10+ch-'0'; ch=getchar(); } return s*w; } ll n,q,cnt,p; ll prime[200]; void dfs(ll ks,ll js,ll ysgs,ll ys) {//ks=开始搜的数,js=结束搜的数,ysgs=约数个数,ys=约数 if(ysgs==p&&ys<q) q=ys; if(ysgs>p) q=ys,p=ysgs;//随时更新 ll t=ys*prime[ks];//把他的倍数一起算出 for(ll i=1;i<=js,t<=n;i++,t*=prime[ks]) dfs(ks+1,i,ysgs*(i+1),t);//原本+新加入倍数中 return; //返回 } int Prime(int k)//求素数 { if(k==1) return 0; if(k==2) return 1; for(int i=2;i<=sqrt(k);i++) if(k%i==0) return 0; return 1; } int main() { n=read(); for(int i=1;i<=100;i++) if(Prime(i)) prime[++cnt]=i;//记录 dfs(1,5000,1,1);//5000可以开的大一点,只要保证搜完即可 printf("%lld",q); return 0; }
完结撒花~~
最后
以上就是爱撒娇秋天最近收集整理的关于[YBtOJ]最多约数的全部内容,更多相关[YBtOJ]最多约数内容请搜索靠谱客的其他文章。
发表评论 取消回复