[JAVA]HDU 4919 Exclusive or

时间:2022-05-14 23:02:21

题意很简单, 就是给个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&plus;B for Input-Output Practice &lpar;III&rpar;

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

  6. java hdu A&plus;B for Input-Output Practice &lpar;IV&rpar;

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

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

    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. ASP&period;NET Web API Model-ParameterBinding

    ASP.NET Web API Model-ParameterBinding 前言 通过上个篇幅的学习了解Model绑定的基础知识,然而在ASP.NET Web API中Model绑定功能模块并不是被 ...

  2. &lbrack;蟒蛇菜谱&rsqb;Python日志记录最佳实践

    # -*- coding: utf8 -*- import logging # 创建一个logger logger = logging.getLogger('mylogger') logger.set ...

  3. fcitx error

    (ERROR-2016 /build/fcitx-J2yftF/fcitx-4.2.9.1/src/lib/fcitx/ui.c:165) no usable user interface.(ERRO ...

  4. 20151208&lowbar;使用windows2012配置weblogic节点管理器

    经过实践,weblogic节点管理器的作用主要有两点: 1.可通过weblogic控制台远程控制被管server启停. 2.可以自动重启被管server的进程,并且对spring框架提供比直接启动更快 ...

  5. object-c学习1

    因为公司需要,开始看object-c,虽然还没ios系统,但现学下语法. 第一个例子不应该是helloWorld吗?但<Learn Objective-C on the Mac>书上不是. ...

  6. mysql事件

    文章参考自http://blog.163.com/duanpeng3@126/blog/static/8854373520105182123112/在使用这个功能之前必须确保event_schedul ...

  7. Bash简单介绍

    Bash不不过一个命令解析程序. 它本身拥有一种程序设计语言,使用这样的语言,能够编写shell脚本来完毕各种各样的工作.而这些工作是使用现成的命令所无法完毕的.Bash脚本能够使用if-then-e ...

  8. &lbrack;微信小程序&rsqb;在应用地图时,如何设置满屏&lpar;高度&rpar;

    微信小程序在做地图功能时 用常规的办法height:100%:来设置高度来占满屏幕是不行的 它不会生效 应该改用单位vh 例如 height:100vh 这样就可以是地图占满整个屏幕高度

  9. Linux中shell和子shell一点点理解

    Linux执行脚本有两种方式,主要区别在于是否建立子shell   1.像sh,bash,./命令是用来执行shell脚本的,在bash/sh命令下,脚本文件可以无"执行权限",即 ...

  10. 设计模式C&plus;&plus;学习笔记之十(Builder建造者模式)

      建造者模式,将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示.一段晦涩难懂的文字,实现创建不同表示的方法就是给创建的过程传入创建的参数.详细的还是看代码吧. 10.1.解释 ...