题意很简单, 就是给个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. JDBC入门连接MySQL查数据

    在MySQL中建立user表,插入数据 create table user( id int, name varchar(10), age int )engine myisam charset utf8 ...

  2. 1.2、Mybatis二级缓存测试

    package me.gacl.test; import me.gacl.domain.User; import me.gacl.util.MyBatisUtil; import org.apache ...

  3. ASPxGridView动态创建表格列编辑模板

    在项目中用到了DevExpress的ASPxGridview控件,每每去配置它的时候,总感觉很是啰嗦,于是想到了用代码自动配置. 于是有了这样的代码: foreach (ZiyuWeb.Entity. ...

  4. 一般处理程序生成简单的图片验证码并通过html验证用户输入的验证码是否正确

    一般处理程序生成简单的图片验证码并通过html验证用户输入的验证码是否正确       最近没事研究了下验证码的的动态生成及通过cookie实现HTML页面对用户输入的验证码的校验,简要如下: 1.写 ...

  5. 40个Java集合面试问题和答案【下】【转载】

    接上文:http://www.cnblogs.com/xujianbo/p/5148083.html 28.哪些集合类是线程安全的? Vector.HashTable.Properties和Stack ...

  6. jQuery 源码分析6: jQuery 基本静态方法(二)

    jQuery.extend({ // 遍历obj的所有值 // args 这参数只能内部调用的会用到 // 注意到,如果回调函数调用失败会直接跳出并中止遍历 // 当有args数组时,使用apply调 ...

  7. 04_线程的创建和启动_使用Callable和Future的方式

    [简述] 从java5开始,java提供了Callable接口,这个接口可以是Runnable接口的增强版, Callable接口提供了一个call()方法作为线程执行体,call()方法比run() ...

  8. struts2的java文件中不能直接弹出script对话框

    需要引入接口 ServletResponseAware public class Login extends ActionSupport implements SessionAware,Servlet ...

  9. ZOJ 1122 Clock(模拟)

    Clock Time Limit: 2 Seconds      Memory Limit: 65536 KB You are given a standard 12-hour clock with ...

  10. C语言经典案例

    题目:企业发放的奖金根据利润提成.利润(I)低于或等于10万元时,奖金可提10%:利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%:20万到40万 ...