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 ...
随机推荐
- JAVA存取PG大对象类型OID数据
转载地址:http://my.oschina.net/liuyuanyuangogo/blog/151537 pg用大对象存储二进制数据的老文档:http://jdbc.postgresql.org/ ...
- Python编程小记
发现这种结构很实用: while True: expression .... if condition: expression break 好吧,我承认我是菜鸟-
- CGI实现页面的动态生成
传统的Web应用开发局限于有限的静态页面(HTML静态页面),不利于系统的扩展,不能提供及时信息,而且修改维护麻烦,所以建立一个动态Web应用程序尤为重要.一方面根据访问者的不同请求返回不同的访问信息 ...
- 总结 group by 的用法
今天用实例总结一下group by的用法. 归纳一下:group by:ALL ,Cube,RollUP,Compute,Compute by 创建数据脚本 Create Table SalesInf ...
- 简单插入排序(C++版)
#include <iostream> using namespace std; /** \ Insert Sort * * Key: * * reserve: tm = a[i] * * ...
- Java-->简单的斗地主发牌流程
package com.dragon.java.hwddz; import java.util.ArrayList; import java.util.HashMap; import java.uti ...
- ZSDR101-跑成品MRP
*&---------------------------------------------------------------------**& Report ZSDR101*&a ...
- Javascript模块化编程(二):AMD规范【转】
作者: 阮一峰 日期: 2012年10月30日 这个系列的第一部分介绍了Javascript模块的基本写法,今天介绍如何规范地使用模块. (接上文) 七.模块的规范 先想一想,为什么模块很重要? 因为 ...
- P364 实战练习(多线程)
尝试定义一个继承Thread类的类,并覆盖run( )方法,在run( )方法中每隔1000毫秒打印一句话. 编写代码如下: 编写PractiseThread类: package org.hanqi. ...
- PHP递归题目
$arr = [ 'a' => 'A', 'b' => 'B', 'c' => [ 'd'=> 'D', 'e'=>[ 'f'=>'F', 'g'=>['h' ...