当前位置:   article > 正文

CSP20230903 梯度求解满分代码_csp梯度求解题解c++

csp梯度求解题解c++
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int mod = 1e9 + 7;
  4. typedef long long ll;
  5. struct node
  6. {
  7. char ch;
  8. ll num;
  9. int type;//0 运算 1 变量 2 表示常量
  10. int l, r;
  11. } a[2000];
  12. int n, m, cnt, dao;
  13. int b[150];
  14. string f;
  15. int judge(char ch)
  16. {
  17. return ch == '+' || ch == '-' || ch == '*';
  18. }
  19. void build()//建立表达式树
  20. {
  21. istringstream ss(f);
  22. string temp;
  23. stack<int> s;
  24. while (ss >> temp)
  25. {
  26. //cout << "分割字符为" << temp << endl;
  27. if (temp.size() == 1 && judge(temp[0]))
  28. {
  29. a[cnt].type = 0;
  30. a[cnt].ch = temp[0];
  31. a[cnt].r = s.top();
  32. s.pop();
  33. a[cnt].l = s.top();
  34. s.pop();
  35. s.push(cnt);
  36. cnt++;
  37. }
  38. else if (temp[0] == 'x')
  39. {
  40. a[cnt].type = 1;
  41. a[cnt].num = stol(temp.substr(1, temp.size()));
  42. //cout << a[cnt].num << endl;
  43. s.push(cnt);
  44. cnt++;
  45. }
  46. else//常数
  47. {
  48. a[cnt].type = 2;
  49. a[cnt].num = stol(temp);
  50. //cout << a[cnt].num << endl;
  51. s.push(cnt);
  52. cnt++;
  53. }
  54. }
  55. //cout << s.size() << " " << cnt << endl;
  56. //cout << last << endl;
  57. }
  58. ll cal(int x, bool check) //计算偏导 1 求 2 原来
  59. {
  60. //cout << "进入计算 " << x << endl;
  61. if (a[x].type == 1)
  62. {
  63. if (check)
  64. {
  65. if (dao != a[x].num)
  66. return 0;
  67. else
  68. return 1;
  69. }
  70. else
  71. return b[a[x].num];
  72. }
  73. else if (a[x].type == 2)
  74. {
  75. if (check)
  76. return 0;
  77. else
  78. return a[x].num;
  79. }
  80. else
  81. {
  82. ll l, r;
  83. if (a[x].ch == '+')
  84. {
  85. if (check)
  86. {
  87. l = cal(a[x].l, 1) % mod;
  88. r = cal(a[x].r, 1) % mod;
  89. return (l + r) % mod;
  90. }
  91. else
  92. return (cal(a[x].l, 0) % mod + cal(a[x].r, 0) % mod) % mod;
  93. }
  94. else if (a[x].ch == '-')
  95. {
  96. if (check)
  97. return cal(a[x].l, 1) % mod - cal(a[x].r, 1) % mod;
  98. else
  99. return cal(a[x].l, 0) % mod - cal(a[x].r, 0) % mod;
  100. }
  101. else
  102. {
  103. if (check)
  104. return (cal(a[x].l, 1) * cal(a[x].r, 0) % mod + cal(a[x].l, 0) * cal(a[x].r, 1) % mod) % mod;
  105. else
  106. return ((cal(a[x].l, 0) % mod ) * (cal(a[x].r, 0) % mod)) % mod;
  107. }
  108. }
  109. return 0;
  110. }
  111. int main()
  112. {
  113. ios::sync_with_stdio(false);
  114. cin.tie(0), cout.tie(0);
  115. cin >> n >> m;
  116. cin.ignore();
  117. getline(cin, f);
  118. build();
  119. // for (int i = 0; i < cnt; i++)
  120. // {
  121. // if (a[i].type == 1)
  122. // {
  123. // cout << 1 << " " << a[i].num << endl;
  124. // }
  125. // else if (a[i].type == 2)
  126. // {
  127. // cout << 2 << " " << a[i].num << endl;
  128. // }
  129. // else
  130. // {
  131. // cout << 3 << " " << a[i].ch << " " << a[i].l << " " << a[i].r << endl;
  132. // }
  133. // }
  134. for (int i = 0; i < m; i++)
  135. {
  136. cin >> dao;
  137. for (int i = 1; i <= n; i++)
  138. cin >> b[i];
  139. ll res = cal(cnt - 1, 1);
  140. cout << (res + mod) % mod << endl;
  141. }
  142. return 0;
  143. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/215708
推荐阅读
相关标签
  

闽ICP备14008679号