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的更多相关文章

  1. Sum of xor

    Sum of xor jdoj-2160 题目大意:给你一个n,求1^2^...^n. 注释:$n<=10^{18}$. 想法:第一道异或的题.先来介绍一下什么是异或.a^b表示分别将两个数变成 ...

  2. UVALive8518 Sum of xor sum

    题目链接:https://vjudge.net/problem/UVALive-8518 题目大意: 给定一个长度为 $N$ 的数字序列 $A$,进行 $Q$ 次询问,每次询问 $[L,R]$,需要回 ...

  3. SOJ 4309 Sum of xor 异或/思维

    Source ftiasch 解题思路: 本题的题解有参考这里,但是那篇年代太久远,讲的也不甚清晰,所以可能会对很多新手造成困扰,所以又写了这一篇. 亦或有很多规律,本题使用到的是n^(n+1)=1, ...

  4. AtCoder ABC 129E Sum Equals Xor

    题目链接:https://atcoder.jp/contests/abc129/tasks/abc129_e 题目大意 给定一个二进制表示的数 L,问有多少对自然数 (a, b) 满足 $a + b ...

  5. Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)

    题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...

  6. [USACO]6.1.3 cow xor(二进制+Trie)

    题意:给你一个序列(n<=100000),求出一个连续的子序列[i,j]使得ai xor ai+1 xor…… xor aj最大,求出这个最大值(其中每个数<=2^21) 分析:题目和求一 ...

  7. codeforces 242E. XOR on Segment 线段树

    题目链接 给n个数, 两种操作, 一种是求区间内的数的和, 一种是将区间内的数异或x. 异或x没有什么思路, 单个异或肯定超时, 区间异或也没有办法做....后来才知道可以按位建线段树, 这样建20棵 ...

  8. ACDream - Xor pairs

    先上题目: Xor pairs Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Sub ...

  9. HDU 3397 Sequence operation

    题目:下列操作 Change operations:0 a b change all characters into '0's in [a , b]1 a b change all character ...

随机推荐

  1. Poj 2411 Mondriaan's Dream(压缩矩阵DP)

    一.Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, ...

  2. 3 K8s安裝ELK+filebeat

    1 Filebeat: apiVersion: v1 kind: Service metadata: name: XX spec: ports: - name: http port: targetPo ...

  3. C#设计模式(11)——外观模式

    一.概念 外观模式提供了一个统一的接口,用来访问子系统中的一群接口.外观定义了一个高层接口,让子系统更容易使用.使用外观模式时,我们创建了一个统一的类,用来包装子系统中一个或多个复杂的类,客户端可以直 ...

  4. [Manacher+bit]Palindrome

    https://nanti.jisuanke.com/t/15428 题目大意:离散表示的字符串,求其最长回文串长度. 解题关键:若只用Manacher算法,在统计sum时会超时,所以加一个树状数组来 ...

  5. Shrio00 Shiro角色授权、Shiro权限授权、开启Shiro缓存

    1 需求01 用户进行过认证登录后,某些接口是有权限限制的:如何实现只有相应权限的用户才可以调用相应接口 2 修改shiro配置类  ShiroConfiguration package cn.xia ...

  6. 树莓派 Learning 002 装机后的必要操作 --- 02 解决中文问题

    树莓派 装机后的必要操作 - 解决中文问题 我的树莓派型号:Raspberry Pi 2 Model B V1.1 装机系统:NOOBS v1.9.2 每一块树莓派,装机后都应该执行的步骤 刚装机后, ...

  7. 38、EST序列拼接流程

    转载:http://fhqdddddd.blog.163.com/blog/static/18699154201241014835362/ http://blog.sina.com.cn/s/blog ...

  8. StringBuffer输出

    public class Test { public static void main(String[] args) { StringBuffer a = new StringBuffer(" ...

  9. 1.jQuery入口函数 与javaScript入口函数

    1.jQuery入口函数 与javaScript入口函数 JQ入口函数: $(document).ready(function(){ }); 或者 $(function(){ }) Js入口函数: w ...

  10. js 把字符串变成函数

    js 把字符串变成函数 eval("(" + fieldEventss[1]+")");