题目链接:
Bestcoder #82 Div2 ztr loves lucky numbers
题意:
ztr喜欢幸运数字,他对于幸运数字有两个要求
1:十进制表示法下只包含4、7
2:十进制表示法下4和7的数量相等
比如47,474477就是
而4,744,467则不是
现在ztr想知道最小的但不小于n的幸运数字是多少?
有
T(1≤T≤105)
组数据,每组数据一个正整数
n
,
分析:
比赛时我是当成字符串来处理,贼麻烦,而且WA了好多。。。
看了别人的代码,发现可以预处理出用next_permutation预处理出所有可能的答案组合,然后用lower_bound()查找即可。真好用啊。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <cmath>
#include <ctime>
#include <cassert>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0)
using namespace std;
const int MAX_N=1000010;
typedef long long ll;
int total,bit[30];
ll ans[MAX_N];
void init()
{
total=0;
for(int i=2;i<=18;i+=2){
for(int j=0;j<i/2;j++) { bit[j] = 4; }
for(int j=i/2;j<i;j++) { bit[j] = 7; }
do{
for(int j=0;j<i;j++){
ans[total]=ans[total]*10+bit[j];
}
//printf("%lld\n",ans[total]);
total++;
}while(next_permutation(bit,bit+i));
}
//printf("total=%d\n",total);
}
int main()
{
IOS;
freopen("B.in","r",stdin);
init();
int T;
ll n;
cin>>T;
while(T--){
cin>>n;
if(n > 777777777444444444uLL){
printf("44444444447777777777\n");
continue;
}
int pos=lower_bound(ans,ans+total,n)-ans;
cout<<ans[pos]<<endl;
}
return 0;
}