题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5676
ztr loves lucky numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 594 Accepted Submission(s): 257
Problem Description
ztr loves lucky numbers. Everybody knows that positive integers are lucky if their decimal representation doesn't contain digits other than 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
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
There are T
(1≤n≤105) cases
For each cases:
The only line contains a positive integer
n(1≤n≤1018) . This number doesn't have leading zeroes.
Output
For each cases
Output the answer
Sample Input
Sample Output
Source
BestCoder Round #82 (div.2)
题目大意:
ztr喜欢幸运数字,他对于幸运数字有两个要求
1:十进制表示法下只包含4、7
2:十进制表示法下4和7的数量相等
比如47,474477就是
而4,744,467则不是
现在ztr想知道最小的但不小于n的幸运数字是多少
解题思路:暴力打出所有幸运数~~~~然后二分查找即可。
详见代码。
#include <iostream>
#include <cstdio>
using namespace std;
#define ll long long
ll a[100000];
int k=0;
void dfs(ll ans,int num4,int num7)
{
if (num4==0&&num7==0)
{
a[k++]=ans;
return;
}
if (num4==0)
{
dfs(ans*10+7,num4,num7-1);
}
else if (num7==0)
{
dfs(ans*10+4,num4-1,num7);
}
else
{
dfs(ans*10+4,num4-1,num7);
dfs(ans*10+7,num4,num7-1);
}
}
int main()
{
int t;
scanf("%d",&t);
for (int i=2;i<=18;i+=2)
dfs(0,i/2,i/2);
while (t--)
{
ll n;
scanf("%lld",&n);
if(n>777777777444444444LL)
{
printf("44444444447777777777\n");
continue;
}
int l=0,r=k;
while (r>l)
{
int mid=(l+r)/2;
// cout<<mid<<endl;
if (a[mid]<n)
{
l=mid+1;
}
else
{
r=mid;
}
}
printf("%lld\n",a[r]);
}
return 0;
}