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

Note: ⊕ denotes bitwise exclusive-or.
Input
The input consists of several tests. For each tests:
A single integer n (2≤n<10 500).
A single integer n (2≤n<10 500).
Output
For each tests:
A single integer, the value of the sum.
A single integer, the value of the sum.
Sample Input
3 4
Sample Output
6 4
Author
Xiaoxu Guo (ftiasch)
Source
现在写java都是先写成c++再对着翻译,先上一份,思路跟标程类似
c++
#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
#define TRUE true
#define FALSE false
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 100005
int dfs(int n)
{
if (n <= 2)
return 0;
if (n == 3)
return 6;
int k;
if (n & 1)
{
k = (n - 1) / 2;
return 4 * dfs(k) + 6 * k;
}
else
{
k = n / 2;
return 2 * dfs(k) + 2 * dfs(k - 1) + 4 * k - 4;
}
}
int contribution(int n,int coe)
{
int result;
if(n&1)
{
result=(n/2)*6;
}
else
{
result=n*2-4;
}
return result*coe;
}
struct node
{
int a;
int b;
};
int main()
{
#ifdef DeBUGs
freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
int n;
while (scanf("%d", &n) + 1)
{
// printf("%d\n", dfs(n));
node val;
val.a=1;
val.b=0;
int result=0;
while(n)
{
result=result+contribution(n,val.a)+contribution(n+1,val.b);
node new_nn;
if(n&1)
{
n=n/2;
new_nn.a=val.a*4+val.b*2;
new_nn.b=val.b*2;
}
else
{
n=n/2-1;
new_nn.a=val.a*2;
new_nn.b=val.a*2+val.b*4;
}
val=new_nn;
}
printf("%d\n", result);
}
return 0;
}
java大数翻译
import java.io.*;
import java.util.Scanner;
import java.math.BigInteger;
class Main
{
public static class Node
{
BigInteger a;
BigInteger b;
}
public static BigInteger get(BigInteger n, BigInteger coe )
{
BigInteger result;
if ((n.intValue() & 1) == 1)
{
result = n.divide(BigInteger.valueOf(2)).multiply(BigInteger.valueOf(6));
}
else
{
result = n.multiply(BigInteger.valueOf(2)).add(BigInteger.valueOf(-4));
}
return result.multiply(coe);
}
public static void main(String[]Args) throws FileNotFoundException
{
InputStream in=System.in;
// InputStream in = new FileInputStream(new File("C:\\Users\\Sky\\Desktop\\1.in"));
Scanner s = new Scanner(in);
BigInteger n;
while (s.hasNext())
{
n = s.nextBigInteger();
Node val = new Node();
val.a = BigInteger.valueOf(1);
val.b = BigInteger.valueOf(0);
BigInteger result = BigInteger.ZERO;
while (n.intValue() != 0)
{
result = result.add(get(n, val.a)).add(get(n.add(BigInteger.ONE), val.b));
Node new_val = new Node();
if ((n.intValue() & 1) == 1)
{
n = n.divide(BigInteger.valueOf(2));
new_val.a = val.a.multiply(BigInteger.valueOf(4)).add(val.b.multiply(BigInteger.valueOf(2)));
new_val.b = val.b.multiply(BigInteger.valueOf(2));
}
else
{
n = n.divide(BigInteger.valueOf(2)).add(BigInteger.ONE.negate());
new_val.a = val.a.multiply(BigInteger.valueOf(2));
new_val.b = val.a.multiply(BigInteger.valueOf(2)).add(val.b.multiply(BigInteger.valueOf(4)));
}
val=new_val;
}
System.out.println(result);
}
}
}
直接递归写了发现很慢,后来听锋爷一个hashmap就过了~~O(∩_∩)O~~
c++版
#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
#define TRUE true
#define FALSE false
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 100005
std::map<int, int> mp;
int dfs(int n)
{
printf("%d\n", n);
if(mp[n]!=0)
return mp[n];
if (n <= 2)
return 0;
if (n == 3)
return 6;
int k;
if (n & 1)
{
k = (n - 1) / 2;
mp[n] = 4 * dfs(k) + 6 * k;
return mp[n];
}
else
{
k = n / 2;
mp[n] = 2 * dfs(k) + 2 * dfs(k - 1) + 4 * k - 4;
return mp[n];
}
}
int main()
{
#ifdef DeBUGs
freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
int n;
while (scanf("%d", &n) + 1)
{
cout<<dfs(n)<<endl;
}
return 0;
}
java版
import java.io.*;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Scanner;
class Main
{
public static BigInteger two = BigInteger.valueOf(2);
public static BigInteger three = BigInteger.valueOf(3);
public static BigInteger onefu = BigInteger.ONE.negate();
public static HashMap<BigInteger, BigInteger>hash=new HashMap<BigInteger, BigInteger>();
public static BigInteger dfs(BigInteger n)
{
if(hash.get(n)!=null)
return hash.get(n);
if (n.compareTo(two) <= 0)
return BigInteger.ZERO;
if (n.compareTo(three) == 0)
return BigInteger.valueOf(6);
BigInteger k;
if ((n.intValue() & 1) == 1)
{
k = (n.add(onefu).divide(two));
BigInteger now=BigInteger.valueOf(4).multiply(dfs(k)).add(BigInteger.valueOf(6).multiply(k));
hash.put(n, now);
return now;
}
else
{
k = n.divide(two);
BigInteger now=two.multiply(dfs(k)).add(two.multiply(dfs(k.add(onefu))))
.add(BigInteger.valueOf(4).multiply(k)).add(BigInteger.valueOf(-4));
hash.put(n, now);
return now;
}
}
public static void main(String[] Args) throws FileNotFoundException
{
InputStream in = System.in;
// InputStream in = new FileInputStream(new
// File("C:\\Users\\Sky\\Desktop\\1.in"));
Scanner s = new Scanner(in);
BigInteger n;
while (s.hasNext())
{
n = s.nextBigInteger();
System.out.println(dfs(n));
}
}
}