Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <cmath>
- #include <ctime>
- using namespace std;
- typedef unsigned long long ull;
- const ull BASE = 10000000000ULL; //upper bound for prime numbers (for original problem 100000)
- inline ull gcd(ull a, ull b) {
- while (b) {
- a %= b;
- swap(a, b);
- }
- return a;
- }
- inline ull add(ull a, ull b, ull mod) {
- if (a + b / 2 >= mod) return (a + b / 2 - mod) + (b - b / 2);
- ull c = a + b;
- return (c >= mod ? c - mod : c);
- }
- inline ull mul(ull a, ull b, ull mod) {
- ull c = 0;
- while (b) {
- if (b & 1) {
- c = add(c, a, mod);
- }
- a = add(a, a, mod);
- b >>= 1;
- }
- return c;
- }
- inline ull powi(ull a, ull b, ull mod) {
- ull c = 1;
- while (b) {
- if (b & 1)
- c = mul(c, a, mod);
- a = mul(a, a, mod);
- b >>= 1;
- }
- return c;
- }
- ull stupid(ull n) {
- for (ull i = 2; i*i <= n; i++) {
- if (n % i == 0)
- return i;
- }
- return 1;
- }
- inline bool miller(ull n, int s, ull t, ull a) {
- ull x = powi(a, t, n);
- if (x == 1 || x == n - 1) return true;
- for (int i = 0; i < s - 1; i++) {
- x = mul(x, x, n);
- if (x == 1) return false;
- if (x == n - 1) return true;
- }
- return false;
- }
- inline bool miller(ull n) {
- ull t = n - 1;
- int s = 0;
- while (~t & 1) {
- s++; t >>= 1;
- }
- if (!miller(n, s, t, 2ULL)) return false;
- if (!miller(n, s, t, 325ULL)) return false;
- if (!miller(n, s, t, 9375ULL % n)) return false;
- if (!miller(n, s, t, 28178ULL % n)) return false;
- if (!miller(n, s, t, 450775ULL % n)) return false;
- if (!miller(n, s, t, 9780504ULL % n)) return false;
- if (!miller(n, s, t, 1795265022ULL % n)) return false;
- return true;
- }
- ull pollard(ull n) {
- if (n < 1000)
- return stupid(n);
- if (miller(n))
- return 1;
- ull x = 997;
- ull y = 1;
- ull i = 0;
- ull stage = 2;
- while (gcd(n, (x > y ? x - y : y - x)) == 1) {
- if (i == stage) {
- y = x;
- stage <<= 1;
- }
- x = add(mul(x, x, n), 1, n);
- i++;
- }
- return gcd(n, (x > y ? x - y : y - x));
- }
- ull reverse(ull n) {
- ull p = 0;
- while (n) {
- p = p * 10 + n % 10;
- n /= 10;
- }
- return p;
- }
- void solve() {
- for (ull x = BASE - 1; x; x--) {
- ull n = x * (BASE / 10ULL) + reverse(x / 10ULL); //palindrome
- ull a = pollard(n); //first divider
- ull b = n / a; //second divider
- if (a != 1 && miller(a) && miller(b) && a < BASE && b < BASE) {
- cout << a << " * " << b << " = " << n << endl;
- break;
- }
- }
- }
- int main() {
- ios::sync_with_stdio(0);
- int times = 30;
- for (int i = times; i--;) {
- solve();
- }
- cerr << clock() * 1.0 / times / CLOCKS_PER_SEC << endl;
- #ifdef AndreaB330
- system("pause");
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement