记录编号 569953 评测结果 AAAAAAA
题目名称 [USACO 2.3.3]零数列 最终得分 100
用户昵称 Gravatarlihaoze 是否通过 通过
代码语言 C++ 运行时间 0.000 s
提交时间 2022-03-19 22:31:24 内存使用 0.00 MiB
显示代码纯文本
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <vector>
  6. #include <string>
  7. #define OPEN(_x) freopen(#_x".in", "r", stdin); freopen(#_x".out", "w", stdout)
  8. #define MAX(_a, _b) [&](int __a, int __b) { return __a < __b ? __b : __a; }((_a), (_b))
  9. #define MIN(_a, _b) [&](int __a, int __b) { return __a > __b ? __b : __a; }((_a), (_b))
  10. #define ABS(_x) [&](int __x) { return __x < 0 ? -__x : __x; }(_x)
  11. #define fi first
  12. #define se second
  13.  
  14. using ll = long long;
  15. using PII = std::pair<int, int>;
  16.  
  17. namespace IO {
  18. template <typename T> inline T read() {
  19. char ch = getchar();
  20. T ret = 0, sig = 1;
  21. while(ch < '0' || ch > '9') { if(ch == '-') sig = -1; ch = getchar(); }
  22. while(ch >= '0' && ch <= '9') ret *= 10, ret += ch - 48, ch = getchar();
  23. return ret * sig;
  24. }
  25. template <typename T> inline void write(T out) {
  26. if(!out) { putchar('0'), putchar(' '); return; }
  27. int stk[100], tt = 0;
  28. if(out < 0) out = -out, putchar('-');
  29. while(out) stk[tt++] = out % 10, out /= 10;
  30. for(register int i = --tt; i>=0; --i) putchar(stk[i] + 48);
  31. putchar(' ');
  32. }
  33. template <typename T> inline void read(T& ret) { ret = IO::read<T>(); }
  34. template <typename T, typename... Args> inline void read(T& x, Args&... args) { IO::read(x), IO::read(args...); }
  35. template <typename T, typename... Args> inline void write(T x, Args... args) { IO::write(x), IO::write(args...); }
  36. };
  37.  
  38.  
  39. int n;
  40. std::vector<char> chosen;
  41. std::vector<std::string> ans;
  42.  
  43. inline bool check() {
  44. int now = 1, x = 0;
  45. char sig = '+';
  46. for (register int i = 0; i<n; ++i) {
  47. if(chosen[i] != ' ') x += now * (sig == '+' ? 1 : -1), now = i + 2, sig = chosen[i];
  48. else now = now * 10 + i + 2;
  49. }
  50. return !x;
  51. }
  52.  
  53. void dfs(int a) {
  54. if(a == n) {
  55. if(check()) {
  56. std::string str;
  57. str += 49;
  58. int cnt = 2;
  59. for (auto i : chosen) {
  60. str += i, str += (cnt ++) + 48;
  61. }
  62. ans.emplace_back(str);
  63. }
  64. return;
  65. }
  66.  
  67. chosen.emplace_back('+');
  68. dfs(a + 1);
  69. chosen.pop_back();
  70.  
  71. chosen.emplace_back('-');
  72. dfs(a + 1);
  73. chosen.pop_back();
  74.  
  75. chosen.emplace_back(' ');
  76. dfs(a + 1);
  77. chosen.pop_back();
  78. }
  79.  
  80. int main() {
  81. #ifdef DEBUG
  82. OPEN(test);
  83. #endif
  84. OPEN(zerosum);
  85. IO::read(n);
  86. dfs(1);
  87. std::sort(ans.begin(), ans.end());
  88. for (auto i : ans) puts(i.c_str());
  89. return 0;
  90. }