Sum vs XOR
https://www.hackerrank.com/contests/hourrank-13/challenges/arthur-and-coprimes
要求找出所有x <= n x + n = x ^ n的个数。
首先如果n的二进制是100101010 n = 298
这样的话
先考虑最高位的1,如果x在同一个位上也是1,就是x是100000000这样的话,是不行的,因为异或并不能使那一位变成0,而加法却把他们变了(进位了),所以,如果那个固定是1,那么x从100000000 --- 100101010都是不行的
考虑第二个1,同样道理,如果这位是1,那么其它的无论怎么取也不行。
那么现在问题就是转化为:在这么多个1中,任选若干个固定,其他的0任取什么值也没问题,有多少种方案。、
怎么算是任选若干个呢,那么先考虑固定第二个1,然后往左统计0的个数(不统计1的个数),右边的全部当作0.
这样贡献 +pow(2, len)。然后枚举第三个1,一路做下去即可
这个时候如果左边还有1,那么只能在那位放0了,因为前面枚举那个位固定为1的时候,已经考虑了后面那些1的位置的两种情况,‘
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const LL one = ;
char str[];
int lenstr;
void bin(LL n) {
if (n / ) {
bin(n / );
}
str[lenstr++] = n % + '';
}
void work() {
// int n;
// cin >> n;
// int ans = 0;
//// cout << n << endl;
// for (int i = 0; i <= n; ++i) {
// int t = n ^ i;
//// cout << t << " fff" << endl;
// if (t == n + i) ++ans;
// }
// cout << ans << endl;
LL n;
cin >> n;
LL ans = ;
for (int i = ; i >= ; --i) {
if ((n & (one << i)) > ) {
ans += n - (one << i) + ;
break;
}
}
bin(n);
for (int i = ; i < lenstr; ++i) {
if (str[i] == '') {
int t = lenstr - - i;
for (int j = i - ; j > ; --j) {
if (str[j] == '') t++;
}
ans += pow(, t);
}
}
cout << n + - ans << endl;
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
work();
return ;
}
复杂度是logn
Sum vs XOR的更多相关文章
- Sum of xor
Sum of xor jdoj-2160 题目大意:给你一个n,求1^2^...^n. 注释:$n<=10^{18}$. 想法:第一道异或的题.先来介绍一下什么是异或.a^b表示分别将两个数变成 ...
- UVALive8518 Sum of xor sum
题目链接:https://vjudge.net/problem/UVALive-8518 题目大意: 给定一个长度为 $N$ 的数字序列 $A$,进行 $Q$ 次询问,每次询问 $[L,R]$,需要回 ...
- SOJ 4309 Sum of xor 异或/思维
Source ftiasch 解题思路: 本题的题解有参考这里,但是那篇年代太久远,讲的也不甚清晰,所以可能会对很多新手造成困扰,所以又写了这一篇. 亦或有很多规律,本题使用到的是n^(n+1)=1, ...
- AtCoder ABC 129E Sum Equals Xor
题目链接:https://atcoder.jp/contests/abc129/tasks/abc129_e 题目大意 给定一个二进制表示的数 L,问有多少对自然数 (a, b) 满足 $a + b ...
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)
题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...
- [USACO]6.1.3 cow xor(二进制+Trie)
题意:给你一个序列(n<=100000),求出一个连续的子序列[i,j]使得ai xor ai+1 xor…… xor aj最大,求出这个最大值(其中每个数<=2^21) 分析:题目和求一 ...
- codeforces 242E. XOR on Segment 线段树
题目链接 给n个数, 两种操作, 一种是求区间内的数的和, 一种是将区间内的数异或x. 异或x没有什么思路, 单个异或肯定超时, 区间异或也没有办法做....后来才知道可以按位建线段树, 这样建20棵 ...
- ACDream - Xor pairs
先上题目: Xor pairs Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Sub ...
- HDU 3397 Sequence operation
题目:下列操作 Change operations:0 a b change all characters into '0's in [a , b]1 a b change all character ...
随机推荐
- 洛谷【P1080】国王游戏
我对贪心的理解:https://www.cnblogs.com/AKMer/p/9776293.html 题目传送门:https://www.luogu.org/problemnew/show/P10 ...
- IP 地址漂移
1.概念 应用访问虚拟ip,当主服务器正常工作时,虚拟ip指向主服务器,当主服务器宕掉后,虚拟ip自动指向从服务器,当主服务器被人修好后,再自动指向主服务器, 这种虚拟ip的指向方式称为ip地址漂移. ...
- asp.net中日志框架Log4Net的使用
Log4Net是用来记录日志的,可以将程序运行过程中的信息输出到一些地方(文件.数据库.EventLog等),日志就是程序的黑匣子,可以通过日志查看系统的运行过程,从而发现系统的问题.日志的作用:将运 ...
- 【转】 Pro Android学习笔记(四九):ActionBar(2):Action图标区
目录(?)[-] ActionBar的隐藏和现实 ActionBar的action图标区 ActionBar的隐藏和现实 ActionBar bar = getActionBar();bar.hide ...
- keil5编译时出现 MDK-Pro middleware is not allowed with this license
转载请注明出处:http://blog.csdn.net/qq_26093511/article/details/51700961 最近在用MDK编译一个别人的工程时,有如上提示: 1.原因是没有注册 ...
- phpstorm断点调试 php.ini 文件中 Xdebug 配置
[XDebug]xdebug.profiler_output_dir="D:\phpStudy\tmp\xdebug"xdebug.trace_output_dir="D ...
- javascript面向对象的测试实例
- Hive之数据查询
Hive之数据查询 发布于:2013 年 10 月 11 日 由 Aaron发布于: Hive 一,排序和聚合 对于排序有两种方式,一种是order by 一种是sort by order by 会对 ...
- Entity Framework Code-First(9.9):DataAnnotations - ForeignKey Attribute
DataAnnotations - ForeignKey Attribute: ForeignKey attribute can be applied to properties of a class ...
- JavaScript学习系列1 基础-变量
1. 在一次面试中,被问到JavaScript中使用严格模式有什么区别, 这里的严格模式指的是 ECMAScript5严格模式 我们首先来看看普通模式,也就是非严格模式 a = 5; console. ...