QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#855356#8228. 排序Withers100 ✓2722ms93384kbC++143.1kb2025-01-12 18:51:142025-01-12 18:51:16

Judging History

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

  • [2025-01-12 18:51:16]
  • 评测
  • 测评结果:100
  • 用时:2722ms
  • 内存:93384kb
  • [2025-01-12 18:51:14]
  • 提交

answer

#include<bits/stdc++.h>
#define Withers using
#define AK namespace
#define IOI std
Withers AK IOI;typedef long long ll;typedef pair<int,int>pii;
#define infll 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define endl '\n'
#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
#define deb(x)cout<<#x<<"="<<x<<" ";
#define pb push_back
#define fi first
#define se second
#define mx3(a,b,c)((a>b?a:b)>c?(a>b?a:b):c)
#define mn3(a,b,c)((a<b?a:b)<c?(a<b?a:b):c)
#define mem(a,b)memset(a,b,sizeof(a))
#define rep(i,a,b)for(int i=a;i<=b;i++)
const int N = 35, M = 15, mod = 1e9 + 7;
int n, m, d[M], ss[M][M];
pii ans;
inline void add(pii &a, pii b)
{
	if(a.first < b.first) a = b;
	else if(a.first == b.first) (a.second += b.second) >= mod && (a.second -= mod);
}

unordered_map<int, int> to[N];
int get(int x, int s)
{
	if(to[x].count(s)) return to[x][s];
	
	to[x][s] = 0;
	for(int i = 0; i < d[x]; i++)
	{
		int t = __builtin_popcount(s & ss[x][i]);
		if(t) to[x][s] |= ss[x][i] & (1 << (t - 1) * d[x] + i + 1) - 1;
	}
	return to[x][s];
}

int dfs(int x, int y, int s)
{
	if(x == m) return 0;
	
	int y1 = __builtin_popcount(s & ss[x + 1][y % d[x + 1]]) * d[x + 1] + y % d[x + 1],
		cnt = y / d[x + 1] - __builtin_popcount(s & ss[x + 1][y % d[x + 1]] & (1 << y) - 1);
	return dfs(x + 1, y1, get(x + 1, s)) + cnt;
}

unordered_map<int, pii> g;
pii dp(int s)
{ 
	if(s == 0) return {0, 1};
	if(g.count(s)) return g[s];
	
	pii v = {0, 0};
	for(int i = 0; i <= n; i++)
		if(s >> i & 1 && (ll)s >> i + d[1] & 1 ^ 1)
		{
			pii t = dp(s ^ 1 << i);
			add(v, {t.first + dfs(1, i, s ^ 1 << i), t.second});
		}
	return g[s] = v;
}

void solve()
{
	//do something
	cin>>n>>m;
	n--;
	for(int i = 1; i <= m; i++) cin>>d[i];
	reverse(d + 1, d + m + 1);
	while(d[m] > n) m--;
	reverse(d + 1, d + m + 1);
	for(int i = 1; i <= m; i++)
		for(int j = 0; j < d[i]; j++)
			for(int k = j; k <= n; k += d[i])
				ss[i][j] |= 1 << k;
	
	int res = 0;
	for(int i = 0; i < d[1]; i++) 
	{
		int t = (n - i) / d[1] + 1;
		res += t * (t - 1) / 2;
	}
	ans = dp((1 << n + 1) - 1);
	cout << ans.first + res << " " << ans.second << endl;
}
void multi()
{
	//rd(tst);
	int tst=1;
	while(tst--)
	{
		solve();
	}
//	pcc();
}
signed main()
{
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	multi();
}
// POWERED BY WITHERS
// THINK ONCE, CODE TWICE
/*things to check
1.  int overflow or long long memory need
2.  recursion/array/binary search/dp/loop bounds
3.  precision
4.  special cases(n=1,bounds)
5.  delete debug statements
6.  initialize(especially multi-tests)
7.  = or == , n or m ,++ or -- , i or j , > or >= , < or <= , - or =
8.  keep it simple and stupid
9.  do not delete, use // instead
10. operator priority
11. is there anything extra to output?
12. if you don't know where the bug is , try to clear some parts of the code
 and check each part seperately.
13. ...
*/
 
/* something to think about
1. greedy? dp? searching? dp with matrix/ segment tree? binary search?
2. If contains "not", why not 正难则反 or few affect?
*/
// lgvc bilibilitdasc zxx zy graygoo tt cyx wsc akioi!

详细

Subtask #1:

score: 18
Accepted

Test #1:

score: 18
Accepted
time: 0ms
memory: 3644kb

input:

2 2
2 1

output:

1 1

result:

ok 2 number(s): "1 1"

Test #2:

score: 18
Accepted
time: 0ms
memory: 3584kb

input:

5 4
5 4 2 1

output:

6 4

result:

ok 2 number(s): "6 4"

Test #3:

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

input:

8 4
6 3 2 1

output:

15 4

result:

ok 2 number(s): "15 4"

Test #4:

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

input:

8 6
8 7 5 4 2 1

output:

14 2

result:

ok 2 number(s): "14 2"

Test #5:

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

input:

8 3
8 7 1

output:

22 7

result:

ok 2 number(s): "22 7"

Test #6:

score: 18
Accepted
time: 0ms
memory: 3880kb

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

input:

16 2
6 1

output:

77 15

result:

ok 2 number(s): "77 15"

Test #8:

score: 27
Accepted
time: 16ms
memory: 4772kb

input:

16 8
10 9 8 7 6 5 4 1

output:

57 5

result:

ok 2 number(s): "57 5"

Test #9:

score: 27
Accepted
time: 18ms
memory: 4668kb

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: 27
Accepted
time: 14ms
memory: 4680kb

input:

16 7
10 9 8 6 5 4 1

output:

49 1

result:

ok 2 number(s): "49 1"

Test #11:

score: 27
Accepted
time: 3ms
memory: 3940kb

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

input:

22 3
5 3 1

output:

100 1

result:

ok 2 number(s): "100 1"

Test #13:

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

input:

22 1
1

output:

231 1

result:

ok 2 number(s): "231 1"

Test #14:

score: 21
Accepted
time: 100ms
memory: 12836kb

input:

22 4
10 8 3 1

output:

97 4

result:

ok 2 number(s): "97 4"

Test #15:

score: 21
Accepted
time: 118ms
memory: 12684kb

input:

22 5
10 7 6 3 1

output:

92 70

result:

ok 2 number(s): "92 70"

Test #16:

score: 21
Accepted
time: 126ms
memory: 13116kb

input:

22 6
10 9 8 7 3 1

output:

97 1

result:

ok 2 number(s): "97 1"

Test #17:

score: 21
Accepted
time: 195ms
memory: 13108kb

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

input:

14 2
10 1

output:

61 210

result:

ok 2 number(s): "61 210"

Test #19:

score: 10
Accepted
time: 0ms
memory: 3816kb

input:

18 2
2 1

output:

117 1

result:

ok 2 number(s): "117 1"

Test #20:

score: 10
Accepted
time: 310ms
memory: 46524kb

input:

30 2
9 1

output:

264 84

result:

ok 2 number(s): "264 84"

Test #21:

score: 10
Accepted
time: 252ms
memory: 39736kb

input:

29 2
9 1

output:

253 36

result:

ok 2 number(s): "253 36"

Test #22:

score: 10
Accepted
time: 35ms
memory: 9584kb

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: 1478ms
memory: 93060kb

input:

30 4
10 9 5 1

output:

188 40

result:

ok 2 number(s): "188 40"

Test #24:

score: 24
Accepted
time: 2580ms
memory: 93260kb

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: 24
Accepted
time: 2401ms
memory: 93292kb

input:

30 8
10 9 8 7 4 3 2 1

output:

154 1

result:

ok 2 number(s): "154 1"

Test #26:

score: 24
Accepted
time: 2465ms
memory: 92684kb

input:

30 8
10 8 7 6 5 4 3 1

output:

155 1

result:

ok 2 number(s): "155 1"

Test #27:

score: 24
Accepted
time: 2119ms
memory: 92488kb

input:

30 6
10 8 6 4 3 1

output:

150 4

result:

ok 2 number(s): "150 4"

Test #28:

score: 24
Accepted
time: 2722ms
memory: 93384kb

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: 24
Accepted
time: 1423ms
memory: 77220kb

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