P1820 寻找AP数

P1820 寻找AP数
两个性质,分解质因数后,连续,且指数递减,dfs就完了

#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
#define inf 2147483647
#define N 1000010
#define p(a) putchar(a)
#define For(i,a,b) for(long long i=a;i<=b;++i)
//by war
//2019.8.19
using namespace std;
long long n,cnt,ans1,ans2;//ans1是数的大小,ans2是因数个数
long long prime[N];
bool vis[N];
void in(long long &x){
    long long y=1;char c=getchar();x=0;
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    x*=y;
}
void o(long long x){
    if(x<0){p('-');x=-x;}
    if(x>9)o(x/10);
    p(x%10+'0');
}

void Euler(){
    For(i,2,1e6){
        if(!vis[i]) prime[++cnt]=i;
        for(long long j=1;j<=cnt&&i*prime[j]<=1e6;j++){
            vis[i*prime[j]]=1;
            if(i%prime[j]==0)
                break;
        }
    }
}

long long ksm(long long a,long long b){
    long long r=1;
    while(b>0){
        if(b&1)
            r*=a;
        a*=a;
        b>>=1;
    }
    return r;
}

void dfs(long long id,long long cnt,long long num,long long m){//第几个质数,指数,大小,因数个数
    if(m==ans2)
        ans1=min(ans1,num);
    if(m>ans2){
        ans1=num;
        ans2=m;
    }
    For(i,1,cnt)
        if(ksm(prime[id],i)<=n&&num*ksm(prime[id],i)<=n)
            dfs(id+1,i,num*ksm(prime[id],i),m*(1+i));
        else
            return;
}

signed main(){
    Euler();
    while(cin>>n){
        ans1=0;ans2=0;
        For(i,1,63)
            if(((long long)1<<i)<=n)
                dfs(2,i,((long long)1<<i),1+i);
            else
                break;
        o(ans1);p('\n');//o(ans2);p('\n');
    }
    return 0;
}

 

posted @ 2019-08-19 15:48  WeiAR  阅读(218)  评论(0编辑  收藏  举报