-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathThe Unlucky 13.cpp
128 lines (101 loc) · 2.61 KB
/
The Unlucky 13.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
Solution by Rahul Surana
***********************************************************
Write a program to calculate the total number of strings that are made of exactly N characters.
None of the strings can have "13" as a substring.
The strings may contain any integer from 0 - 9, repeated any number of times.
Input Format:
First line: T, the number of test cases.
Next T lines: Each contain an integer N.
Output Format:
Print the result of each query mod 10^9.
Answer for each test case should come in a new line.
***********************************************************
*/
#include <bits/stdc++.h>
#define int long long
#define vl vector<ll>
#define vi vector<int>
#define pi pair<int,int>
#define pl pair<ll,ll>
#define all(a) a.begin(),a.end()
#define mem(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define FOR(i,a) for(int i = 0; i < a; i++)
#define trace(x) cerr<<#x<<" : "<<x<<endl;
#define trace2(x,y) cerr<<#x<<" : "<<x<<" | "<<#y<<" : "<<y<<endl;
#define trace3(x,y,z) cerr<<#x<<" : "<<x<<" | "<<#y<<" : "<<y<<" | "<<#z<<" : "<<z<<endl;
#define fast_io std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define mod 1000000009
using namespace std;
void io()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("/home/kashif/cp/input.txt","r",stdin);
freopen("/home/kashif/cp/output.txt","w",stdout);
#endif
}
int i,j,k;
int M[4][4] = {{0,99,-10,2},{1,0,0,0},{0,1,0,0},{0,0,0,10}};
int p;
void multiply(int a[4][4],int b[4][4])
{
int mul[4][4]={0};
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
for(k=0;k<4;k++)
{
p=(a[i][k]*b[k][j]);
mul[i][j]=(mul[i][j]+p);
}
}
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
a[i][j]=mul[i][j]%mod;
}
}
void power(int a[4][4],int n)
{
if(n==1 || n==0)
return;
power(a,n/2);
multiply(a,a);
if (n%2 != 0)
multiply(a,M);
}
int32_t main()
{
fastio();
io();
int t,n,ans;
cin>>t;
while(t--)
{
cin>>n;
if(n==0)
printf("%d\n",1);
else if(n==1)
printf("%d\n",10);
else if(n==2)
printf("%d\n",99);
else if(n==3)
printf("%d\n",980);
else
{
int a[4][4]={{0,99,-10,2},{1,0,0,0},{0,1,0,0},{0,0,0,10}};
power(a,n-3);
p=(a[3][3]*1000);
k=(a[0][0]*20+a[0][1]+100*a[0][3])%mod;
ans=(p-k+mod)%mod;
printf("%lld\n",ans);
}
}
}