数位dp/二分/思维(石油大学G: A-Number and B-Number)

在这里插入图片描述
题意:有两种序列A-Number和B-Number。A-Number是所有数位中有7的和被7整除的数。B-Number是A-Number序列除去下标是数位中有7的和被7整除的数。问:第N个B-Number是。
题解:这个题只要想明白了,就特别水。我们可以考虑二分结果。然后计算他前面有多少个B-Number。如果比N大则缩小,否则增大。那怎么找他是前面有多少个B-Number呢。很简单,我们设F(x)为[0, x]之间A-Number的数量。那么数x之前有ans个B-Number:
a n s = F ( x ) − F ( F ( x ) − 1 ) ans=F(x) -F(F(x)-1) ans=F(x)F(F(x)1)
然后就是数位dp模板了。
下面是ac代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include <map>
#include <queue>
#include <set>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <bitset>
#include <array>
#include <cctype>
#include <time.h>

#pragma GCC optimize(2)

void read_f() { freopen("1.in", "r", stdin); freopen("1.out", "w", stdout); }
void fast_cin() { std::ios::sync_with_stdio(false); std::cin.tie(); }
void run_time() { std::cout << "ESC in : " << clock() * 1000.0 / CLOCKS_PER_SEC << "ms" << std::endl; }
template <typename T>
bool bacmp(const T & a, const T & b) { return a > b; }
template <typename T>
bool pecmp(const T & a, const T & b) { return a < b; }

#define ll long long
#define ull unsigned ll
#define _min(x, y) ((x)>(y)?(y):(x))
#define _max(x, y) ((x)>(y)?(x):(y))
#define max3(x, y, z) ( max( (x), max( (y), (z) ) ) )
#define min3(x, y, z) ( min( (x), min( (y), (z) ) ) )
#define pr(x, y) (make_pair((x), (y)))
#define pb(x) push_back(x);
using namespace std;

const int N = 1e5+5;
ll dp[32][8][2];
int su[32];
ll dfs(int cur, int m,  int d, int flag)
{
    if (cur < 0)
    {
        return d || m == 0;
    }
    if (dp[cur][m][d]!=-1 && !flag) return dp[cur][m][d];
    int mx = flag ? su[cur] : 9;
    ll ans = 0;
    for (int i = 0; i <= mx; i++)
        ans += dfs(cur-1, (m * 10 + i) % 7, d || i == 7, flag && i == mx);
    if (!flag) dp[cur][m][d] = ans;
    return ans;
}
ll sol(ll x)
{
    int cnt = 0;
    while(x)
    {
        su[cnt++] = x % 10;
        x /= 10;
    }
    return dfs(cnt-1, 0, 0, 1);
}
int main()
{
    memset(dp, -1, sizeof(dp));
    ll n;
    while(scanf("%lld", &n) != EOF)
    {
        ull l = 0, r = (1ull << 63) - 1;
        while(l <= r)
        {
            ull mid = (l + r) >> 1;
            ll a = sol((ll)mid);
            if (a - sol(a - 1) < n) l = mid + 1;
            else r = mid - 1;
        }
        ll c = l;
        printf("%lld\n", c);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值