极难的问题 | ||||||
| ||||||
Description | ||||||
那年一个菊苣问了asuka几个问题,把asuka花式吊打。 asuka思考了一下,不能光我一个人被吊打啊,于是他决定把这个题出出来。 如果一个数字中含有4或者7,那么这个数字就是不好看的,因为4明显不ok,而7的横折的弧度不太符合黄金分割,当然这个理由是我瞎编的。 那么问题来了,第n个好看的数字是多少呢? | ||||||
Input | ||||||
第一行一个t t<=10000 代表数据组数 每组的第一行一个n 1<=n<=1e9意义如题面。 | ||||||
Output | ||||||
对于每一组输出一个x代表第n个好看的数字 | ||||||
Sample Input | ||||||
2 1 7 | ||||||
Sample Output | ||||||
1 9 | ||||||
Source | ||||||
“科林明伦杯”计算机学院2018年程序设计竞赛 |
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define LL long long
LL f[11][11];
LL a[16];
void init()
{
f[0][0]=1;
for(int i=1;i<11;i++)
{
for(int j=0;j<10;j++)
{
if(j==4||j==7)f[i][j]=0;
else
for(int k=0;k<10;k++)
f[i][j]+=f[i-1][k];
}
}
}
LL solve(LL n)
{
int pos=1;
while(n)
{
a[pos++]=n%10;
n/=10;
}
a[pos]=0;
LL ans=0;
for(int i=pos-1;i>=1;i--)
{
for(int j=0;j<a[i];j++)
{
ans+=f[i][j];
// cout<<"a[i]"<<a[i]<<"j="<<j<<" ans="<<ans<<endl;
}
if(a[i]==4||a[i]==7)break;
if(i==1)ans+=f[i][a[i]];//注意这一点 f[i][j]表示的是有i位数 最高位为j的满足题意的个数
} //这一步得到<=n满足题意的个数
return ans-1;//此题要求从1-n 所以要减去1 (ans 为0 - n 满足题意的个数)
}
void work(LL n)
{
LL l=1;
LL r=10000000000LL;
LL ans,mid;
while(l<=r)
{
mid=(l+r)/2;
//cout<<"mid="<<mid<<" solve mid="<<solve(mid)<<endl;
if(solve(mid)>=n)
{
r=mid-1;
}
else l=mid+1;
}
cout<<l<<endl;
}
int main()
{
int t;
cin>>t;
init();
while(t--)
{
int n,m;
cin>>n;
// cout<<solve(n)<<endl;
work(n);
}
}