QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#106833#6397. Master of Both IIIwhatever#TL 372ms36360kbC++141.8kb2023-05-19 14:24:062023-05-19 14:24:08

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-19 14:24:08]
  • 评测
  • 测评结果:TL
  • 用时:372ms
  • 内存:36360kb
  • [2023-05-19 14:24:06]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a, I = b; i <= I; ++i)
#define per(i, a, b) for (int i = a, I = b; i >= I; --i)
using i64 = long long;
using pii = pair<int, int>;
using piii = pair<pii, int>;
template<typename T> void up(T &x, T y) { if (x < y) x = y; }
template<typename T> void down(T &x, T y) { if (x > y) x = y; }

const int P = 998244353;
void add(int &x, int y) { (x += y) >= P && (x -= P); }
int Add(int x, int y) { return (x += y) >= P ? (x - P) : x; }
void sub(int &x, int y) { (x -= y) < 0 && (x += P); }
int Sub(int x, int y) { return (x -= y) < 0 ? (x + P) : x; }
void mul(int &x, int y) { x = 1ll * x * y % P; }
int Mul(int x, int y) { return 1ll * x * y % P; }
int power(int a, int b, int c = 1) {
    for (; b; b >>= 1, mul(a, a))
        if (b & 1) mul(c, a);
    return c;
}

const int N = 22, S = 1 << N | 5;
int n, w[N];
i64 dp[S];

// void solve() {

// }

void dfs(int cur, int msk, i64 sum) {
    down(dp[msk], sum);
    rep(i, cur, n - 1) {
        int msk1 = msk;
        rep(j, 0, n - 1) {
            if (msk >> j & 1) {
                msk1 |= 1 << ((i + j) % n);
            }
        }
        if (msk1 == msk) continue;
        dfs(i, msk1, sum + w[i]);
    }
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0);

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

    cin >> n;
    per(i, n, 1) cin >> w[i];
    
    memset(dp, 0x3f, sizeof dp);
    dfs(0, 1, 0);
    rep(i, 0, n - 1) {
        rep(s, 0, (1 << n) - 1) {
            if (s >> i & 1) {
                down(dp[s ^ 1 << i], dp[s]);
            }
        }
    }

    int ans = 0;
    rep(s, 0, (1 << n) - 1) add(ans, Mul(dp[s] % P, s));
    cout << ans << "\n";

    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 9ms
memory: 36132kb

input:

3
2 1 2

output:

45

result:

ok 1 number(s): "45"

Test #2:

score: 0
Accepted
time: 4ms
memory: 36128kb

input:

4
1919810 999999998 999999997 114114514

output:

152175989

result:

ok 1 number(s): "152175989"

Test #3:

score: 0
Accepted
time: 8ms
memory: 36140kb

input:

3
842160586 705327547 868243944

output:

78597628

result:

ok 1 number(s): "78597628"

Test #4:

score: 0
Accepted
time: 4ms
memory: 36320kb

input:

5
198327434 147392532 760837755 771109105 676721155

output:

751568230

result:

ok 1 number(s): "751568230"

Test #5:

score: 0
Accepted
time: 15ms
memory: 36360kb

input:

10
831766351 33417723 223739726 80131988 348810263 415437931 119999060 799356097 512022962 23197703

output:

308170104

result:

ok 1 number(s): "308170104"

Test #6:

score: 0
Accepted
time: 11ms
memory: 36192kb

input:

12
892965903 35291219 261557729 131390943 457251874 944586973 724767219 190756777 658923976 587806068 793999675 378390067

output:

964920873

result:

ok 1 number(s): "964920873"

Test #7:

score: 0
Accepted
time: 13ms
memory: 36192kb

input:

14
249132751 477356204 594343028 32906794 270726189 883801423 329535378 877124753 100792287 152414432 142520554 196476850 924736849 383197276

output:

796031217

result:

ok 1 number(s): "796031217"

Test #8:

score: 0
Accepted
time: 172ms
memory: 36196kb

input:

20
627365465 726842612 947302944 649244156 293375951 318148820 237155023 981487641 688151803 844901013 430309799 733877736 520864483 720366437 28746495 143052089 306590290 18586578 662663479 375430013

output:

179404754

result:

ok 1 number(s): "179404754"

Test #9:

score: 0
Accepted
time: 372ms
memory: 36356kb

input:

21
805448889 595358753 391340394 525130530 272725205 157594893 261894302 29704333 909085958 127205196 104570238 495437480 458664573 599968678 690908307 220500006 735062204 172834136 241126905 814694254 294923292

output:

260115885

result:

ok 1 number(s): "260115885"

Test #10:

score: -100
Time Limit Exceeded

input:

22
983532313 168907597 985120947 845727304 401817563 702073670 841923182 372888321 835052818 409509378 73797974 256997223 101497367 919762407 912878630 297947923 723342631 32114398 409524923 253958495 441987041 591475412

output:


result: