INOI 2015 - Periodic Strings - WA

I was solving the INOI 2015, Periodic String Problem Link.

My submission : Submission
Unforunately, this code is giving a WA on all test cases except first and last… What is wrong???

Any help would be much appreciated. Can somebody please help?

if it is periodic in p^n it is periodic in length p. so it becomes 2^p^n -2^p.
When there are multiple primes you have to use inclusion exclusion. say 2 & 3.
2^6 -2^(6/2) -2^(6/3)+2(6/(2*3) This holds even if there is a power. say 2^2 & 3.
2^12 - 2^(12/2) -2(12/3) +2(12/6). so just find the powers and iterate through all possible sets of prime and add if the number is even and subtract if number of primes chosen is odd.