1. 程式人生 > >K-th Nya Number (數位dp+二分)

K-th Nya Number (數位dp+二分)

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.

InputThe 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).OutputFor 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!

題目大概:

給定p,q,x,y,找出在p和q之間的   含有x個4和y個7的數中   第k大個數。

思路:

這個題和昨天做的一道題,類似,都是最簡單的數位dp基礎上加上二分就好了。

數位dp部分就不說了,誰都會。

在p和q之間二分,利用查找出來的符合條件的數量減去q的符合條件的數量就是第n大的數,利用二分找出第k大個數,就好了,也是基礎二分。

主要是精度控制。

程式碼:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
LL  INF=0xfffffffffLL;
int a[65];
LL dp[65][65][65];
LL x,y;
LL sove(int pos,int q4,int q7,int limit)
{
    if(q4>x||q7>y)return 0;
    if(pos==-1)return (q4==x)&&(q7==y);
    if(!limit&&dp[pos][q4][q7]!=-1)return dp[pos][q4][q7];
    int end=limit?a[pos]:9;
    LL ans=0;
    for(int i=0;i<=end;i++)
    {
        if(i==4)ans+=sove(pos-1,q4+1,q7,limit&&i==end);
        else if(i==7)
        {
            ans+=sove(pos-1,q4,q7+1,limit&&i==end);
        }
        else ans+=sove(pos-1,q4,q7,limit&&i==end);
    }
    if(!limit)dp[pos][q4][q7]=ans;
    return ans;
}
LL go(LL x)
{
    int pos=0;
    while(x)
    {
        a[pos++]=x%10;
        x/=10;
    }
    return sove(pos-1,0,0,1);
}
int main()
{   int t;
    LL n;
    scanf("%d",&t);
    for(int j=1;j<=t;j++)
    {   printf("Case #%d:\n",j);
        memset(dp,-1,sizeof(dp));
        LL p,q;
        int n;
        scanf("%I64d%I64d%I64d%I64d",&p,&q,&x,&y);
        scanf("%d",&n);
        LL w1=go(q);
        LL w2=go(p);
        while(n--)
        {

        LL k;
        scanf("%I64d",&k);
        if(k>w1-w2)
        {
            printf("Nya!\n");
            continue;
        }
        LL l=p,r=q,mid;
        while(l<=r)
        {
            mid=(l+r)/2;
            if(go(mid)-w2<k)
            {
                l=mid+1;
            }
            else
            {
                r=mid-1;
            }

        }
       printf("%I64d\n",l);
        }

    }


    return 0;
}

相關推薦

K-th Nya Number 數位dp+二分

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 477

HDU 3943 K-th Nya Number數位dp+二分

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 numbe

HDU 3943 —— K-th Nya Number數位DP二分答案

給定X和Y的值,一個數字用十進位制表示,對於數位4出現恰好X次,7恰好Y次的數字稱為nya數。 然後給P和Q, 後面是一系列查詢, 每個查詢K就是在區間(P,Q]上找到第K個nya數,不存在則輸出Nya!(= = 這是尖叫的意思麼) 因為數字的範圍比較大,又是跟數位有關,

K-th Nya NumberHDU-3943

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

HDU 3943 K-th Nya Number (數位DP)

HDU 3943 K-th Nya Number 用X個4和Y個7的數為規定的數,然後就是區間統計。 先預處理好dp[i][j][k]表示I位的數中有j個4和k個7的數量。 之後就可以通過高位開始列舉,求出區間內有多少個規定的數,如果詢問大於總數,則輸出"Nya!";

【HDU3943】【K-th Nya Number】【數位+二分找位置】

K-th Nya NumberTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Submission(s): 2493    Accepte

HDU 5787 K-wolf Number數位dp

blog typedef turn pan con target ack cnblogs freopen http://acm.split.hdu.edu.cn/showproblem.php?pid=5787 題意:給出一個範圍[l,r]和整數k,求出在該範圍的數在十進

hdu3709 Balanced Number 數位dp

careful href multi true search target tar sta total 題目傳送門 Balanced Number Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 6553

P2188 小Z的 k 緊湊數 題解數位DP

題目連結 小Z的 k 緊湊數 解題思路 數位DP,把每一個數位的每一個數對應的可能性表示出來,然後求\(num(1,r)-num(1,l-1)\),其中\(num(i,j)\)表示\([i,j]\)區間裡符合要求的數的個數。 其中,\(dp[i][j]\)表示第\(i\)位數字為\(j\)的選擇種數。

3652 B-number數位DP

傳送門 B-number Problem Description A wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the sub- st

HDOJ3943 K-th Nya Number

題意:在(P,Q】區間內,第K大的滿足條件的數是多少 條件是:數位中有X個4,Y個7 分析: 有X個4,Y個7是很簡單的數位dp dp【pos】【x】【y】:當前pos位,現在已經有了x個4,y個7 注意可以有個小剪枝,即x不能超過X,y不能超過Y 二分的方法與HDO

Balanced Number數位DP

it is include 位數 integer 就是 text NPU \n space D - Balanced Number HDU - 3709 A balanced number is a non-negative integer that can be bal

51Nod1009 數字1的數量數位dp演算法

數位dp演算法: void dfs(int a,int b,int c[]) { ll n=a/10,m=a%10,t=n; for(int i=0;i<=m;i++) c[i]+=b;//當前位對低位的影響 for(int i=0;i<10;i++)

hdu 3555 Bomb數位dp入門

Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 24148    Accepted Submissi

HDU2089——不要62數位dp入門

不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 35612    Accepted Submiss

hdu2089 不要62數位dp模板

ac程式碼:#include<iostream> #include<cstring> #include<algorithm> #define r(n) scanf("

HDU 3709 Balanced Number 求區間內的滿足是否平衡的數量 數位dp

平衡數的定義是指,以某位作為支點,此位的左面(數字 * 距離)之和 與右邊相等,距離是指某位到支點的距離; 題意:求區間內滿足平衡數的數量 ;  分析:很好這又是常見的數位dp , 不過不同的是我們這次需要列舉是哪個位置是平衡點 , 一開始我是想說搜尋到最後以為 ,然後得到這個數的位數 ,在判斷

poj 2104 K-th Number 主席樹模板

傳送門 // by spli #include<cstring> #include<cstdio> #include<algorithm> #include<iostream> using namespace

藍橋杯 ALGO-3 K好數數位DP

解題方案:dp,在分析問題的時候可以發現每次都要計算的重複子問題:i位數以數字j為首的有多少個。 #include <iostream> #include <cstdio>

HDU 4507 吉哥系列故事――恨7不成妻數位DP+結構體

開始 bsp 相等 continue 退出 tin get 結構 eof 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=4507 題目大意:如果一個整數符合下面3個條件之一,那麽我們就說這個整數和7有關     1、整數中某一