# CodeForces - 96B Lucky Numbers (easy)

 Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u

Description

Petya loves lucky numbers. Everybody knows that positive integers are lucky if their decimal representation doesn't contain digits other than4 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 Petya came across a positive integer n. Help him to find the least super lucky number which is not less than n.

Input

The only line contains a positive integer n (1 ≤ n ≤ 109). This number doesn't have leading zeroes.

Output

Output the least super lucky number that is more than or equal to n.

Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specificator.

Sample Input

Input
4500

Output
4747

Input
47

Output
47


#include<iostream>
#include<bitset>
#include<cmath>
#include<cstdio>
using namespace std;                 //二进制枚举
int main()
{
long long int s[500] = {47,74};        //由题可知符合条件的数字位数只能是2，4，6，8，10
//预处理出所有符合的数字存在数组中，再枚举得出答案
int t0=0, t1=0;
int t=2;

for (long long int i = 0;i < 16; i++)        //4到10的处理是类似的
{
for (int j = 0; j < 4; ++j)
if ((i & (1 << j))) t1++;
else t0++;
int po = 1;
if(t0 == t1&&t0!=0&&t0!=4)             //二进制枚举，对0和1数量相等的数处理
{
for (int j = 0; j < 4; ++j)
{
if (i & (1 << j)) s[t] = s[t] +  7 * po;
else s[t] = s[t] + 4 * po;
po = po * 10;
}
t++;
}
t0 = 0; t1 = 0;
}

for (long long int i = 0; i < 64; i++)
{
for (int j = 0; j < 6; ++j)
if ((i & (1 << j))) t1++;
else t0++;
int po = 1;
if (t0 == t1&&t0 != 0 && t0 != 6)
{
for (int j = 0; j < 6; ++j)
{
if (i & (1 << j)) s[t] = s[t] + 7 * po;
else s[t] = s[t] + 4 * po;
po = po * 10;
}
t++;
}
t0 = 0; t1 = 0;
}

for (long long int i = 0; i < 256; i++)
{
for (int j = 0; j < 8; ++j)
if ((i & (1 << j))) t1++;
else t0++;
int po = 1;
if (t0 == t1&&t0 != 0 && t0 != 8)
{
for (int j = 0; j < 8; ++j)
{
if (i & (1 << j)) s[t] = s[t] + 7 * po;
else s[t] = s[t] + 4 * po;
po = po * 10;
}
t++;
}
t0 = 0; t1 = 0;
}

for (long long int i = 0; i < 1024; i++)
{
for (long long int j = 0; j < 10; ++j)
if ((i & (1 << j))) t1++;
else t0++;
long long int po = 1;
if (t0 == t1&&t0 != 0 && t0 != 10)
{
for (long long int j = 0; j < 10; ++j)
{
if (i & (1 << j)) s[t] = s[t] + 7 * po;
else s[t] = s[t] + 4 * po;
po = po * 10;
}
t++;
}
t0 = 0; t1 = 0;
}

long long int n;
while (cin >> n)
{
for (int i = 0; i < t;i++)
if (s[i]>=n) { cout << s[i]<<endl; break; }
}

}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120