反素數

反素數定義
大佬博客
題目鏈接
給定一個n求因子個數爲n的最小的值是多少。

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define ULL unsigned long long
using namespace std;
const ULL inf=~0ULL;
int p[16] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53};
ULL ans;
int n;
void dfs(int dep,ULL tmp,int num)//dep爲當前深度也就是枚舉到那個素數了,tmp爲當前的值是多少,num爲當前tmp的因子個數
{
    if(num>n) return ;
    if(num==n) ans=min(ans,tmp);
    for(int i=1;i<=63;i++)
    {
        if(ans/p[dep]<tmp) break;//剪枝,如果用*的話害怕會爆long long
        dfs(dep+1,tmp*=p[dep],num*(i+1));
    }
}
int main()
{
    while(~scanf("%lld",&n))
    {
        ans=inf;
        dfs(0,1,1);
        printf("%llu\n",ans);
    }
}

題目鏈接
題意:給定一個n求n以內的因子個數最多的數。

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define ULL unsigned long long
using namespace std;
const ULL inf=~0ULL;
int p[16] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53};
ULL ans,ans1;
LL n;
void dfs(int dep,ULL tmp,int num)//dep爲當前深度也就是枚舉到那個素數了,tmp爲當前的值是多少,num爲當前tmp的因子個數
{
    if(dep>=16) return ;
    if(num>ans)//ans記錄最多的因子個數
        ans1=tmp,ans=num;
    else if(num==ans)
        ans1=min(ans1,tmp);
    for(int i=1; i<=63; i++)//枚舉次冪
    {
        if(n/p[dep]<tmp) return;//剪枝,如果用*的話害怕會爆long long
        tmp*=p[dep];
        if(tmp>n) return ;
        dfs(dep+1,tmp,num*(i+1));
    }
}
int main()
{
    while(~scanf("%lld",&n))
    {
        ans=0;
        ans1=inf;
        dfs(0,1,1);
        printf("%llu\n",ans1);
    }
}

題目鏈接

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define ULL unsigned long long
using namespace std;
const ULL inf=~0ULL;
int p[16] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53};
ULL ans,ans1;
LL n;
void dfs(int dep,ULL tmp,int num,int limt)//dep爲當前深度也就是枚舉到那個素數了,tmp爲當前的值是多少,num爲當前tmp的因子個數,limt代表前一個素數的次冪是多少
{
    if(num>ans)//ans記錄最多的因子個數
        ans1=tmp,ans=num;
    else if(num==ans)
        ans1=min(ans1,tmp);
    for(int i=1; i<=limt; i++)//枚舉次冪.運用反素數第二個性質剪枝
    {
        if(n/p[dep]<tmp) return;//剪枝,如果用*的話害怕會爆long long
        tmp*=p[dep];
        if(tmp>n) return ;
        dfs(dep+1,tmp,num*(i+1),i);
    }
}
int main()
{
    int ncase;
    scanf("%d",&ncase);
    while(ncase--)
    {
        scanf("%lld",&n);
        ans=0,ans1=inf;
        dfs(0,1,1,63);
        printf("%llu %llu\n",ans1,ans);
    }
}
發佈了110 篇原創文章 · 獲贊 18 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章