http://ide.codingblocks.com/#/s/21200
https://hack.codingblocks.com/contests/c/452/1271
Ugly numbers in hacker blocks
Your code will work fine. But question asks answer for multiple test cases, so if it gives TLE, precompute your answer in an answer and use it.