-
-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1107 from lpha-z/factorize/semiprime
[math/factorize] 因数が近すぎない半素数テストケースの追加 (#930)
- Loading branch information
Showing
5 changed files
with
332 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include <iostream> | ||
#include <cmath> | ||
#include "random.h" | ||
#include "../params.h" | ||
|
||
using namespace std; | ||
using ll = long long; | ||
|
||
template <class T, class U> T pow_mod(T x, U n, T md) { | ||
T r = 1 % md; | ||
x %= md; | ||
while (n) { | ||
if (n & 1) r = (r * x) % md; | ||
x = (x * x) % md; | ||
n >>= 1; | ||
} | ||
return r; | ||
} | ||
|
||
bool is_prime(ll n) { | ||
if (n <= 1) return false; | ||
if (n == 2) return true; | ||
if (n % 2 == 0) return false; | ||
ll d = n - 1; | ||
while (d % 2 == 0) d /= 2; | ||
for (ll a : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) { | ||
if (n <= a) break; | ||
ll t = d; | ||
ll y = pow_mod<__int128_t>(a, t, n); // over | ||
while (t != n - 1 && y != 1 && y != n - 1) { | ||
y = __int128_t(y) * y % n; // flow | ||
t <<= 1; | ||
} | ||
if (y != n - 1 && t % 2 == 0) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
ll generate_semiprime(ll s) { | ||
for (ll t = MAX_A / s; ; --t) { | ||
if (is_prime(t)) return s * t; | ||
} | ||
} | ||
|
||
int main(int, char* argv[]) { | ||
ll seed = atoll(argv[1]); | ||
auto gen = Random(seed); | ||
|
||
// 1 <= t/s <= 100 | ||
ll gen_max = sqrt(MAX_A); | ||
ll gen_min = sqrt(MAX_A / 100); | ||
|
||
int q = MAX_Q; | ||
vector<ll> a(q); | ||
for (int i = 0; i < q; i++) { | ||
ll s; | ||
do { | ||
s = gen.uniform(gen_min, gen_max); | ||
} while(!is_prime(s)); | ||
a[i] = generate_semiprime(s); | ||
} | ||
|
||
printf("%d\n", q); | ||
for (auto x: a) { | ||
printf("%lld\n", x); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
#include <iostream> | ||
#include <cmath> | ||
#include "random.h" | ||
#include "../params.h" | ||
|
||
using namespace std; | ||
|
||
using ll = long long; | ||
using ull = unsigned long long; | ||
template <class T> using V = vector<T>; | ||
|
||
// bit op | ||
int bsf(ull x) { return __builtin_ctzll(x); } | ||
|
||
// binary gcd | ||
ll gcd(ll _a, ll _b) { | ||
ull a = abs(_a), b = abs(_b); | ||
if (a == 0) return b; | ||
if (b == 0) return a; | ||
int shift = bsf(a | b); | ||
a >>= bsf(a); | ||
do { | ||
b >>= bsf(b); | ||
if (a > b) swap(a, b); | ||
b -= a; | ||
} while (b); | ||
return (a << shift); | ||
} | ||
|
||
template <class T, class U> T pow_mod(T x, U n, T md) { | ||
T r = 1 % md; | ||
x %= md; | ||
while (n) { | ||
if (n & 1) r = (r * x) % md; | ||
x = (x * x) % md; | ||
n >>= 1; | ||
} | ||
return r; | ||
} | ||
|
||
bool is_prime(ll n) { | ||
if (n <= 1) return false; | ||
if (n == 2) return true; | ||
if (n % 2 == 0) return false; | ||
ll d = n - 1; | ||
while (d % 2 == 0) d /= 2; | ||
for (ll a : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) { | ||
if (n <= a) break; | ||
ll t = d; | ||
ll y = pow_mod<__int128_t>(a, t, n); // over | ||
while (t != n - 1 && y != 1 && y != n - 1) { | ||
y = __int128_t(y) * y % n; // flow | ||
t <<= 1; | ||
} | ||
if (y != n - 1 && t % 2 == 0) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
ll pollard_single(ll n) { | ||
if (is_prime(n)) return n; | ||
if (n % 2 == 0) return 2; | ||
ll st = 0; | ||
auto f = [&](ll x) { return (__int128_t(x) * x + st) % n; }; | ||
while (true) { | ||
st++; | ||
ll x = st, y = f(x); | ||
while (true) { | ||
ll p = gcd((y - x + n), n); | ||
if (p == 0 || p == n) break; | ||
if (p != 1) return p; | ||
x = f(x); | ||
y = f(f(y)); | ||
} | ||
} | ||
} | ||
|
||
V<ll> pollard(ll n) { | ||
if (n == 1) return {}; | ||
ll x = pollard_single(n); | ||
if (x == n) return {x}; | ||
V<ll> le = pollard(x); | ||
V<ll> ri = pollard(n / x); | ||
le.insert(le.end(), ri.begin(), ri.end()); | ||
return le; | ||
} | ||
|
||
int main(int, char* argv[]) { | ||
long long seed = atoll(argv[1]); | ||
auto gen = Random(seed); | ||
|
||
int q = MAX_Q; | ||
V<ll> a(q); | ||
for (int i = 0; i < q; ++i) { | ||
while(true) { | ||
ll x = gen.uniform(MAX_A - ll(sqrt(MAX_A)), MAX_A); | ||
auto v = pollard(x); | ||
if (v.size() == 2) { | ||
ll p = max(v[0], v[1]); | ||
ll q = min(v[0], v[1]); | ||
if (p / q < 100) { | ||
a[i] = x; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
printf("%d\n", q); | ||
for (auto x: a) { | ||
printf("%lld\n", x); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <algorithm> | ||
#include <cmath> | ||
#include <map> | ||
|
||
using namespace std; | ||
using uint = unsigned int; | ||
using ll = long long; | ||
using ull = unsigned long long; | ||
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); } | ||
template <class T> using V = vector<T>; | ||
template <class T> using VV = V<V<T>>; | ||
|
||
// bit op | ||
int popcnt(uint x) { return __builtin_popcount(x); } | ||
int popcnt(ull x) { return __builtin_popcountll(x); } | ||
int bsr(uint x) { return 31 - __builtin_clz(x); } | ||
int bsr(ull x) { return 63 - __builtin_clzll(x); } | ||
int bsf(uint x) { return __builtin_ctz(x); } | ||
int bsf(ull x) { return __builtin_ctzll(x); } | ||
|
||
//binary gcd | ||
ll gcd(ll _a, ll _b) { | ||
ull a = abs(_a), b = abs(_b); | ||
if (a == 0) return b; | ||
if (b == 0) return a; | ||
int shift = bsf(a|b); | ||
a >>= bsf(a); | ||
do { | ||
b >>= bsf(b); | ||
if (a > b) swap(a, b); | ||
b -= a; | ||
} while (b); | ||
return (a << shift); | ||
} | ||
|
||
template<class T, class U> | ||
T pow_mod(T x, U n, T md) { | ||
T r = 1 % md; | ||
x %= md; | ||
while (n) { | ||
if (n & 1) r = (r * x) % md; | ||
x = (x * x) % md; | ||
n >>= 1; | ||
} | ||
return r; | ||
} | ||
|
||
bool is_prime(ll n) { | ||
if (n <= 1) return false; | ||
if (n == 2) return true; | ||
if (n % 2 == 0) return false; | ||
ll d = n - 1; | ||
while (d % 2 == 0) d /= 2; | ||
for (ll a : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) { | ||
if (n <= a) break; | ||
ll t = d; | ||
ll y = pow_mod<__int128_t>(a, t, n); // over | ||
while (t != n - 1 && y != 1 && y != n - 1) { | ||
y = __int128_t(y) * y % n; // flow | ||
t <<= 1; | ||
} | ||
if (y != n - 1 && t % 2 == 0) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
ll difference_of_square(ll n) { | ||
ll sqrt_n = ll(sqrt(n)); | ||
for (ll i = 0; i < 300; ++i) { | ||
ll t = (sqrt_n + i) * (sqrt_n + i) - n; | ||
ll sqrt_t = ll(sqrt(t)); | ||
if (sqrt_t * sqrt_t == t) return sqrt_n + i - sqrt_t; | ||
} | ||
return 0; | ||
} | ||
|
||
ll pollard_single(ll n) { | ||
if (is_prime(n)) return n; | ||
if (n % 2 == 0) return 2; | ||
ll ds = difference_of_square(n); | ||
if (ds > 1) return ds; | ||
ll st = 0; | ||
auto f = [&](ll x) { return (__int128_t(x) * x + st) % n; }; | ||
while (true) { | ||
st++; | ||
ll x = st, y = f(x); | ||
while (true) { | ||
ll p = gcd((y - x + n), n); | ||
if (p == 0 || p == n) break; | ||
if (p != 1) return p; | ||
x = f(x); | ||
y = f(f(y)); | ||
} | ||
} | ||
} | ||
|
||
V<ll> pollard(ll n) { | ||
if (n == 1) return {}; | ||
ll x = pollard_single(n); | ||
if (x == n) return {x}; | ||
V<ll> le = pollard(x); | ||
V<ll> ri = pollard(n / x); | ||
le.insert(le.end(), ri.begin(), ri.end()); | ||
return le; | ||
} | ||
|
||
int main() { | ||
int q; | ||
scanf("%d", &q); | ||
map<ll, V<ll>> cache; | ||
for (int i = 0; i < q; i++) { | ||
ll a; | ||
scanf("%lld", &a); | ||
if (!cache.count(a)) { | ||
auto v = pollard(a); | ||
sort(v.begin(), v.end()); | ||
cache[a] = v; | ||
} | ||
auto v = cache[a]; | ||
printf("%d", int(v.size())); | ||
for (auto d: v) printf(" %lld", d); | ||
printf("\n"); | ||
} | ||
return 0; | ||
} |