Codeforces Round #456 Div.2 B. New Year's Eve
2018-02-20 17:28
435 查看
Problem
codeforces.com/contest/912/problem/BAnalysis
You can choose k or less than k integers from 1~n, calculate maximum xor result(1) k = 1, You can only choose one integer, so you choose n and get the maximum xor result n
(2) k > 1, whatever k is, such as k = 2, or k = 3, ……, or k = n,
if n has x bits as binary code, the maximum xor result must be 2 ^ x - 1
本题是求从1~n个数中取小于或等于k个数(比如n = 4, k = 3表示从1~4中取1个数或2个数或3个数),对这些数进行异或求和,求最大的异或求和值。
(1)k = 1时,即从1~n个数中1个数,那么最大的数自然是n。结果为n
(2)k > 1时,假如n以二进制表示是x位数,则结果必为2 ^ x - 1
Example 1
n = 4, k = 2choose 4 and 3, maximum = 4 xor 3 = 7
可以取4和3这两个数,最大异或结果 = 4 xor 3 = 7 = 2 ^ 3 - 1
Example 2
n = 4, k = 3Choose two numbers, 4 and 3. Or choose three numbers, 4, 1 and 2.
maximum = 4 xor 3 = 4 xor 1 xor 2 = 7 = 2 ^ 3 - 1
可以取4和3这两个数,也可以取4、1、2这三个数。
最大异或结果 = 4 xor 3 = 4 xor 1 xor 2 = 7 = 2 ^ 3 - 1
Example 3
n = 4, k = 4Choose two numbers, 4 and 3. Or choose three numbers, 4, 1 and 2.
maximum = 7 = 2 ^ 3 - 1
You can not choose all for numbers, because 4 xor 1 xor 2 xor 3 xor 4 = 3, which is not the maximum result.
可以取4和3,也可以取4、1、2。最大异或结果为7。
但不能取4、1、2、3。因为4 xor 1 xor 2 xor 3 xor 4 = 3,不是最大的结果。
Example 4
n = 6, k = 6Just choose 1 and 6, maximum = 6 xor 1 = 7 = 2 ^ 3 - 1
只要在1~6中取6和1两个数就行了,6 xor 1 = 7 = 2 ^ 3 - 1
Code
Python2
import sys n, k = map(int, raw_input().split()) if 1 == k: print n sys.exit(0) res = 1 while res < n: res = res * 2 + 1 print res
C Language
#include <stdio.h> #include <stdint.h> int main(void) { int64_t n, k; scanf("%I64d %I64d", &n, &k); if (1 == k) { printf("%I64d\n", n); return 0; } int bit = 0; for (; n >> bit; bit++); printf("%d", n); printf("%d", bit); printf("%I64d\n", (1LL << bit) - 1); return 0; }
更多内容请关注微信公众号
![](http://upload-images.jianshu.io/upload_images/6946981-abb57ac19dd905a0.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
相关文章推荐
- Codeforces Round #456 (Div. 2) ——B. New Year's Eve(思路)
- coderforce Educational Codeforces Round 6 E.New Year Tree
- 【Codeforces Round 375 (Div 2) A】【水题】The New Year Meeting Friends
- Codeforces Round #100 A. New Year Table (几何精度)
- Educational Codeforces Round 6 E. New Year Tree(DFS序+线段树)
- The New Year: Meeting Friends Codeforces Round#375-A
- Codeforces Round #100 A. New Year Table
- Educational Codeforces Round 6 E. New Year Tree
- Codeforces Round #100//140C - New Year Snowmen C. New Year Snowmen
- 【Educational Codeforces Round 6E】【线段树 dfs序】New Year Tree 子树颜色修改子树颜色数
- Educational Codeforces Round 6 E. New Year Tree(dfs序线段树)
- Codeforces Round #375 (Div. 2) A. The New Year: Meeting Friends 水题
- Codeforces Round #100 (Div. 2) A - New Year Table
- New Year Tree CodeForces - 620E(dfs序+线段树)
- Codeforces Round #471 div.2 ABC题解
- codeforces Round #Pi (div.2) 567ABCD
- Codeforces Round #449 div.2 题解及心路历程
- A. Puzzles CodeForces Round #196 (Div.2)
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- E - New Year and North Pole CodeForces - 750B(状态处理下就好了)