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 ...
随机推荐
- PHP取不定个数数组交集
最近有个需求,有一个N个二维数组,N是动态的,不固定个数,现需取这N个数组的交集内容. 用到的函数是array_intersect_assoc 用法 $result_arr = array_inter ...
- P2633 Count on a tree(主席树)
题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个 ...
- 洛谷 P2839 畅通工程
P2839 畅通工程 题目描述 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连, ...
- HBase为什么快 HBase原理。 HBase几个问题
背景色表示可以自己做实验搞定 1 模拟一组数据 1.2.3.4.5.6.7.8.9.10 1 入 限定符 'one' 2 入 'two' 3 入 'three' 4 f ...
- Service绑定模式
Service绑定模式 使用绑定的Service能够实现组件与Service的通信. 组件与被绑定的Service能够不归属于同一个应用程序.因此通过绑定Service能够实现进程间通信. ...
- nyoj 1104 just for you
just for you 时间限制:1000 ms | 内存限制:65535 KB 难度:0 描写叙述 今天tlp和ly想去看电影了到了电影院才发现买票的人特别多 .ly不想让tlp等着急了,就先 ...
- HDOJ 5098 Smart Software Installer 拓扑排序
拓扑排序: 两个队列,一个放不须要重新启动入度为0的,一个放须要重新启动入度为0的....从不须要重新启动的队列開始,每弹出一个数就更新下入度,遇到入读为0的就增加到对应队列里,当队列空时,记录重新启 ...
- Install Qt 5 on Ubuntu(使用qt-opensource-linux-x64-5.7.0.run进行安装,而且是官方的wiki)
Introduction This is a tutorial for installation of Qt 5.7.0 to Ubuntu 12.10. It may be used also fo ...
- jFinal 关联数据库操作
1.建数据库(我用的是oracle数据库,其他的相对也差不多) -- Create table create table CLASSES ( classesid NUMBER not null, cl ...
- Android真机调试访问本地服务器(localhost)的解决方案
Android系统把它自己作为了localhost!当连接localhost都是他自己啊.. 囧,在这里晕了好久才发现.. 网上介绍的都是模拟器连接本地服务器的,我试着把链接改为http://10.0 ...