Great learning for me:
https://www.hackerrank.com/rest/contests/master/challenges/lucky-numbers/hackers/turuthok/download_solution

Basically it is memorized search application. And we follow a discrete strategy: split it into digits and go digit by digit.

Here is my C++ version of the code above, with comments for easier understanding.

#include <iostream>
#include <limits>
#include <vector>
using namespace std; typedef long long LL; const int D = ; // max digit count
const int MD = ; // max single digit
const LL MAX = std::numeric_limits<LL>::max(); vector<vector<vector<LL>>> dp(D, vector<vector<LL>>(D*MD + , vector<LL>(D*MD*MD + , -)));
vector<int> hi(D);
vector<bool> isPrime(D*MD*MD + , true); LL go(int inx, int sd, int ssd, bool limited)
{
if (inx == D) return isPrime[sd] && isPrime[ssd] ? : ; // Memorized Search
LL &ret = dp[inx][sd][ssd];
if (!limited && ret >= ) return ret; // We only cache unlimited count int upper = limited ? hi[inx] : ;
// DP: count of current digit = sum of all counts of all previous digits
LL r = ;
for (int i = ; i <= upper; i++)
{
r += go(inx + , sd + i, ssd + i * i, limited && i == upper);
}
if (!limited) ret = r; // We only cache unlimited count
return r;
} LL go(LL n)
{
if (n <= ) return ; // split digits
for (int i = D - ; i >= ; i--)
{
hi[i] = n % ;
n /= ;
} return go(, , , true);
} int main()
{
// sieving
isPrime[] = isPrime[] = false;
for (int i = ; i <= D*MD*MD; i++)
if (isPrime[i])
for (int j = i * ; j <= D*MD*MD; j += i) isPrime[j] = false; // go
int t; cin >> t;
while (t--)
{
LL a, b; cin >> a >> b;
cout << (go(b) - go(a - )) << endl;
} return ;
}

HackerRank "Lucky Numbers"的更多相关文章

  1. HDU 5676 ztr loves lucky numbers (模拟)

    ztr loves lucky numbers 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/I Description ztr ...

  2. codeforces 630C Lucky Numbers

    C. Lucky Numbers time limit per test 0.5 seconds memory limit per test 64 megabytes input standard i ...

  3. hdu 5676 ztr loves lucky numbers(dfs+离线)

    Problem Description ztr loves lucky numbers. Everybody knows that positive integers are lucky if the ...

  4. codeforces 630C - Lucky Numbers 递推思路

    630C - Lucky Numbers 题目大意: 给定数字位数,且这个数字只能由7和8组成,问有多少种组合的可能性 思路: 假设为1位,只有7和8:两位的时候,除了77,78,87,88之外还哇哦 ...

  5. hdu 5676 ztr loves lucky numbers 打表+二分

    ztr loves lucky numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  6. ZCMU 2177 Lucky Numbers (easy)

    传送门: http://acm.zcmu.edu.cn/JudgeOnline/problem.php?id=2177 2177: Lucky Numbers (easy) 时间限制: 2 Sec   ...

  7. hdu-5676 ztr loves lucky numbers(乱搞题)

    题目链接: ztr loves lucky numbers  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536/65536 K ( ...

  8. C - Lucky Numbers (easy)

    Problem description Petya loves lucky numbers. Everybody knows that positive integers are lucky if t ...

  9. Codeforces Round #160 (Div. 2)---A. Roma and Lucky Numbers

    Roma and Lucky Numbers time limit per test 1 second memory limit per test 256 megabytes input standa ...

随机推荐

  1. Sed 直接修改文件

    sed最常用的用法莫过于替换文件,然而其默认的模式是直接输出在shell中 sed 's/Old/New/' My_File.txt 如果我们想要sed直接在文件中更改,只需要在sed后面添加 -i ...

  2. Nginx技巧:灵活的server_name,Nginx配置一个服务器多个站点 和 一个站点多个二级域名

    http://www.cnblogs.com/buffer/archive/2011/08/17/2143514.html Nginx强大的正则表达式支持,可以使server_name的配置变得很灵活 ...

  3. Sqlserver 笔记 持续更新

    1.sp_spaceused : sp_spaceused table_name ----------------------------------------------------------- ...

  4. Codeforces 106 C 多重背包

    题目链接:http://codeforces.com/problemset/problem/106/C 根据题意列出式子,设每种蛋糕做了xi个,则对于每种材料bi*xi<=ai. 对于dough ...

  5. magic矩阵

    魔方矩阵 魔方矩阵是有相同的行数和列数,并在每行每列.对角线上的和都相等.你能构造任何大小(除了2x2)的魔方矩阵. 1.历史       魔方又称幻方.纵横图.九宫图,最早记录于我国古代的洛书.据说 ...

  6. hive学习笔记_hive的表创建

    创建hive表注意事项 一.表分隔符必须与读取的数据文件一致,比如例子的分隔符为 '\t'(制表符),hive下默认分隔符是制表符. 二.最好指定分区作为数据之间的区分. 三.创建完表可以desc+表 ...

  7. phpwind数据同步本地后登陆异常

    在讲数据同步到本地之后,发现输入用户名和密码之后点击登陆,依然会返回到之前的页面,并且显示的还是未登录的状态. 解决办法:在后台中将:站点设置--cookie作用域留空即可.

  8. 黑马程序员——JAVA基础之多线程的线程间通讯等

    ------- android培训.java培训.期待与您交流! ---------- 线程间通讯: 其实就是多个线程在操作同一个资源,但是动作不同. wait(); 在其他线程调用此对象的notif ...

  9. Android Studio 常用快捷键以及设置

    常用快捷键: Ctrl+Q 出现文档提示 跟ecplise的 鼠标悬浮差不多 Ctrl+Alt+t 包围代码 Home End 移动光标到文本首和文本尾 Alt+回车 导入当前包 Ctrl+Alt+O ...

  10. java DecimalFormat

    public class Test{ public static void main(String[] args) throws Exception{ /*DecimalFormat参数,如果是0则会 ...