zoukankan      html  css  js  c++  java
  • Codeforces Round #456 (Div. 2) B. New Year's Eve

    传送门:http://codeforces.com/contest/912/problem/B

    B. New Year’s Eve

    time limit per test1 second
    memory limit per test256 megabytes

    Problem Description

    Since Grisha behaved well last year, at New Year’s Eve he was visited by Ded Moroz who brought an enormous bag of gifts with him! The bag contains n sweet candies from the good ol’ bakery, each labeled from 1 to n corresponding to its tastiness. No two candies have the same tastiness.

    The choice of candies has a direct effect on Grisha’s happiness. One can assume that he should take the tastiest ones — but no, the holiday magic turns things upside down. It is the xor-sum of tastinesses that matters, not the ordinary sum!

    A xor-sum of a sequence of integers a1, a2, …, am is defined as the bitwise XOR of all its elements: , here denotes the bitwise XOR operation; more about bitwise XOR can be found here.

    Ded Moroz warned Grisha he has more houses to visit, so Grisha can take no more than k candies from the bag. Help Grisha determine the largest xor-sum (largest xor-sum means maximum happiness!) he can obtain.

    Input

    The sole string contains two integers n and k (1 ≤ k ≤ n ≤ 1018).

    Output

    Output one number — the largest possible xor-sum.

    Examples

    input
    4 3
    output
    7
    input
    6 6
    output
    7

    Note

    In the first sample case, one optimal answer is 1, 2 and 4, giving the xor-sum of 7.

    In the second sample case, one can, for example, take all six candies and obtain the xor-sum of 7.


    解题心得:

    1. 题意就是叫你从1到n中去最多选择k个数,使你选择的数异或起来最大。只要知道异或的运算法则这个题是很简单,直接的出n的二进制数位,然后最大的数就是二进制所有的位全是1就是答案了,如果k是1,那么最大的答案就是n。因为很明显的这些数是从1到n,所以直接选择两数异或起来二进制的各个位变为全1就是最大的了。
    2. 而且这个题的数据范围也给了很大的提示啊,n和k最大都要取到1e18,如果考算法O(n)也过不了啊。

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    ll ans;
    
    ll get_w(ll n)
    {
        ll ans = 0;
        while(n)
        {
            ans++;
            n>>=1;
        }
        return ans;
    }
    
    int main()
    {
        ans = 0;
        ll k,n;
        scanf("%lld%lld",&n,&k);
        ll w = get_w(n);//得到n的二进制的位数
        if(k >= 2)
            ans = pow(2,w)-1;
        else
            ans = n;
        printf("%lld",ans);
        return 0;
    }
  • 相关阅读:
    Web Service 其他服务器检测不到查询测试按钮
    设计模式的原则
    适配器模式
    css局部概念的理解:
    编辑并列DIV
    数据可视化 seaborn绘图(1)
    机器学习笔记(3) 随机森林
    机器学习笔记(2) 集成学习 随机森林先导知识
    机器学习笔记(1)决策树
    【转】各种开源协议及其关系
  • 原文地址:https://www.cnblogs.com/GoldenFingers/p/9107197.html
Copyright © 2011-2022 走看看