CodeForces 632A

本文深入解析了一道来自Codeforces平台的编程难题,详细阐述了解题思路、关键步骤以及实现代码。通过实例分析,旨在帮助读者理解如何在限定时间内解决复杂的算法问题,并提供了一个简洁易懂的解决方案,包括输入输出规范、核心算法逻辑及优化策略。

A - Grandma Laura and Apples
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

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

          真不明白我们学长,拉题都拉到cf 上了,结果他们拉的题自己也做不出几道,,反倒是学弟学妹们踊跃的A了,难道是他们已经A了,,,,,

             cf上的题,,众所周知,全是密密麻麻的英文,,我去。。

     呐,这题看了半天知道了题意,可死活不造输出是怎么得到的,看着榜上那么多人A了,心灰意冷,,后来比完赛回寝室同学说如果是奇数那么卖一半,送半个,比如7个,卖3.5个,送0.5个,所以需要付35块钱,拿走了四个,,瞬间感觉无语了,,,读了好多遍题推算出最后一定是一个苹果,而且字符串最后一定有plus,,,

所以可以逆推,第二天看他们的提交AC答案,方法很多,不过思路清晰快速基本就没什么问题了。。。。。‘

           贴上简洁易懂代码:

                                 

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
    int n,p,i;
    double x;
    long long sum;
    char a[41][10];
    while(~scanf("%d%d",&n,&p))
    {
        x=1.0;
        sum=p/2.0;
        for(i=0;i<n;i++)
            scanf("%s",a[i]);
            for(i=n-2;i>=0;i--)//<span style="font-size: 12.6315793991089px;">字符串最后一定是plus,不用从n-1开始逆推;</span>
            {
                if(strlen(a[i])>4)
                {
                    x=x*2.0+1.0;
                    sum+=p*x/2.0;
                }
                else
                {
                    x=x*2.0;
                    sum+=p*x/2.0;
                }
            }
           printf("%I64d\n",sum);
    }
    return 0;
}


### 关于 Codeforces Problem 1804A 的解决方案 Codeforces 是一个广受欢迎的在线编程竞赛平台,其中问题 1804A 可能涉及特定算法或数据结构的应用。尽管未提供具体题目描述,但通常可以通过分析输入输出样例以及常见解法来推导其核心逻辑。 #### 题目概述 假设该问题是关于字符串处理、数组操作或其他基础算法领域的内容,则可以采用以下方法解决[^2]: 对于某些初学者来说,遇到不熟悉的语言(如 Fortran),可能会感到困惑。然而,在现代竞赛环境中,大多数情况下会使用更常见的语言(C++、Python 或 Java)。因此,如果题目提及某种神秘的语言,可能只是为了增加趣味性而非实际需求。 #### 解决方案思路 以下是基于一般情况下的潜在解答方式之一: ```cpp #include <bits/stdc++.h> using namespace std; int main(){ int t; cin >> t; // 输入测试用例数量 while(t--){ string s; cin >> s; // 获取每组测试数据 // 假设这里需要执行一些简单的变换或者判断条件... bool flag = true; // 初始化标志位为真 for(char c : s){ if(c != 'a' && c != 'b'){ flag = false; break; } } cout << (flag ? "YES" : "NO") << "\n"; // 输出结果 } return 0; } ``` 上述代码片段展示了一个基本框架,适用于许多入门级字符串验证类问题。当然,这仅作为示范用途;真实场景下需依据具体要求调整实现细节。 #### 进一步探讨方向 除了官方题解外,社区论坛也是获取灵感的好地方。通过阅读他人分享的经验教训,能够加深对该类型习题的理解程度。同时注意积累常用技巧并灵活运用到不同场合之中[^1]。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值