QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#142337#6736. Alice and BobLeticiaFCSAC ✓242ms81368kbC++203.3kb2023-08-18 23:36:192023-08-18 23:36:22

Judging History

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

  • [2023-08-18 23:36:22]
  • 评测
  • 测评结果:AC
  • 用时:242ms
  • 内存:81368kb
  • [2023-08-18 23:36:19]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

using ll = int64_t;

const ll MOD = 998244353;

int minv(int a, int m) {
	a %= m; //assert(a);
	return a == 1 ? 1 : int(m - int64_t(minv(m, a)) * m / a);
}

template<unsigned M_> struct modnum {
	static constexpr unsigned M = M_;
	using ll = int64_t; using ull = uint64_t; unsigned x;
	modnum& norm(unsigned a) { x = a < M ? a : a - M; return *this; }
	constexpr modnum(ll a = 0U) : x(unsigned((a %= ll(M)) < 0 ? a + ll(M) : a)) {}
	explicit operator int() const { return x; }
	modnum& operator+=(const modnum& a) { return norm(x + a.x); }
	modnum& operator-=(const modnum& a) { return norm(x - a.x + M); }
	modnum& operator*=(const modnum& a) { x = unsigned(ull(x) * a.x % M); return *this; }
	modnum& operator/=(const modnum& a) { return (*this *= a.inv()); }
	modnum operator+(const modnum& a) const { return (modnum(*this) += a); }
	modnum operator-(const modnum& a) const { return (modnum(*this) -= a); }
	modnum operator*(const modnum& a) const { return (modnum(*this) *= a); }
	modnum operator/(const modnum& a) const { return (modnum(*this) /= a); }
	template<typename T> friend modnum operator+(T a, const modnum& b) { return (modnum(a) += b); }
	template<typename T> friend modnum operator-(T a, const modnum& b) { return (modnum(a) -= b); }
	template<typename T> friend modnum operator*(T a, const modnum& b) { return (modnum(a) *= b); }
	template<typename T> friend modnum operator/(T a, const modnum& b) { return (modnum(a) /= b); }
	modnum operator+() const { return *this; }
	modnum operator-() const { return modnum() - *this; }
	modnum pow(ll e) const {
		if (e < 0) return inv().pow(-e);
		modnum b = x, xe = 1U;
		for (; e; e >>= 1) { if (e & 1) xe *= b; b *= b; }
		return xe;
	}
	modnum inv() const { return minv(x, M); }
	friend modnum inv(const modnum& a) { return a.inv(); }
	explicit operator bool() const { return x; }
	friend bool operator==(const modnum& a, const modnum& b) { return a.x == b.x; }
	friend bool operator!=(const modnum& a, const modnum& b) { return a.x != b.x; }
	friend ostream &operator<<(ostream& os, const modnum& a) { return os << a.x; }
	friend istream &operator>>(istream& in, modnum& n) { ll v_; in >> v_; n = modnum(v_); return in; }
};

using num = modnum<998244353>;


int minv_2(int a) {
    static const int m = MOD;
    
	return a <= 1 ? a : m - int64_t(minv_2(m % a)) * (m / a) % m;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n; cin>>n;

    if(n <= 2){
        cout<<"1\n";
        return 0;
    }
    vector<ll> inv(n+1);
    /*
        5, MOD
        MOD, 5

    */
   inv[1] = 1;
    for (int x = 2; x <= n; ++x) {
        inv[x] = -(MOD/x * inv[MOD % x])%MOD;
        inv[x] += MOD;
        inv[x] %= MOD;
        //cout<<x<<" "<<inv[x]<<" "<<minv(x, MOD)<<" "<<minv_2(x)<<"\n";
    }
    num fact = 1; // (n-1)!
    for(int i =1; i <= n - 2; i++)
        fact *= i;
    
    num ans = fact*(n-1);

    num big = n-2;
    ans = ans + big*fact;
    for(int i = 3; i < n; i++){
        int x = i + i - 1;
        big = big *inv[(n-(i-1))];
        big = big * (n-(x-1)) * (n - (x-2));
        fact = fact * inv[(n-(i-1))];
        ans = ans + big*fact;        
    }
    cout<<ans<<"\n";

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3532kb

input:

1

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3512kb

input:

2

output:

1

result:

ok 1 number(s): "1"

Test #3:

score: 0
Accepted
time: 1ms
memory: 3456kb

input:

10

output:

997920

result:

ok 1 number(s): "997920"

Test #4:

score: 0
Accepted
time: 1ms
memory: 3524kb

input:

100

output:

188898954

result:

ok 1 number(s): "188898954"

Test #5:

score: 0
Accepted
time: 1ms
memory: 3520kb

input:

4

output:

10

result:

ok 1 number(s): "10"

Test #6:

score: 0
Accepted
time: 1ms
memory: 3528kb

input:

8

output:

12336

result:

ok 1 number(s): "12336"

Test #7:

score: 0
Accepted
time: 1ms
memory: 3452kb

input:

16

output:

373118483

result:

ok 1 number(s): "373118483"

Test #8:

score: 0
Accepted
time: 1ms
memory: 3492kb

input:

32

output:

314585464

result:

ok 1 number(s): "314585464"

Test #9:

score: 0
Accepted
time: 1ms
memory: 3456kb

input:

64

output:

627827331

result:

ok 1 number(s): "627827331"

Test #10:

score: 0
Accepted
time: 1ms
memory: 3492kb

input:

128

output:

828497685

result:

ok 1 number(s): "828497685"

Test #11:

score: 0
Accepted
time: 1ms
memory: 3448kb

input:

256

output:

65697890

result:

ok 1 number(s): "65697890"

Test #12:

score: 0
Accepted
time: 1ms
memory: 3576kb

input:

512

output:

854187619

result:

ok 1 number(s): "854187619"

Test #13:

score: 0
Accepted
time: 1ms
memory: 3460kb

input:

1024

output:

513823539

result:

ok 1 number(s): "513823539"

Test #14:

score: 0
Accepted
time: 32ms
memory: 13876kb

input:

1361956

output:

617368199

result:

ok 1 number(s): "617368199"

Test #15:

score: 0
Accepted
time: 176ms
memory: 62440kb

input:

7579013

output:

827172636

result:

ok 1 number(s): "827172636"

Test #16:

score: 0
Accepted
time: 194ms
memory: 66696kb

input:

8145517

output:

710624331

result:

ok 1 number(s): "710624331"

Test #17:

score: 0
Accepted
time: 138ms
memory: 51192kb

input:

6140463

output:

707600568

result:

ok 1 number(s): "707600568"

Test #18:

score: 0
Accepted
time: 74ms
memory: 30684kb

input:

3515281

output:

698302413

result:

ok 1 number(s): "698302413"

Test #19:

score: 0
Accepted
time: 159ms
memory: 57552kb

input:

6969586

output:

69470392

result:

ok 1 number(s): "69470392"

Test #20:

score: 0
Accepted
time: 61ms
memory: 25600kb

input:

2888636

output:

433579983

result:

ok 1 number(s): "433579983"

Test #21:

score: 0
Accepted
time: 239ms
memory: 81368kb

input:

9999998

output:

758172780

result:

ok 1 number(s): "758172780"

Test #22:

score: 0
Accepted
time: 234ms
memory: 81292kb

input:

9999999

output:

605195495

result:

ok 1 number(s): "605195495"

Test #23:

score: 0
Accepted
time: 242ms
memory: 81256kb

input:

10000000

output:

866813682

result:

ok 1 number(s): "866813682"