QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#645348#8285. Shell SortOIer_kzc#AC ✓3505ms11876kbC++201.9kb2024-10-16 17:53:352024-10-16 17:53:36

Judging History

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

  • [2024-10-16 17:53:36]
  • 评测
  • 测评结果:AC
  • 用时:3505ms
  • 内存:11876kb
  • [2024-10-16 17:53:35]
  • 提交

answer

#include <stdio.h>
#include <string.h>
#include <assert.h>

#include <cmath>
#include <vector>

#define LOG(FMT...) fprintf(stderr, FMT)

#define eb emplace_back

using namespace std;

typedef long long LL;
constexpr int N = 16, NS = 2097152, mod = 1e9 + 7;

int n, m, d[N], pr[N], c[N];

void Add(int &x, int y) {
	if ((x += y) >= mod) {
		x -= mod;
	}
}

struct Pair {
	int x, y;
	void operator += (const Pair &t) {
		if (x < t.x) {
			x = t.x, y = t.y;
		} else if (x == t.x) {
			Add(y, t.y);
		}
	}
} f[NS];

int sz;
int ns;
int xs[N], szx;

int cost(int k) {
	static char v[32];
	for (int i = 0; i < sz; ++i) {
		for (int j = 0; j < xs[i]; ++j) {
			v[j * sz + i] = -1;
		}
		for (int j = xs[i]; j < c[i]; ++j) {
			v[j * sz + i] = 1;
		}
	}
	v[xs[k] * sz + k] = 0;
	int ret = 0;
	for (int i = 1; i < m; ++i) {
		int D = d[i];
		for (int r = 0; r < D; ++r) {
			for (int x = r; x < n; x += D) {
				for (int y = x; y >= D && v[y] < v[y - D]; y -= D) {
					ret += (v[y] == 0);
					swap(v[y], v[y - D]);
				}
			}
		}
	}
	return ret;
}

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 0; i < m; ++i) {
		scanf("%d", d + i);
	}
	sz = d[0];
	int extra = 0;
	pr[0] = 1;
	for (int i = 0; i < sz; ++i) {
		c[i] = n / sz + (i < n % sz);
		extra += c[i] * (c[i] - 1) / 2;
		pr[i + 1] = pr[i] * (1 + c[i]);
	}
	ns = pr[sz];
	for (int i = 1; i < ns; ++i) {
		f[i].x = -1, f[i].y = 0;
	}
	f[0] = (Pair){0, 1};
	for (int i = 0; i < ns; ++i) {
		if (f[i].x < 0) {
			continue;
		}
		for (int j = 0; j < sz; ++j) {
			xs[j] = i % pr[j + 1] / pr[j];
		}
		for (int j = 0; j < sz; ++j) {
			int x = xs[j];
			if (x == c[j]) {
				continue;
			}
			Pair t = f[i];
			t.x += cost(j);
			f[i + pr[j]] += t;
		}
	}
	int res1 = extra + f[ns - 1].x;
	int res2 = f[ns - 1].y;
	printf("%d %d\n", res1, res2);
	return 0;
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

2 2
2 1

output:

1 1

result:

ok 2 number(s): "1 1"

Test #2:

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

input:

5 4
5 4 2 1

output:

6 4

result:

ok 2 number(s): "6 4"

Test #3:

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

input:

8 4
6 3 2 1

output:

15 4

result:

ok 2 number(s): "15 4"

Test #4:

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

input:

8 6
8 7 5 4 2 1

output:

14 2

result:

ok 2 number(s): "14 2"

Test #5:

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

input:

8 3
8 7 1

output:

22 7

result:

ok 2 number(s): "22 7"

Test #6:

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

input:

8 1
1

output:

28 1

result:

ok 2 number(s): "28 1"

Test #7:

score: 0
Accepted
time: 1ms
memory: 1664kb

input:

16 2
6 1

output:

77 15

result:

ok 2 number(s): "77 15"

Test #8:

score: 0
Accepted
time: 16ms
memory: 1720kb

input:

16 8
10 9 8 7 6 5 4 1

output:

57 5

result:

ok 2 number(s): "57 5"

Test #9:

score: 0
Accepted
time: 19ms
memory: 1700kb

input:

16 10
10 9 8 7 6 5 4 3 2 1

output:

57 3

result:

ok 2 number(s): "57 3"

Test #10:

score: 0
Accepted
time: 14ms
memory: 1672kb

input:

16 7
10 9 8 6 5 4 1

output:

49 1

result:

ok 2 number(s): "49 1"

Test #11:

score: 0
Accepted
time: 3ms
memory: 1620kb

input:

16 4
7 6 2 1

output:

52 9

result:

ok 2 number(s): "52 9"

Test #12:

score: 0
Accepted
time: 3ms
memory: 1680kb

input:

22 3
5 3 1

output:

100 1

result:

ok 2 number(s): "100 1"

Test #13:

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

input:

22 1
1

output:

231 1

result:

ok 2 number(s): "231 1"

Test #14:

score: 0
Accepted
time: 140ms
memory: 2428kb

input:

22 4
10 8 3 1

output:

97 4

result:

ok 2 number(s): "97 4"

Test #15:

score: 0
Accepted
time: 154ms
memory: 2412kb

input:

22 5
10 7 6 3 1

output:

92 70

result:

ok 2 number(s): "92 70"

Test #16:

score: 0
Accepted
time: 167ms
memory: 3760kb

input:

22 6
10 9 8 7 3 1

output:

97 1

result:

ok 2 number(s): "97 1"

Test #17:

score: 0
Accepted
time: 247ms
memory: 2472kb

input:

22 10
10 9 8 7 6 5 4 3 2 1

output:

109 1

result:

ok 2 number(s): "109 1"

Test #18:

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

input:

14 2
10 1

output:

61 210

result:

ok 2 number(s): "61 210"

Test #19:

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

input:

18 2
2 1

output:

117 1

result:

ok 2 number(s): "117 1"

Test #20:

score: 0
Accepted
time: 731ms
memory: 6084kb

input:

30 2
9 1

output:

264 84

result:

ok 2 number(s): "264 84"

Test #21:

score: 0
Accepted
time: 576ms
memory: 4892kb

input:

29 2
9 1

output:

253 36

result:

ok 2 number(s): "253 36"

Test #22:

score: 0
Accepted
time: 89ms
memory: 2188kb

input:

25 2
8 1

output:

195 8

result:

ok 2 number(s): "195 8"

Test #23:

score: 0
Accepted
time: 1786ms
memory: 11024kb

input:

30 4
10 9 5 1

output:

188 40

result:

ok 2 number(s): "188 40"

Test #24:

score: 0
Accepted
time: 3181ms
memory: 11148kb

input:

30 9
10 9 8 7 6 5 4 3 1

output:

184 6

result:

ok 2 number(s): "184 6"

Test #25:

score: 0
Accepted
time: 2930ms
memory: 10164kb

input:

30 8
10 9 8 7 4 3 2 1

output:

154 1

result:

ok 2 number(s): "154 1"

Test #26:

score: 0
Accepted
time: 2937ms
memory: 11876kb

input:

30 8
10 8 7 6 5 4 3 1

output:

155 1

result:

ok 2 number(s): "155 1"

Test #27:

score: 0
Accepted
time: 2399ms
memory: 10648kb

input:

30 6
10 8 6 4 3 1

output:

150 4

result:

ok 2 number(s): "150 4"

Test #28:

score: 0
Accepted
time: 3505ms
memory: 10056kb

input:

30 10
10 9 8 7 6 5 4 3 2 1

output:

184 6

result:

ok 2 number(s): "184 6"

Test #29:

score: 0
Accepted
time: 1690ms
memory: 8248kb

input:

29 6
10 9 7 5 3 1

output:

129 200

result:

ok 2 number(s): "129 200"

Extra Test:

score: 0
Extra Test Passed