Skip to content

Commit

Permalink
Merge pull request #1242 from maspypy/square_sum
Browse files Browse the repository at this point in the history
問題追加 Represent A Number As Two Square Sum
  • Loading branch information
maspypy authored Sep 12, 2024
2 parents 6359120 + 7af546a commit 96c1e6e
Show file tree
Hide file tree
Showing 23 changed files with 1,581 additions and 0 deletions.
40 changes: 40 additions & 0 deletions number_theory/two_square_sum/checker.cpp
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");
}
41 changes: 41 additions & 0 deletions number_theory/two_square_sum/gen/big2.cpp
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 number_theory/two_square_sum/gen/big_semiprime_random.cpp
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;
}
12 changes: 12 additions & 0 deletions number_theory/two_square_sum/gen/example_00.in
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
11 changes: 11 additions & 0 deletions number_theory/two_square_sum/gen/example_01.in
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
2 changes: 2 additions & 0 deletions number_theory/two_square_sum/gen/fixed_RNG_buster_00.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1
124376107291
13 changes: 13 additions & 0 deletions number_theory/two_square_sum/gen/max.cpp
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 number_theory/two_square_sum/gen/num_of_solution_max_00.in
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
Loading

0 comments on commit 96c1e6e

Please sign in to comment.