题目连接

  • 题意:

    每次给一个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. POJ 3630 Phone List(字典树)

    题意 题意:t个case(1<=t<=40),给你n个电话号码(电话号码长度<10)(1 ≤ n ≤ 10000),如果有电话号码是另一个电话号码的前缀,则称这个通讯录是不相容的,判 ...

  2. ubutun lunix 64安装neo4j 图形数据库

    借鉴链接: https://my.oschina.net/zlb1992/blog/915038

  3. OpenJDK源码研究笔记(六)--观察者模式工具类(Observer和Observable)和应用示例

    本文主要讲解OpenJDK观察者模式的2个工具类,java.util.Observer观察者接口,java.util.Observable被观察者基类. 然后,给出了一个常见的观察者应用示例. Obs ...

  4. 用JS中的cookie实现商品的浏览记录

    最近在做一个购物车效果,为了实现商品的浏览记录效果可是让我百般周折,避免以后忘记特写此随笔与大家共享,希望博友们看后有所收获. 第一步:在一个公用的js文件下getCookie(“liulan”),c ...

  5. 帆软FineBI试用

    FineBI是帆软软件有限公司推出的一款商业智能(Business Intelligence)产品,FineBI的本质是通过分析企业已有的信息化数据,帮助企业发现并解决存在的问题,预测模拟企业将来的发 ...

  6. unity gitignore

    /[Ll]ibrary/ /[Tt]emp/ /[Oo]bj/ /[Bb]uild/ /[Bb]uilds/ /Assets/AssetStoreTools* # Autogenerated VS/M ...

  7. Java基础学习总结(23)——GUI编程

    一.AWT介绍 所有的可以显示出来的图形元素都称为Component,Component代表了所有的可见的图形元素,Component里面有一种比较特殊的图形元素叫Container,Containe ...

  8. C#日期控件datetimepicker保存空值方法

    方法一(推荐): 设置datetimepicker的属性ShowCheckBox为true 在窗口初始化时候,添加代码this.datetimepicker1.Checked = false; 保存日 ...

  9. js 阻断网页选中和右键

    $(document).bind("contextmenu", function () { return false; }); $(document).bind("sel ...

  10. 关于Android手机MTP模式连接的一些设置(win7和ubuntu下,以红米1s为例)

    有些手机的MTP模式在电脑上识别不了,须要一些设置才干够,以下就网上收集来的一些设置方法集中贴过来: 一. win7下 參考:http://blog.ammrli.com/?p=1117 1.在设备管 ...