QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#695078#5. 在线 O(1) 逆元zjy0001100 ✓2546ms28468kbC++175.5kb2024-10-31 19:17:082024-11-05 22:07:35

Judging History

你现在查看的是最新测评结果

  • [2024-11-05 22:07:35]
  • 管理员手动重测本题所有提交记录
  • 测评结果:100
  • 用时:2546ms
  • 内存:28468kb
  • [2024-10-31 19:17:14]
  • 评测
  • 测评结果:100
  • 用时:2141ms
  • 内存:28468kb
  • [2024-10-31 19:17:08]
  • 提交

answer

#include<bits/stdc++.h>
#define LL long long
#define LLL __int128
#define uint unsigned
#define ldb long double
#define uLL unsigned long long
using namespace std;
#define QUICKINV
template < class S, S P_ > class ModInt
{
	static_assert(is_same < S, int > :: value || is_same < S, long long > :: value );
	static_assert(P_ & 1 && 0 < P_ && P_ < ( (S)1 << ( sizeof(S) * 8 - 2 ) ));
	using U = conditional_t < is_same < S, int > :: value , unsigned, unsigned long long >; using D = conditional_t < is_same < S, int > :: value, unsigned long long, __uint128_t >;
	inline constexpr static U uinv(U x) { U y = x; for ( int i = is_same < S, int > :: value ? 4 : 5 ; i-- ; ) y *= 2 - x * y; return y; }
	constexpr static U P = P_, P2 = P << 1, R = -uinv(P), R2 = -(D)P % P; static_assert(P * R == -1);
	inline constexpr static U reduce(D x) { return ( x + (U)x * R * (D)P ) >> ( sizeof(U) * 8 ); }
	inline constexpr ModInt(U x, int) : v(x) {} U v;
public:
	inline constexpr static S mod() { return P; }
	inline constexpr ModInt() : v(0) {}
	inline constexpr ModInt(const ModInt &x) : v(x.v) {}
	template < class T, enable_if_t < numeric_limits < T >::is_integer, int > = 0 > inline constexpr ModInt(T x) { if constexpr ( numeric_limits < T >::is_signed ) if ( __builtin_expect(x < 0, false) ) { __builtin_expect(x + P < 0, false) && ( x %= P ), v = reduce((D)R2 * (U)( x + P )); return; } if constexpr ( sizeof(T) > sizeof(U) ) __builtin_expect(x >= (T)1 << sizeof(U), false) && ( x %= P ); v = reduce((D)R2 * (U)x); }
	inline constexpr S val()const { U x = reduce(v); return ( x - P ) >> ( sizeof(U) * 8 - 1 ) ? x : x - P; }
	template < class T, enable_if_t < numeric_limits < T >::is_integer, int > = 0 > explicit inline constexpr operator T()const { return val(); }
	inline constexpr friend bool operator==(const ModInt &x, const ModInt &y) { return x.val() == y.val(); }
	inline constexpr friend bool operator!=(const ModInt &x, const ModInt &y) { return x.val() != y.val(); }
	inline constexpr ModInt &operator=(const ModInt &x) & { v = x.v; return *this; }
	inline constexpr ModInt &operator++() & { v == P2 - 1 ? v = 0 : v++; return *this; }
	inline constexpr ModInt operator++(int) & { ModInt x = *this; v == P2 - 1 ? v = 0 : v++; return x; }
	inline constexpr ModInt &operator--() & { v ? v-- : v = P2 - 1; return *this; }
	inline constexpr ModInt operator--(int) & { ModInt x = *this; v ? v-- : v = P2 - 1; return x; }
	inline constexpr ModInt operator-()const { return ModInt(v ? P2 - v : 0, 0); }
	inline constexpr ModInt &operator+=(const ModInt &x) & { v += x.v, ( v - P2 ) >> ( sizeof(U) * 8 - 1 ) || ( v -= P2 ); return *this; }
	inline constexpr ModInt &operator-=(const ModInt &x) & { v -= x.v, v >> ( sizeof(U) * 8 - 1 ) && ( v += P2 ); return *this; }
	inline constexpr ModInt &operator*=(const ModInt &x) & { v = reduce((D)v * x.v); return *this; }
	inline constexpr ModInt &operator/=(const ModInt &x) & { return *this *= x.inv(); }
	inline constexpr friend ModInt operator+(ModInt x, const ModInt &y) { return x += y; }
	inline constexpr friend ModInt operator-(ModInt x, const ModInt &y) { return x -= y; }
	inline constexpr friend ModInt operator*(ModInt x, const ModInt &y) { return x *= y; }
	inline constexpr friend ModInt operator/(ModInt x, const ModInt &y) { return x /= y; }
	template < class T, enable_if_t < numeric_limits < T >::is_integer, int > = 0 > inline constexpr ModInt qpow(T y)const { ModInt x = *this, z = 1; while ( y ) { if ( y & 1 ) z *= x; if ( y >>= 1 ) x *= x; } return z; }
	template < class T, enable_if_t < numeric_limits < T >::is_integer, int > = 0 > inline constexpr friend ModInt qpow(const ModInt &x, T y) { return x.qpow(y); }
#ifdef QUICKINV
	static_assert(is_same < S, int > :: value);
private:
	struct inv_helper_T{
		inline static constexpr int K = (__lg(P) + 2) / 3;
		inline static constexpr S B = ((S)1) << K, T = B << K;
		S f[T + 1], g[T + 1];U buf[T * 3 + 1], *I = buf + T, h[T + 1];
		inline constexpr inv_helper_T(){
			fill_n(f + 1, T, 0);
			for (S i = 1; i <= B ; i++ ){
				S s = 0, d = (i << K);
				for(U j = 1; j <= T ; j++ ){
					if ((s += d) >= mod()) s -= mod();
					if (s <= T) {
						if (!f[j]) f[j] = i, g[j] = s;
					}
					else if (s >= mod() - T) {
						if (!f[j]) f[j] = i, g[j] = s - mod();
					}
					else{
						const U t = (mod() - T - s - 1) / d;
						s += t * d, j += t;
					}
				}
			}
			f[0] = 1, I[0] = 0, I[1] = reduce(R2);
			for (S i = 2; i <= T + T; i++ ) I[i] = reduce((D)reduce((D)R2 * (mod() / i)) * ( P2 - I[mod() % i] ));
			for (S i = 1; i <= T; i++ ) buf[T - i] = P2 - I[i];
			for (S i = 0; i <= T; i++ ) h[i] = reduce((D)R2 * f[i]);
		}
		inline ModInt inv(const S&x) const{
			return ModInt(reduce((D)I[g[x >> K] + x % B * f[x >> K]] * h[x >> K]), 0);
		}
	};
	inline static inv_helper_T inv_helper = inv_helper_T();
public:
	inline constexpr ModInt inv() { return inv_helper.inv(val()); }
	inline constexpr friend ModInt inv(ModInt x) { return x.inv(); }
#else
	inline constexpr ModInt inv()const { return qpow(P - 2); }
	inline constexpr friend ModInt inv(const ModInt &x) { return x.inv(); }
#endif
	inline friend istream &operator>>(istream &is, ModInt &x) { S y; is >> y, x = y; return is; }
	inline friend ostream &operator<<(ostream &os, const ModInt &x) { return os << x.val(); }
#ifdef USE_FastIO
	inline void read() & { S x; ::read(x), *this = x; }
	inline void write()const { ::write(val()); }
#endif
};
using MI = ModInt<int, 998244353>;
void init(int p){}

int inv(int x){
	return MI(x).inv().val();
}

Details


Pretests


Final Tests

Test #1:

score: 10
Accepted
time: 24ms
memory: 28468kb

Test #2:

score: 20
Accepted
time: 253ms
memory: 28416kb

Test #3:

score: 30
Accepted
time: 1291ms
memory: 28468kb

Test #4:

score: 20
Accepted
time: 2053ms
memory: 28424kb

Test #5:

score: 20
Accepted
time: 2546ms
memory: 28356kb