注意数乘可能会引发数据的溢出

这是一道关于ACM竞赛的题目,提醒程序员在选择卡片时注意可能导致数据溢出的情况。Appleman拥有n张标有字母的卡片,Toastman需要从中选出k张,Appleman将根据所选卡片上的字母给予相应的硬币。目标是最大化Toastman获得的硬币数。题目提供了一种可能的解决方案,在第一组测试用例中,Toastman可以选择9张D字母的卡片和1张其他字母的卡片,总共可以获得18枚硬币。
摘要由CSDN通过智能技术生成

下面贴上一个题目,提示自己要注意数据,不要溢出了!


B. Appleman and Card Game
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.

Given the description of Appleman's cards. What is the maximum number of coins Toastman can get?

Input

The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman.

Output

Print a single integer – the answer to the problem.

Examples
Input
15 10
DZFDFZDFDDDDDDF
Output
82
Input
6 4
YJSNPI
Output
4
Note

In the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin.




paste my code:

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
const int maxn = 1e5+5;
char record[maxn];
int main()
{
    LL cot[28];//改编数据的类型!
    for (int i=0; i<26; i++){
        cot[i] = 0;
    }
    LL n, k;//原来是int ,后来改变数据的类型就过了
    cin>>n>>k;
    for (int i=0; i<n; i++){
        cin>>record[i];
        cot[record[i]-65]++;
    }
    sort(cot, cot+26);
    int index = 25;
    LL sum =0 ;
    while (k){
        if(k>cot[index]){
            k -= cot[index];
            sum += cot[index]*cot[index];//虽然不是很理解大数的乘法,但是两个在整数范围内的数相乘,若他们的结果超过了整数的范围,那么就会溢出
            index--;
        }
        else{
            sum += k*k;
            k = 0;
        }
    }
    cout<<sum<<endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值