HDU 3943 K-th Nya Number

K-th Nya Number

Time Limit: 1000ms

Memory Limit: 65536KB

This problem will be judged on HDU. Original ID: 3943
64-bit integer IO format: %I64d      Java class name: Main

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.

Input

The 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).

Output

For 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!

Source

2011 Multi-University Training Contest 11 - Host by UESTC

解题:数位dp+二分

 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 }

时间: 2024-10-30 07:18:08

HDU 3943 K-th Nya Number的相关文章

HDU 3943 K-th Nya Number(数位dp+二分)

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 , 172441277 and 47770142 are nya numbers.But 14777 is not a nya number ,because it has only 1 four). Now, Arcueid

hdu 3943

数位dp #include <cstdio> #include <cstdlib> #include <cmath> #include <stack> #include <vector> #include <sstream> #include <cstring> #include <string> #include <map> #include <set> #include <qu

HDU 4006 The kth great number (基本算法-水题)

The kth great number Problem Description Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round Xiao Ming can choose to write down a number, or ask Xiao Bao what the kth great number is. Because the number written by Xiao Ming is too mu

hdu 3509 Buge&#39;s Fibonacci Number Problem

点击此处即可传送 hdu 3509 题目大意:F1 = f1, F2 = f2;; F(n) = a*F(n-1) + b*F(n-2); S(n) = F1^k + F2^k +-.+Fn^k; 求S(n) mod m; 解题思路: 1:首先一个难题就是怎么判断矩阵的维数(矩阵的维数是个变量) 解决方法:开一个比较大的数组,然后再用一个公有变量记一下就行了,具体详见代码: 2:k次方,找规律: 具体上代码吧: /* 2015 - 8 - 16 晚上 Author: ITAK 今日的我要超越昨日

hdu 4006 The kth great number (优先队列+STB+最小堆)

The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 6637    Accepted Submission(s): 2671 Problem Description Xiao Ming and Xiao Bao are playing a simple Numbers game. In a roun

HDU 4006 The kth great number AVL解法

给出动态更新数据,实时问第K个大的数值是什么? 利用AVL数据结构做的一个统计数,比较高级的数据结构内容了. 不知道题目给出的数据值是否有重复,因为我下面的程序是可以处理出现数据重复的情况的. 其中的奥妙是增加了repeat的信息,可以知道出现了当前数组多少次. 主要是知道如何维护这些数据和如何查询,维护数据的函数是pushUp,查询函数是selectKth. 其他就是一般的AVL操作.个人觉得我的AVL写的还算蛮清晰的,有需要的参考一下. #include <stdio.h> inline

HDU 4006 The kth great number(multiset(或者)优先队列)

题目 询问第K大的数 //这是我最初的想法,用multiset,AC了——好吧,也许是数据弱也有可能 //multiset运用——不去重,边插入边排序 //iterator的运用,插入的时候,如果是相同的数没直接放在相同的现有的数据后面的 #include<cstdio> #include<cstring> #include<algorithm> #include<set> using namespace std; //#define IN freopen(

HDU 4006 The kth great number(优先队列&#183;第K大数)

题意  动态查询第K大的数 用小数在前优先队列维护K个数  每要插入一个数时 若这个数小于队首元素那么就不用插入了  否则队首元素出队  这个数入队  每次询问只用输出队首元素就行了 #include<cstdio> #include<queue> using namespace std; int main() { int n, a, k; char op[5]; while(~scanf("%d%d", &n, &k)) { priority_

HDU 5122 K.Bro Sorting(模拟——思维题详解)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5122 Problem Description Matt's friend K.Bro is an ACMer. Yesterday, K.Bro learnt an algorithm: Bubble sort. Bubble sort will compare each pair of adjacent items and swap them if they are in the wrong o