跳到正文

个人记录

最多约数

Da_un约 5 分钟
原文
文章信息与版本

文章信息

发布
2021年8月7日
收录
2026年8月1日
更新
2026年11月16日
篇幅
约 5 分钟
获赞
2

版本

  1. v1当前

题目传送门

25ps25ps

纯暴力

C++
#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;
}

42ps42ps

暴力 ++ 打表

将 10310^3 到 10610^6 用暴力挂出来,直接输出即可。这样得分高

C++
#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;
}

100ps100ps

数学知识:一个合数的约数个数等于它所有的 质因数 的指数加一再相乘。

举个例子,若合数 xx == a1b1a_1^{b_1} ++ a2b2a_2^{b_2} ++ ...... ++ anbna_n^{b_n},则合数 xx的约数个数为(b1+1)(b_1+1) ∗* (b2+1)(b_2+1) ∗* ...... ∗* (bn+1)(b_n+1)。

知道这个就比较好做了,建一个 prime[]prime[] 数组来存 11 至 100100 的素数 ,然后 dfsdfs 随时更新最多约数和拥有最多约数的数,并且把当时搜到的数 xx 的素数的倍数 y(许多)y(许多) 一起算出来,只需将 xx ∗* (( x约数个数约数个数+1)1)即可。对于任意一个合数 yy ,它的约数是多个的,每遇到一个约数 xx ,就改变 yy 的约数个数,当搜到 yy 时,也就完成了,直接返回即可。

MyMy CodeCode

C++
#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;
}

集训留的题,终于A掉了o(╥﹏╥)o

原文
抓取于
  1. Vaino

    %%%