QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#297625#5826. 错排chenxinyang200611 280ms19952kbC++144.9kb2024-01-04 20:53:372024-01-04 20:53:37

Judging History

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

  • [2024-01-04 20:53:37]
  • 评测
  • 测评结果:11
  • 用时:280ms
  • 内存:19952kb
  • [2024-01-04 20:53:37]
  • 提交

answer

#include <bits/stdc++.h>
#define rep(i,j,k) for(int i=(j);i<=(k);i++)
#define per(i,j,k) for(int i=(j);i>=(k);i--)
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define db double
#define ldb long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mkp make_pair
#define eb emplace_back
#define SZ(S) (int)S.size()
#define mod 998244353
//#define mod 1000000007
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3f
using namespace std;

template <class T>
void chkmax(T &x,T y){
    if(x < y) x = y;
}

template <class T>
void chkmin(T &x,T y){
    if(x > y) x = y;
}

inline int popcnt(int x){
    return __builtin_popcount(x);
}

inline int ctz(int x){
    return __builtin_ctz(x);
}

template <int P>
class mod_int
{
    using Z = mod_int;

private:
    static int mo(int x) { return x < 0 ? x + P : x; }

public:
    int x;
    int val() const { return x; }
    mod_int() : x(0) {}
    template <class T>
    mod_int(const T &x_) : x(x_ >= 0 && x_ < P ? static_cast<int>(x_) : mo(static_cast<int>(x_ % P))) {}
    bool operator==(const Z &rhs) const { return x == rhs.x; }
    bool operator!=(const Z &rhs) const { return x != rhs.x; }
    Z operator-() const { return Z(x ? P - x : 0); }
    Z pow(long long k) const
    {
        Z res = 1, t = *this;
        while (k)
        {
            if (k & 1)
                res *= t;
            if (k >>= 1)
                t *= t;
        }
        return res;
    }
    Z &operator++()
    {
        x < P - 1 ? ++x : x = 0;
        return *this;
    }
    Z &operator--()
    {
        x ? --x : x = P - 1;
        return *this;
    }
    Z operator++(int)
    {
        Z ret = x;
        x < P - 1 ? ++x : x = 0;
        return ret;
    }
    Z operator--(int)
    {
        Z ret = x;
        x ? --x : x = P - 1;
        return ret;
    }
    Z inv() const { return pow(P - 2); }
    Z &operator+=(const Z &rhs)
    {
        (x += rhs.x) >= P && (x -= P);
        return *this;
    }
    Z &operator-=(const Z &rhs)
    {
        (x -= rhs.x) < 0 && (x += P);
        return *this;
    }
    Z operator-()
    {
        return -x;
    }
    Z &operator*=(const Z &rhs)
    {
        x = 1ULL * x * rhs.x % P;
        return *this;
    }
    Z &operator/=(const Z &rhs) { return *this *= rhs.inv(); }
#define setO(T, o)                                  \
    friend T operator o(const Z &lhs, const Z &rhs) \
    {                                               \
        Z res = lhs;                                \
        return res o## = rhs;                       \
    }
    setO(Z, +) setO(Z, -) setO(Z, *) setO(Z, /)
#undef setO
    
    friend istream& operator>>(istream& is, mod_int& x)
    {
        long long tmp;
        is >> tmp;
        x = tmp;
        return is;
    }
    friend ostream& operator<<(ostream& os, const mod_int& x)
    {
        os << x.val();
        return os;
    }
};

using Z = mod_int<mod>;
Z power(Z p,ll k){
    Z ans = 1;
    while(k){
        if(k % 2 == 1) ans *= p;
        p *= p;
        k /= 2;
    }
    return ans;
}
Z fact[1000005],ifac[1000005],inv[1000005];

Z C(int N,int M){
    if(N < M || M < 0) return 0;
    return fact[N] * ifac[M] * ifac[N - M];
}

void init(int L){
    fact[0] = 1;
    rep(i,1,L) fact[i] = fact[i - 1] * i;
    ifac[L] = 1 / fact[L];
    per(i,L - 1,0) ifac[i] = ifac[i + 1] * (i + 1);
    rep(i,1,L) inv[i] = ifac[i] * fact[i - 1];
}
int T;
Z result[200005],D[200005];

struct qry{
    int p,q,id;
};
const int B = 450,V = 200000;
inline int from(int x){
    return (x + B - 1) / B;
}
inline int L(int x){
    return x * B - B + 1;
}
vector <qry> Q[4005];
bool cmp(qry _a,qry _b){
    return _a.q < _b.q;
}

int n,m;
Z x,y;
void addn(){
    x = C(m,n + 2) + (n + 1) * (x + y) - m * x;
    swap(x,y);
    n++;
}

void subn(){
    n--;
    y = (y - C(m,n + 2) + (m - n - 1) * x) * inv[n + 1];
    swap(x,y); 
}

void addm(){
    Z temp = (y - m * x - C(m,n + 1)) * inv[n - m];
    y = x + y;
    x = temp;
    m++;
}

int main(){
//    freopen("test.in","r",stdin);
    scanf("%d",&T);
    init(V);
    D[0] = 1;D[1] = 0;
    rep(i,2,V) D[i] = (i - 1) * (D[i - 1] + D[i - 2]);
    rep(i,1,T){
        int p,q;
        scanf("%d%d",&p,&q);
        p -= q;
        if(p < q) continue;
        Q[from(p)].push_back({p,q,i});
//        printf("%d %d\n",p,q);
    }
    rep(p,1,from(V)){
        n = L(p);m = 0;
        x = D[n];y = D[n + 1];
        sort(Q[p].begin(),Q[p].end(),cmp);

        for(qry cur:Q[p]){
            while(n < cur.p) addn();
            while(n > cur.p) subn();
            while(m < cur.q) addm();
//            printf("%d %d %d %d\n",n,m,x.val(),y.val());
            result[cur.id] = x * fact[cur.p] * ifac[cur.p - cur.q];
        }
    }
    rep(i,1,T) printf("%d\n",result[i].val());
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 1
Accepted

Test #1:

score: 1
Accepted
time: 3ms
memory: 17092kb

input:

0

output:


result:

ok 0 number(s): ""

Subtask #2:

score: 0
Wrong Answer

Test #2:

score: 0
Wrong Answer
time: 3ms
memory: 17132kb

input:

10
8 6
5 1
4 2
6 3
8 1
3 1
6 2
3 1
4 1
6 2

output:

0
44
886271462
576764122
14833
348593655
858799687
348593655
520512566
858799687

result:

wrong answer 3rd numbers differ - expected: '4', found: '886271462'

Subtask #3:

score: 10
Accepted

Test #7:

score: 10
Accepted
time: 43ms
memory: 19876kb

input:

200000
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
11 0
12 0
13 0
14 0
15 0
16 0
17 0
18 0
19 0
20 0
21 0
22 0
23 0
24 0
25 0
26 0
27 0
28 0
29 0
30 0
31 0
32 0
33 0
34 0
35 0
36 0
37 0
38 0
39 0
40 0
41 0
42 0
43 0
44 0
45 0
46 0
47 0
48 0
49 0
50 0
51 0
52 0
53 0
54 0
55 0
56 0
57 0
58 0
59 0
60 0
61...

output:

0
1
2
9
44
265
1854
14833
133496
1334961
14684570
176214841
294304226
127281753
910981941
600290115
222488424
11814221
224470198
496426549
442513998
751108780
305347938
340640042
530046225
804025262
745550660
910531421
451058030
554564312
221339670
95158970
145512950
954462889
464137465
737039093
31...

result:

ok 200000 numbers

Subtask #4:

score: 0
Wrong Answer

Test #8:

score: 0
Wrong Answer
time: 280ms
memory: 19952kb

input:

200000
4303 1473
1276 72
967 234
3619 984
1316 384
2679 50
4426 1744
3782 1179
4919 4
805 63
3933 158
1574 528
1277 435
3826 915
2739 68
2286 349
3017 527
3036 476
4280 1764
1504 686
4584 917
1379 145
4764 2178
1881 45
4808 1565
3663 165
4730 2209
2258 103
4181 1687
1636 770
4339 1173
2355 777
3201 ...

output:

281204980
593809836
435336225
426051540
722003152
760387370
155806215
190499165
269454160
328353089
529411791
529154639
413680183
982810397
506575116
71517317
177071061
859815955
187993036
286216531
209826009
607877580
655579669
343443251
598343852
91458625
508090668
931149237
48406924
561882086
944...

result:

wrong answer 1st numbers differ - expected: '855518783', found: '281204980'

Subtask #5:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%

Subtask #6:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%