链接: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也可以

代码:

#include <bits/stdc++.h>
using namespace std; typedef long long LL;
const unsigned int MAXV = (1LL<<32)-1;
const int MAXN = 1e5+10;
int n;
bool flag = true; struct Op
{
string op;
LL va;
}oper[MAXN];
int step = 1; LL Dfs(LL lops)
{
if (step > n)
return 0;
LL res = 0;
while (oper[step].op[0] == 'a')
{
step++;
res++;
}
// cout << "res1:" << res << endl;
while (oper[step].op[0] == 'f')
{
res += Dfs(oper[step++].va);
if (res > MAXV)
flag = false;
while (oper[step].op[0] == 'a')
{
step++;
res++;
}
}
// cout << "res2:" << res << endl;
if (1LL*lops*res > MAXV)
flag = false;
// cout << "res3:" << lops*res << ' ' << step << endl;
step++;
return lops*res;
} int main()
{
// freopen("test.in", "r", stdin);
cin >> n;
for (int i = 1;i <= n;i++)
{
cin >> oper[i].op;
if (oper[i].op[0] == 'f')
cin >> oper[i].va;
}
LL res = Dfs(1);
if (!flag)
cout << "OVERFLOW!!!" << endl;
else
cout << res << endl; return 0;
}

  

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. HTML5 拖放:在相册中对照片进行排序

    1. [代码]index.html     <div class="albums">    <div class="album" id=&qu ...

  2. BZOJ 1638 [Usaco2007 Mar]Cow Traffic 奶牛交通:记忆化搜索【图中边的经过次数】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1638 题意: 给你一个有向图,n个点,m条有向边. 对于所有从入度为0的点到n的路径,找出 ...

  3. Maven项目中使用JUnit进行单元测试

    1.打开maven项目中的pom.xml,添加JUnit 的jar包 2.在src/test/java下右键新建JUnit Test Cast

  4. mvc 让伪静态变得简单

    IIS 部署后访问*.* config 配置: <modules runAllManagedModulesForAllRequests="true">      < ...

  5. bzoj 4515: 游戏 树链剖分+线段树

    题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=4515 题解: 先让我%一发lych大佬点我去看dalao的题解 讲的很详细. 这里纠正一 ...

  6. python爬虫知识点总结(五)正则表达式

    在线正则表达式匹配:http://tool.oschina.net/regex 正则表达式学习:https://c.runoob.com/front-end/854 一.什么是正则表达式? 常见匹配模 ...

  7. win10系统下安装64位Oracle11g+LSQL Developer

    LSQL Developer作为强大的Oracle编辑工具,却只支持32bit,本文提供在安装用LSQL Developer打开64bitOracle的操作方法 工具/原料  oracle11g安装包 ...

  8. Python操作MySQL数据库9个实用实例

    用python连接mysql的时候,需要用的安装版本,源码版本容易有错误提示.下边是打包了32与64版本. MySQL-python-1.2.3.win32-py2.7.exe MySQL-pytho ...

  9. 转学习LINUX的建议

    作为一个新人,怎样学习嵌入式Linux?被问过太多次,特写这篇文章来回答一下.在学习嵌入式Linux之前,肯定要有C语言基础.汇编基础有没有无所谓(就那么几条汇编指令,用到了一看就会).C语言要学到什 ...

  10. UDK更改启动画面及载入动画

    转自:http://www.unrealchina.org/forum.php?mod=viewthread&tid=246&extra=page%3D1 方法很简单: 1.更改启动画 ...