結果
| 問題 |
No.2125 Inverse Sum
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-11-19 10:35:02 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 25 ms / 2,000 ms |
| コード長 | 5,090 bytes |
| コンパイル時間 | 2,273 ms |
| コンパイル使用メモリ | 159,508 KB |
| 最終ジャッジ日時 | 2025-02-08 22:42:28 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 30 |
ソースコード
#pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
#include <bitset>
using namespace std;
typedef long long int ll;
/*
ポラードのρ法
参考資料
- https://qiita.com/Kiri8128/items/eca965fe86ea5f4cbb98
- https://manabitimes.jp/math/1192
*/
// verify:https://www.acmicpc.net/problem/4149
namespace factorize{
using u64 = uint64_t;
using u128 = __uint128_t;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
u64 binary_gcd(u64 a, u64 b) {
if (a == 0) return b;
if (b == 0) return a;
const int n = __builtin_ctzll(a | b);
a >>= __builtin_ctzll(a);
while (b > 0) {
b >>= __builtin_ctzll(b);
if (a > b) std::swap(a, b);
b -= a;
}
return a << n;
}
u128 pow (u128 a, u64 n, u128 mod) {
u128 res = 1;
if (a >= mod) a %= mod;
while (n > 0) {
if (n & 1) {
res *= a;
if (res >= mod) res %= mod;
}
a *= a;
if (a >= mod) a %= mod;
n >>= 1;
}
return res;
}
bool miller_rabin (u64 n, vector<u64> v) {
u64 d = n-1;
while (~d & 1) d >>= 1;
for (u64 a:v) {
if (n <= a) break;
u64 t = d;
u128 y = pow(a, t, n);
while (t != n-1 and y != 1 and y != n-1) {
y *= y; if(y >= n) y %= n;
t *= 2;
}
if (y != n-1 and t % 2 == 0) return false;
}
return true;
}
bool is_prime (u64 n) {
if (n <= 1) return false;
if (~n & 1) return (n == 2);
if (n < (1LL << 30)) {
return miller_rabin(n, {2, 7, 61});
}
else {
return miller_rabin(n, {2, 325, 9375, 28178, 450775, 9780504, 1795265022});
}
}
template <typename T>
T pollard_rho (T n) {
if (~n & 1) return 2;
if (is_prime(n)) return n;
static u128 x,y,c,d;
auto f = [&](u128 x) {return (x * x % n + c) % n;};
auto rnd_ = [&](T l, T r) {return rng() % (r - l + 1) + l;};
x = rnd_(2, n);
y = x;
c = rnd_(1, n);
d = 1;
while (d == 1) {
x = f(x);
y = f(y); y = f(y);
d = binary_gcd((x > y ? x-y : y-x), n);
if ((T)d == n) {
return pollard_rho(n);
}
}
if (is_prime(d)) {
return d;
}
else {
return pollard_rho(d);
}
}
template <typename T>
vector<T> prime_factor (T n) {
vector<T> res;
for (T i = 2; i*i <= n;) {
while (n % i == 0) {
n /= i;
res.emplace_back(i);
}
i += 1 + (~n & 1);
if (i >= 101 and n >= (1<<20)) {
while (n > 1) {
auto p = pollard_rho(n);
while (n % p == 0) {
n /= p;
res.emplace_back(p);
}
}
break;
}
}
if (n > 1) res.emplace_back(n);
sort(res.begin(), res.end());
return res;
}
template <typename T>
map<T, int> factor_count (T n) {
map<T, int> mp;
for (auto &x : prime_factor(n)) mp[x]++;
return mp;
}
template <typename T>
vector<T> divisors(T n) {
if (n == 0) return {};
vector<pair<T, int>> v;
for(auto &p : factor_count(n)) v.push_back(p);
vector<T> res;
auto f = [&](auto self, int i, T x) -> void {
if (i == (int)v.size()) {
res.push_back(x);
return;
}
for (int j = 0; j <= v[i].second; ++j) {
self(self, i + 1, x);
if (j+1 <= v[i].second) {
x *= v[i].first;
}
}
};
f(f, 0, 1);
sort(res.begin(), res.end());
return res;
}
} // namespace factorize
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
ll p,q; cin >> p >> q;
ll g = gcd(p,q);
p /= g; q /= g;
// MQ+NQ=PNM
// PMQ+PNQ=P^2NM
// (PN - Q)(PM - Q) = Q^2
auto d = factorize::divisors(q*q);
vector<pair<ll,ll>> res;
for(int i=0;i<d.size();i++){
ll a = d[i], b = q*q/d[i];
if((a+q)%p == 0 and (b+q)%p == 0){
res.push_back({(a+q)/p, (b+q)/p});
}
}
sort(res.begin(), res.end());
cout << res.size() << "\n";
for(auto p:res){
cout << p.first << " " << p.second << "\n";
}
}