[JAVA]HDU 4919 Exclusive or
题意很简单, 就是给个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的更多相关文章
- HDU 4919 Exclusive or (数论 or 打表找规律)
Exclusive or 题目链接: http://acm.hust.edu.cn/vjudge/contest/121336#problem/J Description Given n, find ...
- hdu 4919 Exclusive or
Exclusive or Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) T ...
- HDU 4919 Exclusive or 数学
题意: 定义 \[f(n)=\sum\limits_{i=1}^{n-1}(i\oplus (n-i))\] 求\(f(n),n \leq 10^{500}\) 分析: 这个数列对应OEIS的A006 ...
- HDU 4919 打表找规律 java睑板 map 递归
== oeis: 点击打开链接 瞎了,x.mod(BigInteger.ValueOf(2)).equal( BigInteger.ValueOf(1)) 写成了 x.mod(BigInteger.V ...
- 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 ...
- 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 ...
- 多校第五场 归并排序+暴力矩阵乘+模拟+java大数&记忆化递归
HDU 4911 Inversion 考点:归并排序 思路:这题呀比赛的时候忘了知道能够用归并排序算出逆序数,可是忘了归并排序的实质了.然后不会做-- 由于看到题上说是相邻的两个数才干交换的时候.感觉 ...
- Java和Flex整合报错(四)
1.错误描述 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -help ...
- Java和Flex整合报错(三)
1.错误描述 信息: Initializing Spring FrameworkServlet 'mvc' 11-13 23:43:42 INFO [localhost-startStop-1] or ...
随机推荐
- Hadoop源代码分析
http://wenku.baidu.com/link?url=R-QoZXhc918qoO0BX6eXI9_uPU75whF62vFFUBIR-7c5XAYUVxDRX5Rs6QZR9hrBnUdM ...
- Plugin with id 'android-apt' not found
解决路径: 1.查看miniSDK最外面的build.gradle 添加这句.具体参考一下demo.谢谢
- Android之Http网络编程(四)
前面几篇博文简单的介绍了一些常见的Http的操作,这些操作几乎都是在新开的线程中进行的网络请求,并在日志中打印出获取到的网络数据.那么,问题来了!(呃~感觉下一句是蓝翔有木有?)如何在把获取到的网络数 ...
- JavaScript中关于创建对象的笔记
1,最基本的两种创建对象的方式:构造函数|| 字面量 构造函数: var person = new Object(); person.name = "chen1zee1"; per ...
- [xUnit]尝试单元测试
参考: http://xunit.github.io/ http://shouldly.readthedocs.org/en/latest/ 创建测试项目工程,类型选择类库,在NuGet中搜索xuni ...
- 模板方法模式(Template Pattern)
模板方法模式:在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中.模板方法使得子类可以在不改变算法的结构下,重新定义算法中的某些步骤. 这个模式是用来创建一个算法模板.模板就是一个方法.更具体地 ...
- 文件服务——Vsftpd
文件传输协议(FTP): 能够让用户在互联网中上传.下载文件的文件协议,FTP服务就是支持FTP传输协议的主机,要想完成文件传输则需要FTP服务端和FTP客户端的配合才行. 通常用户使用FTP客户端软 ...
- Linux作业控制
在Linux中,利用Shell的作业控制是比较常用的操作,在这一节中我们将探究作业控制相关的操作.为了方便我们查看区分不同的进行,我们编写如下程序,其功能是每间隔2秒输出一次自己的编号. /* ** ...
- Linux FTP服务安装和远程登录失败
问题:本机VPlayer安装pure-ftpd ftp服务,通过flashfxp从windows连接出现以下错误: [左] 正在连接到 vmare -> IP=192.168.174.133 ...
- Markdown:纯文本进行网页排版的简单标记语言
Markdown http://daringfireball.net/projects/markdown/ 2016-08-03 Markdown是一种标记语言,对纯文本使用简单的标记符号进行网页格式 ...