結果
問題 | No.3133 法B逆元 |
ユーザー |
|
提出日時 | 2025-05-02 21:26:32 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 11,134 bytes |
コンパイル時間 | 2,919 ms |
コンパイル使用メモリ | 278,284 KB |
実行使用メモリ | 6,272 KB |
最終ジャッジ日時 | 2025-05-02 21:26:40 |
合計ジャッジ時間 | 3,570 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 21 |
ソースコード
// #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 debug_bin(...) 42 #endif template<int id, class data_t, class wider_data_t> struct modular_unfixed_base{ #ifdef LOCAL #define ASSERT(x) assert(x) #else #define ASSERT(x) 42 #endif #define IS_INTEGRAL(T) (is_integral_v<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>) #define IS_UNSIGNED(T) (is_unsigned_v<T> || is_same_v<T, __uint128_t>) static_assert(IS_UNSIGNED(data_t) && IS_UNSIGNED(wider_data_t) && sizeof(data_t) * 2 == sizeof(wider_data_t)); static constexpr bool VARIATE_MOD_FLAG = true; static data_t _mod; static wider_data_t _inverse_mod; static data_t &mod(){ return _mod; } static void precalc_barrett(){ _inverse_mod = (wider_data_t)-1 / _mod + 1; } static void setup(data_t mod = 0){ _primitive_root._data = 0; _INV.clear(); if(!mod) cin >> mod; _mod = mod; ASSERT(1 <= _mod && _mod < data_t(1) << 8 * sizeof(data_t) - 1); precalc_barrett(); } template<class T> static vector<modular_unfixed_base> precalc_power(T base, int SZ){ vector<modular_unfixed_base> res(SZ + 1, 1); for(auto i = 1; i <= SZ; ++ i) res[i] = res[i - 1] * base; return res; } template<class T> static vector<modular_unfixed_base> precalc_geometric_sum(T base, int SZ){ vector<modular_unfixed_base> res(SZ + 1); for(auto i = 1; i <= SZ; ++ i) res[i] = res[i - 1] * base + base; return res; } static vector<modular_unfixed_base> _INV; static void precalc_inverse(int SZ){ if(_INV.empty()) _INV.assign(2, 1); for(auto x = _INV.size(); x <= SZ; ++ x) _INV.push_back(_mod / x * -_INV[_mod % x]); } // _mod must be a prime static modular_unfixed_base _primitive_root; static modular_unfixed_base primitive_root(){ if(_primitive_root) return _primitive_root; if(_mod == 2) return _primitive_root = 1; if(_mod == 998244353) return _primitive_root = 3; data_t divs[20] = {}; divs[0] = 2; int cnt = 1; data_t x = (_mod - 1) / 2; while(x % 2 == 0) x /= 2; for(auto i = 3; 1LL * i * i <= x; i += 2){ if(x % i == 0){ divs[cnt ++] = i; while(x % i == 0) x /= i; } } if(x > 1) divs[cnt ++] = x; for(auto g = 2; ; ++ g){ bool ok = true; for(auto i = 0; i < cnt; ++ i){ if(modular_unfixed_base(g).power((_mod - 1) / divs[i]) == 1){ ok = false; break; } } if(ok) return _primitive_root = g; } } modular_unfixed_base(){ } modular_unfixed_base(const double &x){ _data = _normalize(llround(x)); } modular_unfixed_base(const long double &x){ _data = _normalize(llround(x)); } template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> modular_unfixed_base(const T &x){ _data = _normalize(x); } template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> static data_t _normalize(const T &x){ if(_mod == 1) return 0; if constexpr(is_same_v<data_t, unsigned int>){ ASSERT(_inverse_mod); int sign = x >= 0 ? 1 : -1; data_t v = _mod <= sign * x ? sign * x - ((__uint128_t)(sign * x) * _inverse_mod >> 64) * _mod : sign * x; if(v >= _mod) v += _mod; if(sign == -1 && v) v = _mod - v; return v; } else{ int sign = x >= 0 ? 1 : -1; data_t v = _mod <= sign * x ? sign * x % _mod : sign * x; if(sign == -1 && v) v = _mod - v; return v; } } const data_t &operator()() const{ return _data; } template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> operator T() const{ return data(); } modular_unfixed_base &operator+=(const modular_unfixed_base &otr){ if((_data += otr._data) >= _mod) _data -= _mod; return *this; } modular_unfixed_base &operator-=(const modular_unfixed_base &otr){ if((_data += _mod - otr._data) >= _mod) _data -= _mod; return *this; } template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> modular_unfixed_base &operator+=(const T &otr){ return *this += modular_unfixed_base(otr); } template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> modular_unfixed_base &operator-=(const T &otr){ return *this -= modular_unfixed_base(otr); } modular_unfixed_base &operator++(){ return *this += 1; } modular_unfixed_base &operator--(){ return *this += _mod - 1; } modular_unfixed_base operator++(int){ modular_unfixed_base result(*this); *this += 1; return result; } modular_unfixed_base operator--(int){ modular_unfixed_base result(*this); *this += _mod - 1; return result; } modular_unfixed_base operator-() const{ return modular_unfixed_base(_mod - _data); } modular_unfixed_base &operator*=(const modular_unfixed_base &rhs){ if constexpr(is_same_v<data_t, unsigned long long>){ long long res = _data * rhs._data - _mod * (unsigned long long)(1.L / _mod * _data * rhs._data); _data = res + _mod * (res < 0) - _mod * (res >= (long long)_mod); } else _data = _normalize((wider_data_t)_data * rhs._data); return *this; } template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> modular_unfixed_base &inplace_power(T e){ if(e == 0) return *this = 1; if(_data == 0) return *this = {}; if(_data == 1 || e == 1) return *this; if(_data == mod() - 1) return e % 2 ? *this : *this = -*this; if(e < 0) *this = 1 / *this, e = -e; if(e == 1) return *this; modular_unfixed_base res = 1; for(; e; *this *= *this, e >>= 1) if(e & 1) res *= *this; return *this = res; } template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> modular_unfixed_base power(T e) const{ return modular_unfixed_base(*this).inplace_power(e); } // c + c * x + ... + c * x^{e-1} template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> modular_unfixed_base &inplace_geometric_sum(T e, modular_unfixed_base c = 1){ if(e == 0) return *this = {}; if(_data == 0) return *this = {}; if(_data == 1) return *this = c * e; if(e == 1) return *this = c; if(_data == mod() - 1) return *this = c * abs(e % 2); modular_unfixed_base res = 0; if(e < 0) return *this = geometric_sum(-e + 1, -*this) - 1; if(e == 1) return *this = c * *this; for(; e; c *= 1 + *this, *this *= *this, e >>= 1) if(e & 1) res += c, c *= *this; return *this = res; } // c + c * x + ... + c * x^{e-1} template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> modular_unfixed_base geometric_sum(T e, modular_unfixed_base c = 1) const{ return modular_unfixed_base(*this).inplace_geometric_sum(e, c); } // Returns the minimum integer e>0 with b^e=*this, if it exists // O(sqrt(mod)) applications of hashmap optional<data_t> log(const modular_unfixed_base &b) const{ data_t m = mod(), n = sqrtl(m) + 1, j = 1; modular_unfixed_base e = 1, f = 1; unordered_map<data_t, data_t> A; while(j <= n && (f = e *= b) != *this) A[(e * *this).data()] = j ++; if(e == *this) return j; if(gcd(mod(), e.data()) == gcd(mod(), data())) for(auto i = 2; i < n + 2; ++ i) if(A.count((e *= f).data())) return n * i - A[e.data()]; return {}; } optional<modular_unfixed_base> inverse() const{ make_signed_t<data_t> a = data(), m = _mod, u = 0, v = 1; if(data() < _INV.size()) return _INV[data()]; while(a){ make_signed_t<data_t> t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } if(m != 1) return {}; return modular_unfixed_base{u}; } modular_unfixed_base &operator/=(const modular_unfixed_base &otr){ auto inv_ptr = otr.inverse(); assert(inv_ptr); return *this = *this * *inv_ptr; } #define ARITHMETIC_OP(op, apply_op)\ modular_unfixed_base operator op(const modular_unfixed_base &x) const{ return modular_unfixed_base(*this) apply_op x; }\ template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>\ modular_unfixed_base operator op(const T &x) const{ return modular_unfixed_base(*this) apply_op modular_unfixed_base(x); }\ template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>\ friend modular_unfixed_base operator op(const T &x, const modular_unfixed_base &y){ return modular_unfixed_base(x) apply_op y; } ARITHMETIC_OP(+, +=) ARITHMETIC_OP(-, -=) ARITHMETIC_OP(*, *=) ARITHMETIC_OP(/, /=) #undef ARITHMETIC_OP #define COMPARE_OP(op)\ bool operator op(const modular_unfixed_base &x) const{ return _data op x._data; }\ template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>\ bool operator op(const T &x) const{ return _data op modular_unfixed_base(x)._data; }\ template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>\ friend bool operator op(const T &x, const modular_unfixed_base &y){ return modular_unfixed_base(x)._data op y._data; } COMPARE_OP(==) COMPARE_OP(!=) COMPARE_OP(<) COMPARE_OP(<=) COMPARE_OP(>) COMPARE_OP(>=) #undef COMPARE_OP friend istream &operator>>(istream &in, modular_unfixed_base &number){ long long x; in >> x; number._data = modular_unfixed_base::_normalize(x); return in; } friend ostream &operator<<(ostream &out, const modular_unfixed_base &number){ out << number._data; #ifdef LOCAL cerr << "("; for(auto d = 1; ; ++ d){ if((number * d)._data <= 1000000){ cerr << (number * d)._data; if(d != 1) cerr << "/" << d; break; } else if((-number * d)._data <= 1000000){ cerr << "-" << (-number * d)._data; if(d != 1) cerr << "/" << d; break; } } cerr << ")"; #endif return out; } data_t _data = 0; data_t data() const{ return _data; } #undef ASSERT #undef IS_INTEGRAL #undef IS_UNSIGNED }; template<int id, class data_t, class wider_data_t> data_t modular_unfixed_base<id, data_t, wider_data_t>::_mod; template<int id, class data_t, class wider_data_t> wider_data_t modular_unfixed_base<id, data_t, wider_data_t>::_inverse_mod; template<int id, class data_t, class wider_data_t> vector<modular_unfixed_base<id, data_t, wider_data_t>> modular_unfixed_base<id, data_t, wider_data_t>::_INV; template<int id, class data_t, class wider_data_t> modular_unfixed_base<id, data_t, wider_data_t> modular_unfixed_base<id, data_t, wider_data_t>::_primitive_root; using modular = modular_unfixed_base<0, unsigned int, unsigned long long>; // using modular = modular_unfixed_base<0, unsigned long long, __uint128_t>; modular operator""_m(const char *x){ modular res = 0; long long buffer = 0; long long buffer_width = 1; constexpr long long buffer_th = 1'000'000'000'000'000'000LL; while(*x){ #ifdef LOCAL assert(isdigit(*x)); #endif buffer = buffer * 10 + (*(x ++) - '0'); if((buffer_width *= 10) == buffer_th){ res = buffer_width * res + buffer; buffer = 0; buffer_width = 1; } } res = buffer_width * res + buffer; return res; } int main(){ cin.tie(0)->sync_with_stdio(0); cin.exceptions(ios::badbit | ios::failbit); unsigned long long n; cin >> n; modular::setup(); if(modular::mod() == 1){ cout << "0\n"; } else if(auto inv_ptr = modular{n}.inverse()){ cout << *inv_ptr << "\n"; } else{ cout << "NaN\n"; } return 0; } /* */