Implement a ranking based method for calculating the number of lucky numbers, and implement a way to find the next greater number for a given number and finger print.
Implement a new algorithm for calculating lucky numbers. Instead of iterating all numbers and verify each number is a lucky number or not, there is a pre-processing phase. In pre-processing phase, all primes are iterated, and a finger print whose all digits will sum to a prime will be generated. During query, for each lucky number finger print, a rank for query range start/end will be computed for this finger print, and the number of lucky numbers will be the sum for each diff of these two ranks calculated. But the pre-process seems too slow to pass the online judge, and it will cost 3s to pre-process in my own machine.