Hackonacci Matrix Rotations

Sort by

recency

|

4 Discussions

|

  • + 1 comment

    n,q=map(int,input().split()) for _ in range(q): angle=int(input()) angle=angle%360

    if(angle==0):
        print(0)
        continue
    
    elif(angle==90):
        if(n%7 == 0):
            print(24*(n//7)**2)
    
        elif(n%7 == 1):
            print(24*(n//7+1)**2-44*(n//7+1)+20)
    
        elif(n%7 == 2):
            print(24*(n//7+1)**2-32*(n//7+1)+10)
    
        elif(n%7 == 3):
            print(24*(n//7+1)**2-28*(n//7+1)+8)
    
        elif(n%7 == 4):
            print(24*(n//7+1)**2-18*(n//7+1)+4)
    
        elif(n%7 == 5):
            print(24*(n//7+1)**2-10*(n//7+1))
    
        elif(n%7 == 6):
            print(0)
    
    elif(angle==180):
        if(n%7 == 0):
            print(24*(n//7)**2+6*(n//7)**2)
    
        elif(n%7 == 1):
            print(24*(n//7+1)**2-44*(n//7+1)+20+4*(n//7))
    
        elif(n%7 == 2):
            print(24*(n//7+1)**2-32*(n//7+1)+10-(2*(n//7)**2+8*(n//7)))
    
        elif(n%7 == 3):
            print(24*(n//7+1)**2-28*(n//7+1)+8-(2*(n//7+1)**2-4*(n//7+1)))
    
        elif(n%7 == 4):
            print(24*(n//7+1)**2-18*(n//7+1)+4+(n//7-n))
    
        elif(n%7 == 5):
            print(24*(n//7+1)**2-10*(n//7+1)+6*(n//7+1)**2-6*(n//7+1))
    
        elif(n%7 == 6):
            print(0)
    
    elif(angle==270):
        if(n%7 == 0):
            print(24*(n//7)**2)
    
        elif(n%7 == 1):
            print(24*(n//7+1)**2-44*(n//7+1)+20)
    
        elif(n%7 == 2):
            print(24*(n//7+1)**2-32*(n//7+1)+10)
    
        elif(n%7 == 3):
            print(24*(n//7+1)**2-28*(n//7+1)+8)
    
        elif(n%7 == 4):
            print(24*(n//7+1)**2-18*(n//7+1)+4)
    
        elif(n%7 == 5):
            print(24*(n//7+1)**2-10*(n//7+1))
        elif(n%7 == 6):
            print(0)
    
  • + 0 comments

    I'm going to try to give some guidence without explaining all the logic on this one. We have 3 fundamental parts to this problem. The hackonacci function (I'll call h() for simplicity), understanding the matrix format, and rotating the matrix.

    First: the h() function. This part is pretty easy to get some insight on by building the function literally and testing it with some numbers and trying to see a pattern (if you're timing out, look at memoizing fibonacci functions). It's not a big help, but once you realize that you're testing if h(n) is even or odd, it's easy to see the h(n-2) in the recursive definition can safely be ignored.

    Once you find the pattern, you can create a simple function to output even or odd simply based on whan n%(the size of the pattern) outputs. If you're worried, the length of the pattern is not huge.

    Second: the value of each cell in the matrix is defined as X if (i*j)^2 fed into the h() function where i, j are the indices is even or Y if it's odd. One important thing to note is that, because multiplication is commutative, cell i,j will equal cell j,i. This actually tells us that this is a symmetric matric.

    I recommend building and printing several of these matrices, say up to 20 or so. You'll probably note that the pattern of the matrices also have similarities based on the pattern of the h() function.

    Last (and longest): Ok, so now we need to think about rotations... but we can break 90 and 270 degree rotations into a transpose and reflection. Also, since the matrix is symetric, it's equal to its transpose. Also, reflecting vertically will result in the same matrix as reflecting horizontally, meaning that rotating 90 or 270 degrees will result in the same output.

    Also, thanks to symmetry, a 180 degree rotation will be the same as reflection along the right to left diagonal.

    Now, you should be able to use that to traverse the matrix and compare each cell to its reflection position (either one for 90 or 270).

    I recommend testing a lot of numbers this way. You'll probably note a pattern again.

    If you don't know how look up how to try and build a polynomial solution to sequences. Last hint: think about the pattern and how two matrices of size n and m s.t. n%(length of sequence) = m%(length of sequence) are similar (ok, one more hint, you'll have to build quite a few polynomials: several for both 180 and 90/270).

  • + 0 comments

    This can be solved in O(q) time, once you realize that the even/odd parity of the hackonacci(n) function is periodic, with a prime period.

    Print out the (Y/N) pattern of the Hackonacci Matrix for, say n=41 and see what I mean. It's easier to see if you use something like * and . instead of Y and N.

    This means you don't need a 2000x2000 array or carry around a huge memoization table for the hackonacci function.

    Hint: Do write up the slow, straightforward implementation of the problem statement to give yourself something to compare your optimized output to. The test cases here are pretty thin.

  • + 1 comment

    My program works but experts as usual came with refinements. Following two points can speed up the program by 40%: 1) Matrix need not be created explicitly. 2) Two kinds of rotations give identical results. After doing these improvements, Python3 program runs within 4 sec for the worst case on i7.

No more comments