2018HBCPC部分题解

Mex Query

在这里插入图片描述

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <bits/stdc++.h>
using namespace std;
// 传送门:http://newoj.acmclub.cn/problems/2011
int T;
int n;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> T;
while (T--)
{
set<int> s;
cin >> n;
for (int i = 1; i <= n; i++)
{
int remp;
cin >> remp;
s.insert(remp);
}
set<int>::iterator it;
int cont = 0;
for (it = s.begin(); it != s.end(); it++)
{
if (*it != cont)
{
cout << cont << endl;
break;
}
cont++;
}
}
return 0;
}

icebound的商店

在这里插入图片描述
在这里插入图片描述

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <bits/stdc++.h>
using namespace std;
// http://newoj.acmclub.cn/problems/2012
// 完全背包
#define mod 1000000009
int bag[15];
int ans[3010];
int T;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
bag[1] = 1;
bag[2] = 2;
for (int i = 3; i <= 15; i++)
bag[i] = bag[i - 1] + bag[i - 2];
ans[0] = 1;
for (int i = 1; i <= 15; i++)
{
for (int j = bag[i]; j <= 3010; j++)
{
ans[j] += ans[j - bag[i]];
ans[j] %= mod;
}
}
cin >> T;
for (int i = 1; i <= T; i++)
{
int tar;
cin >> tar;
cout << ans[tar] << endl;
}
return 0;
}

Nim Game

在这里插入图片描述
在这里插入图片描述在这里插入图片描述

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <bits/stdc++.h>
using namespace std;
// 博弈论:Nim
// http://newoj.acmclub.cn/problems/2013
const int MOD = 1e9 + 7;
int T;
int n, m;
int a[1000100];
int sum[1000100];
int f[1000100];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> T;
while (T--)
{
cin >> n >> m;
memset(sum, 0, sizeof(sum));
for (int i = 1; i <= n; i++)
{
cin >> a[i];
sum[i] = sum[i - 1] ^ a[i];
}
for (int i = 1; i <= m; i++)
{
int l, r;
cin >> l >> r;
int result = sum[r] ^ sum[l - 1]; //第i次结果
if (result)
f[i] = 1;
else
f[i] = 0;
}
long long ans = 0;
for (int i = 1; i <= m; i++)
{
ans = ans << 1;
ans += f[i];
ans = ans % MOD;
}
cout << ans << endl;
}
return 0;
}

神殿

在这里插入图片描述

在这里插入图片描述

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <bits/stdc++.h>
using namespace std;
// http://newoj.acmclub.cn/problems/2016
long long l, r;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> l >> r;
while (l <= r)
{
if ((l | l + 1) > r) //(l | l + 1)为的是将l的最低一位0尝试改成1
break;
l = (l | l + 1);
}
cout << l << endl;
return 0;
}

跑图

在这里插入图片描述
TLE解法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <bits/stdc++.h>
using namespace std;
// TLE解法。。。
int n, m;
int graph[510][510];
int ans[510][510];
int cnt;
struct node
{
int x, y;
} point[250010];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
memset(ans, 0x3f, sizeof(ans));
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
{
cin >> graph[i][j];
if (graph[i][j])
{
++cnt;
point[cnt].x = i;
point[cnt].y = j;
}
}
for (int k = 1; k <= cnt; k++)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (graph[i][j])
ans[i][j] = 0;
else
ans[i][j] = min(ans[i][j], abs(point[k].x - i) + abs(point[k].y - j));
}
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cout << ans[i][j];
if (j != m)
cout << " ";
}
cout << endl;
}
return 0;
}

正解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <bits/stdc++.h>
using namespace std;
// http://newoj.acmclub.cn/problems/2018
// 正解:BFS
int n, m;
int graph[510][510];
bool visit[510][510];
int ans[510][510];
int dirx[] = {0, 0, -1, 1};
int diry[] = {1, -1, 0, 0};
struct node
{
int x, y;
};
queue<node> q;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cin >> graph[i][j];
if (graph[i][j])
{
node remp;
remp.x = i, remp.y = j;
visit[i][j] = true;
q.push(remp);
}
}
}
while (q.empty() == false)
{
node remp = q.front();
int rempx = remp.x, rempy = remp.y;
q.pop();
for (int i = 0; i < 4; i++)
{
int nextx = rempx + dirx[i];
int nexty = rempy + diry[i];
if (visit[nextx][nexty] == false && (nextx <= n && nextx >= 1 && nexty <= m && nexty >= 1))
{
ans[nextx][nexty] = ans[rempx][rempy] + 1;
visit[nextx][nexty] = true; //易错点!!一定要第一次更新完就马上标记!因为此时不标记,后面可能被二次标记,这时候就不是最近的了
node next;
next.x = nextx, next.y = nexty;
q.push(next);
}
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cout << ans[i][j];
if (j != m)
cout << " ";
}
cout << endl;
}
return 0;
}

520

在这里插入图片描述

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <bits/stdc++.h>
using namespace std;
// 快速幂
#define MOD 20180520
long long fast_power(long long a, long long b, long long c)
{
long long ans = 1;
a = a % c;
while (b)
{
if (b % 2)
ans = (ans * a) % c;
b /= 2;
a = (a * a) % c;
}
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
long long n;
cin >> n;
cout << fast_power(2, n, MOD);
return 0;
}

icebound的账单

在这里插入图片描述

1