HDU 3943 (二分+数位DP)

K-th Nya Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)
Total Submission(s): 2762    Accepted Submission(s): 869


Problem Description
Arcueid likes nya number very much.
A nya number is the number which has exactly X fours and Y sevens(If X=2 and Y=3 , 172441277 and 47770142 are nya numbers.But 14777 is not a nya number ,because it has only 1 four).
Now, Arcueid wants to know the K-th nya number which is greater than P and not greater than Q.
 

Input
The first line contains a positive integer T (T<=100), indicates there are T test cases.
The second line contains 4 non-negative integers: P,Q,X and Y separated by spaces.
( 0<=X+Y<=20 , 0< P<=Q <2^63)
The third line contains an integer N(1<=N<=100).
Then here comes N queries.
Each of them contains an integer K_i (0<K_i <2^63).
 

Output
For each test case, display its case number and then print N lines.
For each query, output a line contains an integer number, representing the K_i-th nya number in (P,Q].
If there is no such number,please output "Nya!"(without the quotes).
 

Sample Input
   
   
   
   
1 38 400 1 1 10 1 2 3 4 5 6 7 8 9 10
 

Sample Output
   
   
   
   
Case #1: 47 74 147 174 247 274 347 374 Nya! Nya!
 
题意:n个询问,每次求出区间中第k大的有x个4,y个7的数.
先数位DP预处理某一个长度a个4,b个7的数字的个数,然后对于每一个询问二分查找

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

long long dp[22][22][22];
long long l, r;
int x, y;
long long n;
int cnt, bit[22];

long long f (long long num) {
    cnt = 0;
    while (num) {
        bit[++cnt] = num%10;
        num /= 10;
    }
    long long m1 = x, m2 = y;
    long long ans = 0;
    for (int i = cnt; i >= 1; i--) {
        for (int j = 0; j < bit[i]; j++) {
            if (j == 4) {
                if (m1)
                    ans += dp[i-1][m1-1][m2];
            }
            else if (j == 7) {
                if (m2)
                    ans += dp[i-1][m1][m2-1];
            }
            else
                ans += dp[i-1][m1][m2];
        }
        if (bit[i] == 4)
            m1--;
        else if (bit[i] == 7)
            m2--;
        if (m1 < 0 || m2 < 0)
            return ans;
    }
    if (m1 == 0 && m2 == 0)
        ans++;
    return ans;
}

void init () {
    dp[0][0][0] = 1;
    for (int i = 1; i < 22; i++) {
        for (int m1 = 0; m1 <= 20; m1++) {
            for (int m2 = 0; m2 <= 20; m2++) {
                dp[i][m1][m2] += 8*dp[i-1][m1][m2];//01235689
                if (m1)
                    dp[i][m1][m2] += dp[i-1][m1-1][m2];//4
                if (m2)
                    dp[i][m1][m2] += dp[i-1][m1][m2-1];//7
            }
        }
    }
}

int main () {
    //freopen ("in.txt", "r", stdin);
    int t, kase = 0;
    memset (dp, 0, sizeof dp);
    init ();
    scanf ("%d", &t);
    while (t--) {
        printf ("Case #%d:\n", ++kase);
        scanf ("%lld%lld%d%d", &l, &r, &x, &y);
        long long pre = f (l); //cout << pre << endl;
        int q;
        scanf ("%d", &q);
        while (q--) {
            scanf ("%lld", &n);
            long long ll = l, rr = r;
            while (rr-ll > 1) {
                long long mid = (ll+rr)>>1;
                long long cur = f (mid); 
                if (cur-pre >= n)
                    rr = mid;
                else
                    ll = mid;
            }
            if (f (ll)-pre >= n) {
                printf ("%lld\n", ll);
            }
            else if (f (rr)-pre >= n) {
                printf ("%lld\n", rr);
            }
            else
                printf ("Nya!\n");
        }
    }
    return 0;
}



你可能感兴趣的:(HDU 3943 (二分+数位DP))