[关闭]
@Dmaxiya 2018-08-17T10:29:12.000000Z 字数 3195 阅读 977

Hello 2018

Codeforces


Contests 链接:Hello 2018
过题数:3
排名:1626/10360

A. Modular Exponentiation

题意

给定 ,求 的值。

数据范围

题解

由于当 超过 时就已经大于所有 了,所以当 超过这个值时,就直接输出 ,否则输出 的值。

过题代码

  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. LL n, m;
  21. LL two[60];
  22. int main() {
  23. #ifdef LOCAL
  24. freopen("test.txt", "r", stdin);
  25. // freopen("out.txt", "w", stdout);
  26. #endif // LOCAL
  27. ios::sync_with_stdio(false);
  28. two[0] = 1;
  29. for(int i = 1; i < 60; ++i) {
  30. two[i] = two[i - 1] * 2;
  31. }
  32. while(scanf("%I64d%I64d", &n, &m) != EOF) {
  33. if(n >= 60) {
  34. printf("%I64d\n", m);
  35. continue;
  36. }
  37. printf("%I64d\n", m % two[n]);
  38. }
  39. return 0;
  40. }

B. Christmas Spruce

题意

定义一种 个节点的“云杉”树,这种树上的所有非叶子节点,都至少有三个叶子节点,现在给定第 个节点的父节点 ,判断一棵树是否是“云杉”树。

数据范围

题解

一遍 dfs 判断,用个全局变量 flag 标记判断。

过题代码

  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 = 1000 + 100;
  21. int n, u;
  22. bool flag;
  23. vector<int> G[maxn];
  24. int num[maxn];
  25. void dfs(int f, int x) {
  26. int len = G[x].size();
  27. if(len == 0) {
  28. num[x] = 1;
  29. return ;
  30. }
  31. for(int i = 0; i < len; ++i) {
  32. int pos = G[x][i];
  33. if(pos != f) {
  34. dfs(x, pos);
  35. }
  36. if(num[pos] == 1) {
  37. ++num[x];
  38. }
  39. }
  40. if(num[x] < 3) {
  41. flag = false;
  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. while(scanf("%d", &n) != EOF) {
  51. flag = true;
  52. memset(num, 0, sizeof(num));
  53. for(int i = 1; i <= n; ++i) {
  54. G[i].clear();
  55. }
  56. for(int i = 2; i <= n; ++i) {
  57. scanf("%d", &u);
  58. G[u].push_back(i);
  59. }
  60. dfs(1, 1);
  61. if(flag) {
  62. printf("Yes\n");
  63. } else {
  64. printf("No\n");
  65. }
  66. }
  67. return 0;
  68. }

C. Party Lemonade

题意

新年朋友们来开 了,开 就要买柠檬汽水,现在有 种柠檬汽水,编号为 ,每种柠檬汽水有无穷多瓶,第 种柠檬汽水的容量为 升,价格为 ,问,如果至少想要买 升柠檬汽水,求最少可以花多少钱。

数据范围

题解

记录当需要购买 升汽水时,最少需要花费的钱,然后从 开始记忆化搜索一遍。

过题代码

  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 = 200;
  21. const LL INF = 1e18;
  22. LL n, L;
  23. LL ans;
  24. LL num[maxn];
  25. LL two[maxn];
  26. double per[maxn];
  27. map<LL, LL> mp;
  28. LL dfs(LL left) {
  29. if(mp.find(left) != mp.end()) {
  30. return mp[left];
  31. }
  32. if(left <= 0) {
  33. return 0;
  34. }
  35. LL Min = INF;
  36. for(int i = 0; i < n; ++i) {
  37. if(left > two[i]) {
  38. LL tmp = dfs(left % two[i]);
  39. mp[left % two[i]] = tmp;
  40. // printf("mp[%I64d] = %I64d\n", left % two[i], tmp);
  41. Min = min(Min, tmp + left / two[i] * num[i]);
  42. } else {
  43. Min = min(Min, num[i]);
  44. }
  45. }
  46. mp[left] = Min;
  47. // printf("\n");
  48. return Min;
  49. }
  50. int main() {
  51. #ifdef LOCAL
  52. freopen("test.txt", "r", stdin);
  53. // freopen("out.txt", "w", stdout);
  54. #endif // LOCAL
  55. ios::sync_with_stdio(false);
  56. two[0] = 1;
  57. for(int i = 1; i < 63; ++i) {
  58. two[i] = two[i - 1] * 2;
  59. }
  60. // printf("%I64d\n\n", 44981600439878676LL + 345678901LL);
  61. while(scanf("%I64d%I64d", &n, &L) != EOF) {
  62. mp.clear();
  63. ans = 0;
  64. for(int i = 0; i < n; ++i) {
  65. scanf("%I64d", &num[i]);
  66. }
  67. dfs(L);
  68. printf("%I64d\n", mp[L]);
  69. }
  70. return 0;
  71. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注