QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#359004#8228. 排序luanmenglei100 ✓771ms20064kbC++172.3kb2024-03-20 10:29:002024-03-20 10:29:00

Judging History

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

  • [2024-03-20 10:29:00]
  • 评测
  • 测评结果:100
  • 用时:771ms
  • 内存:20064kb
  • [2024-03-20 10:29:00]
  • 提交

answer

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

namespace SOL {

using i64 = long long;
void debug(const char *msg, ...) {
#ifdef CLESIP
    va_list arg;
    static char pbString[512];
    va_start(arg,msg);
    vsprintf(pbString,msg,arg);
    cerr << "[DEBUG] " << pbString << "\n";
    va_end(arg);
#endif    
}
template<typename T, typename L>
bool chkmax(T &x, L y) { if (x < y) return x = y, true; return false; }
template<typename T, typename L>
bool chkmin(T &x, L y) { if (y < x) return x = y, true; return false; }

const int N = 30;
const int M = 10;
const int K = 1.1e6;
const i64 P = 1e9 + 7;
int n, m, d[M], pw[M + 1], cnt[N], a[M], mask[M][M];

struct Node {
	i64 sum, cnt;
} f[K];

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

void trans(Node &lhs, const Node &rhs, int cost) {
	if (lhs.cnt == rhs.cnt + cost)
		mod(lhs.sum, rhs.sum);
	else if (lhs.cnt < rhs.cnt + cost)
		lhs = { rhs.sum, rhs.cnt + cost };
}

int gen_mask(int p, int q, int cnt) {
	return mask[p][q] & ((1 << min(30, cnt * d[p])) - 1);
}

void solve() {
	cin >> n >> m;
	for (int i = 0; i < m; i ++) {
		cin >> d[i];
		for (int j = 0; j < n; j ++)
			mask[i][j % d[i]] |= (1 << j);
	}
	for (int i = 0; i < n; i ++)
		cnt[i % d[0]] ++;
	pw[0] = 1;
	for (int i = 1; i <= d[0]; i ++)
		pw[i] = pw[i - 1] * (cnt[i - 1] + 1);
	// debug("pw[%d] = %d", d[0], pw[d[0]]);
	f[0].sum = 1;
	for (int S = 0; S < pw[d[0]]; S ++) {
		// debug("S: %d", S);
		for (int i = 0; i < d[0]; i ++)
			a[i] = (S / pw[i]) % (cnt[i] + 1);
		for (int i = 0; i < d[0]; i ++) if (a[i] < cnt[i]) {
			int cost = cnt[i] - a[i] - 1, pos = i + (cnt[i] - a[i] - 1) * d[0], T = 0;
			for (int j = 0; j < d[0]; j ++)
				T |= gen_mask(0, j, cnt[j] - a[j] - (i == j));
			for (int j = 1; j < m; j ++) {
				int q = pos % d[j]; 
				cost += __builtin_popcount((T & mask[j][q]) >> pos);
				pos = q + __builtin_popcount(T & mask[j][q]) * d[j];
				int nxt = 0;
				for (int k = 0; k < d[j]; k ++)
					nxt |= gen_mask(j, k, __builtin_popcount(T & mask[j][k]));
				T = nxt;
			}
			// debug("i: %d cost: %d", i, cost);
			trans(f[S + pw[i]], f[S], cost);
		}
	}
	cout << f[pw[d[0]] - 1].cnt % P << " " << f[pw[d[0]] - 1].sum << "\n";
}

}
int main() {
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	SOL::solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 18
Accepted

Test #1:

score: 18
Accepted
time: 1ms
memory: 3656kb

input:

2 2
2 1

output:

1 1

result:

ok 2 number(s): "1 1"

Test #2:

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

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: 3660kb

input:

8 4
6 3 2 1

output:

15 4

result:

ok 2 number(s): "15 4"

Test #4:

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

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: 3660kb

input:

8 3
8 7 1

output:

22 7

result:

ok 2 number(s): "22 7"

Test #6:

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

input:

8 1
1

output:

28 1

result:

ok 2 number(s): "28 1"

Subtask #2:

score: 27
Accepted

Dependency #1:

100%
Accepted

Test #7:

score: 27
Accepted
time: 1ms
memory: 3648kb

input:

16 2
6 1

output:

77 15

result:

ok 2 number(s): "77 15"

Test #8:

score: 0
Accepted
time: 7ms
memory: 3868kb

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: 8ms
memory: 3752kb

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: 6ms
memory: 3788kb

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: 1ms
memory: 3720kb

input:

16 4
7 6 2 1

output:

52 9

result:

ok 2 number(s): "52 9"

Subtask #3:

score: 21
Accepted

Dependency #2:

100%
Accepted

Test #12:

score: 21
Accepted
time: 1ms
memory: 3720kb

input:

22 3
5 3 1

output:

100 1

result:

ok 2 number(s): "100 1"

Test #13:

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

input:

22 1
1

output:

231 1

result:

ok 2 number(s): "231 1"

Test #14:

score: 0
Accepted
time: 30ms
memory: 5196kb

input:

22 4
10 8 3 1

output:

97 4

result:

ok 2 number(s): "97 4"

Test #15:

score: 0
Accepted
time: 38ms
memory: 5348kb

input:

22 5
10 7 6 3 1

output:

92 70

result:

ok 2 number(s): "92 70"

Test #16:

score: 0
Accepted
time: 47ms
memory: 5196kb

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: 71ms
memory: 5308kb

input:

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

output:

109 1

result:

ok 2 number(s): "109 1"

Subtask #4:

score: 10
Accepted

Test #18:

score: 10
Accepted
time: 1ms
memory: 3732kb

input:

14 2
10 1

output:

61 210

result:

ok 2 number(s): "61 210"

Test #19:

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

input:

18 2
2 1

output:

117 1

result:

ok 2 number(s): "117 1"

Test #20:

score: 0
Accepted
time: 54ms
memory: 11612kb

input:

30 2
9 1

output:

264 84

result:

ok 2 number(s): "264 84"

Test #21:

score: 0
Accepted
time: 48ms
memory: 10084kb

input:

29 2
9 1

output:

253 36

result:

ok 2 number(s): "253 36"

Test #22:

score: 0
Accepted
time: 6ms
memory: 4908kb

input:

25 2
8 1

output:

195 8

result:

ok 2 number(s): "195 8"

Subtask #5:

score: 24
Accepted

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

100%
Accepted

Test #23:

score: 24
Accepted
time: 332ms
memory: 19956kb

input:

30 4
10 9 5 1

output:

188 40

result:

ok 2 number(s): "188 40"

Test #24:

score: 0
Accepted
time: 710ms
memory: 19948kb

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: 619ms
memory: 20016kb

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: 589ms
memory: 20064kb

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: 460ms
memory: 19992kb

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: 771ms
memory: 20040kb

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: 357ms
memory: 15860kb

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