kolbka_

Untitled

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