srm 546

来源:互联网 发布:java xe hystrix 编辑:程序博客网 时间:2024/05/16 17:13

欢迎点此观看QvQ

250


Solution

考虑奇偶性,每次定一个可以变成该数的上下界,按二进制位考虑即可

Code

#include <bits/stdc++.h>using namespace std;#define pb push_back#define mp make_pair#define F first#define S secondtypedef long long LL;typedef pair<int, int> pii;struct KleofasTail {    LL gao(LL x, LL k) {        if (k > x)  return 0;        if (k <= 1) return x - k + 1;        LL kk = k + (k % 2 == 0);        LL t = 0;        while (k <= x) {            t += min(x, kk) - k + 1;            k <<= 1;            kk = (kk << 1 | 1);        }        return t;    }    long long countGoodSequences(long long K, long long A, long long B) {        return gao(B, K) - gao(A - 1, K);                  }};

500


Description

求满足大于A(A1015)的且digit1至少出现count1次,digit2至少出现count2的最小的数是多少?
(count1+count215,digit1,digit210)

Solution

很容易想到数位dp,常见数位dp套路,用一个f表示当前数是否与最小值相等,还有cnt1x1,cnt2x2没有用,是否成立。
正常转移即可,注意一点,除了x1,x2,lim,lim+1以外,其他数是没什么意义的,dp即可

Code

#include <bits/stdc++.h>//dpusing namespace std;#define pb push_back#define mp make_pair#define F first#define S secondtypedef long long LL;typedef pair<int, int> pii;int a[16];LL ans;struct FavouriteDigits {    bool dfs(int p, bool f, int x1, int cnt1, int x2, int cnt2, LL now) {        if (p == -1) {            if (cnt1 <= 0 && cnt2 <= 0) {                ans = now;                return 1;            }            return 0;        }        int lim = f ? a[p] : 0;        for (int i = lim; i <= 9; ++i) {            if (f) {                if (!(i == lim || i == x1 || i == x2 || i == lim + 1))  continue;            }            else {                if (!(i == lim || i == x1 || i == x2))  continue;            }            int d1 = cnt1 - (i == x1);            int d2 = cnt2 - (i == x2);            if (!now && !x1)    d1 = cnt1;            if (dfs(p - 1, f & (i == lim), x1, d1, x2, d2, now * 10 + i))   return 1;        }        return 0;    }    long long findNext(long long N, int x1, int cnt1, int x2, int cnt2) {        if (x1 > x2)    swap(x1, x2), swap(cnt1, cnt2);        int p = 0;        while (N) {            a[p++] = N % 10;            N /= 10;        }        p = max(p, cnt1 + cnt2);        dfs(p, 1, x1, cnt1, x2, cnt2, 0);        return ans;    }};
0 0
原创粉丝点击