题目:http://acm.timus.ru/problem.aspx?space=1&num=1748

题意:求n范围内约数个数最多的那个数。

Roughly speaking, for a number to be highly composite it has to have prime factors as small as possible, but not too many of the same. If we decompose a number n in prime factors like this:

where are prime, and the exponents are positive integers, then the number of divisors of n is exactly

Hence, for n to be a highly composite number,

  • the k given prime numbers pi must be precisely the first k prime numbers (2, 3, 5, ...); if not, we could replace one of the given primes by a smaller prime, and thus obtain a smaller number than n with the same number of divisors (for instance 10 = 2 × 5 may be replaced with 6 = 2 × 3; both have four divisors);
  • the sequence of exponents must be non-increasing, that is ; otherwise, by exchanging two exponents we would again get a smaller number than n with the same number of divisors (for instance 18 = 21 × 32 may be replaced with 12 = 22 × 31; both have six divisors).

c++ 代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; #define ll long long
ll n, ansa, ansb;
int p[] = {, , , , , , , , , , , , , , }; void dfs(int pos, ll num, int div, int limit )
{
if ( div>ansb || (div==ansb && num<ansa) ){
ansa = num; ansb = div;
}
if (pos == ) return ; for ( int i=; i<=limit; ++i )
{
if ( n/num < p[pos]) break;
num *= p[pos];
dfs(pos+, num, div*(i+), i);
}
} int main(int argc, char**argv)
{
int T; scanf("%d", &T);
while ( T-- ){
//scanf("%lld", &n);
cin >> n;
if (n==) {
puts("1 1"); continue;
}
ansa = ansb = -;
dfs(, , , );
//printf("%lld %lld\n", ansa, ansb);
cout << ansa << ' ' << ansb << endl;
}
return EXIT_SUCCESS;
}

python 代码  注意这代码会TLE,具体是什嘛原因,我没有具体查了。貌似测试10^18都很快的。

p= [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
ansa=0; ansb=0 def dfs(pos, num, div, limit, n):
global ansa, ansb
if div>ansb or ( div==ansb and ansa>num ):
ansa = num; ansb = div
if pos == 15: return for i in xrange(1, limit+1):
if (n/num) < p[pos] : return
num *= p[pos]
dfs(pos+1, num, div*(i+1), i, n) if __name__ == '__main__':
T = input()
while T:
T -= 1
n = input()
if n == 1:
print 1, 1; continue
ansa = ansb = -1
dfs(0, 1, 1, 60, n)
print ansa, ansb

这种数有点类似与丑数,对于丑数的求法。

丑数:因子只含2,3,5的数。 例如求第n个丑数。

bruteforce 是有用的。效率太低了。

如果有一个丑数数组,那么这个数组接下来一个丑数会是哪个数呢?毫无疑问这个数是有数组里的元素乘上2,3,5里的某一个数,这个数满足大于当前丑数数组最后一个元素,最小的满足这个条件的数就是下一个丑数。即求min( 2*a, 3*b, 5*c ) ,枚举求2a,3b5c是可行的。但二分会是很不错的选择, 复杂度为o(n*3logn)。

def find(p, x):
l = 0; r = len(p)-1
while l<=r:
mid = (l+r)>>1
if x*p[mid]>p[-1]:
r = mid-1
else : l = mid+1
return x*p[l] def solve(n):
p=[1]; cnt = 0 # cnt
while cnt < n:
cnt += 1
a = find(p, 2)
b = find(p, 3)
c = find(p, 5)
p.append(min(a, b, c) )
print p[-1] if __name__ == '__main__':
n = input()
solve(n)

ural 1748 The Most Complex Number 和 丑数的更多相关文章

  1. URAL 1748. The Most Complex Number(反素数)

    题目链接 题意 :给你一个n,让你找出小于等于n的数中因子个数最多的那个数,并且输出因子个数,如果有多个答案,输出数最小的那个 思路 : 官方题解 : (1)此题最容易想到的是穷举,但是肯定超时. ( ...

  2. URAL 1748 The Most Complex Number

    题目链接:https://vjudge.net/problem/11177 题目大意: 求小于等于 n 的最大反素数. 分析: n <= 10^18,而前20个素数的乘积早超过10^18,因此可 ...

  3. LeetCode OJ:Ugly Number(丑数)

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  4. 【easy】263. Ugly Number 判断丑数

    class Solution { public: bool isUgly(int num) { ) return false; ) return true; && num % == ) ...

  5. 264 Ugly Number II 丑数 II

    编写程序找第 n 个丑数.丑数就是只包含质因子 2, 3, 5 的正整数.例如, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 就是前10个丑数.注意:1. 1 一般也被当做丑数2. ...

  6. 313 Super Ugly Number 超级丑数

    编写一段程序来寻找第 n 个超级丑数.超级丑数是指其所有质因数都在长度为k的质数列表primes中的正整数.例如,[1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] ...

  7. Leetcode264. Ugly Number II丑数2

    编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 ...

  8. [LeetCode]313. Super Ugly Number超级丑数,丑数系列看这一道就行了

    丑数系列的题看这一道就可以了 /* 和ugly number2差不多,不过这次的质因子多了,所以用数组来表示质因子的target坐标 target坐标指的是这个质因子此次要乘的前任丑数是谁 */ pu ...

  9. LeetCode OJ 之 Ugly Number (丑数)

    题目: Write a program to check whether a given number is an ugly number. Ugly numbers are positive num ...

随机推荐

  1. 说说对C语言指针的理解

    指针困扰了一些学习编程的人,或许你的老师会告诉你,指针比较难理解. 我当时被老师的话唬住所以学习指针那章的时候都没心情听课.(说得像讲别的内容时我听了似的,开玩笑) 导致了学习链表的时候各种卧槽. * ...

  2. PL/SQL — 集合及常用方法

    PL/SQL中提供了常用的三种集合联合数组.嵌套表.变长数组,而对于这几个集合类型中元素的操作,PL/SQL提供了相应的函数或过程来操纵数组中的元素或下标.这些函数或过程称为集合方法.一个集合方法就是 ...

  3. Get your Windows product key from a script

    The product key is located in the registry under HKLM\Software\Microsoft\Windows NT\CurrentVersion I ...

  4. C++练习题

    1. 用面向对象的程序描述员工拥有的股票,股票有公司,价格,数量属性,且拥有展现基本数据,更新价格,买进,卖出操作,并具有比较两个股票对象股值大小的比较方法. 2. 用面向对象的程序描述一个栈的操作, ...

  5. eclipse, Log4j配置(真心的详细~)

    转自: http://www.cnblogs.com/alipayhutu/archive/2012/06/21/2558249.html a). 新建Java Project>>新建pa ...

  6. 如何登录mysql? cmd怎么连接mysql数据库

    Mysql开源数据库,任何人都可以下载安装使用.那么安装好的mysql如何登陆连接mysql数据库呢? 连接mysql数据库的几种方法 一 Mysql命令行连接 一般对于刚刚安装好的mysql,如果勾 ...

  7. 【Linux安全】查看是否存在特权用户以及是否存在空口令用户

    查看是否存在特权用户 通过判断uid是否为0来查找系统是否存在特权用户,使用命令awk即可查出. [root@pentester ~]# awk -F: '$3==0 {print $1}' /etc ...

  8. 【HDOJ】1706 The diameter of graph

    这么个简单的题目居然没有人题解.floyd中计算数目,同时注意重边. /* 1706 */ #include <iostream> #include <string> #inc ...

  9. mac 修改密码后 频繁输入钥匙串问题修复方法

    就一句话就是 清空钥匙串缓存 下面是具体方法 进入硬盘目录-->资源库-->Keychains 删除里面的文件夹(这个文件夹里面有 keychain-2.db keychain-2.db- ...

  10. Wpf配置文件属性

    public MainWindow() { InitializeComponent(); this.WindowState = Properties.Settings.Default.WindowSt ...