【AcWing 198】 反素数 dfs 数论

对于任何正整数x,其约数的个数记作g(x),例如g(1)=1、g(6)=4。

如果某个正整数x满足:对于任意的小于x的正整数 i,都有g(x)>g(i) ,则称x为反素数。

例如,整数1,2,4,6等都是反素数。

现在给定一个数N,请求出不超过N的最大的反素数。

输入格式
一个正整数N。

输出格式
一个整数,表示不超过N的最大反素数。

数据范围
1≤N≤2∗109
输入样例:
1000
输出样例:
840

题意:如题

思路:

依照题意,不难得出x是【1,x】中约数个数最大的那个,而且其中没有个数相等的(如果个数相等应该选那个数才对)。
1.因为因子个数之和唯一分解之后的幂次有关,又不能超过n,那我从小到大拿质因子最优。(次数相同的时候乘积更小,还能往后继续凑),就是按照2 3 5 7 1…这样的顺序拿。
2.顺延上面的贪心思路,我们把次数大的丢给前面的质数(小的质数)肯定也是更优的,理由同上。
3.这样发现2e9范围内能连续累乘的素数不超过9个,所以就可以拿着9个素数来dfs枚举次数即可。

AC代码:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

ll ps[] {2, 3 , 5 , 7, 11 , 13 ,  17 , 19, 23,  29};
ll n;
ll Map[15][33];
ll ans, num = 0;

void dfs(ll x, ll sum, ll up, ll cnt)
{
    if(cnt > num || cnt==num && sum < ans) ans = sum, num = cnt;
    if(x>=9) return;
    rep(i,1,up)
    {
        ll cur = qpow(ps[x],i);
        if(cur>n||cur*sum>n) break;
        dfs(x+1, sum*cur,i, cnt*(i+1));
    }
}

int main()
{
    n  = read(); ans = n;
    dfs(0,1, 30,1);
    cout<<ans<<'\n';
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45492531/article/details/107773002
198
今日推荐