help in optimizing solution of a spoj problem.

prob : SPOJ.com - Problem COOLNUMS

my soln : qw9NDK - Online C++0x Compiler & Debugging Tool - Ideone.com

It is giving TLE please help!