http://codeforces.com/contest/186/problem/C
how to start thinking about this type of problems
okay now i know the solution but i dont know the way of thinking to solve this type of problems
thank you in advance
# | User | Rating |
---|---|---|
1 | ecnerwala | 3649 |
2 | Benq | 3581 |
3 | orzdevinwang | 3570 |
4 | Geothermal | 3569 |
4 | cnnfls_csy | 3569 |
6 | tourist | 3565 |
7 | maroonrk | 3531 |
8 | Radewoosh | 3521 |
9 | Um_nik | 3482 |
10 | jiangly | 3468 |
# | User | Contrib. |
---|---|---|
1 | maomao90 | 174 |
2 | awoo | 164 |
3 | adamant | 162 |
4 | TheScrasse | 159 |
5 | nor | 158 |
6 | maroonrk | 156 |
7 | -is-this-fft- | 151 |
8 | SecondThread | 147 |
9 | orz | 146 |
10 | pajenegod | 145 |
http://codeforces.com/contest/186/problem/C
how to start thinking about this type of problems
okay now i know the solution but i dont know the way of thinking to solve this type of problems
thank you in advance
Name |
---|
Well, It's easy to see that at each step we will have 2i rows, also for each row the number of upwards triangles = i.
So all we need is to know the number of rows, then the answer is under the mod of course.
I think most of the problems with only one input n, we need to observe a pattern, or equation, ..
Firstly, solve for small input data by brute force, logical ways, count on a paper, researching, etc. Try to understand what it is mean and get conclusion.
Secondly, solve it fast and compare with slow solution on small input data values.
This is my solution without reading tutorial. Let
a(t)
andb(t)
— number of triangles "UP" and triangles "DOWN" in integer timet
. From test in this problem we have:Conclusion: on each iteration
a(t+1) = 3 * a(t) + b(t)
, because all triangles "UP" from iterationt
are duplicated 3 times, and all tiangles "DOWN" are duplicated 1 times and overturn,b(t+1) = a(t) + 3 * b(t)
analogically. So, we have solution inO(n)
. How to speed up? Use matrixes — this is the way to speed up slow solution! We have formulas:Let
x(t) = {a(t), b(t)}
— 2D vector, andA = {{3,1}, {1,3}}
— 2D matrix. From formulas:This is multiplication "matrix on matrix" and "matrix on vector". Finally formula for fixed integer time
t
:Just use quick pow in
O(log(n))
time and getting answer. Source.Do not forget to compare the results with the slow algorithm on small numbers before sending.
UPD: accepted