HDU3943.K-th Nya Number

首先没有把要求输入K_i用long long保存,结果无限超时
第二的话数组开小了,以为2^63约等于10^9,T^T
题目思路就是数位dp加二分

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
ll dp[25][25][25];
ll a[25];
ll P,Q;
int x,y;

ll dfs(ll pos,ll j,ll k,ll lim){ //记忆化搜素
    if(pos<=0)
        return j==x&&k==y;
    if(j>x||k>y)
        return 0;
    if(!lim&&dp[pos][j][k]!=-1)
        return dp[pos][j][k];
    ll num= lim?a[pos]:9;
    ll ans=0;
    for(int i=0;i<=num;i++){
        ans+=dfs(pos-1,j+(i==4),k+(i==7),lim&&(i==num));
    }
    if(!lim)
        dp[pos][j][k]=ans;
    return ans;
}

ll solve(ll n){
    int len=0;
    while(n){
        a[++len]=n%10;
        n/=10;
    }
    a[len+1]=0;
    return dfs(len,0,0,1);
}


int main(){
    int t;
    scanf("%d",&t);
    for(int case1=1;case1<=t;case1++){
        cout<<"Case #"<<case1<<":"<<endl;
        scanf("%lld%lld%d%d",&P,&Q,&x,&y);
        mem1(dp);
        ll b=solve(Q);
        ll a=solve(P);
        int n;
        ll m;
        scanf("%d",&n);
        while(n--){
            scanf("%lld",&m);
            if(m>b-a){
                printf("Nya!\n");
                continue;
            }
            else{
                m+=a;    //二分
                ll low=P+1;
                ll high=Q;
                ll mid;
                while(low<high){
                    mid=(low+high)>>1;
                    if(solve(mid)>=m){
                        high=mid;
                    }
                    else
                        low=mid+1;
                }
                printf("%lld\n",low);
            }
        }
    }
    return 0;
}

你可能感兴趣的:(HDU3943.K-th Nya Number)