题目连接

  • 题意:
    每次给一个n,求
    (2≤n<10500)
  • 分析:
    先说一下自己的想法,如果将n换成二进制数,也就一两千位左右,那么一位一位处理是可以接受的。将0-n写成二进制形式后,显然所有数某一个二进制位是有一个循环节的,那么我们就可以从这里入手直接求解
import java.io.*;
import java.math.*;
import java.util.*;public class Main {public static BigInteger zero = BigInteger.ZERO;public static BigInteger one = BigInteger.ONE;public static BigInteger two = BigInteger.valueOf(2);public static BigInteger three = BigInteger.valueOf(3);public static BigInteger four = BigInteger.valueOf(4);public static BigInteger six = BigInteger.valueOf(6);public static BigInteger Down(BigInteger now, BigInteger L) {BigInteger mid = now.divide(L).multiply(L).add(L.shiftRight(1));if (now.subtract(mid).signum() < 0)return mid;return mid.add(L.shiftRight(1));}public static BigInteger Up(BigInteger now, BigInteger L) {BigInteger start = now.divide(L).multiply(L);BigInteger mid = start.add(L.shiftRight(1));if (now.subtract(mid).signum() < 0)return start.subtract(one);return mid.subtract(one);}public static int getValue(BigInteger now, BigInteger L) {BigInteger mid = now.divide(L).multiply(L).add(L.shiftRight(1));if (now.subtract(mid).signum() < 0)return 0;return 1;}public static BigInteger solve(BigInteger nl, BigInteger nr, BigInteger gl, BigInteger L) {BigInteger ret = zero, step = Down(nl, L).subtract(nl), t = nr.subtract(Up(nr, L));if (step.subtract(t).signum() > 0)step = t;while (nl.add(step).subtract(gl).signum() <= 0) {if ((getValue(nl, L) ^ getValue(nr, L)) == 1)ret = ret.add(step);nl = nl.add(step); nr = nr.subtract(step);step = Down(nl, L).subtract(nl); t = nr.subtract(Up(nr, L));if (step.subtract(t).signum() > 0)step = t;}if (gl.subtract(nl).add(one).signum() >= 0 && (getValue(nl, L) ^ getValue(nr, L)) == 1)ret = ret.add(gl.subtract(nl).add(one));return ret;}public static void main(String[] args) {BigInteger n, L, tans, nl, ans;Scanner cin = new Scanner(System.in);while (cin.hasNext()) {n = cin.nextBigInteger();L = two;ans = zero;while (L.subtract(n.shiftLeft(1)).signum() <= 0)//(L <= n * 2){tans = zero;if (n.divide(L).shiftRight(1).signum() > 0) {tans = solve(zero, n, L.subtract(one), L);}nl = n.divide(L).shiftRight(1).multiply(L);tans = n.divide(L).shiftRight(1).multiply(tans).add(solve(nl, n.subtract(nl), n.subtract(one).shiftRight(1), L));ans = ans.add(tans.multiply(L));L = L.shiftLeft(1);}System.out.println(ans.subtract(n.shiftLeft(1)));}}
}

学习一下题解的方法,关键在于:(2 * k) ^ x = (2 * k + 1) ^ x
之后就学习一下题解的公式化简方法了

import java.util.*;
import java.math.*;public class Main {static BigInteger n, ret;static BigInteger one = BigInteger.valueOf(1);static BigInteger two = BigInteger.valueOf(2);static BigInteger four = BigInteger.valueOf(4);static BigInteger six = BigInteger.valueOf(6);static HashMap<BigInteger, BigInteger> mp = new HashMap<BigInteger, BigInteger>();public static BigInteger fun(BigInteger n) {if (n.equals(BigInteger.ZERO) || n.equals(BigInteger.ONE)) return BigInteger.ZERO;if (mp.containsKey(n))return mp.get(n);BigInteger k = n.shiftRight(1);if (n.testBit(0)) {ret = four.multiply(fun(k)).add(six.multiply(k));mp.put(n, ret);return ret;}else {ret = (fun(k).add(fun(k.subtract(one))).add(k.shiftLeft(1)).subtract(two)).shiftLeft(1);mp.put(n, ret);return ret;}}public static void main(String[] args) {Scanner cin = new Scanner(System.in);while (cin.hasNext()) {n = cin.nextBigInteger();mp.clear();System.out.println(fun(n));}}
}

Exclusive or相关推荐

  1. R语言plotly可视化:plotly可视化箱图、相同数据集对比使用不同分位数算法的可视化差异(quartilemethod参数、linear、inclusive、exclusive)

    R语言plotly可视化:plotly可视化箱图.相同数据集对比使用不同分位数算法的可视化差异(quartilemethod参数.linear.inclusive.exclusive) 目录

  2. ORA-01102: cannot mount database in EXCLUSIVE mode

    今天在STARTUP一数据库时,发生如下错误: [oracle@bogon ~]$ sqlplus "/as sysdba" SQL*Plus: Release 11.2.0.1. ...

  3. SQL Server,Could not obtain exclusive lock on database 'model'

    创建SQL Server 数据库时出现错误"Could not obtain exclusive lock on database 'model'" 尝试以下方法: 1. to t ...

  4. 启动weblogic的错误:Could not obtain an exclusive lock to the embedded LDAP data files directory...

    http://hi.baidu.com/kaisep/item/0e4bf6ee5da001d1ea34c986 源地址 启动weblogic的错误:Could not obtain an exclu ...

  5. [architecture]-arm exclusive机制介绍

    关键词:spinlock,原子操作,独占访问,exclusive,inclusive,mutex,Semaphore,互斥,同步,同步原语 快速链接: .

  6. [ARM-assembly]-ARMV8的exclusive和inexclusive的介绍

    ★★★ 个人博客导读首页-点击此处 ★★★ AMRv8-aarch64架构中的A64指令中,提供了如下的一些exclusive指令,用来支持exclusive操作 那为什么,arm在加入exclusi ...

  7. Activiti之 Exclusive Gateway

    一.Exclusive Gateway Exclusive Gateway(也称为XOR网关或更多技术基于数据的排他网关)经常用做决定流程的流转方向.当流程到达该网关的时候,所有的流出序列流到按照已定 ...

  8. ORA-19573: cannot obtain exclusive enqueue for datafile 1

    还原Oracle数据库时出现ORA-19870和ORA-19573错误,如: RMAN> restore database; Starting restore at 11-DEC-12 usin ...

  9. Dynamics AX 2009 启动时提示“…the application files in exclusive mode”的解决

    问题 启用AOS服务时出错,在事件查看器中记录的提示信息为 "Object Server 01: The application files are not accessible. This ...

  10. 2021牛客多校3 - Kuriyama Mirai and Exclusive Or(差分+倍增)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的序列,现在需要执行 mmm 次操作,每次操作分为下列两种类型: 0 l r x:区间 [l,r][l,r][l,r] 内的数都异或上 xxx 1 ...

最新文章

  1. pyquery获取不到网页完整源代码_PyQuery 详解
  2. python时间去掉t_Python的set集合详解
  3. Windows Embedded CE 6.0开发初体验(三)设置Boot-loader
  4. OSChina 周五乱弹 —— 如何向妹子解释越位
  5. css3中transform中的translate(),scale(),skew(),一个参数时的情况
  6. Raki的读paper小记:LAMOL: LANGUAGE MODELING FOR LIFELONG LANGUAGE LEARNING
  7. Linux readelf命令
  8. Python爬虫5-API和爬虫
  9. Python(初学者):调用函数输出
  10. Java获取IP归属地
  11. Google assisant 2018谷歌IO大会 谷歌助理背后的系统设计
  12. 计算机能不能升级固态硬盘,老电脑可以用固态硬盘吗?老电脑升级安装固态硬盘的注意事项...
  13. Mac下安装Mavenidea配置maven
  14. python调用通达信数据_通达信数据格式及5分钟信息提取
  15. 安防监控摄像头接入云端实现直播、录像和大屏展示
  16. 《灵飞经》3·印神无双 第十三章 剑奕星斗 3
  17. Java基础(19)IO流
  18. 10月25日lol服务器维护,《LOL》维护到几点10月25日 英雄联盟10.25维护到几点结束...
  19. GeoGebra画正态分布密度曲线两种方式
  20. bs架构 mysql_什么是bs架构

热门文章

  1. 使用 ChromaKey 滤镜进行抠图
  2. SMI慧荣主控U盘量产后恢复教程
  3. VC++ 获取Revit元素的几何信息
  4. 光伏二次设置--远动通讯屏
  5. 百数电商进销存管理系统:破除传统的单一管理模式
  6. 校园网络工程规划与设计
  7. AppCan中标首都机场移动平台项目
  8. abap MODIFY常用语法解析
  9. 微信正在变成另外一个超级浏览器
  10. 【Python3笔记】三、Python第一程序画出奥迪车标