Bits - CodeForces 485C

本文详细解析如何使用C++解决Codeforces平台上的力赛题,题目要求在给定区间内找到二进制下1的个数最多的整数,且在数量相同时选择最小的数。通过预设数组存储特定二进制数并累加,直至超过上限,进而找到答案。
C. Bits
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Let's denote as  the number of bits set ('1' bits) in the binary representation of the non-negative integer x.

You are given multiple queries consisting of pairs of integers l and r. For each query, find the x, such that l ≤ x ≤ r, and is maximum possible. If there are multiple such numbers find the smallest of them.

Input

The first line contains integer n — the number of queries (1 ≤ n ≤ 10000).

Each of the following n lines contain two integers li, ri — the arguments for the corresponding query (0 ≤ li ≤ ri ≤ 1018).

Output

For each query print the answer in a separate line.

Sample test(s)
input
3
1 2
2 4
1 10
output
1
3
7
Note

The binary representations of numbers from 1 to 10 are listed below:

110 = 12

210 = 102

310 = 112

410 = 1002

510 = 1012

610 = 1102

710 = 1112

810 = 10002

910 = 10012

1010 = 10102


题目链接:http://codeforces.com/problemset/problem/485/C
题目大意:给定一个区间[l, r],求这个区间里的数在转化为二进制的情况下,1的个数最多的数字;同时,当1的个数相同的时候,输出数最小的那个。
题目思路:先设置一个数组,用来存储1、10、100、1000这些二进制数,然后对这些数进行累加,直到大于等于r为止。此时的sum就是1的个数最多的那个数,但是有可能不在这个区间中,所以我们需要进行判断。假如他大于r的话,就减去最后相加的那个数,这样可以保证得出的结果是区间中1最多的但是数值最小的。


C++源代码如下:

#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <iomanip>
#include <cstring>
#include <iostream>
#include <algorithm>
#define For(i, n) for (int i = 1; i <= n; i++)
#define ForK(i, k, n) for (int i = k; i <= n; i++)
#define ForD(i, n) for (int i = n; i >= 0; i--)
#define Rep(i, n) for (int i = 0; i < n; i++)
#define RepD(i, n) for (int i = n; i >= 0; i--)
#define MemI(a) memset(a, 0, sizeof(a))
#define MemC(a) memset(a, '\0', sizeof(a))
#define PI acos(-1)
#define eps 1e-8
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;

ll bit[65];
int main()
{
    bit[0] = 1;
    for (int i = 1; i <= 63; i++)
    {
        bit[i] = 2 * bit[i - 1];
    }
    int t;
    ll l, r, sum;
    scanf("%d", &t);
    while (t--)
    {
        sum = 0;
        scanf("%I64d%I64d", &l, &r);
        int i;
        for (i = 0; sum <= r; i++)
        {
            sum += bit[i];
        }
        i--;
        while (sum > r)
        {
            sum -= bit[i];
            if (sum < l)
                sum += bit[i];
            i--;
        }
        printf("%I64d\n", sum);
    }
    return 0;
}



内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值