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.
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.
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.
2 10 half halfplus
15
3 10 halfplus halfplus halfplus
55
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.
主要把题意看懂就很好想了,input最后一行一定是halfplus,所以从1开始往上算。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
using namespace std;
int main()
{
int n,p,i;
int a[50];
char v[10]="half";
scanf("%d %d",&n,&p);
char ch=getchar();
for(i=1;i<=n;i++)
{
char c[10];
gets(c);
if(strcmp(c,v)==0){a[i]=1;}
else a[i]=0;
}
long long num=1,m=0,nn=0;
for(i=n-1;i>=1;i--)
{
if(a[i]==1)num*=2;
else num=num*2+1;
}
while(num)
{
if(num%2)
{m+=(num/2);nn++;}
else
m+=(num/2);
num/=2;
}
printf("%lld\n",m*p+nn*p/2);
return 0;
}