Codeforces848A-From Y to Y

A. From Y to Y
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

From beginning till end, this message has been waiting to be conveyed.

For a given unordered multiset of n lowercase English letters ("multi" means that a letter may appear more than once), we treat all letters as strings of length 1, and repeat the following operation n - 1 times:

  • Remove any two elements s and t from the set, and add their concatenation s + t to the set.

The cost of such operation is defined to be , where f(s, c) denotes the number of times character cappears in string s.

Given a non-negative integer k, construct any valid non-empty set of no more than 100 000 letters, such that the minimum accumulative cost of the whole process is exactly k. It can be shown that a solution always exists.

Input

The first and only line of input contains a non-negative integer k (0 ≤ k ≤ 100 000) — the required minimum cost.

Output

Output a non-empty string of no more than 100 000 lowercase English letters — any multiset satisfying the requirements, concatenated to be a string.

Note that the printed string doesn't need to be the final concatenated string. It only needs to represent an unordered multiset of letters.

题目大意:首先背景是要求一段字符串,并且字符串内的字符间可以互相合并,合并时会产生值,而这个值的计算公式如题目中的数学

                  公式,其中f(s,c)表明字符c在s串中的出现次数,现在给出一个数字k,要求一段字符串使得该字符串进行合并时所能产生的

                 值的和的最小值刚好等于k.

                首先要做一下自我检讨,开这题之前内心其实是很想放弃的,虽然晚上有点疲惫的因素存在,但在赛场上是什么时候都不能放弃的,

                 这个心态也导致我最后差一点AC掉这道题(第二天把赛后几分钟改好的代码交上去1A),所以以后无论面对多难的题,只要还有

                时间,都要尽力去做。

                回到解法上来吧,首先我推出一段字符串合并的值好像是恒定的(可能是我推错了),那么我可以只对字符串内相同字符各自分析,

               如aaaaab,则先只对a,aa,....,aaaaa分析,同时发现相同字符串合并时值是有公式的,值为1+2+....+n(n为字符串长度减1),得到公式为

               n*(n+1)/2,那么后面只要不断对k和n*(n+1)/2进行比较即可得到解答了。

            

#include<bits/stdc++.h>
using namespace std;
int ax[105];
int main()
{
    int k,cnt=1;
    cin>>k;
    int z=floor(sqrt(k*2)),t=0;
    if(z*(z+1)>k*2)
        z--;
    while(t<=z)
        cout<<"a",t++;
    while(z*(z+1)/2!=k)
     {
       k=k-z*(z+1)/2;
       z=floor(sqrt(k*2));
       if(z*(z+1)>k*2)
          z--;
       if(z<0)
        break;
       char rec='a'+cnt;
       t=0,cnt++;
       while(t<=z)
        cout<<rec,
         t++;
     }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值