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

时间: 2024-10-31 18:54:49

Exclusive or的相关文章

错误 ORA-01102: cannot mount database in EXCLUSIVE mode 的处理方法

问题描述: 在RAC环境中,想开启归档日志,其中使用 SQL> alter system set cluster_database=false scope=spfile;-->把集群修改为单实例 修改好后主节点,恢复集群双节点 SQL> alter system set cluster_database=true scope=spfile;-->把集群修改为双实例 当前数据库主节点已经open状态,查询状态是open 备节点已是shutdown状态,启动当前节点时报: ORA-01

ORA-01102: cannot mount database in EXCLUSIVE mode

ORA-01102: cannot mount database in EXCLUSIVE mode 安装完ORACEL 10g数据库后,启动数据库时遇到ORA-01102: cannot mount database in EXCLUSIVE mode [[email protected] ~]$ sqlplus / as sysdba   SQL*Plus: Release 10.2.0.1.0 - Production on Thu Apr 10 13:53:08 2014   Copyr

Exclusive access control to a processing resource

A data processing system is provided with multiple processors that share a main memory. Semaphore values associated with data elements within the memory system, including the main memory, are used to establish exclusive access permissions to those da

VMware强制关闭,造成(failed to get exclusive lock on the configuration file...)错误

参考文章:http://blog.csdn.net/qyee16/article/details/6764753 笔记本蓝屏,造成VMware非法关闭,开机重启VMware后,发现无法打开虚拟机了,报错如下: 点击确定后,出现Internal Error错误: 解决办法很简单,打开任务管理器 --> 进程,然后把所有vmware相关的进程都关闭,在重新打开vmware就好了: 红框中为需要关闭的进程 VMware强制关闭,造成(failed to get exclusive lock on th

ORA-19573: cannot obtain exclusive enqueue for datafile 1

还原Oracle数据库时出现ORA-19870和ORA-19573错误,如: RMAN> restore database; Starting restore at 11-DEC-12 using channel ORA_DISK_1 channel ORA_DISK_1: starting datafile backupset restore channel ORA_DISK_1: specifying datafile(s) to restore from backup set restor

Android事务 IMMEDIATE与EXCLUSIVE模式

事务是数据库保证数据唯一性和一致性的技术,对于数据库一个或一组写操作要保证是一个原子操作就需要使用事务,android使用事务的常见形式如下: SQLiteDatabase db = null; ...  db.beginTransaction(); try {    db.setTransactionSuccessful();    ... } finally {    db.endTransaction(); } 那么db.beginTransaction是一个什么操作? 我们来看下SQLi

domino用户foxmail接收邮件时报“Maildrop opened for exclusive access by another user”的解决办法

[背景] 近期,不少用户foxmail接收domino系统的邮件时报"Maildrop opened for exclusive access by another user". [解决办法] 在notes.ini增加参数POP_SESSION_TIMEOUT=10问题不在出现(可以在控制台输入 set config POP_SESSION_TIMEOUT=10) 参考资料 http://www-01.ibm.com/support/docview.wss?uid=swg2124758

oracle 11g rac启动报错ORA-01102: cannot mount database in EXCLUSIVE mode

启动oracle 11g rac数据库时出现以下错误,只能启动其中一个节点,另一个节点启动不了,可能是以前修改cluster_database这个参数引起的.在oralc rac架构这个参数必须设置为TRUE. 错误信息: ORA-01102: cannot mount database in EXCLUSIVE mode 解决方法: rac1节点: SQL> show parameter cluster_database; NAME                              

hdu 4919 Exclusive or

Exclusive or Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 327    Accepted Submission(s): 137 Problem Description Given n, find the value of Note: ⊕ denotes bitwise exclusive-or. Input The