Guest User

Untitled

a guest
Jan 16th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.86 KB | None | 0 0
  1. package problems;
  2.  
  3. public class PE160 {
  4.    
  5.     public static long solve(long N, int k) {
  6.         int mod = 1; for (int i=0; i<k; i++) mod*=10;
  7.        
  8.         // factorials excluding multiples of 5 and dividing by 2 for each such multiple
  9.         long[] prods = new long[mod+1];
  10.         prods[0] = 1;
  11.         long prod = 1;
  12.         for (int i=1; i<=mod; i++) {
  13.             if (i%5==0) prod/=2;
  14.             else prod = (prod*i) % (10*mod);
  15.             prods[i] = prod%mod;
  16.         }
  17.        
  18.         long res = 1;
  19.        
  20.         while (N > 0) {
  21.             // we have N/mod of these
  22.             long count = N/mod;
  23.             for (int i=0; i<count; i++)
  24.                 res = (res * prods[mod]) % mod;
  25.             // and what remains
  26.             res = (res * prods[(int)(N%mod)]) % mod;
  27.            
  28.             // we excluded N/5 numbers
  29.             N /= 5;
  30.         }
  31.        
  32.         return res;
  33.     }
  34.    
  35.     public static void solve(){
  36.         System.out.println(solve(1000000000000L, 5));
  37.     }
  38.    
  39.     public static void main(String[] args) {
  40.         solve();
  41.     }
  42. }
Add Comment
Please, Sign In to add comment