Calculate in [L, R] how many numbers whose sum of digits and sum of squares of digits are primes ?
Limit :
* 1 <= L <= R <= 10 ^ 18
* T <= 10 ^ 4: is the number of queries [L, R].
For example, in the [1, 20] there are 4 numbers which are 11, 12, 14, 16.
This code might help! Let me know if it works
$$$1 \le l,r \le 10^{18}$$$
Your code doesn't work. It would get TLE because you are iterating over the whole range $$$[L,R]$$$ which can have length up to $$$10$$$^$$$18$$$.
Note: it is also possible to check if number is prime in $$$O(sqrt(n))$$$.
Learn Digit DP
.
I code digit dp for each query and TLE
.
I think digit can pass easily if you smartly handle states, even naive digit dp will pass too I guess
$$$dp[i][j][k][l]$$$ = first i numbers, j(0/1) determines <= target number, k is the current sum of digits, l is current sum of square digits
There are only actually 18*2*9*18*81*18 = 8,503,056 possible states regardless of T
UPD: You might be wondering: How to do this without having the reset dp array every time? There's a neat trick that allows for resetting dp only once at the start, by reversing the numbers and doing the dp from right to left instead.
I will do digit dp
please help me❤️
I've coded it here
Your code doesn't seem to work, have you checked it? For example, if l = 57 and r = 73, your code outputs 0, but the correct answer is 3 (58, 61, 65)
It works fine. I edited the code some minutes after posting it, maybe you tried the old version
any reason why you are not using seive for prime checking?
No need to, primes<=1460
Any link for this problem.