【BZOJ】1053: [HAOI2007]反素数ant(贪心+dfs)

http://www.lydsy.com/JudgeOnline/problem.php?id=1053

约数个数等于分解出的质因数的(指数+1)的乘积这个就不用说了吧。。。

然后好神的题在于贪心。。。orz

首先分解质因子后,较小的数的指数一定大于等于较大的数的指数。(否则可以将较大的数多出来的质数填到小的数那里也符合条件)

然后对于约数个数相同的数,那么选最小的数(显然的吧)

所以按前边的分析连乘12个质因子就会超过20亿,因此只需要枚举12个即可

然后就过了orz

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mkpii make_pair<int, int>
#define pdi pair<double, int>
#define mkpdi make_pair<double, int>
#define pli pair<ll, int>
#define mkpli make_pair<ll, int>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
#define printarr1(a, b) for1(_, 1, b) cout << a[_] << '\t'; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }

const ll p[13]={0,2,3,5,7,11,13,17,19,23,29,31,37};
ll n;
int ans1, ans2;
void dfs(int now, ll sum, int cnt, int last) {
	if(sum>n || now>12) return;
	if(ans2==cnt && ans1>sum) ans1=sum;
	if(ans2<cnt) ans1=sum, ans2=cnt;
	ll t=1;
	for1(i, 0, last) {
		dfs(now+1, sum*t, cnt*(i+1), i);
		t*=p[now];
		if(t>n) return;
	}
}

int main() {
	read(n);
	dfs(1, 1, 1, 30);
	print(ans1);
	return 0;
}

  

 


 

 

Description

 

对于任何正整数x,其约数的个数记作g(x)。例如g(1)=1、g(6)=4。
如果某个正整数x满足:g(x)>g(i) 0<i<x,则称x为反质数。例如,整数1,2,4,6等都是反质数。
现在给定一个数N,你能求出不超过N的最大的反质数么?

Input

一个数N(1<=N<=2,000,000,000)。

Output

不超过N的最大的反质数。

Sample Input

1000

Sample Output

840

HINT

 

Source

 

 
posted @ 2014-11-14 17:37  iwtwiioi  阅读(468)  评论(0编辑  收藏  举报