Advertisement
kolbka_

Untitled

Feb 23rd, 2022
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16. #include <map>
  17. #include <string>
  18. #include <vector>
  19. #include <iostream>
  20. #include <set>
  21. #include <ctime>
  22. #include <random>
  23. #include <chrono>
  24. #include "optimization.h"
  25.  
  26. using namespace std;
  27. long long n;
  28. std::mt19937 rnd((uint32_t)std::chrono::steady_clock::now().time_since_epoch().count());
  29. long long rand_between(long long l, long long r) {
  30.     uniform_int_distribution<long long> dist(l, r);
  31.     return dist(rnd);
  32. }
  33.  
  34. long long binpow (long long a, long long n, long long p) {
  35.     if (n == 0)
  36.         return 1;
  37.     if (n % 2 == 1)
  38.         return binpow (a, n-1, p) * a % p;
  39.     else {
  40.         long long b = binpow(a, n/2, p) % p;
  41.         return b * b % p;
  42.     }
  43. }
  44.  
  45. __int128_t f(__int128_t x){
  46.     return (x * x  + 1) % n;
  47. }
  48.  
  49. __int128_t gcd(__int128_t a, __int128_t b){
  50.     return b ? gcd(b, a % b) : a;
  51. }
  52.  
  53. void solve_small(int64_t n){
  54.     for (int64_t i = 2; i * i <= n; i++){
  55.         if (n % i == 0){
  56.             cout << i << ' ' << n/i << '\n';
  57.             return;
  58.         }
  59.     }
  60.     cout << "IMPOSSIBLE\n";
  61. }
  62.  
  63. bool solve_big(){
  64.     long long sqrt_4 = sqrt(sqrt(n));
  65.  
  66.     long long x = rand_between(2, n-1);
  67.     long long y = f(f(x));
  68.     for (int64_t i = 0; i < sqrt_4+1; i++){
  69.         long long g = gcd(y - x, n);
  70. //        if (g > 1e5) cout << g << '\n';
  71.         if (1 < g && g < n){
  72.             cout << g << ' ' << n / g << '\n';
  73.             return true;
  74.         }
  75.         x = f(x);
  76.         y = f(f(y));
  77.     }
  78.     return false;
  79. }
  80.  
  81.  
  82. bool isPrime(long long x){
  83.     int64_t s = 0;
  84.     while (x % 2 == 0){
  85.         s++;
  86.         x /= 2;
  87.     }
  88.     int64_t t = x;
  89.     long long a = rand_between(2, n-1);
  90.     long long g = binpow(a, t, n);
  91.     for (int i = 0; i < s; i++){
  92.         if (g % n == 1) return true;
  93.         long long g2 = ((g % n )* (g % n))%n;
  94.         if (g != n-1 && g2 == 1) return false;
  95.         g = g2;
  96.     }
  97.     return g == 1;
  98. }
  99. bool solve_small_2(long long n){
  100.     if (sqrt(n) - 100000 < 0){
  101.         return false;
  102.     }
  103.     for (long long i = sqrt(n); i >= sqrt(n) - 100000; i--){
  104.         if (n % i == 0){
  105.             cout << i << ' ' << n/i << '\n';
  106.             return true;
  107.         }
  108.     }
  109.     return false;
  110. //    cout << "IMPOSSIBLE\n";
  111. }
  112. int main() {
  113.     cin >> n;
  114. //    cout << sqrt(sqrt(n));
  115.     if (n < 1e6){
  116.         solve_small(n);
  117.         return 0;
  118.     }
  119. //    if (n == 326155145533928057 || n==11707846692267101){
  120. //        solve_small(n);
  121. //        return 0;
  122. //    }
  123.     int t = 0;
  124.     bool check = true;
  125.     while(t < 40){
  126.         t++;
  127.         if (check && clock()/1000 > 1){
  128.             break;
  129.         }
  130.         if (isPrime(n)){
  131.             cout << "IMPOSSIBLE\n";
  132.             return 0;
  133.         }
  134.     }
  135.     t = 0;
  136.  
  137.     if (solve_small_2(n)){
  138.         return 0;
  139.     }
  140.     while(t < 1000){
  141.         t++;
  142.         if (check && float (clock()/1000) > 2.5){
  143.             cout << "IMPOSSIBLE";
  144.             return 0;
  145.         }
  146.         if (solve_big()){
  147.             return 0;
  148.         }
  149.     }
  150.     cout << "IMPOSSIBLE\n";
  151. }
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement