Guest User

Untitled

a guest
Aug 22nd, 2013
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import java.math.*;
  4. import java.awt.geom.*;
  5.  
  6. import static java.lang.Math.*;
  7.  
  8. public class Solution implements Runnable {
  9.    
  10.     Random rnd = new Random();
  11.    
  12.     long modMul(long a, long b, long mod) {
  13.         long ans = 0;
  14.         while (b > 0) {
  15.             if ((b & 1) != 0) {
  16.                 ans += a;
  17.                 ans %= mod;
  18.             }
  19.             b >>= 1;
  20.             a += a;
  21.             a %= mod;
  22.         }
  23.         return ans;
  24.     }
  25.    
  26.     long gcd(long a, long b) {
  27.         while (b != 0) {
  28.             a %= b;
  29.             long temp = a;
  30.             a = b;
  31.             b = temp;
  32.         }
  33.         return a;
  34.     }
  35.    
  36.     boolean rho = true;
  37.    
  38.     void factorize(long n, ArrayList<Long> result) {
  39.         if (!rho) {
  40.             int nc = (int)n;
  41.             for (int i = 2; i * i <= nc; i++) {
  42.                 while (nc % i == 0) {
  43.                     result.add((long)i);
  44.                     nc /= i;
  45.                 }
  46.             }
  47.         } else {
  48.             if (BigInteger.valueOf(n).isProbablePrime(20)) {
  49.                 result.add(n);
  50.             } else {
  51.                 pollardRho(n, result);
  52.             }
  53.         }
  54.     }
  55.    
  56.     void pollardRho(long n, ArrayList<Long> result) {
  57.         while (true) {
  58.             long x = rnd.nextLong() % n;
  59.             if (x < 0) {
  60.                 x += n;
  61.             }
  62.             long y = x;
  63.             for (int i = 2; i <= (1 << 10); i++) {
  64.                 //x = modMul(x, x, n) - 1;
  65.                 x = (x * x - 1) % n;
  66.                 if (x < 0) {
  67.                     x += n;
  68.                 }
  69.                 long val = y - x;
  70.                 if (val < 0) {
  71.                     val += n;
  72.                 }
  73.                 long d = gcd(val, n);
  74.                 if (d != 1 && d != n) {
  75.                     factorize(d, result);
  76.                     factorize(n / d, result);
  77.                     return;
  78.                 }
  79.                 if ((i & (i - 1)) == 0) {
  80.                     y = x;
  81.                 }
  82.             }
  83.         }
  84.     }
  85.  
  86.     public void solve() throws Exception {
  87.         int cnt = 100;
  88.         long first = 30000;
  89.         long[] primes = new long[cnt];
  90.         for (int i = 0, j = 0; j < cnt; i++) {
  91.             if (BigInteger.valueOf(first + i).isProbablePrime(100)) {
  92.                 primes[j++] = first + i;
  93.             }
  94.         }
  95.         long start = System.nanoTime();
  96.         for (int i = 0; i < 10000; i++) {
  97.             long n = primes[rnd.nextInt(cnt)] * primes[rnd.nextInt(cnt)];
  98.             ArrayList<Long> result = new ArrayList<>();
  99.             factorize(n, result);
  100.             if (i % 1000 == 0) {
  101.                 out.println(i);
  102.                 out.flush();
  103.             }
  104.         }
  105.         out.println(System.nanoTime() - start);
  106.     }
  107.  
  108.     static Throwable uncaught;
  109.  
  110.     BufferedReader in;
  111.     FastScanner sc;
  112.     PrintWriter out;
  113.  
  114.     @Override
  115.     public void run() {
  116.         try {
  117.             in = new BufferedReader(new InputStreamReader(System.in));
  118.             out = new PrintWriter(System.out);
  119.             sc = new FastScanner(in);
  120.             solve();
  121.         } catch (Throwable uncaught) {
  122.             Solution.uncaught = uncaught;
  123.         } finally {
  124.             out.close();
  125.         }
  126.     }
  127.  
  128.     public static void main(String[] args) throws Throwable {
  129.         Thread thread = new Thread(null, new Solution(), "", (1 << 26));
  130.         thread.start();
  131.         thread.join();
  132.         if (Solution.uncaught != null) {
  133.             throw Solution.uncaught;
  134.         }
  135.     }
  136.  
  137. }
  138.  
  139. class FastScanner {
  140.  
  141.     BufferedReader in;
  142.     StringTokenizer st;
  143.  
  144.     public FastScanner(BufferedReader in) {
  145.         this.in = in;
  146.     }
  147.  
  148.     public String nextToken() throws Exception {
  149.         while (st == null || !st.hasMoreTokens()) {
  150.             st = new StringTokenizer(in.readLine());
  151.         }
  152.         return st.nextToken();
  153.     }
  154.  
  155.     public int nextInt() throws Exception {
  156.         return Integer.parseInt(nextToken());
  157.     }
  158.  
  159.     public long nextLong() throws Exception {
  160.         return Long.parseLong(nextToken());
  161.     }
  162.  
  163.     public double nextDouble() throws Exception {
  164.         return Double.parseDouble(nextToken());
  165.     }
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment