CodeForces 632A(역방향 시뮬레이션)
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit
Status
Practice
CodeForces 632A
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
Input
2 10
half
halfplus
Output
15
Input
3 10
halfplus
halfplus
halfplus
Output
55
제목의 대의: n명이 사과를 사면 짝수가 남을 때 반을 사고, 사과가 홀수가 남을 때 먼저 반을 사고 반값에 사과를 하나 사면 결국 사과가 마침 다 팔린다.농민 수입은 얼마입니까?
해체 사고방식: 시작 상태를 모르지만 종료 상태를 알기 때문에 우리는 뒤에서 앞으로 밀고 반대로 시뮬레이션을 할 수 있다.
코드는 다음과 같습니다.
#include
#include
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<
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.