[关闭]
@Dmaxiya 2018-08-17T02:14:45.000000Z 字数 3174 阅读 827

Codeforces Round #475 (Div. 1)

Codeforces


Contests 链接:Codeforces Round #475 (Div. 1)
过题数:2
排名:306/1883

A. Alternating Sum

题意

给定 的值,以及 项系数 ,所有系数不是 就是 ,这 项系数是以 为循环节的,且 ,给定前 项系数,求公式 的值。

输入

第一行为 个整数 ,第二行包含一个长度为 的字符串,字符串只包含 +- 这两种字符,表示系数的正负。

输出

输出计算结果。

样例

输入
2 2 3 3
+-+
输出
7
提示
输入
4 1 5 1
-
输出
999999228
提示

题解

只观察每 个数字,会发现这这些项是以 为公比的等差数列,套上等比数列求和公式就可以得到答案,注意公比为 的情况。

过题代码

  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 <functional>
  15. #include <algorithm>
  16. using namespace std;
  17. #define LL long long
  18. const LL MOD = 1000000009;
  19. const int maxn = 100000 + 100;
  20. LL n, a, b, k;
  21. char str[maxn];
  22. LL fast_pow(LL res, LL n) {
  23. LL ans;
  24. for(ans = 1; n != 0; n >>= 1) {
  25. if(n % 2 == 1) {
  26. ans = (ans * res) % MOD;
  27. }
  28. res = (res * res) % MOD;
  29. }
  30. return ans;
  31. }
  32. void exgcd(LL a, LL b, LL &x, LL &y) {
  33. if(b == 0) {
  34. x = 1;
  35. y = 0;
  36. return ;
  37. }
  38. exgcd(b, a % b, y, x);
  39. y -= x * (a / b);
  40. }
  41. LL inv(LL x) {
  42. LL xx, yy;
  43. exgcd(x, MOD, xx, yy);
  44. xx = ((xx % MOD) + MOD) % MOD;
  45. return xx;
  46. }
  47. int main() {
  48. #ifdef LOCAL
  49. freopen("test.txt", "r", stdin);
  50. #endif // LOCAL
  51. while(scanf("%I64d%I64d%I64d%I64d", &n, &a, &b, &k) != EOF) {
  52. if(k >= n + 1) {
  53. k = n + 1;
  54. }
  55. scanf("%s", str);
  56. LL tmp = 0;
  57. LL inva = inv(a);
  58. LL invb = inv(b);
  59. LL aa = fast_pow(a, n + 1);
  60. LL bb = invb;
  61. for(LL i = 0; i < k; ++i) {
  62. aa = (aa * inva) % MOD;
  63. bb = (bb * b) % MOD;
  64. if(str[i] == '+') {
  65. tmp += (aa * bb) % MOD;
  66. } else {
  67. tmp -= (aa * bb) % MOD;
  68. }
  69. tmp = ((tmp % MOD) + MOD) % MOD;
  70. }
  71. LL q = fast_pow((inva * b) % MOD, k);
  72. LL nn = (n + 1) / k;
  73. if(q == 1) {
  74. tmp = (nn * tmp) % MOD;
  75. } else {
  76. LL fenzi = (((1 - fast_pow(q, nn)) % MOD) + MOD) % MOD;
  77. LL fenmu = ((1 - q) % MOD + MOD) % MOD;
  78. fenzi = (fenzi * inv(fenmu)) % MOD;
  79. tmp = (tmp * fenzi) % MOD;
  80. }
  81. printf("%I64d\n", tmp);
  82. }
  83. return 0;
  84. }

B. Destruction of a Tree

题意

给定一个 个节点的树,每次可以从树上选择一个度为偶数的节点进行删除,每删除一个节点,与这个节点相连的所有边都被删除,问能否删除整棵树。

输入

第一行为一个整数 ,第二行为 个整数 ,如果 ,表示节点 之间有一条连边,数据保证给定的结构是一棵树。

输出

如果无法删除整棵树,输出 ,否则在第一行输出 ,接下去 行输出 个整数,表示删除节点的顺序。

样例

输入
5
0 1 2 1 2
输出
YES
1
2
3
5
4
提示
删点过程如下:

输入
4
0 1 2 3
输出
NO

题解

从叶子节点往树根 ,只要发现某个节点的度为偶数,就立即删除这个节点并往下 ,最后注意剪枝。

过题代码

  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 <functional>
  15. #include <algorithm>
  16. using namespace std;
  17. #define LL long long
  18. const int maxn = 200000 + 100;
  19. int n, p, cnt;
  20. int ans[maxn], deg[maxn];
  21. bool vis[maxn];
  22. vector<int> G[maxn];
  23. queue<int> que;
  24. void dfs(int f, int x, bool flag) {
  25. int len = G[x].size();
  26. if(!flag) {
  27. for(int i = 0; i < len; ++i) {
  28. int pos = G[x][i];
  29. if(!vis[pos] && pos != f) {
  30. dfs(x, pos, false);
  31. }
  32. }
  33. }
  34. if(deg[x] % 2 == 0) {
  35. deg[x] = 0;
  36. ans[cnt++] = x;
  37. vis[x] = true;
  38. for(int i = 0; i < len; ++i) {
  39. int pos = G[x][i];
  40. if(!vis[pos]) {
  41. --deg[pos];
  42. if(pos != f) {
  43. dfs(x, pos, true);
  44. }
  45. }
  46. }
  47. }
  48. }
  49. int main() {
  50. #ifdef LOCAL
  51. freopen("test.txt", "r", stdin);
  52. #endif // LOCAL
  53. while(scanf("%d", &n) != EOF) {
  54. cnt = 0;
  55. for(int i = 1; i <= n; ++i) {
  56. G[i].clear();
  57. deg[i] = 0;
  58. vis[i] = false;
  59. }
  60. for(int i = 1; i <= n; ++i) {
  61. scanf("%d", &p);
  62. if(p != 0) {
  63. G[i].push_back(p);
  64. G[p].push_back(i);
  65. ++deg[i];
  66. ++deg[p];
  67. }
  68. }
  69. dfs(1, 1, false);
  70. if(cnt != n) {
  71. printf("NO\n");
  72. continue;
  73. }
  74. printf("YES\n");
  75. for(int i = 0; i < cnt; ++i) {
  76. printf("%d\n", ans[i]);
  77. }
  78. }
  79. return 0;
  80. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注