2022 Jiangsu Collegiate Programming Contest

2022江苏省赛

K:

以nunhehheh开头,a结尾的为合法串。构建一个串,使其字串合法的种数为给定的数。

思路:aa能构成3种,aaa能构成7种即2^n - 1种贡献,先把nunhehhe定下来(少一个h),h后面有i个a,就能有2^i - 1 种贡献,再在最后一个a前面加一个h,加一种贡献,这样就能有2^i种贡献。

eg:n = 7,  7 >= 2^2,在第二个a前加一个h,在第一个a前加一个h,贡献为2^2 - 1 + 1 = 4, n -= 4;

                   3 >= 2^1,在第一个a前加一个h,在第一个a前加一个h,贡献为2^1 - 1 + 1 = 2, n -= 2;

                   还剩1,                                       在第一个a前加一个h,贡献为1,n-=1;

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int , int>
const int N = 1e6 + 10;
const int mod = 1e9 + 7, inf = 0x3f3f3f3f;
int pow2[32], cnt[32];

void init(){
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    //预处理2的i次方
    pow2[0] = 1;
    for(int i = 1; i < 32; i ++){
        pow2[i] = pow2[i - 1] * 2;
    }
}

void solve(int n){
    memset(cnt, 0, sizeof cnt);
    //cout << n << ' ' << cnt[31] << endl;
    for(int i = 31; i > 0; i --){
        if(n >= pow2[i]){
            cnt[i] ++;
            cnt[1] ++;
            n -= pow2[i];
        }
    }

    if(n & 1) cnt[1] ++;

    cout << "nunhehhe";
    for(int i = 31; i >= 1; i --){
        for(int j = 0; j < cnt[i]; j ++){
            cout << 'h';
        }
        cout << 'a';
    }
    cout << endl;
}

signed main()
{
    init();

    int q;
    cin >> q;
    while(q --){
        int n;
        cin >> n;
        solve(n);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值