[关闭]
@Dmaxiya 2020-12-15T23:28:49.000000Z 字数 18620 阅读 881

2018 Multi-University Training Contest 3

暑期集训


链接:2018 Multi-University Training Contest 3
过题数:5
排名:146/815
成员:官展鹏,冯彦博,孙昊哲

A. Ascending Rating

题意

给定一个长度为 的序列 ,对每一个长度为 的区间 ,都有以下操作:

  1. 最开始有一个 值,初始值分别为
  2. ,每扫描一个数字,如果当前数字大于 ,就将 更新为当前数字,并将

每一个连续的区间 的初始值都为 ,求每个区间最终的 值和 值。

输入

第一行为一个整数 ,接下来有 组数据,每组数据第一行为 个整数 ,第二行为 个整数 ,表示序列的前 项,第 到第 项由以下公式生成:

数据保证

输出

每组数据输出两个数字 ,其中:

样例

输入
1
10 6 10 5 5 5 5
3 2 2 1 5 7 6 8 2 9
输出
46 11

题解

从后往前维护一个单调队列,维护到第 个区间时队列的大小即 ,队列中的最大值就是 ,扫一遍只需要

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cstring>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <bitset>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <iomanip>
  18. #include <unordered_set>
  19. #include <unordered_map>
  20. using namespace std;
  21. #define LL long long
  22. const int maxn = 10000000 + 100;
  23. int T, n, m, k, p, q, r, MOD, head, tail;
  24. LL A, B;
  25. int num[maxn], que[maxn];
  26. int add(int a, int b) {
  27. int ret = a + b;
  28. if(ret >= MOD) {
  29. return ret - MOD;
  30. }
  31. return ret;
  32. }
  33. int main() {
  34. #ifdef LOCAL
  35. freopen("test.txt", "r", stdin);
  36. // freopen("out.txt", "w", stdout);
  37. #endif // LOCAL
  38. ios::sync_with_stdio(true);
  39. scanf("%d", &T);
  40. while(T--) {
  41. head = tail = 0;
  42. scanf("%d%d%d%d%d%d%d", &n, &m, &k, &p, &q, &r, &MOD);
  43. for(int i = 1; i <= k; ++i) {
  44. scanf("%d", &num[i]);
  45. }
  46. for(int i = k + 1; i <= n; ++i) {
  47. num[i] = add((LL)p * num[i - 1] % MOD, (LL)q * i % MOD);
  48. num[i] = add(num[i], r);
  49. }
  50. for(int i = 0; i < m; ++i) {
  51. int Index = n - i;
  52. while(head != tail && num[Index] >= num[que[head]]) {
  53. --head;
  54. }
  55. que[++head] = Index;
  56. }
  57. A = num[que[tail + 1]] ^ (n - m + 1);
  58. B = (head - tail) ^ (n - m + 1);
  59. for(int i = n - m; i >= 1; --i) {
  60. while(head != tail && num[i] >= num[que[head]]) {
  61. --head;
  62. }
  63. que[++head] = i;
  64. if(que[tail + 1] >= i + m) {
  65. ++tail;
  66. }
  67. A += num[que[tail + 1]] ^ i;
  68. B += (head - tail) ^ i;
  69. }
  70. printf("%I64d %I64d\n", A, B);
  71. }
  72. return 0;
  73. }

C. Dynamic Graph Matching

题意

定义一种图的匹配:在图上选择任意条边,在被选择的边中任意两条边之间没有公共点,则这些边就是图上的一种匹配。
在一个 个节点的图上,最初这张图上的边数为 ,有 次操作,每次操作为以下两种中的一种:

  1. :在节点 和节点 之间添加一条边;
  2. :删掉一条在节点 之间的边,在删边之前保证节点 之间至少存在一条边。

在每次操作之后,对于每一个 ,输出图上边数为 的匹配的数量,两个节点之间的多条边视为不同的边。

输入

第一行为一个整数 ,接下去有 组数据,每组数据第一行为两个整数 ,接下去 行每行由一个字符 和两个数字 组成,表示一次操作。

输出

对于每一次操作,输出 个整数,分别表示当 时,图上边数为 的匹配数量对 取模的结果。

样例

输入
1
4 8
+ 1 2
+ 3 4
+ 1 3
+ 2 4
- 1 2
- 3 4
+ 1 2
+ 3 4
输出
1 0
2 1
3 1
4 2
3 1
2 1
3 1
4 2

题解

我们用 个整数对应图上点的选择方案,整数的第 个二进制位为 表示不选择第 个点,否则表示选择第 个点,则一个数字表示一种选择点的状态。用 记录某种匹配的边覆盖的点的状态为 的情况下,匹配的数量,则 的个数必须为偶数,否则匹配数量就为
每当节点 之间的边的数量 ,就会影响所有选点状态同时包含 两点的 值,如果 的第 位都为 ,则 ,当 +,否则
初始状态 ,用 记录在匹配边的数量为 时的总方案数,设 为数字 的二进制位中 的数量,则 ,其中

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cstring>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <bitset>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <iomanip>
  18. #include <unordered_set>
  19. #include <unordered_map>
  20. using namespace std;
  21. #define LL long long
  22. const int maxn = 1000 + 100;
  23. const int MOD = 1000000000 + 7;
  24. int T, n, m, u, v, d;
  25. char ch[2];
  26. int dp[maxn], ans[maxn], bit[maxn];
  27. int add(int a, int b) {
  28. int ret = a + b;
  29. if(ret < 0) {
  30. return ret + MOD;
  31. }
  32. if(ret >= MOD) {
  33. return ret - MOD;
  34. }
  35. return ret;
  36. }
  37. void solve(int u, int v, int d) {
  38. int tmp = (1 << u) | (1 << v);
  39. for(int i = 1; i < (1 << n); ++i) {
  40. if((bit[i] & 1) == 1) {
  41. continue;
  42. }
  43. if((i & tmp) == tmp) {
  44. dp[i] = add(dp[i], d * dp[i ^ tmp]);
  45. ans[bit[i] >> 1] = add(ans[bit[i] >> 1], d * dp[i ^ tmp]);
  46. }
  47. }
  48. }
  49. int main() {
  50. #ifdef LOCAL
  51. freopen("test.txt", "r", stdin);
  52. // freopen("out.txt", "w", stdout);
  53. #endif // LOCAL
  54. ios::sync_with_stdio(true);
  55. for(int i = 0; i < maxn; ++i) {
  56. bit[i] = 0;
  57. for(int j = 0; j < 10; ++j) {
  58. if(((i >> j) & 1) == 1) {
  59. ++bit[i];
  60. }
  61. }
  62. }
  63. scanf("%d", &T);
  64. while(T--) {
  65. scanf("%d%d", &n, &m);
  66. memset(dp, 0, sizeof(int) * (1 << n));
  67. memset(ans + 1, 0, sizeof(int) * (n >> 1));
  68. dp[0] = 1;
  69. for(int i = 0; i < m; ++i) {
  70. scanf("%s%d%d", ch, &u, &v);
  71. d = ch[0] == '+'? 1: -1;
  72. solve(u - 1, v - 1, d);
  73. for(int j = 1; j <= (n >> 1); ++j) {
  74. if(j != 1) {
  75. printf(" ");
  76. }
  77. printf("%d", ans[j]);
  78. }
  79. printf("\n");
  80. }
  81. }
  82. return 0;
  83. }

D. Euler Function

题意

输出第 小的 为合数的 的值,其中 为欧拉函数( 表示 的数字个数)。

输入

第一行为一个整数 ,接下去 行每行一个整数

输出

对于每一个 ,输出第 小的合法的数字。

样例

输入
2
1
2
输出
5
7

题解

通过打表找规律可以发现,除了 时答案为 外,其他答案都为

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cfloat>
  7. #include <cstring>
  8. #include <string>
  9. #include <vector>
  10. #include <list>
  11. #include <queue>
  12. #include <stack>
  13. #include <map>
  14. #include <set>
  15. #include <algorithm>
  16. using namespace std;
  17. #define LL long long
  18. int main() {
  19. #ifdef Dmaxiya
  20. freopen("test.txt", "r", stdin);
  21. #endif // Dmaxiya
  22. ios::sync_with_stdio(false);
  23. int T, k;
  24. scanf("%d", &T);
  25. while(T--) {
  26. scanf("%d", &k);
  27. if(k == 1) {
  28. printf("5\n");
  29. } else {
  30. printf("%d\n", k + 5);
  31. }
  32. }
  33. return 0;
  34. }

F. Grab The Tree

题意

和小 玩一个游戏,在一棵 个节点的树上,每个节点都有一个权值 ,由小 先开始选点,小 选择的点中,任意两个节点不能同时为某条边的两个端点,之后剩下的点全是小 的,小 的最终分数就是他所选择的所有点的权值异或值,小 的分数也是他拥有的点的权值异或值,问在小 采取最优策略的情况下,谁会获胜,还是平局。

输入

第一行包含一个整数 ,接下去有 组数据,每组数据第一行为一个整数 ,第二行为 个整数 ,接下去 行每行两个整数 ,表示节点 之间有一条边。

输出

如果小 会获胜,则输出 ,如果小 会获胜,则输出 ,否则输出

样例

输入
1
3
2 2 2
1 2
1 3
输出
Q

题解

如果树上所有点的异或值为 ,无论小 如何选点,小 的分数都会和小 的分数相等,如果树上所有点的异或值非 ,则小 只需要选择最大值,就可以让树上所有其他点异或值的最高位小于小 选择的点的权值,小 必胜。

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cstring>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <bitset>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <iomanip>
  18. #include <unordered_set>
  19. #include <unordered_map>
  20. using namespace std;
  21. #define LL long long
  22. int T, n, num, Xor, u, v;
  23. int main() {
  24. #ifdef LOCAL
  25. freopen("test.txt", "r", stdin);
  26. // freopen("out.txt", "w", stdout);
  27. #endif // LOCAL
  28. ios::sync_with_stdio(true);
  29. scanf("%d", &T);
  30. while(T--) {
  31. Xor = 0;
  32. scanf("%d", &n);
  33. for(int i = 0; i < n; ++i) {
  34. scanf("%d", &num);
  35. Xor ^= num;
  36. }
  37. for(int i = 1; i < n; ++i) {
  38. scanf("%d%d", &u, &v);
  39. }
  40. if(Xor == 0) {
  41. printf("D\n");
  42. } else {
  43. printf("Q\n");
  44. }
  45. }
  46. return 0;
  47. }

G. Interstellar Travel

题意

在一个二维平面上总共有 个点,坐标分别为 ,小 要从坐标为 的点到达坐标 的点,他每次只能从横坐标小的点到达横坐标大的点,即 ,如果他要从坐标为 的点到达 的点,则需要的代价为 ,问要完成目标的最小代价。

输入

第一行为一个整数 ,接下去有 组数据,每组数据第一行为一个整数 ,接下去 行每行两个整数 ,数据保证

输出

每组数据输出一个序列 ,表示小 选择的路径为 表示路径上第 个点的编号为 ,点的编号按输入顺序从 确定,其中 ,如果有多组解,输出字典序最小的一组。

样例

输入
1
3
0 0
3 0
4 0
输出
1 2 3

题解

起点为 ,且经过两个点的代价为两个点对应的向量叉积,即这两点与原点构成的三角形面积,若从向量 到向量 是以顺时针旋转,则这两个向量的叉积就为负数,为了使代价最小,也就是使负数的绝对值——三角形的面积最大,因此这条路径就是从原点到终点的一个凸壳。
首先对所有点去重,然后构造凸壳,构造过程中保留凸壳上的共线点,所有拐点必然是要作为答案的,但是共线点上可能存在比下一个拐点编号更小的点,为使答案的字典序最小,必须选择这些共线点上编号小于下一个拐点的一系列点,且这一系列点的字典序也要最小,在这些共线点中可以用单调栈来维护需要选择的字典序最小的点的编号。

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cstring>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <bitset>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <iomanip>
  18. using namespace std;
  19. #define LL long long
  20. const int maxn = 200000 + 100;
  21. struct Point {
  22. int x, y;
  23. int Index;
  24. Point() {}
  25. Point(int xx, int yy) {
  26. x = xx;
  27. y = yy;
  28. }
  29. };
  30. bool operator<(const Point &a, const Point &b) {
  31. return a.x == b.x? a.y < b.y: a.x < b.x;
  32. }
  33. LL operator^(const Point &a, const Point &b) {
  34. return (LL)a.x * b.y - (LL)a.y * b.x;
  35. }
  36. Point operator-(const Point &a, const Point &b) {
  37. return Point(a.x - b.x, a.y - b.y);
  38. }
  39. bool operator!=(const Point &a, const Point &b) {
  40. return a.x != b.x || a.y != b.y;
  41. }
  42. int T, n, top, cnt, Mon_top;
  43. Point point[maxn], sta[maxn], p[maxn], Mon_sta[maxn];
  44. map<int, int> mp;
  45. int main() {
  46. #ifdef LOCAL
  47. freopen("test.txt", "r", stdin);
  48. // freopen("out.txt", "w", stdout);
  49. #endif // LOCAL
  50. ios::sync_with_stdio(false);
  51. scanf("%d", &T);
  52. while(T--) {
  53. mp.clear();
  54. cnt = 0;
  55. scanf("%d", &n);
  56. for(int i = 1; i <= n; ++i) {
  57. scanf("%d%d", &point[i].x, &point[i].y);
  58. point[i].Index = i;
  59. mp[point[i].x] = max(mp[point[i].x], point[i].y);
  60. }
  61. sort(point + 1, point + 1 + n);
  62. p[0].x = p[0].y = -1;
  63. for(int i = 1; i <= n; ++i) {
  64. if(point[i].y == mp[point[i].x]) {
  65. if(point[i] != p[cnt]) {
  66. p[++cnt] = point[i];
  67. } else if(point[i].Index < p[cnt].Index) {
  68. p[cnt] = point[i];
  69. }
  70. }
  71. }
  72. top = 0;
  73. for(int i = 1; i <= cnt; ++i) {
  74. while(top > 1 && ((sta[top - 1] - sta[top - 2]) ^ (p[i] - sta[top - 2])) > 0) {
  75. --top;
  76. }
  77. sta[top++] = p[i];
  78. }
  79. printf("1");
  80. for(int i = 0; i < top - 1; ) {
  81. int End = i + 1;
  82. while(End + 1 < top && ((sta[End + 1] - sta[End]) ^ (sta[End] - sta[i])) == 0) {
  83. ++End;
  84. }
  85. Mon_top = 0;
  86. for(int j = i + 1; j <= End; ++j) {
  87. while(Mon_top > 0 && sta[j].Index < Mon_sta[Mon_top - 1].Index) {
  88. --Mon_top;
  89. }
  90. Mon_sta[Mon_top++] = sta[j];
  91. }
  92. for(int j = 0; j < Mon_top; ++j) {
  93. printf(" %d", Mon_sta[j].Index);
  94. }
  95. i = End;
  96. }
  97. printf("\n");
  98. }
  99. return 0;
  100. }

H. Monster Hunter

题意

在一棵 个节点的树上,他最初的位置在节点 处,除了 以外,其他节点都有一个怪兽,小 需要沿着树上路径行走,每到达一个节点 ,就要与这个节点上的怪兽进行战斗,战斗将会损失 ,战斗过程中若 值小于 则游戏结束,战斗结束后若小 值不小于 ,则他的 值将会增加 ,每个节点上的怪兽被消灭后,再次走到这个节点时就不需要再进行战斗,问如果要消灭树上所有的怪兽,小 最初至少需要有多少点 值。

输入

第一行为一个整数 ,接下去有 组数据,每组数据第一行为一个整数 ,接下去 行每行两个整数 ,第 行整数表示节点 上的战斗 增减值,接下去 行每行两个整数 ,表示节点 之间有一条边,数据保证

输出

输出通关所需要的最小的初始 值。

样例

输入
1
4
2 6
5 4
6 2
1 2
2 3
3 4
输出
3

题解

首先不考虑树上路径的限制,如果每个怪兽可以以任意次序进行战斗, 的怪兽必然要比 的怪兽先进行攻击,这样才能获得最大的 ,在 的所有怪兽中,必然要先攻击 值小的,在 大的所有怪兽中,如果先攻击第 只怪兽再攻击第 只怪兽,在攻击后一只怪兽的战斗过程中的 值为 ,如果反过来先攻击第 只怪兽,在后一只怪兽的战斗过程中的 值为 ,可以发现攻击的次序与 无关,而只与 有关,必然要先攻击 值大的。
通过以上规则可以确定一个最优的攻击怪兽的次序 ,现在考虑树上路径的限制,攻击每只怪兽之前必须要先打败它所有的祖父节点,因此对于每个节点 ,判断该节点的父节点是否为 ,若为 ,则可以直接攻击,否则将 值合并到它的父节点的 上表示在攻击完它的父节点后必须立即攻击第 个节点,父节点的 值应更新为

然后将所有 的子节点的父节点改为 ,更新父节点并快速排序的过程可以用堆来维护,更改父节点的操作可以用并查集来维护。

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cstring>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <bitset>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <iomanip>
  18. using namespace std;
  19. #define LL long long
  20. const int maxn = 100000 + 100;
  21. struct Node {
  22. LL a, b;
  23. int fa, Index;
  24. };
  25. bool operator<(const Node &a, const Node &b) {
  26. bool flag_a = a.a < a.b;
  27. bool flag_b = b.a < b.b;
  28. if(flag_a != flag_b) {
  29. return flag_a < flag_b;
  30. }
  31. if(flag_a) {
  32. return a.a > b.a;
  33. }
  34. return a.b < b.b;
  35. }
  36. bool operator!=(const Node &a, const Node &b) {
  37. return a.a != b.a || a.b != b.b || a.fa != b.fa || a.Index != b.Index;
  38. }
  39. int T, n, u, v;
  40. int fa[maxn];
  41. Node node[maxn];
  42. vector<int> G[maxn];
  43. priority_queue<Node> que;
  44. void Init() {
  45. for(int i = 1; i <= n; ++i) {
  46. fa[i] = i;
  47. G[i].clear();
  48. }
  49. }
  50. int Find(int x) {
  51. return x == fa[x]? x: fa[x] = Find(fa[x]);
  52. }
  53. void unit(int x, int y) {
  54. int xx = Find(x);
  55. int yy = Find(y);
  56. fa[xx] = yy;
  57. }
  58. void dfs(int f, int x) {
  59. node[x].fa = f;
  60. int len = G[x].size();
  61. for(int i = 0; i < len; ++i) {
  62. int pos = G[x][i];
  63. if(pos != f) {
  64. dfs(x, pos);
  65. }
  66. }
  67. }
  68. int main() {
  69. #ifdef LOCAL
  70. freopen("test.txt", "r", stdin);
  71. // freopen("out.txt", "w", stdout);
  72. #endif // LOCAL
  73. ios::sync_with_stdio(false);
  74. scanf("%d", &T);
  75. while(T--) {
  76. scanf("%d", &n);
  77. Init();
  78. node[1].a = node[1].b = 0;
  79. node[1].Index = 1;
  80. for(int i = 2; i <= n; ++i) {
  81. scanf("%I64d%I64d", &node[i].a, &node[i].b);
  82. node[i].Index = i;
  83. }
  84. for(int i = 1; i < n; ++i) {
  85. scanf("%d%d", &u, &v);
  86. G[u].push_back(v);
  87. G[v].push_back(u);
  88. }
  89. dfs(1, 1);
  90. for(int i = 2; i <= n; ++i) {
  91. que.push(node[i]);
  92. }
  93. while(!que.empty()) {
  94. Node tmp = que.top();
  95. que.pop();
  96. if(node[tmp.Index] != tmp || tmp.Index == 1) {
  97. continue;
  98. } int f = Find(tmp.fa);
  99. unit(tmp.Index, f);
  100. LL a = -min(-node[f].a, -node[f].a + node[f].b - tmp.a);
  101. LL b = -node[f].a + node[f].b - tmp.a + tmp.b + a;
  102. node[f].a = a;
  103. node[f].b = b;
  104. que.push(node[f]);
  105. }
  106. printf("%I64d\n", node[1].a);
  107. }
  108. return 0;
  109. }

I. Random Sequence

题意

有一个长度为 的序列 和长度为 的序列 ,如果 ,则 的值可能为 之间的任意值,取得任意值的概率为 ,求公式:

的期望值。

输入

第一行为一个整数 ,接下去有 组数据,每组数据第一行为两个整数 ,第二行为 个整数 ,第三行为 个整数

输出

输出所求公式的期望值对 取模后的结果,若期望值无法用整数表示,则先将期望值化简为 后,输出 的值,其中 满足

样例

输入
2
6 8
4 8 8 4 6 5
10 20 30 40 50 60 70 80
4 3
0 0 0 0
3 2 4
输出
8000
3

题解

由于任意两个 之间的选择是相互独立的,所以
如果定义状态 表示到第 的所有合法情况的和,则可以通过枚举第 位的所有合法值 计算 的值,其递推公式为:

注意当 的值非 的值只能取 ,只有 的值为 才可以从 枚举所有值,对 同理。这种递推式的时间复杂度为 ,必然超时。
我们可以定义状态 表示到第 时所有合法情况下 的和,由公约数关系可以得到, 必须满足 能整除 整除 ,于是有下列递推式:
其中 时的取值限制同上, 表示 能整除 ,在 的限制下, 所有合法的状态可以减少到 种,因此时间复杂度为 ,其中 的计算可以 通过预处理将复杂度降低到
最后是 的初始值的确定:
其中 的取值限制同上, 初始值即表示取前 位为 的合法状态数,系数为 ,初始化方式为暴力,时间复杂度为
最后将 乘上总方案数对 的逆元就是答案,总方案数为 ,其中 序列中 的个数。

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cstring>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <bitset>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <iomanip>
  18. using namespace std;
  19. #define LL long long
  20. const int maxn = 101;
  21. const int MOD = 1000000007;
  22. int T, n, m, ans, cnt, inv;
  23. int a[maxn], v[maxn];
  24. int Gcd[maxn][maxn], dp[maxn][maxn][maxn][maxn];
  25. int add(int a, int b) {
  26. a += b;
  27. if(a >= MOD) {
  28. return a - MOD;
  29. }
  30. if(a < 0) {
  31. return a + MOD;
  32. }
  33. return a;
  34. }
  35. int gcd(int x, int y) {
  36. return y == 0? x: gcd(y, x % y);
  37. }
  38. void prepare_gcd() {
  39. for(int i = 1; i < maxn; ++i) {
  40. Gcd[0][i] = Gcd[i][0] = i;
  41. for(int j = 1; j < maxn; ++j) {
  42. Gcd[i][j] = gcd(i, j);
  43. }
  44. }
  45. }
  46. void Init() {
  47. for(int i = 3; i <= n; ++i) {
  48. for(int z = 1; z <= m; ++z) {
  49. for(int y = z; y <= m; y += z) {
  50. for(int x = y; x <= m; x += y) {
  51. dp[i][x][y][z] = 0;
  52. }
  53. }
  54. }
  55. }
  56. for(int x = 1; x <= m; ++x) {
  57. if(a[3] != 0 && x != a[3]) {
  58. continue;
  59. }
  60. for(int y = 1; y <= m; ++y) {
  61. if(a[2] != 0 && a[2] != y) {
  62. continue;
  63. }
  64. for(int z = 1; z <= m; ++z) {
  65. if(a[1] != 0 && z != a[1]) {
  66. continue;
  67. }
  68. ++dp[3][x][Gcd[x][y]][Gcd[Gcd[x][y]][z]];
  69. }
  70. }
  71. }
  72. }
  73. LL fast_pow(LL res, LL n) {
  74. LL ans;
  75. for(ans = 1; n != 0; n >>= 1) {
  76. if((n & 1) == 1) {
  77. ans = (ans * res) % MOD;
  78. }
  79. res = (res * res) % MOD;
  80. }
  81. return ans;
  82. }
  83. int main() {
  84. #ifdef Dmaxiya
  85. freopen("test.txt", "r", stdin);
  86. // freopen("test1.out", "w", stdout);
  87. #endif // Dmaxiya
  88. ios::sync_with_stdio(false);
  89. prepare_gcd();
  90. scanf("%d", &T);
  91. while(T--) {
  92. cnt = 0;
  93. scanf("%d%d", &n, &m);
  94. for(int i = 1; i <= n; ++i) {
  95. scanf("%d", &a[i]);
  96. if(a[i] == 0) {
  97. ++cnt;
  98. }
  99. }
  100. for(int i = 1; i <= m; ++i) {
  101. scanf("%d", &v[i]);
  102. }
  103. Init();
  104. for(int i = 3; i < n; ++i) {
  105. for(int z = 1; z <= m; ++z) {
  106. for(int y = z; y <= m; y += z) {
  107. for(int x = y; x <= m; x += y) {
  108. if(dp[i][x][y][z] == 0) {
  109. continue;
  110. }
  111. for(int xx = 1; xx <= m; ++xx) {
  112. if(a[i + 1] != 0 && a[i + 1] != xx) {
  113. continue;
  114. }
  115. dp[i + 1][xx][Gcd[xx][x]][Gcd[xx][y]] = add(dp[i + 1][xx][Gcd[xx][x]][Gcd[xx][y]], (LL)dp[i][x][y][z] * v[Gcd[xx][z]] % MOD);
  116. }
  117. }
  118. }
  119. }
  120. }
  121. ans = 0;
  122. for(int k = 1; k <= m; ++k) {
  123. for(int j = k; j <= m; j += k) {
  124. for(int i = j; i <= m; i += j) {
  125. ans = add(ans, dp[n][i][j][k]);
  126. }
  127. }
  128. }
  129. ans = (LL)ans * fast_pow(fast_pow(m, cnt), MOD - 2) % MOD;
  130. printf("%d\n", ans);
  131. }
  132. return 0;
  133. }

L. Visual Cube

题意

给定一个长方体的长宽高 ,按照样例输出长方体。

输入

第一行为一个整数 ,接下去 行每行三个整数

输出

按样例输出。

样例

输入
2
1 1 1
6 2 4
输出

题解

由于前面覆盖后面,上面覆盖下面,右边覆盖左边,所以可以依次从后往前,从下往上,从左往右输出网格。

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cstring>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <bitset>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <iomanip>
  18. using namespace std;
  19. #define LL long long
  20. const int maxn = 100 + 100;
  21. int T, a, b, c, row, col;
  22. char str[maxn][maxn];
  23. void Draw(int x, int y, bool back) {
  24. for(int i = 2 * c; i >= 0; i -= 2) {
  25. for(int j = 0; j <= 2 * a; j += 2) {
  26. str[x + i][y + j] = '+';
  27. if(!back) {
  28. str[x + i - 1][y + j] = '.';
  29. str[x + i - 1][y + j + 1] = '/';
  30. str[x + i][y + j + 1] = '.';
  31. }
  32. if(i != 2 * c) {
  33. str[x + i + 1][y + j] = '|';
  34. }
  35. if(j != 0) {
  36. str[x + i][y + j - 1] = '-';
  37. }
  38. if(i != 2 * c && j != 0) {
  39. str[x + i + 1][y + j - 1] = '.';
  40. }
  41. }
  42. }
  43. }
  44. int main() {
  45. #ifdef LOCAL
  46. freopen("test.txt", "r", stdin);
  47. // freopen("out.txt", "w", stdout);
  48. #endif // LOCAL
  49. ios::sync_with_stdio(false);
  50. scanf("%d", &T);
  51. while(T--) {
  52. scanf("%d%d%d", &a, &b, &c);
  53. row = 2 * c + 1 + 2 * b;
  54. col = 2 * a + 1 + 2 * b;
  55. for(int i = 0; i < row; ++i) {
  56. memset(str[i], '.', sizeof(char) * col);
  57. str[i][col] = '\0';
  58. }
  59. for(int i = 0; i <= 2 * b; i += 2) {
  60. int j = 2 * b - i;
  61. bool back = false;
  62. if(i == 0) {
  63. back = true;
  64. }
  65. Draw(i, j, back);
  66. }
  67. for(int i = 0; i < row; ++i) {
  68. printf("%s\n", str[i]);
  69. }
  70. }
  71. return 0;
  72. }

M. Walking Plan

题意

给一个 个节点 条边的有向图,第 条边的两个端点为 ,边的长度为 次询问,每次询问从节点 至少走过 条路径的最小距离。

输入

第一行包含一个整数 ,接下去有 组数据,每组数据第一行为两个整数 ,接下去 行每行三个整数 ,接着为一个整数 ,接下去 行每行三个整数

输出

对于每次询问,输出最短路径长度,如果无法从节点 到达 ,则输出

样例

输入
2
3 3
1 2 1
2 3 10
3 1 100
3
1 1 1
1 2 1
1 3 1
2 1
1 2 1
1
2 1 1
输出
111
1
11
-1

题解

定义 为从节点 恰好经过 步到达节点 的最短距离(即原图按输入取最小值),无法到达设为 表示从 点出发恰好经过 步到达 点的最短路径,则有递推式:

定义 表示从 恰好经过 步到达 的最短距离, 表示从 最少经过 次到达 的最短距离,这样 就可以分为 两个部分,通过枚举中转点得到答案:
第一部分可以直接从上面的递推式得到,第二部分可以在原图上令 后跑一遍 ,再将从 恰好经过 步到达 的最短距离距离 通过 转化为

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cstring>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <bitset>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <iomanip>
  18. using namespace std;
  19. #define LL long long
  20. const int Size = 51;
  21. const int maxn = 101;
  22. int n;
  23. struct Matrix {
  24. int num[Size][Size];
  25. void Init() {
  26. for(int i = 1; i <= n; ++i) {
  27. memset(num[i], 0x3f, sizeof(int) * (n + 1));
  28. }
  29. }
  30. void Set_zero() {
  31. for(int i = 1; i <= n; ++i) {
  32. num[i][i] = 0;
  33. }
  34. }
  35. void operator=(const Matrix &m) {
  36. for(int i = 1; i <= n; ++i) {
  37. memcpy(num[i], m.num[i], sizeof(int) * (n + 1));
  38. }
  39. }
  40. void Combine(const Matrix &m, Matrix &ans) {
  41. Matrix ret;
  42. ret.Init();
  43. for(int i = 1; i <= n; ++i) {
  44. for(int j = 1; j <= n; ++j) {
  45. for(int k = 1; k <= n; ++k) {
  46. ret.num[i][j] = min(ret.num[i][j], num[i][k] + m.num[k][j]);
  47. }
  48. }
  49. }
  50. ans = ret;
  51. }
  52. void floyd() {
  53. for(int k = 1; k <= n; ++k) {
  54. for(int i = 1; i <= n; ++i) {
  55. for(int j = 1; j <= n; ++j) {
  56. num[i][j] = min(num[i][j], num[i][k] + num[k][j]);
  57. }
  58. }
  59. }
  60. }
  61. };
  62. int T, m, u, v, dis, q, k, INF, ans;
  63. Matrix A[maxn], B[maxn], G;
  64. int main() {
  65. #ifdef LOCAL
  66. freopen("test.txt", "r", stdin);
  67. // freopen("test1.out", "w", stdout);
  68. #endif // LOCAL
  69. ios::sync_with_stdio(false);
  70. memset(&INF, 0x3f, sizeof(int));
  71. scanf("%d", &T);
  72. while(T--) {
  73. scanf("%d%d", &n, &m);
  74. G.Init();
  75. for(int i = 0; i < m; ++i) {
  76. scanf("%d%d%d", &u, &v, &dis);
  77. G.num[u][v] = min(G.num[u][v], dis);
  78. }
  79. A[0].Init();
  80. A[0].Set_zero();
  81. B[0].Init();
  82. B[0].Set_zero();
  83. for(int i = 1; i < maxn; ++i) {
  84. B[i - 1].Combine(G, B[i]);
  85. }
  86. for(int i = 1; i < maxn; ++i) {
  87. A[i - 1].Combine(B[100], A[i]);
  88. }
  89. G.Set_zero();
  90. G.floyd();
  91. for(int i = 0; i < maxn; ++i) {
  92. G.Combine(B[i], B[i]);
  93. }
  94. scanf("%d", &q);
  95. while(q--) {
  96. scanf("%d%d%d", &u, &v, &k);
  97. ans = INF;
  98. for(int i = 1; i <= n; ++i) {
  99. ans = min(ans, A[k / 100].num[u][i] + B[k % 100].num[i][v]);
  100. }
  101. if(ans == INF) {
  102. printf("-1\n");
  103. } else {
  104. printf("%d\n", ans);
  105. }
  106. }
  107. }
  108. return 0;
  109. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注