POJ 3208 Apocalypse Someday

Description

The number 666 is considered to be the occult “number of the beast” and is a well used number in all major apocalypse themed blockbuster movies. However the number 666 can’t always be used in the script so numbers such as 1666 are used instead. Let us call the numbers containing at least three contiguous sixes beastly numbers. The first few beastly numbers are 666, 1666, 2666, 3666, 4666, 5666…

Given a 1-based index n, your program should return the nth beastly number.

Input

The first line contains the number of test cases T (T ≤ 1,000).

Each of the following T lines contains an integer n (1 ≤ n ≤ 50,000,000) as a test case.

Output

For each test case, your program should output the nth beastly number.

Sample Input

3
2
3
187

Sample Output

1666
2666
66666

解题思路:按位统计+二分。

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 20;
ll dp[maxn][10][10][2];

ll power(int x) {
    ll ret = 1;
    for(int i = 1; i <= x; ++i) {
        ret *= 10;
    }
    return ret;
}

void init() {
    memset(dp, 0, sizeof(dp));
    for(int i = 0; i <= 9; ++i) {
        for(int j = 0; j <= 9; ++j) {
            dp[2][i][j][0] = 1;
        }
    }
    for(int i = 3; i <= 12; ++i) {
        for(int j = 0; j <= 9; ++j) {
            for(int k = 0; k <= 9; ++k) {
                for(int l = 0; l <= 9; ++l) {
                    if(j == 6 && k == 6 && l == 6) {
                        dp[i][j][k][1] += dp[i-1][k][l][0];
                        dp[i][j][k][1] += dp[i-1][k][l][1];
                    } else {
                        dp[i][j][k][0] += dp[i-1][k][l][0];
                        dp[i][j][k][1] += dp[i-1][k][l][1];
                    }
                }
            }
        }
    }
    return ;
}
int bit[20], blen;

ll check() {
    bool ok = false;
    for(int i = 0; i + 2 < blen; ++i) {
        if(bit[i] == 6 && bit[i+1] == 6 && bit[i+2] == 6) {
            ok = true;
            break;
        }
    }
    return ok;
}

ll dfs(int pos, bool have) {
    if(pos < 0) return check();
    ll ret = 0;
    for(int i = 0; i < bit[pos]; ++i) {
        if(have) {
            ret += power(pos);
        } else {
            if(pos + 2 < blen) {
                if(bit[pos+2] == 6 && bit[pos+1] == 6 && i == 6) {
                    ret += dp[pos+2][bit[pos+1]][i][0];
                    ret += dp[pos+2][bit[pos+1]][i][1];
                } else {
                    ret += dp[pos+2][bit[pos+1]][i][1];
                }
            } else {
                ret += dp[pos+2][bit[pos+1]][i][1];
            }
        }
    }
    if(pos + 2 < blen && bit[pos+2] == 6 && bit[pos+1] == 6 && bit[pos] == 6) {
        have = true;
    }
    ret += dfs(pos-1, have);
    return ret;
}

ll solve(ll x) {
    blen = 0;
    while(x) {
        bit[blen++] = x % 10;
        x /= 10;
    }
    bit[blen++] = 0;
    return dfs(blen-2, false);
}

int main() {

    init();
    int T;
    ll n;
    scanf("%d", &T);
    while(T--) {
        cin >> n;
        ll l = 1, r = 66600000000LL, mid;
        ll ans = r;
        while(l <= r) {
            mid = (l + r) / 2;
            if(solve(mid) >= n) {
                ans = min(ans, mid);
                r = mid - 1;
            } else {
                l = mid + 1;
            }
        }
        cout << ans << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值