結果
| 問題 |
No.2929 Miracle Branch
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-10-14 22:08:17 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,977 bytes |
| コンパイル時間 | 3,045 ms |
| コンパイル使用メモリ | 138,008 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-10-14 22:08:43 |
| 合計ジャッジ時間 | 9,865 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 WA * 1 |
| other | AC * 9 WA * 34 |
ソースコード
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#ifdef local
#include <C++/core/io/debug_print.hpp>
#else
#define dump(...) void(0);
#endif
#include <iostream>
#include <ranges>
#include <algorithm>
#include <numeric>
#include <vector>
#include <cstdint>
namespace man {
constexpr uint LIM = 2e5;
bool miller(const uint64_t n);
namespace internal {
#ifndef TEMPLATE
typedef __uint128_t u128;
#endif
uint bsf(const uint64_t n){ return __builtin_ctzll(n); }
uint64_t gcd(uint64_t a, uint64_t b) {
if(a == 0) {
return b;
}
if(b == 0) {
return a;
}
const uint shift = internal::bsf(a | b);
a >>= internal::bsf(a);
do {
b >>= internal::bsf(b);
if(a > b) {
std::swap(a, b);
}
b -= a;
} while(b > 0);
return a << shift;
}
uint64_t mod_pow(const uint64_t a, uint64_t b, const uint64_t mod) {
uint64_t r = 1;
u128 x = a % mod;
while(b > 0) {
if(b & 1) {
r = (u128(r) * x) % mod;
}
x = (u128(x) * x) % mod;
b >>= 1;
}
return r;
}
uint64_t find(const uint64_t n) {
if(miller(n)) {
return n;
}
if(n % 2 == 0) {
return 2;
}
int st = 0;
const auto f = [&](const uint64_t x) -> uint64_t { return (u128(x) * x + st) % n; };
while(true) {
st++;
uint64_t x = st, y = f(x);
while(true) {
const uint64_t p = gcd(y - x + n, n);
if(p == 0 || p == n) {
break;
}
if(p != 1) {
return p;
}
x = f(x);
y = f(f(y));
}
}
}
}
bool miller(const uint64_t n) {
if(n <= 1) {
return false;
}
if(n == 2) {
return true;
}
if(n % 2 == 0) {
return false;
}
uint64_t d = n - 1;
while(d % 2 == 0) {
d /= 2;
}
for(const uint a: {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) {
if(n <= a) {
break;
}
uint64_t t = d, y = internal::mod_pow(a, t, n);
while(t != n - 1 && y != 1 && y != n - 1) {
y = internal::mod_pow(y, 2, n);
t <<= 1;
}
if(y != n - 1 && t % 2 == 0) {
return false;
}
}
return true;
}
std::vector<uint64_t> rho(const uint64_t n) {
if(n == 1) {
return {};
}
const uint64_t x = internal::find(n);
if(x == n) {
return {x};
}
std::vector<uint64_t> le = rho(x);
const std::vector<uint64_t> ri = rho(n / x);
le.insert(le.end(), ri.begin(), ri.end());
return le;
}
/**
* @brief Pollard's Rho
* @docs docs/pollard_rho.md
*/
}
int main() {
std::cin.tie(nullptr) -> sync_with_stdio(false);
using namespace std::views;
int64_t x;
std::cin >> x;
if(x == 1) {
std::cout << "2\n1 2\nb g\n";
std::exit(0);
}
auto pf = man::rho(x);
while(std::ssize(pf) > 1) {
std::ranges::sort(pf, std::greater<>());
const auto l = pf.back();
pf.pop_back();
const auto r = pf.back();
if(l * r <= l + r + 1) {
pf.pop_back();
pf.emplace_back(l * r);
} else {
pf.emplace_back(l);
break;
}
}
dump(pf);
const int n = std::ssize(pf);
const auto ret = std::accumulate(pf.cbegin(), pf.cend(), 0L) + n;
if(ret > man::LIM) {
std::cout << "-1\n";
std::exit(0);
}
std::cout << ret << '\n';
int k = n;
for(const auto i: iota(0, n)) {
for([[maybe_unused]] const auto _: iota(0U, pf[i])) {
std::cout << i + 1 << ' ' << ++k << '\n';
}
}
for([[maybe_unused]] const auto _: iota(0, n)) {
std::cout << "b ";
}
for(const auto i: iota(0, ret - n)) {
std::cout << 'g' << " \n"[i + 1 == ret - n];
}
}