ITRIX12E - R Numbers

R - Numbers

R-numbers are numbers which have the property that they do not have the digit '0' and sum of every two adjacent digits of the number is prime. 123 is a R-number because 1+2 =3 and 2+3 =5 and 3, 5 are primes.

How many R-numbers can be formed with at most length N?

i.e. R-numbers of length 1 + R-numbers of length 2 + R-numbers of length 3 + ... R-numbers of length N.

Length of a number = Number of digits in the number.

Only four single digit numbers are R-numbers which are nothing but single digit primes 2, 3, 5, 7.

Input Specification

The first line of the input file contains T which denotes the number of test cases. The next T lines contain an integer N <= 10^9.

Output Specification

Print the numbers of R-numbers modulo 1000000007. [10^9+7];

Example

Sample Input:
2
1
2

Sample Output:
4
33

Added by:Radhakrishnan Venkataramani
Date:2012-03-12
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Own problem

hide comments
2024-11-14 20:35:18
@ harshit2202
Add another row and column to the transition matrix and one row to the base matrix. In the added cell in the base matrix you can accumulate the sum of answers for all possible digit numbers up to N by making the transition matrix add all the values of the base matrix together and put the result in the mentioned cell (You can for example fill the added row in the transition matrix with 1s).
2023-06-23 06:33:16
Can someone help me why its giving run time and what is better optimisation for this solution
<snip>
[Simes]: this is not the place for a code review, use the forum.


Last edit: 2023-06-23 08:09:50
2021-07-20 13:30:31
Used G.P. sum with common ratio in the form of matrix.
2019-03-16 08:41:34
I can find it for particular N in log N
How to find it for atmost N because is N is too large
2018-10-07 09:41:51
Amazing problem , solved using linear recurrence relation :)
2018-09-08 14:40:48
ONE HELL OF A QUESTION
2018-08-23 17:36:48
(correct) answers for a few values of n:
1: 4
2: 33
3: 130
4: 454
5: 1545
10: 663370
100: 26514125
1000: 906016252
10000: 396515823
100000: 827019605
1000000: 294370820
10000000: 29572369
100000000: 530109085
1000000000: 757510247
2016-06-03 09:16:25
NICE QUESTION LEARNT A LOT
2016-05-15 08:59:27 harkirat
@david : wrong
2013-09-24 20:17:39 David
Sorry - my bad and WA at the time.
IP:
3
3
4
5

OP:
130
454
1545

Last edit: 2021-04-06 19:23:47
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.