-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpe684.py
234 lines (218 loc) · 6.1 KB
/
pe684.py
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
"""
n, s(n), S(n),
1 1 1
2 2 3
3 3 6
4 4 10
5 5 15
6 6 21
7 7 28
8 8 36
9 9 45 (54 - 9 or tri(9))
---------
10 19 64 = 45 + 19 = 45 + (20 - 1) = 45 + (2 * 10 - 1)
11 29 93 = 45 + ((2 + 3) * 10 - 2)
12 39 132 = 45 + ((2 + 3 + 4) * 10 - 3)
: 132, 181, 240, 309, 388 =
17 89 477
18 99 576 (540 - 9 + 54 - 9) = 54 * 11 - 9 * 2
-----------
19 199 775
20 299 1074
:
26 899 4968
27 999 5967 (5400 - 9 + 540 - 9 + 54 - 9) = 54 * 111 - 9 * 3
------
28 1999 7966
29 2999 10965 = 5967 + tri(1+(n%9))-1 * 10^(n//9) - (n%9))
:
"""
MOD = 1_000_000_007
# returns the triangle number for the index in 1..9
TRI = [-1,1,3,6,10,15,21,28,36,45,55]
def solve684():
total = 0
f_2 = 0
f_1 = 1
for i in range(2,50):
f = f_2 + f_1
# print(f"f{i} = {f}, mod 9 = {f%9}")
f_2 = f_1
f_1 = f
sf = S(f)
# sfm = sf % MOD
print(f"f{i} = f; S({f}) = {sf}") # %= {sfm}")
total += sf
total %= MOD
print(f"PE 684 Answer = {total}")
def S(n):
# n => q,r: 1 => 0,1; 9 => 0,9; 10 => 1,1; 18 => 1,9; 19 => 3,1
q = (n-1) // 9
r = ((n-1) % 9) + 1
print(" S", n, 9, q, r)
if n < 10:
return TRI[n]
part1 = ((TRI[1+r] - 1) * tens2(q) - r) % MOD
print(" part1", part1)
part2 = S2m(q)
print(" part2", part2)
part3 = (9 * q) % MOD
# print(" parts", part1, part2, part3)
return (part1 + part2 - part3) % MOD
def tens(exp):
# ignore mod for now
# return 10**exp
# 10^exp % 1_000_000_0007
# where exp could be very large (> 10 digits)
q = exp // 18
r = exp % 18
# print("tens", exp, 18, q, r)
tens = 10**r
# This starts to really slow down when q > 2_000_000 ~fib43
# See tens2 for an alternative
total = (tens % MOD) * ((49**q) % MOD)
# print(" tens = ", total)
return total % MOD
def tens2(exp):
q = exp // 18
r = exp % 18
# print("tens", exp, 18, q, r)
total = 1
for _ in range(0,q):
total *= 49
if total > 1_000_000_000_000_000_000:
total %= MOD
total *= 10**r
total %= MOD
return total
def S2(n):
if n == 0:
return 0
ones = 1
for _ in range(1,n):
ones *= 10
ones += 1
# print(54*ones)
return 54 * ones
def S2m(n):
if n == 0:
return 0
# about 20 digits will fit in a 64bit int, we will stop at 18
ones = 111_111_111_111_111_111 % MOD
q = n // 18
r = n % 18
# print(q,r)
total = 0
for _ in range(0,q):
total *= 49 # 49 = 10**18 % MOD
total += ones # 49 = 10**18 % MOD
total %= MOD
total *= 10**r
total %= MOD
rest = 0
for _ in range(0,r):
rest *= 10
rest += 1
# print("r", r, rest)
rest %= MOD
# print(54, total, rest)
# print(54 * (total + rest))
return (54 * (total + rest)) % MOD
def print_fibs():
f_2 = 0
print("f0 = 0")
f_1 = 1
print("f1 = 1")
for i in range(2,91):
f = f_2 + f_1
print(f"f{i} = {f}, mod 9 = {f%9}")
f_2 = f_1
f_1 = f
def print_mods():
t = 1_000_000_00
for i in range(1,20):
t *= 10
# t += 54
# x = t - 9*i
print(f"10^{i+8} => {t % MOD}; {t}, % {MOD}")
# 1_000_000_000_000_000_000 = 10^18 % 1_000_000_007 = 49
"""
10^9 => 1000000000; 1000000000, % 1000000007
10^10 => 999999937; 10000000000, % 1000000007
10^11 => 999999307; 100000000000, % 1000000007
10^12 => 999993007; 1000000000000, % 1000000007
10^13 => 999930007; 10000000000000, % 1000000007
10^14 => 999300007; 100000000000000, % 1000000007
10^15 => 993000007; 1000000000000000, % 1000000007
10^16 => 930000007; 10000000000000000, % 1000000007
10^17 => 300000007; 100000000000000000, % 1000000007
10^18 => 49; 1000000000000000000, % 1000000007
10^19 => 490; 10000000000000000000, % 1000000007
10^20 => 4900; 100000000000000000000, % 1000000007
10^21 => 49000; 1000000000000000000000, % 1000000007
10^22 => 490000; 10000000000000000000000, % 1000000007
10^23 => 4900000; 100000000000000000000000, % 1000000007
10^24 => 49000000; 1000000000000000000000000, % 1000000007
10^25 => 490000000; 10000000000000000000000000, % 1000000007
10^26 => 899999972; 100000000000000000000000000, % 1000000007
10^27 => 999999664; 1000000000000000000000000000, % 1000000007
"""
def print_mods2():
t = 11_111_111
for i in range(1,20):
t *= 10
t += 1
# t += 54
# x = t - 9*i
print(f"[1]*{i+8} => {t % MOD}; {t}, % {MOD}")
# print_fibs()
# print_mods()
# print_mods2()
solve684()
# for i in range(16,22):
# print(i, 10**i % MOD, tens(i), tens2(i) )
# print(i, S2(i) % MOD, S2m(i) )
# print(S2(41) % MOD, S2m(41) )
# print(S2(67) % MOD, S2m(67) )
# for i in range(1,21):
# print(f"S({i}) = {S(i)}")
#S(2880067194370816120)
"""
S 1 9 0 1
f2 = f; S(1) = 1 %= 1
S 2 9 0 2
f3 = f; S(2) = 3 %= 3
S 3 9 0 3
f4 = f; S(3) = 6 %= 6
S 5 9 0 5
f5 = f; S(5) = 15 %= 15
S 8 9 0 8
f6 = f; S(8) = 36 %= 36
S 13 9 1 4
parts 136 54 9
f7 = f; S(13) = 181 %= 181
S 21 9 2 3
parts 897 594 18
f8 = f; S(21) = 1473 %= 1473
S 34 9 3 7
parts 34993 5994 27
f9 = f; S(34) = 40960 %= 40960
S 55 9 6 1
parts 1999999 5999994 54
f10 = f; S(55) = 7999939 %= 7999939
S 89 9 9 8
parts 43999999992 5999999994 81
f11 = f; S(89) = 49999999905 %= 999999562
S 144 9 15 9
parts 53999999999999991 5999999999999994 135
f12 = f; S(144) = 59999999999999850 %= 579999857
S 233 9 25 8
parts 439999999999999999999999992 59999999999999999999999994 225
f13 = f; S(233) = 499999999999999999999999761 %= 499999593
S 377 9 41 8
parts 4399999999999999999999999999999999999999992 599999999999999999999999999999999999999994 369
f14 = f; S(377) = 4999999999999999999999999999999999999999617 %= 4999533
S 610 9 67 7
parts 349999999999999999999999999999999999999999999999999999999999999999993 59999999999999999999999999999999999999999999999999999999999999999994 603
f15 = f; S(610) = 409999999999999999999999999999999999999999999999999999999999999999384 %= 347371750
"""