HackerRank "Lucky Numbers"
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"的更多相关文章
- HDU 5676 ztr loves lucky numbers (模拟)
ztr loves lucky numbers 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/I Description ztr ...
- codeforces 630C Lucky Numbers
C. Lucky Numbers time limit per test 0.5 seconds memory limit per test 64 megabytes input standard i ...
- hdu 5676 ztr loves lucky numbers(dfs+离线)
Problem Description ztr loves lucky numbers. Everybody knows that positive integers are lucky if the ...
- codeforces 630C - Lucky Numbers 递推思路
630C - Lucky Numbers 题目大意: 给定数字位数,且这个数字只能由7和8组成,问有多少种组合的可能性 思路: 假设为1位,只有7和8:两位的时候,除了77,78,87,88之外还哇哦 ...
- hdu 5676 ztr loves lucky numbers 打表+二分
ztr loves lucky numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- ZCMU 2177 Lucky Numbers (easy)
传送门: http://acm.zcmu.edu.cn/JudgeOnline/problem.php?id=2177 2177: Lucky Numbers (easy) 时间限制: 2 Sec ...
- hdu-5676 ztr loves lucky numbers(乱搞题)
题目链接: ztr loves lucky numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K ( ...
- C - Lucky Numbers (easy)
Problem description Petya loves lucky numbers. Everybody knows that positive integers are lucky if t ...
- 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 ...
随机推荐
- Tomcat9源码编译及导入Eclipse(转)
1.下载tomcat源码.建议下载最新版本tomcat9. svn地址:http://svn.apache.org/repos/asf/tomcat/tc9.0.x/branches/gsoc-jas ...
- Open vSwitch安装及配置
一. Open vSwitch简介 1.1概述 Open vSwitch是一个高质量的.多层虚拟交换机,使用开源Apache 2.0许可协议,由Nicira Networks开发,主要实现代码为可移植 ...
- Azure 云助手正式发布
Azure云服务在中国市场风生水起,越来越多的用户选择Azure作为平台将业务转向云端.随着移动互联网在中国的蓬勃发展,手机应用的体验深入人们的生活及工作.用户管控云服务也不应该只局限于电脑前,而是可 ...
- pcr free library 介绍
一句话:illumina的建库方法,建库时间段,质量还好... the adapters are different in the PCR-free kit compared to the stand ...
- 设计模式 : Template method 模板方法模式 -- 行为型
设计模式中,模板模式面向的是方法级别的流程.(不过好像世界上大部分问题,都可以抽象点.抽象点吧,最后抽象到一个方法里面吧.) 1. 一个方法,可以用来描述一个流程,这个流程涉及多个环节,不同环节可 ...
- 用过sessionid防钓鱼
http://www.cnblogs.com/BearsTaR/archive/2010/08/24/URL_SESSION_ID_LEEK.html DisableUrlSessionFilter
- ✡ leetcode 162. Find Peak Element --------- java
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- 编写linux驱动所用到的头文件(转)
转自:http://blog.csdn.net/lufeiop02/article/details/6448497 关于linux驱动(应用)程序头文件使用 收藏 驱动程序: #include < ...
- [原创]cocos2d-x研习录-第一阶 背景介绍 之 cocos2d家族史
Cocos2D是一个2D开源游戏引擎,它最早是由Ricardo Quesada(阿根廷人,社区简称Riq)和他的朋友们用Python开发的,用于开发2D游戏和基于2D图形的任何应用.最早引擎的名字源自 ...
- Unity学习资源
NGUI文档及视频: http://www.tasharen.com/forum/index.php?topic=6754 动态更新的解决方案: http://game.ceeger.com/foru ...