题目连接

  • 题意:

    每次给一个n。求

    (2≤n<10500)
  • 分析:

    先说一下自己的想法,假设将n换成二进制数,也就一两千位左右,那么一位一位处理是能够接受的。

    将0-n写成二进制形式后,显然全部数某一个二进制位是有一个循环节的。那么我们就能够从这里入手直接求解

import java.io.*;
import java.math.*;
import java.util.*; public class Main {
public static BigInteger zero = BigInteger.ZERO;
public static BigInteger one = BigInteger.ONE;
public static BigInteger two = BigInteger.valueOf(2);
public static BigInteger three = BigInteger.valueOf(3);
public static BigInteger four = BigInteger.valueOf(4);
public static BigInteger six = BigInteger.valueOf(6); public static BigInteger Down(BigInteger now, BigInteger L) {
BigInteger mid = now.divide(L).multiply(L).add(L.shiftRight(1));
if (now.subtract(mid).signum() < 0)
return mid;
return mid.add(L.shiftRight(1));
} public static BigInteger Up(BigInteger now, BigInteger L) {
BigInteger start = now.divide(L).multiply(L);
BigInteger mid = start.add(L.shiftRight(1));
if (now.subtract(mid).signum() < 0)
return start.subtract(one);
return mid.subtract(one);
} public static int getValue(BigInteger now, BigInteger L) {
BigInteger mid = now.divide(L).multiply(L).add(L.shiftRight(1));
if (now.subtract(mid).signum() < 0)
return 0;
return 1;
} public static BigInteger solve(BigInteger nl, BigInteger nr, BigInteger gl, BigInteger L) {
BigInteger ret = zero, step = Down(nl, L).subtract(nl), t = nr.subtract(Up(nr, L));
if (step.subtract(t).signum() > 0)
step = t;
while (nl.add(step).subtract(gl).signum() <= 0) {
if ((getValue(nl, L) ^ getValue(nr, L)) == 1)
ret = ret.add(step);
nl = nl.add(step); nr = nr.subtract(step);
step = Down(nl, L).subtract(nl); t = nr.subtract(Up(nr, L));
if (step.subtract(t).signum() > 0)
step = t;
}
if (gl.subtract(nl).add(one).signum() >= 0 && (getValue(nl, L) ^ getValue(nr, L)) == 1)
ret = ret.add(gl.subtract(nl).add(one));
return ret;
} public static void main(String[] args) {
BigInteger n, L, tans, nl, ans;
Scanner cin = new Scanner(System.in);
while (cin.hasNext()) {
n = cin.nextBigInteger();
L = two;
ans = zero;
while (L.subtract(n.shiftLeft(1)).signum() <= 0)//(L <= n * 2)
{
tans = zero;
if (n.divide(L).shiftRight(1).signum() > 0) {
tans = solve(zero, n, L.subtract(one), L);
}
nl = n.divide(L).shiftRight(1).multiply(L);
tans = n.divide(L).shiftRight(1).multiply(tans).add(solve(nl, n.subtract(nl), n.subtract(one).shiftRight(1), L));
ans = ans.add(tans.multiply(L));
L = L.shiftLeft(1);
}
System.out.println(ans.subtract(n.shiftLeft(1)));
}
}
}

学习一下题解的方法。关键在于:(2 * k) ^ x = (2 * k + 1) ^ x

之后就学习一下题解的公式化简方法了



import java.util.*;
import java.math.*; public class Main {
static BigInteger n, ret;
static BigInteger one = BigInteger.valueOf(1);
static BigInteger two = BigInteger.valueOf(2);
static BigInteger four = BigInteger.valueOf(4);
static BigInteger six = BigInteger.valueOf(6);
static HashMap<BigInteger, BigInteger> mp = new HashMap<BigInteger, BigInteger>();
public static BigInteger fun(BigInteger n) {
if (n.equals(BigInteger.ZERO) || n.equals(BigInteger.ONE))
return BigInteger.ZERO;
if (mp.containsKey(n))
return mp.get(n);
BigInteger k = n.shiftRight(1);
if (n.testBit(0)) {
ret = four.multiply(fun(k)).add(six.multiply(k));
mp.put(n, ret);
return ret;
}
else {
ret = (fun(k).add(fun(k.subtract(one))).add(k.shiftLeft(1)).subtract(two)).shiftLeft(1);
mp.put(n, ret);
return ret;
}
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while (cin.hasNext()) {
n = cin.nextBigInteger();
mp.clear();
System.out.println(fun(n));
}
}
}

Exclusive or的更多相关文章

  1. ORA-01102: cannot mount database in EXCLUSIVE mode

    安装完ORACEL 10g数据库后,启动数据库时遇到ORA-01102: cannot mount database in EXCLUSIVE mode [oracle@DB-Server ~]$ s ...

  2. Activiti之 Exclusive Gateway

    一.Exclusive Gateway Exclusive Gateway(也称为XOR网关或更多技术基于数据的排他网关)经常用做决定流程的流转方向.当流程到达该网关的时候,所有的流出序列流到按照已定 ...

  3. 启动weblogic的错误:Could not obtain an exclusive lock to the embedded LDAP data files directory

    http://hi.baidu.com/kaisep/item/0e4bf6ee5da001d1ea34c986 源地址 启动weblogic的错误:Could not obtain an exclu ...

  4. informatica9.5.1资源库为machine in exclusive mode(REP_51821)

    错误信息: [PCSF_10007]Cannot connect to repository [Rs_RotKang] because [REP_51821]Repository Service is ...

  5. Delphi 异或,英文为exclusive OR,或缩写成xor

    异或,英文为exclusive OR,或缩写成xor 异或(xor)是一个数学运算符.它应用于逻辑运算.异或的数学符号为“⊕”,计算机符号为“xor”.其运算法则为: a⊕b = (¬a ∧ b) ∨ ...

  6. HDU 4919 Exclusive or (数论 or 打表找规律)

    Exclusive or 题目链接: http://acm.hust.edu.cn/vjudge/contest/121336#problem/J Description Given n, find ...

  7. If one session has a shared or exclusive lock on record R in an index, another session cannot insert

    If one session has a shared or exclusive lock on record R in an index, another session cannot insert ...

  8. Shared and Exclusive Locks 共享和排它锁

    14.5 InnoDB Locking and Transaction Model InnoDB 锁和事务模型 14.5.1 InnoDB Locking 14.5.2 InnoDB Transact ...

  9. ORA-19573: cannot obtain exclusive enqueue for datafile 1

    还原Oracle数据库时出现ORA-19870和ORA-19573错误,如: RMAN> restore database; Starting restore at 11-DEC-12 usin ...

  10. hdu 4919 Exclusive or

    Exclusive or Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) T ...

随机推荐

  1. Laravel API 允许跨域访问

    服务器A请求服务器B的接口,那么一般会出现跨域问题.全解跨域请求处理办法 XMLHttpRequest cannot load http://api.console.vms3.com/api/user ...

  2. 我的CSDN原创高质量免积分下载资源列表(持续更新)

    最近几个月,我在CSDN平台,发表了大量原创高质量的项目,并给出了相应的源码.文档等相关资源. 为了方便CSDN用户或潜在需求者,下载到自己想要的资源,特分类整理出来,欢迎大家下载. 我的原则:原创高 ...

  3. photoshop快捷键汇总

    图层应用相关快捷键: 复制图层:Ctrl+j 盖印图层:Ctrl+Alt+Shift+E 向下合并图层:Ctrl+E 合并可见图层:Ctrl+Shift+E 激活上一图层:Alt+中括号(]) 激活下 ...

  4. 【Henu ACM Round#20 A】 Fancy Fence

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 看看有没有(n-2)*180/n等于输入的a就好. [代码] #include <bits/stdc++.h> usin ...

  5. [Python] Accessing Array Elements

    NumPy Reference: Indexing Note: Indexing starts at 0 (zero). def slicing(): a = np.random.rand(5,4) ...

  6. POJ2369 Permutations【置换群】

    题目链接: http://poj.org/problem?id=2369 题目大意: 给定一个序列.问最少须要多少次置换才干变为 1.2.-.N 的有序序列.比方说给 定5个数的序列 4 1 5 2 ...

  7. Protocol Buffers的基础说明和使用

    我们開始须要使用protobuf的原因是,在处理OJ的contest模块的时候,碰到一个问题就是生成contestRank的时候.须要存储非常多信息. 假设我们採用model存储的话,那么一方面兴许假 ...

  8. NET Remoting原理及应用

    .NET Remoting原理及应用实例: Remoting:(本文摘自百度百科) 简介:        什么是Remoting,简而言之,我们可以将其看作是一种分布式处理方 式.从微软的产品角度来看 ...

  9. javascript学习笔记总结

    1 有些浏览器可能不支持JavaScript,我们可以使用如下的方法对它们隐藏JavaScript代码. <html> <body> <script type=" ...

  10. Vue中对data的操作

    1. {{a}} var vm = new Vue({ el: '#app', data: { a: { a: 1, b: 2 } } })   vm.a.c = 'sadoisad' // 按理说是 ...