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. fn

    var avalon = function (el) { return new avalon.init(el) } avalon.init = function (el) { this[0] = th ...

  2. Hive on Spark运行环境搭建

    Hive是基于Hadoop的开源数据仓库工具,提供了类似于SQL的HiveQL语言,使得上层的数据分析人员不用知道太多MapReduce的知识就能对存储于Hdfs中的海量数据进行分析.由于这一特性而收 ...

  3. MapReduce 计算模型

    前言 本文讲解Hadoop中的编程及计算模型MapReduce,并将给出在MapReduce模型下编程的基本套路. 模型架构 在Hadoop中,用于执行计算任务(MapReduce任务)的机器有两个角 ...

  4. 二 J2EE 概述

    一 WEB 应用 1. WEB 应用工作方式:B/S 模式 (浏览器/服务器模式) 2. WEB 应用结构组成: a. WEB 服务器:是安装在 WEB 服务器计算机上的一个软件包,负责接收用户请求并 ...

  5. CGI实现页面的动态生成

    传统的Web应用开发局限于有限的静态页面(HTML静态页面),不利于系统的扩展,不能提供及时信息,而且修改维护麻烦,所以建立一个动态Web应用程序尤为重要.一方面根据访问者的不同请求返回不同的访问信息 ...

  6. 无法Ping通windows 7主机

    今天在使用Filezilla FTP_SERVER新建了一个windows主机上的FTP服务器. 建立完成之后,从MAC上无法进行连接也甚至连主机也无法连接,怀疑是防火墙的问题. 网上一搜确实是,但是 ...

  7. Spring MVC 使用拦截器优雅地实现权限验证功能

    在上一篇 SpringAOP 实现功能权限校验功能 中虽然用AOP通过抛异常,请求转发等勉强地实现了权限验证功能,但感觉不是那么完美,应该用拦截器来实现才是最佳的,因为拦截器就是用来拦截请求的,在请求 ...

  8. 1.PHP站内搜索 分类: PHP开发实例 2015-07-31 22:48 4人阅读 评论(0) 收藏

    PHP站内搜索:多关键字.加亮显示 1.SQL语句中的模糊查找 $sql = "SELECT * FROM `message` WHERE `content`like '%$k[0]%' a ...

  9. JavaScript个人学习记录总结(二)——验证表单输入之模式匹配

    该示例检查从文本窗口部件中获取姓名和电话号码这两个表单数据的有效性.当文本框中的值发生变化时,即引发一个change事件,从而可以调用一个函数来检查这两个输入值的格式是否正确. validator.h ...

  10. WebStorm 使用

    Sublime 很强大,但是在项目越来越大而复杂的时候,会显得力不从心.比如函数追踪功能的确实,找个创建函数的地方很麻烦 这时候就该 WebStorm 出场了 0.无法输入中文句号.顿号等是 JDK ...