标签:
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!
1 #include <bits/stdc++.h> 2 using namespace std; 3 using LL = long long; 4 const int maxn = 22; 5 LL dp[maxn][maxn][maxn]; 6 int bt[maxn],x,y; 7 LL dfs(int len,int a,int b,bool flag){ 8 if(len == -1) return a == x && y == b; 9 if(!flag && dp[len][a][b] != -1) return dp[len][a][b]; 10 int u = flag?bt[len]:9; 11 LL ans = 0; 12 for(int i = 0; i <= u; ++i){ 13 if(i == 4) ans += dfs(len-1,a + 1,b,flag&&i == u); 14 else if(i == 7) ans += dfs(len-1,a,b + 1,flag&&i==u); 15 else ans += dfs(len-1,a,b,flag&&i==u); 16 } 17 if(!flag) dp[len][a][b] = ans; 18 return ans; 19 } 20 LL solve(LL x){ 21 int cnt = 0; 22 while(x){ 23 bt[cnt++] = x%10; 24 x /= 10; 25 } 26 return dfs(cnt - 1,0,0,true); 27 } 28 int main(){ 29 int kase,m,cs = 1; 30 scanf("%d",&kase); 31 while(kase--){ 32 LL P,Q,K; 33 scanf("%I64d%I64d%d%d",&P,&Q,&x,&y); 34 memset(dp,-1,sizeof dp); 35 scanf("%d",&m); 36 printf("Case #%d:\n",cs++); 37 LL tmp = solve(P); 38 while(m--){ 39 scanf("%I64d",&K); 40 K += tmp; 41 LL low = P + 1,high = Q,ans = -1; 42 while(low <= high){ 43 LL mid = (low + high)>>1; 44 if(solve(mid) >= K){ 45 ans = mid; 46 high = mid - 1; 47 }else low = mid + 1; 48 } 49 if(ans == -1) puts("Nya!"); 50 else printf("%I64d\n",ans); 51 } 52 } 53 return 0; 54 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4974581.html