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 input consists of several tests. For each tests:

A single integer n (2≤n<10500).

Output

For each tests:

A single integer, the value of the sum.

Sample Input

3
4

Sample Output

6
4

Author

Xiaoxu Guo (ftiasch)

Source

2014 Multi-University Training Contest 5

题解:

下面是官方给的答案:

鉴于比较难以理解官方大神的讲解,这里我研究了一下,下面给出求解化简过程:

图片有些不清晰,凑合看吧 T^T

代码:

import java.util.*;
import java.io.*;
import java.math.*;

public class Main {

	public static BigInteger zero=BigInteger.valueOf(0);
	public static BigInteger one=BigInteger.valueOf(1);
	public static BigInteger two=BigInteger.valueOf(2);
	public static BigInteger four=BigInteger.valueOf(4);
	public static BigInteger six=BigInteger.valueOf(6); 

	public static HashMap<BigInteger,BigInteger> map=new HashMap<BigInteger,BigInteger>();

	public static BigInteger solve(BigInteger n)
	{
		if(map.containsKey(n))
			return map.get(n);
		BigInteger t=BigInteger.valueOf(0);
		BigInteger k=n.divide(two);
		BigInteger r=n.mod(two);

		if(r.compareTo(one)==0)
			t=solve(k).multiply(four).add(k.multiply(six));
		else  {
			t=two.multiply(solve(k));
			t=t.add(two.multiply(solve(k.subtract(one))));
			t=t.add(four.multiply(k));
			t=t.subtract(four);
		}
		map.put(n, t);
		return t;
	}

	public static void main(String []args)
	{
        BigInteger n;
        map.put(zero, zero);
        map.put(one,zero);
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext())
        {
        	n=cin.nextBigInteger();
        	System.out.println(solve(n));
        }
	}
}
/*
借鉴别人的代码,学习了一下java大数和HashMap的用法,可以当作模版来使用了。

*转载请注明出处,谢谢。
*/

hdu 4919 Exclusive or

时间: 2024-08-27 15:04:41

hdu 4919 Exclusive or的相关文章

[JAVA]HDU 4919 Exclusive or

题意很简单, 就是给个n, 算下面这个式子的值. 重点是n的范围:2≤n<10500. 比赛的时候 OEIS一下得到了一个公式: a[0]=a[1]=a[2]=0; n为偶数 : 2*a(n/2)+2*a(n/2-1)+4*(n/2-1); n为奇数 : 4*a((n-1)/2)+6*((n-1)/2) 然后勇敢的打了一发暴力...想也知道肯定TLE... 之后 学到了一种机智的按位算的方法 1 import java.io.*; 2 import java.util.*; 3 import j

HDU 4919 打表找规律 java大数 map 递归

== oeis: 点击打开链接 瞎了,x.mod(BigInteger.ValueOf(2)).equal( BigInteger.ValueOf(1)) 写成了 x.mod(BigInteger.ValueOf(2)).equal( 1 ) T^T100块没了... import java.math.*; import java.util.*; import static java.lang.System.out; import java.io.*; public class Main { s

HDU 5175 Misaki&#39;s Kiss again (异或运算,公式变形)

Misaki's Kiss again Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 201    Accepted Submission(s): 57 Problem Description After the Ferries Wheel, many friends hope to receive the Misaki's kiss

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi&#39;an Online) 题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Number Sequence Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequ

hdu 1710 Binary Tree Traversals 前序遍历和中序推后序

题链;http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4205    Accepted Submission(s): 1904 Problem Description A binary tree i

hdu 5008(2014 ACM/ICPC Asia Regional Xi&#39;an Online ) Boring String Problem(后缀数组&amp;二分)

Boring String Problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 219    Accepted Submission(s): 45 Problem Description In this problem, you are given a string s and q queries. For each que

HDU 5014 Number Sequence(异或 进制问题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequence b, the inte

hdu 4920 Matrix multiplication(矩阵乘法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4920 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 989    Accepted Submission(s): 396 Problem Description Given two matr

HDU 1710 二叉树的遍历 Binary Tree Traversals

Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4330    Accepted Submission(s): 1970 Problem Description A binary tree is a finite set of vertices that is either empty or