标签:数位dp
1 38 400 1 1 10 1 2 3 4 5 6 7 8 9 10
Case #1: 47 74 147 174 247 274 347 374 Nya! Nya!
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<stack> #include<vector> #include<set> #include<map> #define L(x) (x<<1) #define R(x) (x<<1|1) #define MID(x,y) ((x+y)>>1) #define eps 1e-8 typedef __int64 ll; #define fre(i,a,b) for(i = a; i <b; i++) #define free(i,b,a) for(i = b; i >= a;i--) #define mem(t, v) memset ((t) , v, sizeof(t)) #define ssf(n) scanf("%s", n) #define sf(n) scanf("%d", &n) #define sff(a,b) scanf("%d %d", &a, &b) #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c) #define pf printf #define bug pf("Hi\n") using namespace std; #define INF 0x3f3f3f3f #define N 25 ll dp[N][N][N]; int bit[N]; ll le,ri; int x,y,k; ll dfs(int pos,int prex,int prey,int bound) { if(pos==0) return prex==0&&prey==0; if(prex<0||prey<0) return 0; if(!bound&&dp[pos][prex][prey]!=-1) return dp[pos][prex][prey]; int up=bound ? bit[pos]:9; ll temp=0; int i; fre(i,0,up+1) { temp+=dfs(pos-1,prex-(i==4),prey-(i==7),bound&&i==up); } if(!bound) dp[pos][prex][prey]=temp; return temp; } ll fdd(ll n) { int len=0; while(n) { bit[++len]=n%10; n/=10; } return dfs(len,x,y,1); } void solve() { ll prex=fdd(le); ll prey=fdd(ri); ll lle=le,rri=ri; ll mid,ans; k+=prex; while(lle<=rri) { mid=(lle+rri)>>1; if(fdd(mid)>=k) { ans=mid; rri=mid-1; } else lle=mid+1; } pf("%I64d\n",ans); } int main() { int i,j,t,ca=0; sf(t); mem(dp,-1); while(t--) { pf("Case #%d:\n",++ca); scanf("%I64d%I64d",&le,&ri); sff(x,y); ll numle=fdd(le); ll numri=fdd(ri); //pf("%I64d %I64d\n",numle,numri); int m; sf(m); while(m--) { sf(k); if(numle+k>numri) puts("Nya!"); else solve(); } } return 0; }
HDU 3943 K-th Nya Number(数位dp+二分)
标签:数位dp
原文地址:http://blog.csdn.net/u014737310/article/details/45112073