Exclusive or
- 题意:
每次给一个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的更多相关文章
- ORA-01102: cannot mount database in EXCLUSIVE mode
安装完ORACEL 10g数据库后,启动数据库时遇到ORA-01102: cannot mount database in EXCLUSIVE mode [oracle@DB-Server ~]$ s ...
- Activiti之 Exclusive Gateway
一.Exclusive Gateway Exclusive Gateway(也称为XOR网关或更多技术基于数据的排他网关)经常用做决定流程的流转方向.当流程到达该网关的时候,所有的流出序列流到按照已定 ...
- 启动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 ...
- informatica9.5.1资源库为machine in exclusive mode(REP_51821)
错误信息: [PCSF_10007]Cannot connect to repository [Rs_RotKang] because [REP_51821]Repository Service is ...
- Delphi 异或,英文为exclusive OR,或缩写成xor
异或,英文为exclusive OR,或缩写成xor 异或(xor)是一个数学运算符.它应用于逻辑运算.异或的数学符号为“⊕”,计算机符号为“xor”.其运算法则为: a⊕b = (¬a ∧ b) ∨ ...
- HDU 4919 Exclusive or (数论 or 打表找规律)
Exclusive or 题目链接: http://acm.hust.edu.cn/vjudge/contest/121336#problem/J Description Given n, find ...
- 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 ...
- Shared and Exclusive Locks 共享和排它锁
14.5 InnoDB Locking and Transaction Model InnoDB 锁和事务模型 14.5.1 InnoDB Locking 14.5.2 InnoDB Transact ...
- ORA-19573: cannot obtain exclusive enqueue for datafile 1
还原Oracle数据库时出现ORA-19870和ORA-19573错误,如: RMAN> restore database; Starting restore at 11-DEC-12 usin ...
- hdu 4919 Exclusive or
Exclusive or Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) T ...
随机推荐
- Python学习七步走
在周五的下午三点钟(为什么是这个时间?因为事情总会在周五下午三点钟发生),你收到一条通知,客户发现你的软件出现一个错误.在有了初步的怀疑后,你联系运维,查看你的软件日志以了解发生了什么,因为你记得收到 ...
- 【模板】后缀排序(SA数组)
[模板]后缀排序 题目背景 这是一道模板题. 题目描述 读入一个长度为 \(n\) 的由大小写英文字母或数字组成的字符串,请把这个字符串的所有非空后缀按字典序从小到大排序,然后按顺序输出后缀的第一个字 ...
- OpenJDK源码研究笔记(七)–Java字节码文件(.class)的结构
最近在看OpenJDK源码的过程中,顺便看了Java编译器(javac)的源码. 为了理解javac的源码,需要先搞懂Java字节码文件(.class)的结构. 于是,我就认真看了下OpenJDK中J ...
- 【Codeforces Round #421 (Div. 2) A】Mister B and Book Reading
[题目链接]:http://codeforces.com/contest/820/problem/A [题意] 每天看书能看v页; 且这个v每天能增加a; 但是v有上限v1; 然后每天还必须往回看t页 ...
- hdu 1575 矩阵连乘2
#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> ...
- Android Recovery OTA升级(一)—— make otapackage
文件夹 文件夹 概述 make otapackage BUILT_TARGET_FILES_PACKAGE ota_from_target_files WriteFullOTAPackage Sign ...
- Hadoop HDFS (4) Hadoop Archives
用HDFS存储小文件是不经济的,由于每一个文件都存在一个block里,每一个block的metadata又在namenode的内存里存着,所以,大量的小文件.会吃掉大量的namenode的内存.(注意 ...
- 《AndroidStudio每日一贴》11. 重构之提炼为常量
提炼为常量是从暂时变量高速提炼出静态常量,这也是我们经常使用的重构手段. 很多其它有用技巧请查看<AndroidStudio有用指南> 操作步骤: ➤ 菜单条: Refactor -> ...
- ADO.NET数据读取封装
public class sqlserver { //private string sqlstr = System.ConfigurationManager.ConnectionStrings[&qu ...
- Servlet之doPost获取表单参数
/** * 获取表单参数 */ private void readForm() { // TODO Auto-generated method stub Enumeration e = request ...