AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 应用
  • 更多
    • 题解
    • 分享
    • 商店
    • 问答
    • 吐槽
  • App
  • 登录/注册

AcWing 4982. 进制    原题链接    中等

作者: 作者的头像   Albert_Tesla ,  2023-05-26 22:15:50 ,  所有人可见 ,  阅读 25


1


#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
LL a, b;

int cal(string s1, string s2) {
    int len1 = s1.length(), len2 = s2.length();
    int res = 0;

    for(int i = len1 + 1; i <= len2 - 1; i ++)
        res += (i - 1);

    if(len1 != len2) {

        string s3 = "", s4 = "";
        for(int i = 1; i <= len1; i ++) s3 += '1';
        for(int i = 1; i <= len2; i ++) s4 += '1';

        for(int i = 1; i < len1; i ++) {
            string tmp = s3;
            tmp[i] = '0';
            if(tmp >= s1) res ++;
        }

        for(int i = 1; i < len2; i ++) {
            string tmp = s4;
            tmp[i] = '0';
            if(tmp <= s2) res ++;
        }

    } else {
        string s3 = "";
        for(int i = 1; i <= len1; i ++) s3 += '1';
        for(int i = 1; i < len1; i ++) {
            string tmp = s3;
            tmp[i] = '0';
            if(tmp >= s1 && tmp <= s2) res ++;
        }
    }

    return res;
}

void yuri() {
    LL x1 = a, x2 = b;
    string s1 = "", s2 = "";

    while(x1 != 0) {
        char ch = (x1 % 2) + '0';
        s1 += ch;
        x1 /= 2;
    }

    reverse(s1.begin(), s1.end());

    while(x2 != 0) {
        char ch = (x2 % 2) + '0';
        s2 += ch;
        x2 /= 2;
    }

    reverse(s2.begin(), s2.end());

    cout << cal(s1, s2) << '\n';
}

int main() {
    cin >> a >> b;

    yuri();
    return 0;
}

0 评论

你确定删除吗?

© 2018-2023 AcWing 版权所有  |  京ICP备17053197号-1
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息