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 ...
随机推荐
- matlab之text()函数
text()函数用来给图加上说明性文字. 格式:text(x,y,'txt') 或者:text(x,y,'txt','color','k') x,y是位置 txt是说明文字,如果说明性文字是数字,则这 ...
- 虫草医药网站html模板
虫草医药网站html模板是一款宝王虫草医药网站模板html源码整站下载. 模板地址:http://www.huiyi8.com/sc/8783.html
- HDU 1005 Number Sequence:矩阵快速幂
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 题意: 数列{f(n)}: f(1) = 1, f(2) = 1, f(n) = ( A*f(n ...
- Smarty模板重点汇总
Smarty模板重点回顾:1.功能:前后端分离:2.实现方法:通过使用Smarty的核心类来实现,利用display方法来读取模板文 件,用正则进行替换,替换完保存到临时文件,再将临时文件加载到当前页 ...
- CodeForces - 697F:Legen... (AC自动机+矩阵)
Barney was hanging out with Nora for a while and now he thinks he may have feelings for her. Barney ...
- BZOJ4307: Maishroom & Class
感觉有一点题面没有说得特别明确,就是一个人代替了其他人之后,另一个可以被他代替的人就不能让他来代替自己了. 每个人向自己可以代替的人连边,额外增加一个源点$r$向所有助教连边.第一问答案是$r$不能到 ...
- java定时器,留着用
说明:该定时器作用是 设定定时器首次执行的时间firstTime和执行间隔period,如firstTime=2015-3-25 9:00:00,period=24小时,若程序启动时,已经超过firs ...
- [转]BFC 神奇背后的原理
BFC 已经是一个耳听熟闻的词语了,网上有许多关于 BFC 的文章,介绍了如何触发 BFC 以及 BFC 的一些用处(如清浮动,防止 margin 重叠等).虽然我知道如何利用 BFC 解决这些问题, ...
- 51 nod 1522 上下序列——序列dp
题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1522 很好的思想.考虑从小到大一对一对填数,这样也能对它的大小限制 ...
- LED接口
通常我们都听到别人说DVI接口.VGA接口等,可是这些接口是什么意思,led显示屏厂家小编为您解析其含义与不同之处. 1.DVI接口 DVI接口相校于VGA.HDMI.DP等接口来说体积较大,是目 ...