链接:https://codeforces.com/contest/1175/problem/B

题意:

You are given a function ff written in some basic language. The function accepts an integer value, which is immediately written into some variable xx. xx is an integer variable and can be assigned values from 00 to 232−1232−1. The function contains three types of commands:

  • for nn — for loop;
  • end — every command between "for nn" and corresponding "end" is executed nn times;
  • add — adds 1 to xx.

After the execution of these commands, value of xx is returned.

Every "for nn" is matched with "end", thus the function is guaranteed to be valid. "for nn" can be immediately followed by "end"."add" command can be outside of any for loops.

Notice that "add" commands might overflow the value of xx! It means that the value of xxbecomes greater than 232−1232−1 after some "add" command.

Now you run f(0)f(0) and wonder if the resulting value of xx is correct or some overflow made it incorrect.

If overflow happened then output "OVERFLOW!!!", otherwise print the resulting value of xx.

思路:

模拟,我用递归写的。

用stack也可以

代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long LL;
  5. const unsigned int MAXV = (1LL<<32)-1;
  6. const int MAXN = 1e5+10;
  7. int n;
  8. bool flag = true;
  9.  
  10. struct Op
  11. {
  12. string op;
  13. LL va;
  14. }oper[MAXN];
  15. int step = 1;
  16.  
  17. LL Dfs(LL lops)
  18. {
  19. if (step > n)
  20. return 0;
  21. LL res = 0;
  22. while (oper[step].op[0] == 'a')
  23. {
  24. step++;
  25. res++;
  26. }
  27. // cout << "res1:" << res << endl;
  28. while (oper[step].op[0] == 'f')
  29. {
  30. res += Dfs(oper[step++].va);
  31. if (res > MAXV)
  32. flag = false;
  33. while (oper[step].op[0] == 'a')
  34. {
  35. step++;
  36. res++;
  37. }
  38. }
  39. // cout << "res2:" << res << endl;
  40. if (1LL*lops*res > MAXV)
  41. flag = false;
  42. // cout << "res3:" << lops*res << ' ' << step << endl;
  43. step++;
  44. return lops*res;
  45. }
  46.  
  47. int main()
  48. {
  49. // freopen("test.in", "r", stdin);
  50. cin >> n;
  51. for (int i = 1;i <= n;i++)
  52. {
  53. cin >> oper[i].op;
  54. if (oper[i].op[0] == 'f')
  55. cin >> oper[i].va;
  56. }
  57. LL res = Dfs(1);
  58. if (!flag)
  59. cout << "OVERFLOW!!!" << endl;
  60. else
  61. cout << res << endl;
  62.  
  63. return 0;
  64. }

  

Educational Codeforces Round 66 (Rated for Div. 2) B. Catch Overflow!的更多相关文章

  1. Educational Codeforces Round 66 (Rated for Div. 2) A. From Hero to Zero

    链接:https://codeforces.com/contest/1175/problem/A 题意: You are given an integer nn and an integer kk. ...

  2. Educational Codeforces Round 66 (Rated for Div. 2) A

    A. From Hero to Zero 题目链接:http://codeforces.com/contest/1175/problem/A 题目 ou are given an integer n ...

  3. Educational Codeforces Round 66 (Rated for Div. 2)

    A.直接模拟. #include<cstdio> #include<cstring> #include<iostream> #include<algorith ...

  4. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  5. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  6. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  7. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

  8. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

  9. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...

随机推荐

  1. ps炫光素材

    炫光闪电笔刷,炫光闪电笔刷,雷电笔刷,自然闪电Photoshop笔刷下载ps炫光素材 素材下载:http://www.huiyi8.com/sc/8695.html

  2. SocketSessionConfig参数设制

    Mina 是一个韩国人写的基本java NIO的一个高性能的传输框架,我们的搜索就是基本它作为一个搜索服务开放接口了.对于系统的TIME_WAIT过多,造成服务器的负载过高,这个问题我也不用多说了,这 ...

  3. c++类之间的关系

    我们知道,表达方式分为很多种,对于同一种事物,比如爱情,画家用图画和色彩表达爱恋:音乐家用音符和节奏表达喜爱之情,作家用文字表现爱慕. 而程序员怎么办? 程序员构建类,用类来表达单身之苦.因此,类就是 ...

  4. 0x01

    随便记录点想法什么的, 这个博客的编辑界面挺简陋的...

  5. MFC默认窗口类名称

    // special AFX window class name mangling #ifndef _UNICODE #define _UNICODE_SUFFIX #else #define _UN ...

  6. phpstorm 2016.3.2 的最新破解方法(截止2017-2-20)

    最新更新 ,http://idea.imsxm.com 这个地址是亲测可用的,针对最新的phpstorm 2016.3.2的版本.使用方式和下面一致,选择license server.然后复制http ...

  7. AtCoder Grand Contest 028 A:Two Abbreviations

    题目传送门:https://agc028.contest.atcoder.jp/tasks/agc028_a 题目翻译 给你两个串\(s\)与\(t\),长度分别为\(n,m\).问你存不存在一个串长 ...

  8. Python调试指南

    http://blog.sina.com.cn/s/blog_a15aa56901017u0p.html http://www.cnblogs.com/coderzh/archive/2009/12/ ...

  9. 2012年浙大:Head of a Gang

    题目描述: One way that the police finds the head of a gang is to check people's phone calls. If there is ...

  10. 安装时后的idea,项目不能运行,pom.xml文件不能下载到本地仓库,maven配置是正确的

    安装时后的idea,项目不能运行,pom.xml文件不能下载到本地仓库,maven配置是正确的 项目上传到svn后,同事下载项目后,没有识别出来mavn中的pom.xml文件,导致idea不能自动下载 ...