题意很简单, 就是给个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. R cannot be resolved to a variable 解决办法

    Android开发过程中,碰到R cannot be resolved to a variable的报错信息,好像没有很确定的错误原因,一般来说,我总结出几个可能的解决方法,希望试过以后管用... 1 ...

  2. android N刷机

    1. 打开开发者选项2. 在开发者选项页面打开 oem解锁3. 运行 adb reboot bootloader4. 运行 fastboot flashing unlock5. 解压你下载的刷机包 6 ...

  3. 修改ckeditor/ckfinder上传文件文件夹 路径以日期格式命名

    修改/ckfinder/config.ascx文件: string dateDir = DateTime.Today.ToString("yyyyMM/"); ResourceTy ...

  4. Docker Machine, Compose, and Swarm: How They Work Together

    The three tools are now neatly packaged into what’s called the Docker Toolbox. Docker Machine1/ crea ...

  5. IOS开发之NSObject协议类方法说明

    oc中NSObject类是所有类的基类,所有类都要继承自它,那么它的方法就显得特别重要,因为所有类都会有这些基本的方法. 看看oc的源码中NSObject是这样定义的: @interface NSOb ...

  6. UICollectionView的简单使用

    ChildModel.h #import <Foundation/Foundation.h> @interface ChildModel : NSObject @property (non ...

  7. Easyui-Combobox多选下拉框

    因为工作需要,引入combobox多选下拉框,并且获取选择的值并以","分开. 效果如下: 代码如下: <html> <head> <title> ...

  8. js个人笔记

    一.删除元素 <!DOCTYPE html> <html> <head> <title>删除元素</title> </head> ...

  9. eNSP

    L2交换机 sysvlan 200q interface Vlanif 200ip address 192.168.0.1 24dis thisq interface Ethernet 0/0/1po ...

  10. Java大数操作类

    Java的大数操作分为BigInteger和BigDecimal,但这两给类是分开使用的,有时候在编程的时候显得略微繁琐,现在编写了一个将二者合二为一的大数操作类. 大数操作类代码如下: packag ...