题意很简单, 就是给个n, 算下面这个式子的值.

$\sum\limits_{i=1}^{n-1} i \otimes (n-i)$

重点是n的范围:2≤n<10500

比赛的时候 OEIS一下得到了一个公式:

$a_0=a_1=a_2=0$;

n为偶数 : $2 \times a_{\frac{n}{2}}+2 \times a_{\frac{n}{2}-1}+4\times (\frac{n}{2}-1) $

n为奇数 : $4\times a_{\frac{n-1}{2}}+6\times\frac{n-1}{2}$

然后勇敢的打了一发暴力...想也知道肯定TLE...

之后 学到了一种机智的按位算的方法

 import java.io.*;
import java.util.*;
import java.math.*; public class Main
{
static BigInteger yi=BigInteger.ONE;
static BigInteger er=BigInteger.valueOf(2);
static BigInteger li=BigInteger.ZERO;
public static void main(String[] args)
{
InputReader in = new InputReader();
PrintWriter out = new PrintWriter(System.out);
BigInteger []bit=new BigInteger[2005];
bit[0]=yi;
for(int i=1; i<=2000; i++)
bit[i]=bit[i-1].multiply(er);
while(in.hasNext())
{
BigInteger n=new BigInteger(in.next());
int []wei=new int[2005];
int d=0;
BigInteger []a=new BigInteger[2005];
BigInteger []b=new BigInteger[2005];
BigInteger tmp=n;
while(tmp.compareTo(li)!=0)
{
if(tmp.mod(er).equals(li))
wei[d++]=0;
else
wei[d++]=1;
tmp=tmp.divide(er);
}
BigInteger sum=li, ji=yi;
for(int i=0; i<d; i++)
{
if(wei[i]>0)
sum=sum.add(ji);
ji=ji.multiply(er);
a[i+1]=sum;
}
sum=li;
for(int i=d; i>=0; i--)
{
sum=sum.multiply(er).add(BigInteger.valueOf(wei[i]));
b[i+1]=sum;
}
a[0]=li;
BigInteger ans=li;
for(int i=0; i<d; i++)
if(wei[i]==0)
{
BigInteger an=(bit[i].subtract(a[i]).subtract(yi)).multiply(b[i+2]).multiply(bit[i]);
ans=ans.add(an).add(an);
}
else
{
BigInteger an=((a[i].add(yi)).multiply(b[i+2].add(yi)).subtract(yi)).multiply(bit[i]);
ans=ans.add(an).add(an);
}
out.println(ans);
}
out.close();
}
}
class InputReader
{
BufferedReader buf;
StringTokenizer tok;
InputReader()
{
buf = new BufferedReader(new InputStreamReader(System.in));
}
boolean hasNext()
{
while(tok == null || !tok.hasMoreElements())
{
try
{
tok = new StringTokenizer(buf.readLine());
}
catch(Exception e)
{
return false;
}
}
return true;
}
String next()
{
if(hasNext())
return tok.nextToken();
return null;
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
BigInteger nextBigInteger()
{
return new BigInteger(next());
}
BigDecimal nextBigDecimal()
{
return new BigDecimal(next());
}
}

HDOJ 4919

再之后 学习了一下map的记忆化搜索

 import java.io.*;
import java.util.*;
import java.math.*; public class Main
{
static BigInteger yi=BigInteger.ONE;
static BigInteger er=BigInteger.valueOf(2);
static BigInteger li=BigInteger.ZERO;
static BigInteger sa=BigInteger.valueOf(3);
static BigInteger si=BigInteger.valueOf(4);
static BigInteger liu=BigInteger.valueOf(6);
static HashMap<BigInteger, BigInteger> a=new HashMap<BigInteger, BigInteger>();
public static BigInteger dfs(BigInteger n)
{
if(a.containsKey(n))
return a.get(n);
BigInteger m;
if(n.mod(er).equals(li))
{
BigInteger aa=n.divide(er);
BigInteger bb=dfs(aa).multiply(er);
BigInteger cc=dfs(aa.subtract(yi)).multiply(er);
m=(aa.subtract(yi)).multiply(si).add(bb).add(cc);
}
else
{
BigInteger aa=(n.subtract(yi)).divide(er);
m=dfs(aa).multiply(si).add(aa.multiply(liu));
}
a.put(n, m);
return m;
}
public static void main(String[] args)
{
InputReader in = new InputReader();
PrintWriter out = new PrintWriter(System.out);
a.put(li, li);
a.put(yi, li);
a.put(er, li);
while(in.hasNext())
{
BigInteger n=new BigInteger(in.next());
out.println(dfs(n));
}
out.close();
}
}
class InputReader
{
BufferedReader buf;
StringTokenizer tok;
InputReader()
{
buf = new BufferedReader(new InputStreamReader(System.in));
}
boolean hasNext()
{
while(tok == null || !tok.hasMoreElements())
{
try
{
tok = new StringTokenizer(buf.readLine());
}
catch(Exception e)
{
return false;
}
}
return true;
}
String next()
{
if(hasNext())
return tok.nextToken();
return null;
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
BigInteger nextBigInteger()
{
return new BigInteger(next());
}
BigDecimal nextBigDecimal()
{
return new BigDecimal(next());
}
}

HDOJ 4919

[JAVA]HDU 4919 Exclusive or的更多相关文章

  1. HDU 4919 Exclusive or (数论 or 打表找规律)

    Exclusive or 题目链接: http://acm.hust.edu.cn/vjudge/contest/121336#problem/J Description Given n, find ...

  2. hdu 4919 Exclusive or

    Exclusive or Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) T ...

  3. HDU 4919 Exclusive or 数学

    题意: 定义 \[f(n)=\sum\limits_{i=1}^{n-1}(i\oplus (n-i))\] 求\(f(n),n \leq 10^{500}\) 分析: 这个数列对应OEIS的A006 ...

  4. HDU 4919 打表找规律 java睑板 map 递归

    == oeis: 点击打开链接 瞎了,x.mod(BigInteger.ValueOf(2)).equal( BigInteger.ValueOf(1)) 写成了 x.mod(BigInteger.V ...

  5. java hdu A+B for Input-Output Practice (III)

    A+B for Input-Output Practice (III) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32 ...

  6. java hdu A+B for Input-Output Practice (IV)

    A+B for Input-Output Practice (IV) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/327 ...

  7. 多校第五场 归并排序+暴力矩阵乘+模拟+java大数&amp;记忆化递归

    HDU 4911 Inversion 考点:归并排序 思路:这题呀比赛的时候忘了知道能够用归并排序算出逆序数,可是忘了归并排序的实质了.然后不会做-- 由于看到题上说是相邻的两个数才干交换的时候.感觉 ...

  8. Java和Flex整合报错(四)

    1.错误描述 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -help ...

  9. Java和Flex整合报错(三)

    1.错误描述 信息: Initializing Spring FrameworkServlet 'mvc' 11-13 23:43:42 INFO [localhost-startStop-1] or ...

随机推荐

  1. JAXB - XML Schema Types, Defining an Enumeration

    If you want a data type that enumerates discrete values you should use a restriction of the schema t ...

  2. 【MINA】序列化和反序列化我们要考虑的问题

    概念 序列化:将java对象转换为字节序列的过程叫做序列化 反序列化:将字节对象转换为java对象的过程叫做反序列化 要解决的问题 1.序列化时间 2.反序列化时间 3.bytes大小 4.操作方便 ...

  3. java web 优化札记

    1.效果最明显最简单最省事的优化是SSD,一般优化效率3倍起,(未必对,但是说明很多瓶颈问题都是存储问题) 2.垂直扩容省了开发时间,短期来看是最快的,缺点是会消耗更多的资源,而且有瓶颈,另外如果应用 ...

  4. ArrayList、Vector、LinkedList的区别及其优缺点? (转载)

    原文链接:http://blog.csdn.net/wangzff/article/details/7296648 ArrayList,LinkedList,Vestor这三个类都实现了java.ut ...

  5. Jquery中Ajax异步请求中的async参数的作用

    之前不知道这个参数的作用,上网找了前辈的博客,在此收录到自己的博客,希望能帮到更多的朋友: test.html <a href="javascript:void(0)" on ...

  6. 崩溃信息:Message from debugger: Terminated due to signal 9

    是因为你在调试的时候主动了结束了程度,如上滑结束了程序

  7. [译]JavaScript 错误和处理

    JavaScript的调试是一个噩梦:一些错误刚开始很难理解,并且给出的错误函数也经常是没用的.如果把错误都列出来并给出解决办法会不会很有用呢. 下面列出了JavaScript一系列的奇怪错误.对于同 ...

  8. ifstream:incomplete type is not allowed

    IntelliSense: incomplete type is not allowed ifstream inputFile; Need to add this: #include <fstr ...

  9. DLL详解及Denpendcy Walker的使用

    下面的文章被N次转载,为了尊重原作,\(^o^)/~,贴出最早发布这篇文章的地址及作者.   动态链接库 Windows的活动大陆 2006-07-26 09:21  作者:狂ρκ来源:电脑爱好者 在 ...

  10. [转]优化PHP程序的方法

    1. If a method c++an be static, declare it static. Speed improvement is by a factor of 4. 如果一个方法可静态化 ...