HDU 3943 (二分+数位DP)
来源:互联网 发布:大学生就业it培训班 编辑:程序博客网 时间:2024/05/19 18:48
K-th Nya Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others)Total Submission(s): 2762 Accepted Submission(s): 869
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 wants to know the K-th nya number which is greater than P and not greater than Q.
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).
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).
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
138 400 1 11012345678910
Sample Output
Case #1:4774147174247274347374Nya!Nya!
题意:n个询问,每次求出区间中第k大的有x个4,y个7的数.
先数位DP预处理某一个长度a个4,b个7的数字的个数,然后对于每一个询问二分查找
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cmath>using namespace std;long long dp[22][22][22];long long l, r;int x, y;long long n;int cnt, bit[22];long long f (long long num) { cnt = 0; while (num) { bit[++cnt] = num%10; num /= 10; } long long m1 = x, m2 = y; long long ans = 0; for (int i = cnt; i >= 1; i--) { for (int j = 0; j < bit[i]; j++) { if (j == 4) { if (m1) ans += dp[i-1][m1-1][m2]; } else if (j == 7) { if (m2) ans += dp[i-1][m1][m2-1]; } else ans += dp[i-1][m1][m2]; } if (bit[i] == 4) m1--; else if (bit[i] == 7) m2--; if (m1 < 0 || m2 < 0) return ans; } if (m1 == 0 && m2 == 0) ans++; return ans;}void init () { dp[0][0][0] = 1; for (int i = 1; i < 22; i++) { for (int m1 = 0; m1 <= 20; m1++) { for (int m2 = 0; m2 <= 20; m2++) { dp[i][m1][m2] += 8*dp[i-1][m1][m2];//01235689 if (m1) dp[i][m1][m2] += dp[i-1][m1-1][m2];//4 if (m2) dp[i][m1][m2] += dp[i-1][m1][m2-1];//7 } } }}int main () { //freopen ("in.txt", "r", stdin); int t, kase = 0; memset (dp, 0, sizeof dp); init (); scanf ("%d", &t); while (t--) { printf ("Case #%d:\n", ++kase); scanf ("%lld%lld%d%d", &l, &r, &x, &y); long long pre = f (l); //cout << pre << endl; int q; scanf ("%d", &q); while (q--) { scanf ("%lld", &n); long long ll = l, rr = r; while (rr-ll > 1) { long long mid = (ll+rr)>>1; long long cur = f (mid); if (cur-pre >= n) rr = mid; else ll = mid; } if (f (ll)-pre >= n) { printf ("%lld\n", ll); } else if (f (rr)-pre >= n) { printf ("%lld\n", rr); } else printf ("Nya!\n"); } } return 0;}
0 0
- hdu 3943 数位dp+二分
- HDU 3943 (二分+数位DP)
- HDU 3943 二分+数位dp
- HDU 3943 K-th Nya Number(数位dp+二分)
- hdu-3943[数位dp(记忆化搜索)+二分]
- HDU 3943 数位DP
- HDU 3271 SNIBB 数位DP+二分
- spoj1182(数位dp+二分)
- HDU 3943 —— K-th Nya Number(数位DP,二分答案)
- hdu 3943 经典数位dp好题
- hdu 3555 数位dp
- hdu 3555 数位dp
- HDU 3652 数位DP
- HDU 3555 数位DP
- HDU 3555 \数位DP
- HDU 2089 数位DP
- hdu 3555 数位DP
- hdu 3709 数位DP
- 用mysqldump远程备份mysql数据库
- 排序算法的思想分析以及java实现
- 单链表
- C++第3次上机实验--个人所得税计算器
- 20160410
- HDU 3943 (二分+数位DP)
- java对象的序列化与反序列化(测试说明)
- 学习小结
- leetcode 297. Serialize and Deserialize Binary Tree
- ListView 列表播放视频,支持横竖屏无缝切换
- servlet入门(四)--response对象
- Date 日期类
- CQOI2016游记
- SQL基本操作 - 新增、查看、更新、删除数据库