632A.Gradma Laura and Apples
2016-03-07 19:51
246 查看
A. Grandma Laura and Apples
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
类似猴子摘桃问题
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
类似猴子摘桃问题
#!/usr/bin/python # -*-coding:utf-8 -*- b,p=map(int,raw_input().split(' ')) customer=[] for i in range(b): customer.append(raw_input()) sum=0 for i in range(len(customer)-1,-1,-1): if customer[i]=='halfplus': sum=sum*2+1 else: sum=sum*2 a=0 while(sum!=0): if(sum%2==0): a=(sum/2)*p+a sum=sum/2 else: a=((sum-1)/2)*p+a+p/2 sum=(sum-1)/2 print a
相关文章推荐
- 【小程序】日期类date的使用方法
- IOS-CocoaPods的详细安装与使用
- iOS编程语言 swift 语言初学总结
- 【Android】自定义View —— 水波纹
- [小程序]时间类Calendar方法的一些普通用法
- android 切换全屏显示
- 【Android】更改 TimePicker 字体颜色
- Android Bitmap
- MKNetworkKit介绍以及使用
- Activity被回收导致fragment的getActivity为null的解决办法
- Android中自己定义组件和它的属性
- android屏幕旋转时Activity不重新调用onCreate的方法
- LeakCanary——直白的展现Android中的内存泄露
- iOS开发拓展篇—CoreLocation定位服务
- 《Android开发艺术探索》 自定义View
- iOS开发遇到的坑之七--上传app Stroe被拒绝:The app references non-public symbols in : _UICreateCGImageFromIOSurface
- [函数] Firemonkey iOS 指定目录不要备份到 iCloud
- 仿微信字母快速查找联系人界面
- Plugin with id 'android-apt' not found
- Android应用--如何向Android应用中加入广告