Bestcoder #82 Div2 ztr loves lucky numbers(next_permutation)

题目链接:
Bestcoder #82 Div2 ztr loves lucky numbers
题意:
ztr喜欢幸运数字,他对于幸运数字有两个要求
1:十进制表示法下只包含4、7
2:十进制表示法下4和7的数量相等
比如47,474477就是
而4,744,467则不是

现在ztr想知道最小的但不小于n的幸运数字是多少?
T(1T105) 组数据,每组数据一个正整数 n 1n1018

分析:
比赛时我是当成字符串来处理,贼麻烦,而且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;
}

​​

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值