CodeForces 632A(反向模拟)
来源:互联网 发布:苹果time capsule知乎 编辑:程序博客网 时间:2024/04/30 03:52
Description
Grandma Laura came to the market to sell some apples. During the day she sold all the apples she had. But grandma is old, so she forgot how many apples she had brought to the market.
She precisely remembers she had n buyers and each of them bought exactly half of the apples she had at the moment of the purchase and also she gave a half of an apple to some of them as a gift (if the number of apples at the moment of purchase was odd), until she sold all the apples she had.
So each buyer took some integral positive number of apples, but maybe he didn't pay for a half of an apple (if the number of apples at the moment of the purchase was odd).
For each buyer grandma remembers if she gave a half of an apple as a gift or not. The cost of an apple is p (the number p is even).
Print the total money grandma should have at the end of the day to check if some buyers cheated her.
Input
The first line contains two integers n and p (1 ≤ n ≤ 40, 2 ≤ p ≤ 1000) — the number of the buyers and the cost of one apple. It is guaranteed that the number p is even.
The next n lines contains the description of buyers. Each buyer is described with the string half if he simply bought half of the apples and with the string halfplus if grandma also gave him a half of an apple as a gift.
It is guaranteed that grandma has at least one apple at the start of the day and she has no apples at the end of the day.
Output
Print the only integer a — the total money grandma should have at the end of the day.
Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.
Sample Input
2 10halfhalfplus
15
3 10halfplushalfplushalfplus
55
题目大意:有n个人买苹果,当苹果剩余偶数时买走一半,当苹果剩余奇数时,先买走一半,再用半价买走一个苹果,最终苹果恰好卖完.农民收入为多少.
解体思路:因为不知道起始状态,但知到终止状态,所以我们可以从尾向前推,反向模拟一遍.
代码如下:
#include<iostream>#include<string>using namespace std;string s[45];int main(){int n,p,i;long long sum,now;sum=now=0;cin>>n>>p;for(i=n-1;i>=0;i--)cin>>s[i];for(i=0;i<=n-1;i++){if(s[i]=="half"){sum+=now*p;now=2*now;}else{sum+=now*p+p/2;now=now*2+1;}}cout<<sum<<endl;return 0;}
- CodeForces 632A(反向模拟)
- codeforces 691A 模拟
- Codeforces 67A【模拟】
- Codeforces 449A 模拟
- Codeforces 1A 【模拟】
- codeforces 855A 模拟
- codeforces 187A 模拟
- Codeforces 330A Cakeminator(模拟)
- 模拟 之 Codeforces 416A
- CodeForces 651A:Joysticks【模拟】
- 【CodeForces 651A】Joysticks 模拟
- Codeforces 300A Array 【模拟】
- CodeForces 407A-Triangle【模拟】
- Codeforces 390A( 模拟题)
- CodeForces - 404A(模拟题)
- 【模拟】Codeforces 705A Hulk
- CodeForces - 730A 贪心+模拟
- codeforces 632A Grandma Laura and Apples 模拟
- 2016春季学习——追逐
- Problem C: C语言习题 字符串长度
- KNN及其改进算法的python实现
- 树状数组与逆序对数
- iOS进阶——coreData的使用与学习
- CodeForces 632A(反向模拟)
- MySQL知识点整理
- Problem D: C语言习题 字符串比较
- NVIDIA Jetson TK1学习与开发(二):入门指导
- 自定义view 之 网格
- 第二周项目三小试循环
- php的学习笔记之面向对象(六)static
- AlphaGo来了,终结者不远了
- Javascript面向对象编程(二):构造函数的继承