[关闭]
@qq290637843 2020-11-24T20:30:02.000000Z 字数 16541 阅读 246

2020杭电第六场

A、Road To The 3rd Building 6827

  1. #include <bits/stdc++.h>
  2. std::stringstream iss;
  3. std::stringstream oss;
  4. std::string instr;
  5. using ul = std::uint32_t;
  6. using li = std::int32_t;
  7. using ull = std::uint64_t;
  8. using ll = std::int64_t;
  9. const ul mod = 1e9 + 7;
  10. ul plus(ul a, ul b)
  11. {
  12. return a + b < mod ? a + b : a + b - mod;
  13. }
  14. ul minus(ul a, ul b)
  15. {
  16. return a < b ? a + mod - b : a - b;
  17. }
  18. ul mul(ul a, ul b)
  19. {
  20. return ull(a) * ull(b) % mod;
  21. }
  22. ul T;
  23. ul n;
  24. const ul maxn = 2e5;
  25. ul inv[maxn + 2];
  26. ul H[maxn + 2];
  27. int main()
  28. {
  29. std::ios::sync_with_stdio(false);
  30. std::cin.tie(0);
  31. iss.tie(0);
  32. oss.tie(0);
  33. std::getline(std::cin, instr, char(0));
  34. iss.str(instr);
  35. inv[1] = 1;
  36. H[1] = 1;
  37. for (ul i = 2; i <= maxn + 1; ++i) {
  38. inv[i] = minus(0, mul(mod / i, inv[mod % i]));
  39. H[i] = plus(H[i - 1], inv[i]);
  40. }
  41. iss >> T;
  42. for (ul Case = 1; Case <= T; ++Case) {
  43. iss >> n;
  44. ul ans = 0;
  45. for (ul k = 1; k <= n; ++k) {
  46. ul t;
  47. iss >> t;
  48. ul alpha = 0;
  49. alpha = plus(alpha, mul(n + 1, H[n]));
  50. alpha = minus(alpha, mul(k, H[k]));
  51. alpha = minus(alpha, mul(n + 1 - k, H[n - k]));
  52. ans = plus(ans, mul(alpha, t));
  53. }
  54. ans = mul(mul(ans, mul(inv[n + 1], inv[n])), 2);
  55. oss << ans << '\n';
  56. }
  57. std::cout << oss.str();
  58. return 0;
  59. }

B、Little Rabbit's Equation 6828

简单题

  1. #include <bits/stdc++.h>
  2. std::stringstream iss;
  3. std::stringstream oss;
  4. std::string instr;
  5. using ul = std::uint32_t;
  6. using li = std::int32_t;
  7. using ull = std::uint64_t;
  8. using ll = std::int64_t;
  9. std::string s;
  10. std::vector<ul> a, b, c;
  11. const ul base = 998244353;
  12. ul plus(ul a, ul b)
  13. {
  14. return a + b < base ? a + b : a + b - base;
  15. }
  16. ul minus(ul a, ul b)
  17. {
  18. return a < b ? a + base - b : a - b;
  19. }
  20. ul mul(ul a, ul b)
  21. {
  22. return ull(a) * ull(b) % base;
  23. }
  24. void exgcd(li a, li b, li& x, li& y)
  25. {
  26. if (b) {
  27. exgcd(b, a % b, y, x);
  28. y -= a / b * x;
  29. } else {
  30. x = 1;
  31. y = 0;
  32. }
  33. }
  34. ul inverse(ul a)
  35. {
  36. li x, y;
  37. exgcd(a, base, x, y);
  38. return x < 0 ? x + li(base) : x;
  39. }
  40. bool check(char x)
  41. {
  42. return '0' <= x && x <= '9' || 'A' <= x && x <= 'F';
  43. }
  44. ul encode[256];
  45. bool check(ul r, char op)
  46. {
  47. ul aa = 0, bb = 0, cc = 0;
  48. for (auto t : a) {
  49. if (t >= r) {
  50. return false;
  51. }
  52. aa = mul(aa, r);
  53. aa = plus(aa, t);
  54. }
  55. for (auto t : b) {
  56. if (t >= r) {
  57. return false;
  58. }
  59. bb = mul(bb, r);
  60. bb = plus(bb, t);
  61. }
  62. for (auto t : c) {
  63. if (t >= r) {
  64. return false;
  65. }
  66. cc = mul(cc, r);
  67. cc = plus(cc, t);
  68. }
  69. if (op == '+') {
  70. return plus(aa, bb) == cc;
  71. } else if (op == '-') {
  72. return minus(aa, bb) == cc;
  73. } else if (op == '*') {
  74. return mul(aa, bb) == cc;
  75. } else {
  76. return mul(aa, inverse(bb)) == cc;
  77. }
  78. }
  79. int main()
  80. {
  81. std::ios::sync_with_stdio(false);
  82. std::cin.tie(0);
  83. iss.tie(0);
  84. oss.tie(0);
  85. std::getline(std::cin, instr, char(0));
  86. iss.str(instr);
  87. for (ul i = '0'; i <= '9'; ++i) {
  88. encode[i] = i - '0';
  89. }
  90. for (ul i = 'A'; i <= 'F'; ++i) {
  91. encode[i] = i - 'A' + 10;
  92. }
  93. while (iss >> s) {
  94. a.resize(0);
  95. b.resize(0);
  96. c.resize(0);
  97. ul i;
  98. for (i = 0; i != s.size() && check(s[i]); ++i) {
  99. a.push_back(encode[s[i]]);
  100. }
  101. char op = s[i];
  102. for (++i; i != s.size() && check(s[i]); ++i) {
  103. b.push_back(encode[s[i]]);
  104. }
  105. for (++i; i != s.size() && check(s[i]); ++i) {
  106. c.push_back(encode[s[i]]);
  107. }
  108. bool flag = false;
  109. for (ul r = 2; r <= 16; ++r) {
  110. if (check(r, op)) {
  111. oss << r << '\n';
  112. flag = true;
  113. break;
  114. }
  115. }
  116. if (!flag) {
  117. oss << "-1\n";
  118. }
  119. }
  120. std::cout << oss.str();
  121. return 0;
  122. }

C、Borrow 6829

官方题解如下:
image.png-65.1kB
image.png-68kB

  1. #include <bits/stdc++.h>
  2. std::stringstream iss;
  3. std::stringstream oss;
  4. std::string instr;
  5. using ul = std::uint32_t;
  6. using li = std::int32_t;
  7. using ull = std::uint64_t;
  8. using ll = std::int64_t;
  9. const ul base = 998244353;
  10. ul plus(ul a, ul b)
  11. {
  12. return a + b < base ? a + b : a + b - base;
  13. }
  14. ul minus(ul a, ul b)
  15. {
  16. return a < b ? a + base - b : a - b;
  17. }
  18. ul mul(ul a, ul b)
  19. {
  20. return ull(a) * ull(b) % base;
  21. }
  22. ul T;
  23. const ul maxn = 1e6;
  24. ul fac[maxn + 1];
  25. ul fiv[maxn + 1];
  26. ul inv[maxn + 1];
  27. ul twopowinv[maxn + 1];
  28. ul x, y, z;
  29. ul sbi(ul a, ul b)
  30. {
  31. return mul(fac[a + b], mul(fiv[a], fiv[b]));
  32. }
  33. int main()
  34. {
  35. std::ios::sync_with_stdio(false);
  36. std::cin.tie(0);
  37. iss.tie(0);
  38. oss.tie(0);
  39. std::getline(std::cin, instr, char(0));
  40. iss.str(instr);
  41. fac[0] = 1;
  42. for (ul i = 1; i <= maxn; ++i) {
  43. fac[i] = mul(fac[i - 1], i);
  44. }
  45. inv[1] = 1;
  46. fiv[0] = fiv[1] = 1;
  47. for (ul i = 2; i <= maxn; ++i) {
  48. inv[i] = base - mul(base / i, inv[base % i]);
  49. fiv[i] = mul(fiv[i - 1], inv[i]);
  50. }
  51. twopowinv[0] = 1;
  52. for (ul i = 1; i <= maxn; ++i) {
  53. twopowinv[i] = mul(twopowinv[i - 1], inv[2]);
  54. }
  55. iss >> T;
  56. for (ul Case = 1; Case <= T; ++Case) {
  57. iss >> x >> y >> z;
  58. if (x < z) {
  59. std::swap(x, z);
  60. }
  61. if (x < y) {
  62. std::swap(x, y);
  63. }
  64. ul a = x - y, b = x - z;
  65. if ((a + b) % 3 != 0) {
  66. oss << "-1\n";
  67. continue;
  68. }
  69. if (a >= b + b) {
  70. oss << (a + a - b) / 3 * 2 << '\n';
  71. continue;
  72. }
  73. if (b >= a + a) {
  74. oss << (b + b - a) / 3 * 2 << '\n';
  75. continue;
  76. }
  77. ul na = (a + a - b) / 3, nb = (b + b - a) / 3;
  78. a = na;
  79. b = nb;
  80. ul ans = 0;
  81. for (ul s = 1; s <= a; ++s) {
  82. ans = plus(ans, mul(mul(s + a + b, sbi(a - s, b - 1)), twopowinv[a + b - s]));
  83. }
  84. for (ul t = 1; t <= b; ++t) {
  85. ans = plus(ans, mul(mul(t + a + b, sbi(a - 1, b - t)), twopowinv[a + b - t]));
  86. }
  87. oss << ans << '\n';
  88. }
  89. std::cout << oss.str();
  90. return 0;
  91. }

D、Asteroid in Love 6830

简单题

  1. #include <bits/stdc++.h>
  2. std::stringstream iss;
  3. std::stringstream oss;
  4. std::string instr;
  5. using ul = std::uint32_t;
  6. using li = std::int32_t;
  7. using ull = std::uint64_t;
  8. using ll = std::int64_t;
  9. class vec {
  10. public:
  11. ll x = 0;
  12. ll y = 0;
  13. vec()=default;
  14. vec(ll a, ll b): x(a), y(b) {}
  15. };
  16. vec operator+(const vec& a, const vec& b)
  17. {
  18. return vec(a.x + b.x, a.y + b.y);
  19. }
  20. vec operator-(const vec& a, const vec& b)
  21. {
  22. return vec(a.x - b.x, a.y - b.y);
  23. }
  24. ll operator*(const vec& a, const vec& b)
  25. {
  26. return a.x * b.x + a.y * b.y;
  27. }
  28. ll operator^(const vec& a, const vec& b)
  29. {
  30. return a.x * b.y - a.y * b.x;
  31. }
  32. ul T;
  33. ul n;
  34. std::vector<vec> star[3];
  35. std::vector<vec> buttom;
  36. std::vector<vec> top;
  37. ll findans(const std::vector<vec>& v, const vec& a, const vec& b)
  38. {
  39. vec dir = b - a;
  40. ul l = 0, r = v.size();
  41. while (l + 1 != r) {
  42. ul mid = l + r >> 1;
  43. ul midl = mid - 1;
  44. if ((dir ^ v[mid]) >= (dir ^ v[midl])) {
  45. l = mid;
  46. } else {
  47. r = mid;
  48. }
  49. }
  50. return dir ^ (v[l] - a);
  51. }
  52. int main()
  53. {
  54. std::ios::sync_with_stdio(false);
  55. std::cin.tie(0);
  56. iss.tie(0);
  57. oss.tie(0);
  58. std::getline(std::cin, instr, char(0));
  59. iss.str(instr);
  60. iss >> T;
  61. for (ul Case = 1; Case <= T; ++Case) {
  62. star[0].resize(0);
  63. star[1].resize(0);
  64. star[2].resize(0);
  65. iss >> n;
  66. for (ul i = 1; i <= n; ++i) {
  67. vec tmp;
  68. ul c;
  69. iss >> tmp.x >> tmp.y >> c;
  70. star[c].push_back(tmp);
  71. }
  72. for (ul c = 0; c <= 2; ++c) {
  73. if (star[c].size() == 1) {
  74. continue;
  75. }
  76. std::sort(star[c].begin(), star[c].end(), [](const vec& a, const vec& b){return a.x != b.x ? a.x < b.x : a.y < b.y;});
  77. buttom.resize(0);
  78. for (const auto& curr : star[c]) {
  79. while (buttom.size() >= 2 && (buttom.back() - buttom[buttom.size() - 2] ^ curr - buttom.back()) <= 0) {
  80. buttom.pop_back();
  81. }
  82. buttom.push_back(curr);
  83. }
  84. std::reverse(star[c].begin(), star[c].end());
  85. top.resize(0);
  86. for (const auto& curr : star[c]) {
  87. while (top.size() >= 2 && (top.back() - top[top.size() - 2] ^ curr - top.back()) <= 0) {
  88. top.pop_back();
  89. }
  90. top.push_back(curr);
  91. }
  92. star[c].resize(0);
  93. for (const auto& curr : buttom) {
  94. star[c].push_back(curr);
  95. }
  96. star[c].pop_back();
  97. for (const auto& curr : top) {
  98. star[c].push_back(curr);
  99. }
  100. star[c].pop_back();
  101. }
  102. if (star[0].size() < star[1].size()) {
  103. std::swap(star[0], star[1]);
  104. }
  105. if (star[0].size() < star[2].size()) {
  106. std::swap(star[0], star[2]);
  107. }
  108. std::sort(star[0].begin(), star[0].end(), [](const vec& a, const vec& b){return a.x != b.x ? a.x < b.x : a.y < b.y;});
  109. buttom.resize(0);
  110. for (const auto& curr : star[0]) {
  111. while (buttom.size() >= 2 && (buttom.back() - buttom[buttom.size() - 2] ^ curr - buttom.back()) <= 0) {
  112. buttom.pop_back();
  113. }
  114. buttom.push_back(curr);
  115. }
  116. std::reverse(star[0].begin(), star[0].end());
  117. top.resize(0);
  118. for (const auto& curr : star[0]) {
  119. while (top.size() >= 2 && (top.back() - top[top.size() - 2] ^ curr - top.back()) <= 0) {
  120. top.pop_back();
  121. }
  122. top.push_back(curr);
  123. }
  124. ll ans = 0;
  125. for (const auto& a : star[1]) {
  126. for (const auto& b : star[2]) {
  127. if (a.x > b.x) {
  128. ans = std::max(ans, findans(buttom, a, b));
  129. ans = std::max(ans, findans(top, b, a));
  130. } else {
  131. ans = std::max(ans, findans(top, a, b));
  132. ans = std::max(ans, findans(buttom, b, a));
  133. }
  134. }
  135. }
  136. oss << (ans >> 1) << ((ans & 1) ? ".5\n" : ".0\n");
  137. }
  138. std::cout << oss.str();
  139. return 0;
  140. }

E、Fragrant numbers 6831

简单题

  1. #include <bits/stdc++.h>
  2. std::stringstream iss;
  3. std::stringstream oss;
  4. std::string instr;
  5. using ul = std::uint32_t;
  6. using li = std::int32_t;
  7. using ull = std::uint64_t;
  8. using ll = std::int64_t;
  9. const ul maxlen = 11;
  10. std::string s = " 1145141919114514191911451419191145141919114514191911451419191145141919114514191911451419191145141919";
  11. std::vector<ul> ans[maxlen + 1][maxlen + 1];
  12. li out[5001];
  13. ul T;
  14. ul n;
  15. ul cnt;
  16. int main()
  17. {
  18. std::ios::sync_with_stdio(false);
  19. std::cin.tie(0);
  20. iss.tie(0);
  21. oss.tie(0);
  22. std::getline(std::cin, instr, char(0));
  23. iss.str(instr);
  24. for (ul len = 1; len <= maxlen; ++len) {
  25. for (ul l = 1, r = l + len - 1; r <= maxlen; ++l, ++r) {
  26. if (len <= 4) {
  27. ul t = 0;
  28. for (ul i = l; i <= r; ++i) {
  29. t *= 10;
  30. t += s[i] - '0';
  31. }
  32. if (t <= 5000) {
  33. ans[l][r].push_back(t);
  34. }
  35. }
  36. for (ul ml = l, mr = l + 1; mr <= r; ++ml, ++mr) {
  37. for (ul a : ans[l][ml]) {
  38. for (ul b : ans[mr][r]) {
  39. if (a + b <= 5000) {
  40. ans[l][r].push_back(a + b);
  41. }
  42. if (a * b <= 5000) {
  43. ans[l][r].push_back(a * b);
  44. }
  45. }
  46. }
  47. std::sort(ans[l][r].begin(), ans[l][r].end());
  48. ans[l][r].resize(std::unique(ans[l][r].begin(), ans[l][r].end()) - ans[l][r].begin());
  49. }
  50. if (l == 1) {
  51. for (auto t : ans[l][r]) {
  52. if (!out[t]) {
  53. out[t] = r;
  54. ++cnt;
  55. }
  56. }
  57. }
  58. }
  59. }
  60. out[3] = out[7] = -1;
  61. iss >> T;
  62. for (ul Case = 1; Case <= T; ++Case) {
  63. iss >> n;
  64. oss << out[n] << '\n';
  65. }
  66. std::cout << oss.str();
  67. return 0;
  68. }

F、A Very Easy Graph Problem 6832

简单题

  1. #include <bits/stdc++.h>
  2. std::stringstream iss;
  3. std::stringstream oss;
  4. std::string instr;
  5. using ul = std::uint32_t;
  6. using li = std::int32_t;
  7. using ull = std::uint64_t;
  8. using ll = std::int64_t;
  9. const ul mod = 1e9 + 7;
  10. ul plus(ul a, ul b)
  11. {
  12. return a + b < mod ? a + b : a + b - mod;
  13. }
  14. ul mul(ul a, ul b)
  15. {
  16. return ull(a) * ull(b) % mod;
  17. }
  18. ul T;
  19. ul n, m;
  20. const ul maxn = 1e5;
  21. ul fa[maxn + 1];
  22. ul rk[maxn + 1];
  23. ul cnt[maxn + 1][3];
  24. ul al[2];
  25. std::vector<std::pair<ul, ul>> edge[maxn + 1];
  26. ul getfather(ul x)
  27. {
  28. return x == fa[x] ? x : fa[x] = getfather(fa[x]);
  29. }
  30. bool connect(ul x, ul y)
  31. {
  32. x = getfather(x);
  33. y = getfather(y);
  34. if (x == y) {
  35. return false;
  36. }
  37. if (rk[x] > rk[y]) {
  38. fa[y] = x;
  39. } else if (rk[x] < rk[y]) {
  40. fa[x] = y;
  41. } else {
  42. fa[y] = x;
  43. ++rk[x];
  44. }
  45. return true;
  46. }
  47. ul ans;
  48. void search(ul curr, ul prev)
  49. {
  50. for (const auto& e : edge[curr]) {
  51. ul next = e.first;
  52. ul v = e.second;
  53. if (next == prev) {
  54. continue;
  55. }
  56. search(next, curr);
  57. ans = plus(ans, mul(v, mul(cnt[next][0], al[1] - cnt[next][4])));
  58. ans = plus(ans, mul(v, mul(cnt[next][5], al[0] - cnt[next][0])));
  59. cnt[curr][0] += cnt[next][0];
  60. cnt[curr][6] += cnt[next][7];
  61. }
  62. }
  63. int main()
  64. {
  65. std::ios::sync_with_stdio(false);
  66. std::cin.tie(0);
  67. iss.tie(0);
  68. oss.tie(0);
  69. std::getline(std::cin, instr, char(0));
  70. iss.str(instr);
  71. iss >> T;
  72. for (ul Case = 1; Case <= T; ++Case) {
  73. al[0] = al[1] = 0;
  74. iss >> n >> m;
  75. for (ul i = 1; i <= n; ++i) {
  76. fa[i] = i;
  77. rk[i] = 0;
  78. cnt[i][0] = cnt[i][8] = 0;
  79. ul a;
  80. iss >> a;
  81. cnt[i][a] = 1;
  82. ++al[a];
  83. edge[i].resize(0);
  84. }
  85. ans = 0;
  86. for (ul i = 1, t = 2; i <= m; ++i, t = plus(t, t)) {
  87. ul u, v;
  88. iss >> u >> v;
  89. if (connect(u, v)) {
  90. edge[u].push_back(std::pair<ul, ul>(v, t));
  91. edge[v].push_back(std::pair<ul, ul>(u, t));
  92. }
  93. }
  94. search(1, 0);
  95. oss << ans << '\n';
  96. }
  97. std::cout << oss.str();
  98. return 0;
  99. }

G、A Very Easy Math Problem 6833

  1. #include <bits/stdc++.h>
  2. std::stringstream iss;
  3. std::stringstream oss;
  4. std::string instr;
  5. using ul = std::uint32_t;
  6. using li = std::int32_t;
  7. using ull = std::uint64_t;
  8. using ll = std::int64_t;
  9. const ul base = 1e9 + 7;
  10. ul plus(ul a, ul b)
  11. {
  12. return a + b < base ? a + b : a + b - base;
  13. }
  14. ul minus(ul a, ul b)
  15. {
  16. return a < b ? a + base - b : a - b;
  17. }
  18. ul mul(ul a, ul b)
  19. {
  20. return ull(a) * ull(b) % base;
  21. }
  22. ul mpow(ul a, ul b)
  23. {
  24. ul ret = 1;
  25. while (b) {
  26. if (b & 1) {
  27. ret = mul(a, ret);
  28. }
  29. a = mul(a, a);
  30. b >>= 1;
  31. }
  32. return ret;
  33. }
  34. ul T;
  35. ul n, x, k;
  36. const ul maxn = 2e5;
  37. ul mu[maxn + 1];
  38. ul mu2[maxn + 1];
  39. ul smuixk[maxn + 1];
  40. ul smu2ixk[maxn + 1];
  41. ul sik[maxn + 1];
  42. ul sik_x[maxn + 1];
  43. bool notprime[maxn + 1];
  44. std::vector<ul> prime;
  45. ul ans;
  46. ul ans2[maxn + 1];
  47. bool al[maxn + 1];
  48. int main()
  49. {
  50. std::ios::sync_with_stdio(false);
  51. std::cin.tie(0);
  52. iss.tie(0);
  53. oss.tie(0);
  54. std::getline(std::cin, instr, char(0));
  55. iss.str(instr);
  56. iss >> T >> k >> x;
  57. mu[1] = 1;
  58. mu2[1] = 1;
  59. smuixk[1] = 1;
  60. smu2ixk[1] = 1;
  61. for (ul i = 2; i <= maxn; ++i) {
  62. if (!notprime[i]) {
  63. prime.push_back(i);
  64. mu[i] = minus(0, 1);
  65. mu2[i] = 1;
  66. }
  67. smuixk[i] = plus(mul(mu[i], mpow(mpow(i, x), k)), smuixk[i - 1]);
  68. smu2ixk[i] = plus(mul(mul(mu2[i], mpow(mpow(i, x), k)), i), smu2ixk[i - 1]);
  69. for (ul p : prime) {
  70. if (i * p > maxn) {
  71. break;
  72. }
  73. notprime[i * p] = true;
  74. if (i % p == 0) {
  75. mu[i * p] = 0;
  76. mu2[i * p] = 0;
  77. break;
  78. } else {
  79. mu[i * p] = minus(0, mu[i]);
  80. mu2[i * p] = mu2[i];
  81. }
  82. }
  83. }
  84. for (ul i = 1; i <= maxn; ++i) {
  85. ul ik = mpow(i, k);
  86. sik[i] = plus(sik[i - 1], ik);
  87. sik_x[i] = mpow(sik[i], x);
  88. }
  89. for (ul Case = 1; Case <= T; ++Case) {
  90. iss >> n;
  91. ul ans = 0;
  92. for (ul d = 1, dd; d <= n; d = dd + 1) {
  93. ul nqd = n / d;
  94. dd = n / nqd;
  95. if (!al[nqd]) {
  96. al[nqd] = true;
  97. for (ul e = 1, ee; e <= nqd; e = ee + 1) {
  98. ul nqdqe = nqd / e;
  99. ee = nqd / nqdqe;
  100. ans2[nqd] = plus(ans2[nqd], mul(minus(smuixk[ee], smuixk[e - 1]), sik_x[nqdqe]));
  101. }
  102. }
  103. ans = plus(ans, mul(minus(smu2ixk[dd], smu2ixk[d - 1]), ans2[nqd]));
  104. }
  105. oss << ans << '\n';
  106. }
  107. std::cout << oss.str();
  108. return 0;
  109. }

H、Yukikaze and Smooth numbers 6834

I、Divisibility 6835

简单题

  1. #include <bits/stdc++.h>
  2. std::stringstream iss;
  3. std::stringstream oss;
  4. std::string instr;
  5. using ul = std::uint32_t;
  6. using li = std::int32_t;
  7. using ull = std::uint64_t;
  8. using ll = std::int64_t;
  9. ul T;
  10. ull b, x;
  11. int main()
  12. {
  13. std::ios::sync_with_stdio(false);
  14. std::cin.tie(0);
  15. iss.tie(0);
  16. oss.tie(0);
  17. std::getline(std::cin, instr, char(0));
  18. iss.str(instr);
  19. iss >> T;
  20. for (ul Case = 1; Case <= T; ++Case) {
  21. iss >> b >> x;
  22. oss << (b % x == 1 ? "T\n" : "F\n");
  23. }
  24. std::cout << oss.str();
  25. return 0;
  26. }

J、Expectation 6836

简单题 矩阵树定理证明见

  1. #include <bits/stdc++.h>
  2. std::stringstream iss;
  3. std::stringstream oss;
  4. std::string instr;
  5. using ul = std::uint32_t;
  6. using li = std::int32_t;
  7. using ull = std::uint64_t;
  8. using ll = std::int64_t;
  9. const ul base = 998244353;
  10. ul plus(ul a, ul b)
  11. {
  12. return a + b < base ? a + b : a + b - base;
  13. }
  14. ul minus(ul a, ul b)
  15. {
  16. return a < b ? a + base - b : a - b;
  17. }
  18. ul mul(ul a, ul b)
  19. {
  20. return ull(a) * ull(b) % base;
  21. }
  22. void exgcd(li a, li b, li& x, li& y)
  23. {
  24. if (b) {
  25. exgcd(b, a % b, y, x);
  26. y -= a / b * x;
  27. } else {
  28. x = 1;
  29. y = 0;
  30. }
  31. }
  32. ul inverse(ul a)
  33. {
  34. li x, y;
  35. exgcd(a, base, x, y);
  36. return x < 0 ? x + li(base) : x;
  37. }
  38. ul T;
  39. ul n, m;
  40. const ul maxn = 100;
  41. ul mat[32][maxn + 1][maxn + 1];
  42. ul smat[maxn + 1][maxn + 1];
  43. ul det(ul a[][maxn + 1], ul n)
  44. {
  45. ul ret = 1;
  46. for (ul i = 1; i <= n; ++i) {
  47. ul id;
  48. for (id = i; i <= n && !a[id][i]; ++i);
  49. if (id != i) {
  50. for (ul j = i; j <= n; ++j) {
  51. std::swap(a[i][j], a[id][j]);
  52. }
  53. ret = minus(0, ret);
  54. }
  55. ret = mul(ret, a[i][i]);
  56. for (ul j = i + 1; j <= n; ++j) {
  57. ul tmp = minus(0, mul(a[j][i], inverse(a[i][i])));
  58. for (ul k = i; k <= n; ++k) {
  59. a[j][k] = plus(a[j][k], mul(a[i][k], tmp));
  60. }
  61. }
  62. }
  63. return ret;
  64. }
  65. int main()
  66. {
  67. std::ios::sync_with_stdio(false);
  68. std::cin.tie(0);
  69. iss.tie(0);
  70. oss.tie(0);
  71. std::getline(std::cin, instr, char(0));
  72. iss.str(instr);
  73. iss >> T;
  74. for (ul Case = 1; Case <= T; ++Case) {
  75. iss >> n >> m;
  76. for (ul i = 0; i != 32; ++i) {
  77. for (ul j = 1; j <= n; ++j) {
  78. std::memset(mat[i][j] + 1, 0, sizeof(ul) * n);
  79. }
  80. }
  81. for (ul i = 1; i <= n; ++i) {
  82. std::memset(smat[i] + 1, 0, sizeof(ul) * n);
  83. }
  84. for (ul i = 1; i <= m; ++i) {
  85. ul u, v, w;
  86. iss >> u >> v >> w;
  87. smat[u][u] = plus(smat[u][u], 1);
  88. smat[v][v] = plus(smat[v][v], 1);
  89. smat[u][v] = minus(smat[u][v], 1);
  90. smat[v][u] = minus(smat[v][u], 1);
  91. for (ul j = 0; j != 32; ++j) {
  92. if (w >> j & 1) {
  93. mat[j][u][u] = plus(mat[j][u][u], 1);
  94. mat[j][v][v] = plus(mat[j][v][v], 1);
  95. mat[j][u][v] = minus(mat[j][u][v], 1);
  96. mat[j][v][u] = minus(mat[j][v][u], 1);
  97. }
  98. }
  99. }
  100. ul s = det(smat, n - 1);
  101. ul ans = 0;
  102. for (ul i = 31; ~i; --i) {
  103. ans = plus(ans, ans);
  104. ans = plus(ans, det(mat[i], n - 1));
  105. }
  106. oss << mul(ans, inverse(s)) << '\n';
  107. }
  108. std::cout << oss.str();
  109. return 0;
  110. }

K、Kirakira 6837

线段极角排序裸题

  1. #include <bits/stdc++.h>
  2. std::stringstream iss;
  3. std::stringstream oss;
  4. std::string instr;
  5. using ul = std::uint32_t;
  6. using li = std::int32_t;
  7. using ull = std::uint64_t;
  8. using ll = std::int64_t;
  9. const ul base = 31607;
  10. ul plus(ul a, ul b)
  11. {
  12. return a + b < base ? a + b : a + b - base;
  13. }
  14. ul minus(ul a, ul b)
  15. {
  16. return a < b ? a + base - b : a - b;
  17. }
  18. ul mul(ul a, ul b)
  19. {
  20. return a * b % base;
  21. }
  22. void exgcd(li a, li b, li& x, li& y)
  23. {
  24. if (b) {
  25. exgcd(b, a % b, y, x);
  26. y -= a / b * x;
  27. } else {
  28. x = 1;
  29. y = 0;
  30. }
  31. }
  32. ul inverse(ul a)
  33. {
  34. li x, y;
  35. exgcd(a, base, x, y);
  36. return x < 0 ? x + li(base) : x;
  37. }
  38. ul reg(li a)
  39. {
  40. a %= li(base);
  41. return a < 0 ? a + li(base) : a;
  42. }
  43. ul T;
  44. ul n;
  45. const ul maxn = 1e3;
  46. class vec {
  47. public:
  48. li x = 0;
  49. li y = 0;
  50. vec()=default;
  51. vec(li a, li b): x(a), y(b) {}
  52. };
  53. vec operator+(const vec& a, const vec& b)
  54. {
  55. return vec(a.x + b.x, a.y + b.y);
  56. }
  57. vec operator-(const vec& a, const vec& b)
  58. {
  59. return vec(a.x - b.x, a.y - b.y);
  60. }
  61. li operator*(const vec& a, const vec& b)
  62. {
  63. return a.x * b.x + a.y * b.y;
  64. }
  65. li operator^(const vec& a, const vec& b)
  66. {
  67. return a.x * b.y - a.y * b.x;
  68. }
  69. vec point[maxn + 1];
  70. ul p[maxn + 1];
  71. using pulul = std::pair<ul, ul>;
  72. std::vector<pulul> dirs;
  73. li sgn(li x)
  74. {
  75. return x < 0 ? -1 : (x > 0 ? 1 : 0);
  76. }
  77. ul type(const vec& v)
  78. {
  79. static const ul types[3][3] = {{3, 3, 2}, {4, 0, 2}, {4, 1, 1}};
  80. return types[sgn(v.x) + 1][sgn(v.y) + 1];
  81. }
  82. bool cmp(const vec& a, const vec& b, const vec& c, const vec& d)
  83. {
  84. vec ab = b - a;
  85. vec cd = d - c;
  86. ul tab = type(ab);
  87. ul tcd = type(cd);
  88. if (tab != tcd) {
  89. return tab < tcd;
  90. }
  91. li tmp = ab ^ cd;
  92. if (tmp != 0) {
  93. return tmp > 0;
  94. }
  95. vec ac = c - a;
  96. tmp = ab ^ ac;
  97. if (tmp != 0) {
  98. return tmp > 0;
  99. }
  100. tmp = ab * ac;
  101. if (tmp != 0) {
  102. return tmp > 0;
  103. }
  104. return ab * (d - b) > 0;
  105. }
  106. ul ord[maxn + 1];
  107. ul pos[maxn + 1];
  108. ul pre[maxn + 1];
  109. int main()
  110. {
  111. std::ios::sync_with_stdio(false);
  112. std::cin.tie(0);
  113. iss.tie(0);
  114. oss.tie(0);
  115. std::getline(std::cin, instr, char(0));
  116. iss.str(instr);
  117. pre[0] = 1;
  118. iss >> T;
  119. for (ul Case = 1; Case <= T; ++Case) {
  120. iss >> n;
  121. for (ul i = 1; i <= n; ++i) {
  122. iss >> point[i].x >> point[i].y;
  123. ul u, v;
  124. iss >> u >> v;
  125. p[i] = mul(u, inverse(v));
  126. }
  127. dirs.resize(0);
  128. for (ul i = 1; i + 1 <= n; ++i) {
  129. for (ul j = i + 1; j <= n; ++j) {
  130. dirs.push_back(pulul(i, j));
  131. dirs.push_back(pulul(j, i));
  132. }
  133. }
  134. std::sort(dirs.begin(), dirs.end(), [](const pulul& a, const pulul& b){return cmp(point[a.first], point[a.second], point[b.first], point[b.second]);});
  135. for (ul i = 1; i <= n; ++i) {
  136. ord[i] = i;
  137. }
  138. std::sort(ord + 1, ord + n + 1, [](ul a, ul b){return point[a].y != point[b].y ? point[a].y < point[b].y : point[a].x < point[b].x;});
  139. for (ul i = 1; i <= n; ++i) {
  140. pos[ord[i]] = i;
  141. pre[i] = mul(pre[i - 1], minus(1, p[ord[i]]));
  142. }
  143. ul ans = 0;
  144. for (const auto dir : dirs) {
  145. ans = plus(mul(mul(mul(p[dir.first], p[dir.second]), pre[pos[dir.first] - 1]), reg(point[dir.first] ^ point[dir.second])), ans);
  146. std::swap(pos[dir.first], pos[dir.second]);
  147. pre[pos[dir.second]] = mul(pre[pos[dir.second] - 1], minus(1, p[dir.second]));
  148. }
  149. oss << mul(ans, inverse(2)) << '\n';
  150. }
  151. std::cout << oss.str();
  152. return 0;
  153. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注