Educational Codeforces Round 9 A. Grandma Laura and Apples 水题
2016-03-02 22:11
471 查看
A. Grandma Laura and Apples
题目连接:
http://www.codeforces.com/contest/632/problem/ADescription
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 10half
halfplus
Sample Output
15Hint
题意
有n个顾客买苹果,每个苹果p元half就是这个顾客买了一半的苹果
halfplus就是这个顾客买了一半苹果,最后还送了他半个苹果
最后恰好卖完所有苹果
问你赚了多少钱
题解:
倒着推就好了当成模拟题做就行了
代码
#include<bits/stdc++.h> using namespace std; long long ans = 0; string s[45]; int main() { int n,p;scanf("%d%d",&n,&p); for(int i=0;i<n;i++) cin>>s[i]; long long now = 0; for(int i=n-1;i>=0;i--) { if(s[i]=="half") { ans+=now*p; now=now*2; } else { ans+=p/2+now*p; now=now*2+1; } } cout<<ans<<endl; }
相关文章推荐
- 从AOP框架学习iOS Runtime
- WebView的用法
- Jetty实战之 嵌入式Jetty运行web app
- Unity中加入Android项目的Build步骤 .<unity引用Android的jar包><Android对unity做二次开发>
- iOS 利用 framework 进行动态更新
- 给 iOS 开发者的 Sketch 入门教程
- Android 实现圆角按钮(selector和shape的简单用法)
- iOS高效添加圆角效果实战讲解
- android开发style详解
- android事件监听
- Windows Azure - App Services
- Android如何使用so文件和Android studio中导入so
- ViewFlipper左右滑动事件
- Android-AR环境搭建
- The Angry Flappy Bird - 愤怒的飞翔小鸟
- [android] sharedPreference入门
- iOS精确计算 货币计算
- iOS开发问题总结
- [Unity3D]蓝港面试题
- android-浅谈DVM与JVM的区别