QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#109237#6397. Master of Both IIIwsyearRE 221ms19688kbC++173.2kb2023-05-27 23:36:432023-05-27 23:36:45

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-27 23:36:45]
  • 评测
  • 测评结果:RE
  • 用时:221ms
  • 内存:19688kb
  • [2023-05-27 23:36:43]
  • 提交

answer

#include <bits/stdc++.h>
#ifdef dbg
#define D(...) fprintf(stderr, __VA_ARGS__)
#define DD(...) D(#__VA_ARGS__ " = "), debug_helper::debug(__VA_ARGS__), D("\n")
#include "C:\Users\wsyear\Desktop\OI\templates\debug.hpp"
#else
#define D(...) ((void)0)
#define DD(...) ((void)0)
#endif
#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 SZ(v) int((v).size())
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
#define gc getchar
#define pc putchar
using ll = long long;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;

using namespace std;

template <class T = int> T read() {
  T x = 0; bool f = 0; char ch = gc();
  while (!isdigit(ch)) f = ch == '-', ch = gc();
  while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = gc();
  return f ? -x: x;
}
template <class T> void write(T x) {
  if (x >= 0) { if (x > 9) write(x / 10); pc(x % 10 + 48); }
  else { pc('-'); if (x < -9) write(-x / 10); pc(48 - x % 10); }
}

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*=(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
};
const int P = 998244353;
using Z = mod_int<P>;

const int N = 22;

int n, a[N];
ll dp[1 << N];

int main() {
  n = read();
  rep (i, 0, n - 1) a[n - i] = read();
  rep (i, 0, (1 << n) - 1) dp[i] = 1E18;
  dp[1] = 0;
  rep (mask, 1, (1 << n) - 1) rep (k, 0, n - 1) {
    int m1 = (mask << k) & ((1 << n) - 1), m2 = mask >> (n - k);
    dp[mask | m1 | m2] = min(dp[mask | m1 | m2], dp[mask] + a[k]);
  }
  per (mask, (1 << n) - 1, 1) rep (k, 0, n - 1) if (mask >> k & 1) {
    dp[mask ^ (1 << k)] = min(dp[mask ^ (1 << k)], dp[mask]);
  }
  Z ans = 0;
  rep (mask, 1, (1 << n) - 1) ans += Z(mask) * dp[mask];
  write(ans.val());
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
2 1 2

output:

45

result:

ok 1 number(s): "45"

Test #2:

score: 0
Accepted
time: 2ms
memory: 3488kb

input:

4
1919810 999999998 999999997 114114514

output:

152175989

result:

ok 1 number(s): "152175989"

Test #3:

score: 0
Accepted
time: 2ms
memory: 3356kb

input:

3
842160586 705327547 868243944

output:

78597628

result:

ok 1 number(s): "78597628"

Test #4:

score: 0
Accepted
time: 0ms
memory: 3356kb

input:

5
198327434 147392532 760837755 771109105 676721155

output:

751568230

result:

ok 1 number(s): "751568230"

Test #5:

score: 0
Accepted
time: 2ms
memory: 3380kb

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: 2ms
memory: 3376kb

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: 3ms
memory: 3492kb

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: 108ms
memory: 11680kb

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: 221ms
memory: 19688kb

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
Runtime Error

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: