这道题一开始想的是能拆分的指数至少是4,然后就是6,8这样的,所以枚举2^16内的质数,用set存它们的偶数次幂,注意判断溢出就行。结果发现512=2^9忽略了这种情况。再一看网上很多博客,发现指数只要是合数就行(能够拆分),总结出如下思路:
对于a^x,x是合数一定满足条件,所以筛出64以内的合数(打表),枚举底数,判断溢出。
当枚举底数a时,可以有指数x的剪枝:(a^x)<(2^64-1) => x<ln(2^64-1)/ln(i)(换底公式)这个公式不能直接用QAQ...需要转换一下。。看了网上代码都是变成了酱紫:x=ceil(64*log(2)/log(i))-1。好神奇的操作_(:з」∠)_
另外第一次用iterator,发现真的太好用了。。附上AC代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
#define ll unsigned long long
const int com[100]=
{
4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 26,
27, 28, 30, 32, 33, 34, 35, 36, 38, 39, 40, 42, 44, 45, 46,
48, 49, 50, 51, 52, 54, 55, 56, 57, 58, 60, 62, 63, 64,//必须有64,不然多输出一个0
};//合数表,指数的所有情况
set<ll>st;
int main()
{
st.clear();
for(ll i=2;i<(1<<16);i++)//底数
{
int x=ceil(64*log(2)/log(i))-1;//指数的最大值
ll tmp=i*i*i*i;
st.insert(tmp);
for(int j=1;com[j]<=x;j++)
{
if(com[j]-com[j-1]==1)
tmp*=i;
else
tmp*=i*i;
st.insert(tmp);
}
}
printf("1\n");
for(set<ll>::iterator i=st.begin();i!=st.end();i++)
printf("%llu\n",*i);
return 0;
}