forked from yosupo06/library-checker-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
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 yosupo06#1242 from maspypy/square_sum
問題追加 Represent A Number As Two Square Sum
- Loading branch information
Showing
23 changed files
with
1,581 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,40 @@ | ||
#include "testlib.h" | ||
|
||
#include <vector> | ||
#include <tuple> | ||
#include <cassert> | ||
|
||
using namespace std; | ||
using ll = long long; | ||
|
||
ll read_ans_cnt(ll N, InStream& stream) { | ||
ll n = stream.readInt(); | ||
vector<pair<ll, ll>> ANS(n); | ||
for (int i = 0; i < n; ++i) { | ||
ll x = stream.readLong(0LL, 1LL << 30); | ||
ll y = stream.readLong(0LL, 1LL << 30); | ||
stream.ensure(x * x + y * y == N); | ||
ANS[i] = {x, y}; | ||
} | ||
// check distinct | ||
sort(ANS.begin(), ANS.end()); | ||
for (int i = 0; i < n - 1; ++i) stream.ensure(ANS[i] != ANS[i + 1]); | ||
return n; | ||
} | ||
|
||
int main(int argc, char* argv[]) { | ||
registerTestlibCmd(argc, argv); | ||
|
||
int Q = inf.readInt(); | ||
for (int q = 0; q < Q; ++q) { | ||
ll N = inf.readLong(); | ||
ll ans_cnt = read_ans_cnt(N, ans); | ||
ll ouf_cnt = read_ans_cnt(N, ouf); | ||
if (ans_cnt > ouf_cnt) { | ||
quitf(_wa, "Not found all solutions. N=%lld, ans_cnt=%lld, ouf_cnt=%lld", N, ans_cnt, ouf_cnt); | ||
} else if (ans_cnt < ouf_cnt) { | ||
quitf(_fail, "Writer does not find all solutions! N=%lld, ans_cnt=%lld, ouf_cnt=%lld", N, ans_cnt, ouf_cnt); | ||
} | ||
} | ||
quitf(_ok, "OK"); | ||
} |
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,41 @@ | ||
#include <iostream> | ||
#include "random.h" | ||
#include "../params.h" | ||
|
||
using namespace std; | ||
|
||
vector<long long> enum_prime(long long st, long long ed) { | ||
if (st == 1) st = 2; | ||
vector<bool> is_prime(ed - st + 1, true); | ||
for (long long i = 2; i * i <= ed; i++) { | ||
for (long long j = (st + i - 1) / i * i; j <= ed; j += i) { | ||
is_prime[j - st] = false; | ||
} | ||
} | ||
vector<long long> primes; | ||
for (long long i = st; i <= ed; i++) { | ||
if (is_prime[i - st]) primes.push_back(i); | ||
} | ||
return primes; | ||
} | ||
|
||
int main(int, char* argv[]) { | ||
|
||
|
||
long long seed = atoll(argv[1]); | ||
auto gen = Random(seed); | ||
|
||
auto primes = enum_prime(1'000'000'000 - 1'000'000, 1'000'000'000); | ||
int k = int(primes.size()); | ||
int q = MAX_Q; | ||
vector<long long> a(q); | ||
for (int i = 0; i < q; i++) { | ||
a[i] = primes[gen.uniform(0, k - 1)] * primes[gen.uniform(0, k - 1)]; | ||
} | ||
|
||
printf("%d\n", q); | ||
for (auto x: a) { | ||
printf("%lld\n", x); | ||
} | ||
return 0; | ||
} |
116 changes: 116 additions & 0 deletions
116
number_theory/two_square_sum/gen/big_semiprime_random.cpp
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
11 | ||
0 | ||
1 | ||
2 | ||
3 | ||
4 | ||
5 | ||
6 | ||
7 | ||
8 | ||
9 | ||
10 |
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,11 @@ | ||
10 | ||
5 | ||
25 | ||
125 | ||
13 | ||
65 | ||
998244353 | ||
1000000007 | ||
1000000009 | ||
536870912 | ||
1073741824 |
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,2 @@ | ||
1 | ||
124376107291 |
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,13 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include "../params.h" | ||
|
||
int main(int, char **argv) { | ||
const long long seed = atoll(argv[1]); | ||
printf("%lld\n", MAX_Q); | ||
for (int i = 0; i < MAX_Q; ++i) { | ||
const long long a = MAX_A - seed * MAX_Q - i; | ||
printf("%lld\n", a); | ||
} | ||
return 0; | ||
} |
101 changes: 101 additions & 0 deletions
101
number_theory/two_square_sum/gen/num_of_solution_max_00.in
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,101 @@ | ||
100 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 | ||
331854559557386125 | ||
663709119114772250 |
Oops, something went wrong.