Can anyone provide some hints to this problem? Although N <= 10^18 but there is some pattern which I am not able to detect.