很好的思维题,搜索的剪枝用到极致了,一开始用CF上27E的搜索各种超时,总之做得很不顺,心情也不是很好,幸好找了个朋友二了一下。
ACcode:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
const int ns=49000,top=15;
const LL INF=((LL)1<<62)+1;
LL ans;
int m,k,type,cnt;
int ip[ns+10];
int p[100]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47};
void prim()
{
for (int i=1;i<=ns;i++)
{
ip[i]+=i;
for (int j=i;j<=ns;j+=i)
{
ip[j]--;
}
if (!ip[ip[i]]) ip[ip[i]]=i;
ip[i]=0;
}
}
void dfs(int fm,LL t,int pos)
{
if (fm==k&&ans>t) ans=t;
if (fm>=k||(pos>=top)) return ;
for (int i=1; i<=62; i++)
{
if (t>ans/p[pos]||fm*(i+1)>k) break;
t*=p[pos];
if (k%(fm*(i+1))==0)
dfs(fm*(i+1),t,pos+1);
}
}
int main()
{
prim();
int T,cas=0;
scanf("%d",&T);
while (T--)
{
scanf("%d%d",&type,&k);
if (type)
{
ans=ip[k];
}
else
{
ans=INF;
dfs(1,1,0);
}
printf("Case %d: ",++cas);
if (ans==0) printf("Illegal\n");
else if (ans>=INF) printf("INF\n");
else printf("%I64d\n",ans);
}
return 0;
}