Educational Codeforces Round 9 -- A - Grandma Laura and Apples

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.

Examples
input
2 10
half
halfplus
output
15
input
3 10
halfplus
halfplus
halfplus
output
55
Note

In the first sample at the start of the day the grandma had two apples. First she sold one apple and then she sold a half of the second apple and gave a half of the second apple as a present to the second buyer.


大体题意:
Laura要卖苹果,(有很多但不知道数量),最终所有苹果都卖光了!
有n个人买苹果,如果那个人是half,他就买所有苹果的一半,如果那个人是halfplus,则他买一半,Laura还会送半个苹果!问最终Laura赚多少钱!
思路:
看题看了好久,感觉有些类似3n+1问题,
最终是所有苹果都卖光的,从后往前查找,sum = 0表示当前苹果总数,如果是halfplus,则总数应该是(sum + 0.5 )× 2,如果是half,则当前苹果总数是sum × 2,然后让每一段乘以苹果单价p即可!

代码如下:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 10000 + 10;
const int maxt = 100 + 10;
const int INF = 1e8;
const double eps = 1e-8;
const double pi = acos(-1.0);
typedef long long ll;
typedef unsigned long long llu;
int main()
{
    ll n,p;
    char str[100][100];
    while(scanf("%I64d%I64d",&n,&p) == 2){
        double sum = 0;
        ll mo = 0;
        for (int i = 0; i < n; ++i)scanf("%s",str[i]);
        for (int i = n -1; i >= 0; --i){
            if (!strcmp(str[i],"halfplus")){
                sum = (sum + 0.5) * 2;
                mo += sum / 2.0 * p;
            }else {
                sum = sum * 2;
                mo+=sum/2*p;
            }
        }
        printf("%I64d\n",mo);
    }
    return 0;
}



你可能感兴趣的:(C语言)