CodeForces - 96B Lucky Numbers (easy)

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

 Status

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 477444 are lucky and 517467 are not.

Lucky number is super lucky if it's decimal representation contains equal amount of digits 4 and 7. For example, numbers 477744,474477 are super lucky and 4744467 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; }
	}
	
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这波lucio来全学了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值