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. 解决chrome同步问题

    原方法为 修改文件:“C:\WINDOWS\system32\drivers\etc\hosts”,但是经常失败,需要重新修改,很是麻烦. 更好的方法是使用 “谷歌访问助手(chrome版)” 具体参 ...

  2. 跟开涛老师学shiro -- 身份验证

    身份验证,即在应用中谁能证明他就是他本人.一般提供如他们的身份ID一些标识信息来表明他就是他本人,如提供身份证,用户名/密码来证明. 在shiro中,用户需要提供principals (身份)和cre ...

  3. Codeforces Round #277 (Div. 2) A B C 水 模拟 贪心

    A. Calculating Function time limit per test 1 second memory limit per test 256 megabytes input stand ...

  4. 2015GitWebRTC编译实录6

    2015.07.20 libbitrate_controller 编译通过依赖system_wrappers lib,编写测试代码时需要注意.[425/1600 ] CXX obj /webrtc/m ...

  5. 网站优化之-SEO在网页制作中的应用(信息来自慕课网课程笔记)

    一.SEO基本介绍. 1.搜索引擎工作原理. 2.seo简介:SEarch Engine Optimization,搜索引擎优化.为了提升网页在搜索引擎自然搜索结果中的收录数量及排序位置而做的优化行为 ...

  6. UVALive-7303 Aquarium (最小生成树)

    题目大意:在nxm的方格中,每一个1x1的小方格中都有一堵沿对角线的墙,并且每堵墙都有一个坚固程度,这些墙将nxm的方格分割成了若干个区域.现在要拆除一些墙,使其变成一个区域. 题目分析:将区域视作点 ...

  7. Mac下的利器们介绍

    先说说一些快捷键吧,从windows下过来还不很习惯: ctrl + 开关 关机等提示 ctrl+shift+开关 关闭显示器 cmd+option+v 相当于剪贴 cmd+tab,对于最小化了的窗口 ...

  8. 控制文本和外观------Style Binding(Style属性绑定)

    目的 style绑定是添加或删除一个或多个DOM元素上的style值.比如当数字变成负数时高亮显示,或者根据数字显示对应宽度的Bar.(注:如果你不是应用style值而是应用CSS class的话,请 ...

  9. java的nio之:java的nio系列教程之FileChannel

    一:Java NIO的FileChannel===>Java NIO中的FileChannel是一个连接到文件的通道.可以通过文件通道读写文件. ===>FileChannel无法设置为非 ...

  10. 在easyui datagrid中formatter数据后使用linkbutton

    http://ntzrj513.blog.163.com/blog/static/2794561220139245411997/ formatter:function(value,rowData,ro ...