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 ...
随机推荐
- Java web application——Listener
应用程序事件提供ServletContext和HttpSession以及ServletRequest对象状态更改的通知,用户编写响应状态更改的事件监听器类,并配置和部署他们.Servlet容器会调用事 ...
- Linux 下安装 redis 详情
一:将redis 压缩包上传到 Linux usr/local下 (一):在local 下创建一个 redis 目录 (二):上传redis压缩包到此目录下. 二:Linux 进入 local目录下 ...
- Pythonx_day1
# python3中的 str 和 byte(即二进制)转换 msg = "β" # 转换为二进制,打印,‘encoding = 'utf-8'为值定转换原str的编码格式’ pr ...
- Raphaeljs入门到精通(一)
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...
- [DLX精确覆盖+打表] hdu 2518 Dominoes
题意: 就是给12种图形,旋转,翻折.有多少种方法构成n*m=60的矩形 思路: 裸的精确覆盖.就是建图麻烦 个人太挫,直接手写每一个图形的各种形态 须要注意的是最后的答案须要除以4 代码: #inc ...
- MAC 文件加密
MAC 文件加密 (2011-10-08 00:08:52) 标签: 杂谈 分类: MAC技巧篇 我们都有一些私密的文件需要保存,在苹果操作系统下,如何为这些文件或文件夹设置密码呢? 利用系统自带的创 ...
- Metasploit - Tips for Evading Anti-Virus
绕过杀毒软件,有很多钟方法.此处介绍一种,编写python程序调用shellcode,并使用Pyinstaler将python程序编译为exe程序. 准备工作:(Windows XP环境下编译) 将P ...
- ddr sdram self-refresh & auto-refresh
以下是EDD5116AFTA数据手册的摘录.不过看过了还是不太明白二者的区别. self-refresh:Self-refresh entry [SELF]This command starts se ...
- LVDS原理及设计指南
LVDS是一种低摆幅的差分信号技术,它使得信号能在差分PCB 线对或平衡电缆上以 几百Mbps的速率传输,其低压幅和低电流驱动输出实现了低噪声和低功耗. IEEE 在两个标准中对LVDS ...
- 英语影视台词---二、Inception
英语影视台词---二.Inception 一.总结 一句话总结:盗梦空间 1.You're waiting for a train..A train that will take you far aw ...