HDU 3943 K-th Nya Number [數位DP]

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. 

題意:問(P,Q]範圍內,第K個恰有X個4,Y個7的數。

解法:比較明顯的數位DP,找第K個的過程可以考慮二分,只是需要注意的是P是不能取的(英語坑爹啊)

記憶化搜索的狀態爲dfs(pos,sum4,sum7,limit) ,表示到第pos位,有sum4個4,sum7個7,之前是否爲prefix(limit爲bool),具體可以看代碼:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<iostream>
#include<stdlib.h>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<bitset>
#pragma comment(linker, "/STACK:1024000000,1024000000")
template <class T>
bool scanff(T &ret){ //Faster Input
    char c; int sgn; T bit=0.1;
    if(c=getchar(),c==EOF) return 0;
    while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();
    sgn=(c=='-')?-1:1;
    ret=(c=='-')?0:(c-'0');
    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
    if(c==' '||c=='\n'){ ret*=sgn; return 1; }
    while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;
    ret*=sgn;
    return 1;
}
#define inf 1073741823
#define llinf 4611686018427387903LL
#define PI acos(-1.0)
#define lth (th<<1)
#define rth (th<<1|1)
#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)
#define drep(i,a,b) for(int i=int(a);i>=int(b);i--)
#define gson(i,root) for(int i=ptx[root];~i;i=ed[i].next)
#define tdata int testnum;scanff(testnum);for(int cas=1;cas<=testnum;cas++)
#define mem(x,val) memset(x,val,sizeof(x))
#define mkp(a,b) make_pair(a,b)
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define pb(x) push_back(x)
using namespace std;
typedef unsigned long long  ll;
typedef pair<int,int> pii;


ll num[33];
ll dp[33][33][33];
ll n4,n7;

ll dfs(ll pos,ll pre4,ll pre7,bool lim){
    if(pos==0){
        if(pre4==0&&pre7==0)return 1;
        else return 0;
    }
    if(!lim&&~dp[pos][pre4][pre7])return dp[pos][pre4][pre7];
    ll sum=0,d=9;
    if(lim)d=num[pos];
    rep(i,0,d){
        if(i==4){
            if(pre4>0)sum+=dfs(pos-1,pre4-1,pre7,lim&&i==d);
        }
        else if(i==7){
            if(pre7>0)sum+=dfs(pos-1,pre4,pre7-1,lim&&i==d);
        }
        else sum+=dfs(pos-1,pre4,pre7,lim&&i==d);
    }
    if(!lim)dp[pos][pre4][pre7]=sum;
    return sum;
}

ll solve(ll x){
    int len=0;
    for(;x;x/=10)num[++len]=x%10;
    return dfs(len,n4,n7,1);
}


ll ql,qr,qn,l,r;
int main(){
    mem(dp,-1);
    tdata{
        scanff(ql);scanff(qr);scanff(n4);scanff(n7);
        scanff(qn);
        ll sum1=solve(ql);
        printf("Case #%d:\n",cas);
        rep(i,1,qn){
            ll k;
            l=ql+1,r=qr;
            scanff(k);
            if(l>r){
                printf("Nya!\n");
                continue;
            }
            while(r-l>1){
                ll m=(l+r)>>1;
                ll sum2=solve(m);
                if(sum2-sum1>=k)r=m;
                else l=m;
            }
            if(solve(l)-sum1==k)printf("%llu\n",l);
            else if(solve(r)-sum1==k)printf("%llu\n",r);
            else printf("Nya!\n");
        }
    }
    return 0;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章