Acwing-198-反素数(约数, 数学)
2024-09-01 01:00:53
链接:
https://www.acwing.com/problem/content/200/
题意:
对于任何正整数x,其约数的个数记作g(x),例如g(1)=1、g(6)=4。
如果某个正整数x满足:对于任意的小于x的正整数 i,都有g(x)>g(i) ,则称x为反素数。
例如,整数1,2,4,6等都是反素数。
现在给定一个数N,请求出不超过N的最大的反素数。
思路:
考虑前11个素数的乘积大于2e9, 可以对n用前10个质数进行质数分解.
考虑2^31 > 2e9. 然后限制一下范围, DFS跑一遍.
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MAXV = 2e9;
int Pri[20] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
int Cnt[20];
LL cnt, res, n;
LL Pow(LL a, int b)
{
LL tmp = 1;
while (b > 0)
{
if (b&1)
tmp *= a;
a *= a;
b >>= 1;
}
return tmp;
}
void Dfs(int step, LL val)
{
// cout << step << ' ' << val << endl;
if (step > 10)
{
LL tmp = 1;
for (int i = 1;i <= 10;i++)
tmp *= (Cnt[i]+1);
if (tmp > cnt)
{
cnt = tmp;
res = val;
}
else if (tmp == cnt && val < res)
{
cnt = tmp;
res = val;
}
return;
}
for (int i = 0;i <= 30;i++)
{
if (val*Pow(Pri[step], i) > n)
break;
Cnt[step] = i;
Dfs(step+1, val*Pow(Pri[step], i));
}
}
int main()
{
memset(Cnt, 0, sizeof(Cnt));
scanf("%lld", &n);
if (n == 1)
{
puts("1");
return 0;
}
Dfs(1, 1);
printf("%lld\n", res);
return 0;
}
最新文章
- ASP.NET MVC Html.BeginForm 设置 timeout
- MySQL 游标
- HashPasswordForStoringInConfigFile中的Md5算法并非常用的Md5算法
- c语言是如何实现泛型链表
- Linux下网络故障诊断
- WP 类似扑克牌布局控件和类似扑克卡片控件
- python 发送邮件例子
- 框架应用 : Spring MVC - 开发详述
- shell编程--流程控制for,do-while,if-then,break,continue,case等
- C# 实体转为json字符串
- codeforces_Codeforces Round #541 (Div. 2)_abc
- 【译】Optaplanner开发手册本地化: (0) - 前言及概念
- JDK和CGLIB动态代理区别
- Good Bye 2018
- Linux基础命令---lp打印文件
- js md5 中文
- 【未完待续】API接口
- hadoop中 bin/hadoop fs -ls ls: `.&#39;: No such file or directory问题
- javascript进阶笔记(3)
- 在Mac上搭建ReactNative开发环境