Description
任务:编一个程序:
1、从ANT.IN中读入自然数n。
2、计算不大于n的最大Antiprime数。
3、将结果输出到ANT.OUT中。
Input
Output
Sample Input
Sample Output
Source
问题可以转化成求n以内约数最多的数,约数相同则取小的。
逆用唯一分解定理,从小到大枚举每个质因数的使用个数(由数据范围限定最多枚举到23),搜索答案。
1 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 #define LL long long 8 using namespace std; 9 const int pri[11]={0,2,3,5,7,11,13,17,19,23,0}; 10 LL ans=0; 11 LL mx=0; 12 LL n; 13 void dfs(LL now,LL res,int last_mx,int pos){ 14 //当前累计值,当前累计因数个数,上个质因数使用次数,枚举位置 15 if(res>mx || (res==mx && now<ans)){ 16 mx=res; ans=now; 17 } 18 if(pos==10)return; 19 for(int cnt=1;cnt<=last_mx;cnt++){ 20 now*=pri[pos]; 21 if(now>n)return; 22 dfs(now,res*(cnt+1),cnt,pos+1); 23 } 24 return; 25 } 26 int main(){ 27 scanf("%lld",&n); 28 dfs(1,1,500,1); 29 printf("%lld\n",ans); 30 return 0; 31 }