結果
| 問題 |
No.2821 A[i] ← 2A[j] - A[i]
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-07-26 22:20:22 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,162 bytes |
| コンパイル時間 | 3,607 ms |
| コンパイル使用メモリ | 271,524 KB |
| 実行使用メモリ | 10,752 KB |
| 最終ジャッジ日時 | 2024-07-26 22:20:39 |
| 合計ジャッジ時間 | 7,061 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | TLE * 1 -- * 32 |
ソースコード
// #include <bits/allocator.h> // Temp fix for gcc13 global pragma
// #pragma GCC target("avx2,bmi2,popcnt,lzcnt")
// #pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
// #include <x86intrin.h>
using namespace std;
#if __cplusplus >= 202002L
using namespace numbers;
#endif
#ifdef LOCAL
#include "Debug.h"
#else
#define debug_endl() 42
#define debug(...) 42
#define debug2(...) 42
#define debugbin(...) 42
#endif
// Correctness proved in https://github.com/kth-competitive-programming/kactl/blob/master/doc/modmul-proof.pdf
// twice faster than (__int128_t)a * b % M
using ull = unsigned long long;
ull mod_mul(ull a, ull b, ull M){
long long res = a * b - M * ull(1.L / M * a * b);
return res + M * (res < 0) - M * (res >= (long long)M);
}
ull mod_pow(ull b, ull e, ull mod){
ull res = 1;
for(; e; b = mod_mul(b, b, mod), e >>= 1) if(e & 1) res = mod_mul(res, b, mod);
return res;
}
// Millar Rabin Primality Test
// 7 times slower than a^b mod c
bool isprime(ull n){
if(n < 2 || n % 6 % 4 != 1) return (n | 1) == 3;
ull s = __builtin_ctzll(n - 1), d = n >> s;
for(ull a: {2, 325, 9375, 28178, 450775, 9780504, 1795265022}){
ull p = mod_pow(a, d, n), i = s;
while(p != 1 && p != n - 1 && a % n && i --) p = mod_mul(p, p, n);
if(p != n - 1 && i != s) return false;
}
return true;
}
// Pollard rho algorithm
// O(n^1/4)
ull get_factor(ull n){
auto f = [n](ull x){ return mod_mul(x, x, n) + 1; };
ull x = 0, y = 0, t = 30, prd = 2, i = 1, q;
while(t ++ % 40 || gcd(prd, n) == 1){
if(x == y) x = ++ i, y = f(x);
if(q = mod_mul(prd, max(x, y) - min(x, y), n)) prd = q;
x = f(x), y = f(f(y));
}
return gcd(prd, n);
}
// Returns the prime factors in arbitrary order
vector<ull> factorize(ull n){
if(n == 1) return {};
if(isprime(n)) return {n};
ull x = get_factor(n);
auto l = factorize(x), r = factorize(n / x);
l.insert(l.end(), r.begin(), r.end());
return l;
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(ios::badbit | ios::failbit);
int n;
cin >> n;
auto get_pf = [&](long long x)->map<long long, long long>{
map<long long, long long> pf;
for(auto p: factorize(x)){
if(!pf.contains(p)){
pf[p] = p;
}
else{
pf[p] *= p;
}
}
return pf;
};
set<long long> s;
map<long long, multiset<long long>> pfs;
long long g = 0;
for(auto i = 0; i < n; ++ i){
long long x;
cin >> x;
if(!s.contains(x)){
if(s.empty()){
s.insert(x);
}
else if((int)s.size() == 1){
long long y = *s.begin();
g = abs(x - y);
s.insert(x);
}
else if((int)s.size() == 2){
s.insert(x);
g = 0;
for(auto it = s.begin(); next(it) != s.end(); ++ it){
long long dif = *next(it) - *it;
g = gcd(g, dif);
for(auto [p, pow]: get_pf(dif)){
pfs[p].insert(pow);
}
}
}
else{
auto it = s.lower_bound(x);
int expect = (int)s.size() - 1;
for(auto [p, pow]: get_pf(*it - *prev(it))){
if((int)pfs[p].size() == expect){
g /= *pfs[p].begin();
}
pfs[p].erase(pow);
if((int)pfs[p].size() == expect - 1){
g *= *pfs[p].begin();
}
}
}
}
cout << g << "\n";
}
return 0;
}
/*
*/