Educational Codeforces Round 66 (Rated for Div. 2) B. Catch Overflow!
链接: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!的更多相关文章
- 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. ...
- 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 ...
- Educational Codeforces Round 66 (Rated for Div. 2)
A.直接模拟. #include<cstdio> #include<cstring> #include<iostream> #include<algorith ...
- 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 ...
- 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 ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
- 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 ...
- 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 ...
随机推荐
- mysql 优化 实现命中率100%
配置你的mysql配置文件:主要是配置[mysqld]后面的内容. 1,优化远程连接速度. 在[mysqld]下面添加skip-name-resolve skip-name-resolve 选项就能禁 ...
- j2ee项目Java代码性能优化要点(抄书)
亚信联创科技出版的. 1.与log4j有关的性能问题 Logger对象的标准定义方式: private static transient Logger log=Logger.getLogger(cre ...
- poj1094Sorting It All Out——拓扑排序
题目:http://poj.org/problem?id=1094 看到此题,首先觉得这是一种层层递进的关系,所以可以想到用拓扑排序: 就像人工排序,每次需要找到一个最小的,再找到新的最小的……所以用 ...
- 百度地图API的第一次接触——标注和信息窗的使用
1.定义js函数,用于在指定位置添加标注,在标注位置添加并打开信息窗口 function addMarker(point, index){ // 创建图标对象 var myIcon = new BMa ...
- 批处理中格式化Date
@Echo Off Set _Date=%date% If "%_Date%A" LSS "A" (Set _NumTok=1-3) Else (Set _Nu ...
- 三台主机搭建LAMP(apache、mariadb、php)
实验环境:均是CentOS7 httpd:172.16.254.88 2.4.6 PHP:172.16.250.140 5.4.16 mariadb:172.16.250.94 5.5.52 第三 ...
- IOS技能要求
iOS专业技能: 拥有独立开发iOS APP应用的能力,并且独立开发过多款应用:熟练掌握iOS主流界面布局设计,基本控件和自定义控件的使用,事件处理及事件分发机制:熟练掌握工厂.代理.观察者.单例等多 ...
- python UnicodeEncodeError: 'ascii' codec can't encode characters 解决办法
程序直接运行没有问题,一旦用nohup python test.py 执行遇中文就报python UnicodeEncodeError错误: 最终测试出的解决方法为,在开头添加红色的三句: #!/us ...
- [poj3417]Network(LCA+树形dp)
题意:给出一棵无根树,然后下面再给出m条边,把这m条边连上,每次你去两条边,规定一条是树边,一条是新边,问有多少种方案能使树断裂. 解题关键:边权转化为点权,记录每条边被环覆盖的次数,通过val[a] ...
- Auto Layout Guide----(一)-----Understanding Auto Layout
Understanding Auto Layout 理解自动布局 Auto Layout dynamically calculates the size and position of all the ...