QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#662643#9254. Random VariablesKiharaToumaWA 2ms12540kbC++141.8kb2024-10-21 08:20:542024-10-21 08:20:54

Judging History

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

  • [2024-10-21 08:20:54]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:12540kb
  • [2024-10-21 08:20:54]
  • 提交

answer

//qoj9254
#include <bits/stdc++.h>
using namespace std;

const int N = 1e3 + 10;
typedef long long ll;
int T, n;
ll m, P, f[N][N], C[N][N], ans;

struct Mod{
    ll m, p;
    void init(int pp){
        m = ((__int128)1 << 64) / pp;
        p = pp;
    }
    ll operator ()(ll x){
        return x - ((__int128(x) * m) >> 64) * p;
    }
} mod;

ll qp(ll x, ll y){
    ll ans = 1;
    while(y){
        if(y & 1){
            ans = mod(ans * x);
        }
        x = mod(x * x);
        y >>= 1;
    }
    return ans;
}

int main(){
    scanf("%d%lld", &T, &P);
    n = 1000;
    mod.init(P);
    C[0][0] = 1;
    for(int i = 1; i <= n; ++ i){
        C[i][0] = 1;
        for(int j = 1; j <= i; ++ j){
            C[i][j] = C[i-1][j] + C[i-1][j-1];
            if(C[i][j] >= P){
                C[i][j] -= P;
            }
        }
    }
    while(T--){
        ans = 0;
        scanf("%d%lld", &n, &m);
        for(int k = 0; k < n; ++ k){
            f[0][0] = 1;
            for(int i = 1; i <= n; ++ i){
                f[i][i/(k+1)+1] = 0;
                for(int j = 0; j * (k + 1) <= i; ++ j){
                    f[i][j] = mod(f[i-1][j] * (m - j));
                    if(i >= k + 1 && j){
                        ll tmp = mod(f[i-k-1][j-1] * (m - j + 1));
                        tmp = mod(tmp * C[n-(i-k)][k]);
                        f[i][j] -= tmp;
                        if(f[i][j] < 0){
                            f[i][j] += P;
                        }
                    }
                }
            }
            for(int j = 0; j * (k + 1) <= n; ++ j){
                ans += f[n][j];
                f[n][j] = 0;
                if(ans >= P){
                    ans -= P;
                }
            }
        }
        printf("%lld\n", mod(- ans + P + n * qp(m, n)));
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 12460kb

input:

3 123456789
3 2
5 5
7 7

output:

18
7145
2066323

result:

ok 3 lines

Test #2:

score: -100
Wrong Answer
time: 2ms
memory: 12540kb

input:

100 2
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
2 1
2 2
2 3
2 4
2 5
2 6
2 7
2 8
2 9
2 10
3 1
3 2
3 3
3 4
3 5
3 6
3 7
3 8
3 9
3 10
4 1
4 2
4 3
4 4
4 5
4 6
4 7
4 8
4 9
4 10
5 1
5 2
5 3
5 4
5 5
5 6
5 7
5 8
5 9
5 10
6 1
6 2
6 3
6 4
6 5
6 6
6 7
6 8
6 9
6 10
7 1
7 2
7 3
7 4
7 5
7 6
7 7
7 8
7 9
7 10
8 1
8 2...

output:

19
20
35
36
51
52
67
68
83
84
44
64
96
132
136
196
176
260
216
324
127
188
627
1176
2831
4468
8303
11604
18575
24116
296
568
3620
12340
27404
57924
90876
160340
213232
342632
1011
1888
40319
103112
340167
625532
1456467
2412332
4743963
7314396
2684
6792
229100
793816
2447836
7568036
16875600
4261938...

result:

wrong answer 1st lines differ - expected: '1', found: '19'