HackerRank - lucky-numbers(数位DP)

A number is called lucky if the sum of its digits, as well as the sum of the squares of its digits is a prime number. How many numbers between A and B(both inclusive) are lucky?

Lucky numbers are not to be confused with Lucky Numbers

Input Format

The first line contains the number of test cases T. Each of the next T lines contains two integers, A and B.

Constraints

Output Format

Output T lines. Each line contains the required answer for the corresponding test case.

Sample Input

2
1 20
120 130
Sample Output

4
1
Explanation

For the first case, the lucky numbers are 11, 12, 14, 16.
For the second case, the only lucky number is 120.
题意:
定义 一个数,如果他各个数字之和是素数并且各个数字平方和也是素数那么它就是幸运数
给一个[A,B]
问A~B中间(包括AB)有多少个幸运数

分析:
因为AB范围很大1e18 所以不可能暴力
但是1e18的数字和是可以很小的
sum=9*18,sqsum=81*18;
我们现在按位暴力枚举[0,B]的每个数,判断是否能构成幸运数。
发现其实没有那么多种情况,
对于当前枚举到的数影响以后结果的只有当前的sum和sqsum
所以可以DP
定义状态F[I][J][K] f[20][200][2000]
表示前i位和为j 平方和为k的 幸运数的个数
然后记忆化搜索一下就好了

#include <iost
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值