QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#92202#6132. Repair the ArtworkksunhokimTL 179ms6628kbC++172.7kb2023-03-30 14:14:522023-03-30 14:15:04

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-30 14:15:04]
  • 评测
  • 测评结果:TL
  • 用时:179ms
  • 内存:6628kb
  • [2023-03-30 14:14:52]
  • 提交

answer

#include <bits/stdc++.h>
#include <vector>
using namespace std;
using ll = long long;

#define dbg(x) "(" << #x <<": " << (x) << ") "
template<typename Ostream, typename Cont>
enable_if_t<is_same_v<Ostream,ostream>, Ostream&> 
operator<<(Ostream& os, const Cont& v){
	os<<"[";
	for(auto& x:v){os<<x<<", ";}
	return os<<"]";
}
template<typename ...Ts>
ostream& operator<<(ostream& os, const pair<Ts...>& p){
	return os<<"{"<<p.first<<", "<<p.second<<"}";
}


// Modular Integer
// does arithmetics (mod P) automatically
int MOD = 1000000007; // 998244353
struct mint {
  int v=0; 
  int val() { return v; }
  mint() = default;
  mint(ll v) : v(v % MOD) { v += (v < 0) * MOD; }
};
mint& operator+=(mint& a, mint b) { if ((a.v += b.v) >= MOD) a.v -= MOD; return a; }
mint& operator-=(mint& a, mint b) { if ((a.v -= b.v) < 0) a.v += MOD; return a; }
mint operator+(mint a, mint b) { return a += b; }
mint operator-(mint a, mint b) { return a -= b; }
mint operator*(mint a, mint b) { return mint((ll) a.v * b.v); }
mint& operator*=(mint& a, mint b) { return a = a * b; }
mint pow(mint a, ll p) { return p == 0 ? 1 : pow(a * a, p / 2) * (p & 1 ? a : 1); }
mint inv(mint a) { return pow(a, MOD - 2); }
mint operator/(mint a, mint b) { return a * inv(b); }

ostream& operator<<(ostream& os, mint p){
	return os<<p.val();
}

void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> A(n);
  for (int i=0;i<n;i++)
    cin >> A[i];
  const int MK = 5050;
  int K = n*(n+1)/2;
  vector dp(n+1, array<array<mint,2>, MK>{});
  dp[0][0][0] = 1;
  int max_k = 0;
  for (int i=0;i<n;i++){
    vector next(n+1, array<array<mint,2>, MK>{});
    for (int last=0;last<=i;last++){
      for (int k=0;k<=max_k;k++){
        for (int z=0;z<2;z++){
          if (A[i] == 2) {
            if (k+(i-last+1) <= K) {
              max_k = max(max_k, k+(i-last+1));
              next[last][k+(i-last+1)][z] += dp[last][k][z];
            }
            next[i+1][k][z^1] += dp[last][k][z];
          } else if (A[i] == 1) {
            next[i+1][k][z] += dp[last][k][z];
          } else {
            if (k+(i-last+1) <= K) {
              max_k = max(max_k, k+(i-last+1));
              next[last][k+(i-last+1)][z] += dp[last][k][z];
            }
          }
        }
      }
    }
    dp = move(next);
  }

  mint ans = 0;
  for (int i=0;i<=n;i++){
    for (int j=0;j<=K;j++){
      for (int z=0;z<2;z++){
        mint nd = pow(mint(j),m) * dp[i][j][z];
        if (z == 0)
          ans += nd;
        else
          ans -= nd;
      }
    }
  }
  cout << ans.v << "\n";
}

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);

  int t;
  cin >> t;
  while (t--)
    solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 3672kb

input:

3
2 2
2 0
3 2
2 1 0
3 1
2 1 0

output:

8
3
1

result:

ok 3 number(s): "8 3 1"

Test #2:

score: 0
Accepted
time: 179ms
memory: 6628kb

input:

100
2 1
0 1
2 1
2 1
2 1
1 1
1 6
2
1 14
2
3 12
2 2 2
6 13
2 2 0 2 0 2
7 14
0 0 0 0 2 2 0
5 8
2 2 0 0 0
5 5
2 2 0 0 0
12 3
0 2 0 2 2 0 1 2 2 2 2 0
7 11
2 2 0 1 0 1 0
4 4
2 1 2 2
7 5
1 1 0 0 1 0 0
2 14
2 1
15 17
2 2 1 2 0 0 0 0 2 0 1 0 0 0 0
15 11
1 1 2 0 1 2 0 0 1 0 2 1 1 1 1
15 18
1 0 1 0 2 2 1 2 0 1...

output:

1
1
0
1
1
175715347
833406719
467966815
458805426
650344
2208
537089254
146
7776
1
703335050
123067364
355668256
487954758
53774922
544070885
436748805
369291507
760487845
513270785
501075264
487417783
464534262
979007529
137956839
143317512
648268264
851188473
702545117
946416372
595191705
35054850...

result:

ok 100 numbers

Test #3:

score: -100
Time Limit Exceeded

input:

1000
20 673037423
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
3 774964932
2 2 2
17 730319736
2 2 1 1 2 2 2 2 2 2 2 2 2 1 2 2 1
11 893285699
2 2 2 1 2 1 2 2 2 1 2
16 98149251
1 2 1 2 1 2 1 1 2 1 2 2 2 2 1 2
7 556953277
1 2 2 1 2 2 2
3 228111342
1 1 1
11 640995044
2 2 1 1 2 2 1 1 1 1 1
19 741419324
1 1 2 ...

output:


result: