The 2022 ICPC Asia Regionals Online Contest (I)

D题
The 2022 ICPC Asia Regionals Online Contest (I)
03:14:13
H Step Debugging
作者 pku
单位 北京大学
Rikka is a contestant in C-- Collegiate Programming Contest (CCPC), a competition for writing programs using a popular language C–.

A C-- expression is inductively defined in the following way.

arithmetic performs some arithmetic calculations.

library invokes a function in the standard library of C–.

For any C-- expression e and integer w∈[1,100], repeat e for w times repeats expression e for w times.

For any two C-- expressions e
1

,e
2

, e
1

e
2

runs expressions e
1

,e
2

in order.

A C-- program is a C-- expression followed by token fin, representing the end of the program.

Today, Rikka takes part in the online selection of CCPC. She writes a C-- program, but it fails in passing all test cases. Now, Rikka wants to
figure out what goes wrong in this program using step debugging. However, such a choice is risky: Stepping into a library function of C-- may be judged as cheating.

Therefore, before debugging, Rikka wants to mature the risk of stepping into library functions. She wants to calculate how many times the C-- program invokes library functions.

输入格式:
The input contains a single line, representing the C-- program written by Rikka.

The input guarantees the following four points.

The input C-- program is valid.

The input file is no larger than 10kB.

There is no extra space. Concretely, there is no leading space and succeeding space, and there is exactly one space between any two tokens.

There is a line break right after the end of the program, i.e., token fin.

输出格式
Output a single number, representing the times of invoking library functions.

The answer may be very large. You only need to output the answer module 20220911.

输入样例:
library arithmetic library library fin
repeat library arithmetic for 5 times library arithmetic fin
repeat library repeat repeat library for 3 times arithmetic library for 3 times for 100 times fin
repeat library repeat arithmetic library for 3 times repeat library for 2 times arithmetic for 4 times arithmetic fin
输出样例:
3
6
1300
24
题目意思:
让你求从l到r的好数个数
好数定义: 二进制情况下后缀零的个数与一的相等。

题解:
有两种做法:

  1. 从l出手 构造出从第一个大于l的后缀0 的个数为i的 一位一位变一然后加一
  2. 直接构造所有好数 ,发散性思维 1 -> 10 11 -> 101 100 110 111 保证线性构造即可。
#include <iostream>
#include <algorithm>
#include <map>
#include<vector>

using namespace std;
typedef long long ll;
vector<string> v[40][40], res, dp[40][40];
vector<long long> ans;
string li[40];
int st[40][40];
// 后缀0的个数与1 的个数相等

/*
 *    2*i+k=n(i>=1)
 *
 *   二位: 10
 *   三位: 无
 *   四位: 1100  1
 */
void get_yi(int i, int j) {
    if (st[i][j] == 1)return;
    if (i == 1) {
        st[i][j] = 1;
        return;
    }
    if (st[i - 1][j] == 0) {
        get_yi(i - 1, j);
        st[i - 1][j] = 1;
    }
    for (int k = 0; k < v[i - 1][j].size(); k++) {
        string &s = v[i - 1][j][k];
        v[i][j].push_back(s + "0");
    }

    if (j >= 1) {
        if (st[i - 1][j - 1] == 0) {
            get_yi(i - 1, j - 1);
            st[i - 1][j - 1] = 1;
        }
        for (int k = 0; k < v[i - 1][j - 1].size(); k++) {
            string &s = v[i - 1][j - 1][k];
            v[i][j].push_back(s + "1");
        }
    }
    st[i][j] = 1;
}

void get_houzhui(int i, int j) {
    get_yi(i - j - 2, j - 2);
    for (int k = 0; k < v[i - j - 2][j - 2].size(); k++) {
        string &s = v[i - j - 2][j - 2][k];
        //cout << i << ' ' << j << ' ' << s << endl;
        dp[i][j].push_back("1" + s + li[j]);
    }
}


void init() {
    li[1] = "10";
    for (int i = 2; i <= 20; i++) {
        li[i] = li[i - 1] + "0";
    }
    res.push_back("10");
    res.push_back("1100");
    v[1][1].push_back("1");
    v[1][0].push_back("0");
    //get_houzhui(6, 3);

    for (int i = 5; i <= 30; i++) {  //位
        for (int j = 2; j <= i / 2; j++) {
            get_houzhui(i, j);
            for (int k = 0; k < dp[i][j].size(); k++) {
                res.push_back(dp[i][j][k]);
            }
        }
    }
}

bool cmp(string a, string b) {
    if (a.size() != b.size())return a.size() < b.size();
    for (int i = 0; i < a.size(); i++) {
        int resa = a[i] - '0';
        int resb = b[i] - '0';
        if (resa != resb) {
            return resa <= resb;
        }
    }
}

int main() {
    ios::sync_with_stdio(0);
    init();
    sort(res.begin(), res.end(), cmp);
    for (int i = 0; i < 100; i++) {
        cout << res[i] << endl;
        long long s = 0;
        for (int j = 0; j < res[i].size(); j++) {
            int u = res[i][j] - '0';
            s = 2 * s + u;
        }
        ans.push_back(s);
    }
    sort(ans.begin(), ans.end());
    int T;
    cin >> T;
    while (T--) {
        int l, r;
        cin >> l >> r;
        int resl = lower_bound(ans.begin(), ans.end(), l) - ans.begin();
        int resr = lower_bound(ans.begin(), ans.end(), r) - ans.begin();
        int f = 0;
        for (int i = resl; i <= resr; i++) {
            if (ans[i] <= r && ans[i] >= l) {
                f = 1;
                cout << ans[i] << ' ';
            }
        }
        if (!f) {
            cout << -1 << ' ';
        }
        cout << '\n';
    }
    return 0;

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值