The loop which is running for i =2 to t-1 should be removed, and matrix exponentiation can be used, which reduces complexity to O(log t).
cos(N∗X)=2∗cos(X)∗cos((N−1)∗X)−cos((N−2)∗X), this can be solved by matrix exponentiation.