hdu 4937 Lucky Number

时间:2022-06-30 19:14:16

虽然算法清晰的不能再清晰,但是实现总是边角料错这错那。

题目大意:

  给出n,找出一些进制,使得n在该进制下仅为3,4,5,6表示

解题思路:

  首先,4-10000进制直接枚举计算出每一位

  此外,最多只有3位,因为10000进制以上且小于1e12,最多3位,直接枚举每一位计算进制N即可

注意:如果类似我用二分或者直接求二次根式,要开个map储存已经计算出来的N进行判重,虽然数据比较弱可以不用判。最多4^3个吧,多了可能会重。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <deque>
#include <cmath>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
using namespace std; long long x,N;
int ans;
int a[];
map<int,bool> mapp; bool check(long long x)
{
if(x<N)
{
if(x>= && x<=) return ;
else return ;
}
for(int i=; i<=min(,N-); i++)
if((x-i)%N== && check((x-i)/N))
return ;
return ;
} void dfs(int p)
{
if(p== || p==)
{
long long l=1e4,r=1e12;
if(p==) r=1e6;
while(l<r-)
{
long long m=(l+r)/;
long long tmp=;
for(int i=; i<=p-; i++)
tmp=tmp*m+a[i];
if(tmp>x) r=m;
else l=m;
}
long long tmp=;
for(int i=; i<=p-; i++)
tmp=tmp*l+a[i];
if(tmp==x && l!=1e4 && !mapp[l])
{
ans++;
mapp[l]=;
}
if(l!=r && r!=1e4)
{
tmp=;
for(int i=; i<=p-; i++)
tmp=tmp*r+a[i];
if(tmp==x && !mapp[r])
{
ans++;
mapp[r]=;
}
}
}
if(p==) return;
for(int i=; i<=; i++)
{
a[p]=i;
dfs(p+);
}
}
int main()
{
//freopen("1003.in","r",stdin);
int tt;
scanf("%d",&tt);
for(int t=; t<=tt; t++)
{
mapp.clear();
scanf("%I64d",&x);
printf("Case #%d: ",t);
if(x>=&&x<=)
{
printf("-1\n");
continue;
}
ans=;
for(N=; N<=; N++)
if(check(x))
ans++;
dfs();
printf("%d\n",ans);
}
return ;
}