Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package problems;
- public class PE160 {
- public static long solve(long N, int k) {
- int mod = 1; for (int i=0; i<k; i++) mod*=10;
- // factorials excluding multiples of 5 and dividing by 2 for each such multiple
- long[] prods = new long[mod+1];
- prods[0] = 1;
- long prod = 1;
- for (int i=1; i<=mod; i++) {
- if (i%5==0) prod/=2;
- else prod = (prod*i) % (10*mod);
- prods[i] = prod%mod;
- }
- long res = 1;
- while (N > 0) {
- // we have N/mod of these
- long count = N/mod;
- for (int i=0; i<count; i++)
- res = (res * prods[mod]) % mod;
- // and what remains
- res = (res * prods[(int)(N%mod)]) % mod;
- // we excluded N/5 numbers
- N /= 5;
- }
- return res;
- }
- public static void solve(){
- System.out.println(solve(1000000000000L, 5));
- }
- public static void main(String[] args) {
- solve();
- }
- }
Add Comment
Please, Sign In to add comment