• 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 
    #include 
    #include 
    #include
    
    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;
    
    }
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
  • 相关阅读:
    吃透 Spring 系列—AOP部分
    Shell 结构化命令
    探索服务器潜能:创意项目、在线社区与其他应用
    子类加@Data后,IDEA调试时“出现”父类属性无值
    Mybatis - 常用 SQL 语句设计思路及具体实现 - 数据存在则更新,不存在则插入、批量更新、批量插入、连表查询 + - 字段加减法
    Spring简介、IOC容器
    导数求切线
    1.0 Zookeeper 教程
    Cookie与Session简单入门
    做客户成功岗位有必要考PMP吗?
  • 原文地址:https://blog.csdn.net/weixin_51626694/article/details/126937316