Description
Lucky number is super lucky if it's decimal representation contains equal amount of digits 4 and 7. For example, numbers 47, 7744, 474477 are super lucky and 4, 744, 467 are not.
One day ztr came across a positive integer n. Help him to find the least super lucky number which is not less than n.
Input
For each cases:
The only line contains a positive integer . This number doesn't have leading zeroes.
Output
Output the answer
Sample Input
2 4500 47
Sample Output
4747 47
注意会爆long long 就算开无符号也只是两倍也会爆
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef unsigned long long LL;
const int N = 100010;
LL num[N];
int k;
void dfs(int pos,int cnt1,int cnt2,LL sum)
{
if(pos>=19)
return ;
if(cnt1==cnt2&&cnt1+cnt2)
num[k++]=sum;
dfs(pos+1,cnt1+1,cnt2,sum*10+4);
dfs(pos+1,cnt1,cnt2+1,sum*10+7);
return ;
}
int main()
{
k=0;
dfs(0,0,0,0);
sort(num,num+k);
int t;
scanf("%d", &t);
while(t--)
{
LL x;
scanf("%I64u", &x);
int pos = lower_bound(num,num+k,x)-num;
if(pos==k)
printf("44444444447777777777\n");
else
printf("%I64u\n",num[pos]);
}
return 0;
}