Exclusive or

article/2025/9/20 19:34:10

题目连接

  • 题意:
    每次给一个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));}}
}



http://chatgpt.dhexx.cn/article/g7mPq9wN.shtml

相关文章

Maven依赖冲突解决之exclusions

Maven依赖冲突解决之exclusions 1. 背景 作为java生态下开发者,往往需要使用大量线程的第三方库,一般都是以jar包形式存在。maven作为事实上主流的jar包依赖管理工具,Idea和Eclipse都支持创建maven工程来管理jar包依赖。使用maven进行jar包依赖管理时,maven会自行管理jar包…

【带你吃透C++】引用详解(引用和指针的区别)

引用 1 引用概念 引用不是新定义一个变量&#xff0c;而是给已存在变量取了一个别名&#xff0c;编译器不会为引用变量开辟内存空间&#xff0c;它和它引用的变量共用同一块内存空间。 比如&#xff1a;李逵&#xff0c;在家称为"铁牛"&#xff0c;江湖上人称"…

浅析C++中引用与指针的区别

引用和指针的概念及区别 1. 引用及指针概念1.1 指针概念1.2 引用概念 2. 引用与指针的区别 1. 引用及指针概念 如果熟悉指针和引用的使用&#xff0c;应该能感觉到指针和引用在很多场景使用起来还是有很大的相似性的&#xff0c;尽管它们在语法层面上是俩个完全不同的概念。 那…

C语言-引用和指针的区别?

①引用的格式&#xff1a;数据类型 &引用名 变量名&#xff1b; 指针的格式&#xff1a;数据类型 *变量名指向的变量地址&#xff1b;②使用引用一定要进行初始化 指针为了不出现野指针&#xff0c;也要进行初始化为NULL ③引用只能对数组的元素使用&#xff0c;不能对整个…

c++引用与指针的区别

目录 一、从语法上来讲 二、从汇编层面来看 一、从语法上来讲 1.指针是存储某个实例的地址&#xff0c;引用是实例的别名 2.程序为指针分配内存区域&#xff0c;而不为引用分配内存区域 3.指针使用时要加 “ * ”&#xff0c;解引用&#xff0c;引用可以直接使用 例&…

C++引用和指针的区别

作者&#xff1a;RainMan 链接&#xff1a;https://www.zhihu.com/question/37608201/answer/545635054 来源&#xff1a;知乎 著作权归作者所有。商业转载请联系作者获得授权&#xff0c;非商业转载请注明出处。 引用是C引入的重要机制&#xff08;C语言没有引用&#xff0…

引用和指针的区别

引用和指针的区别: C的引用(Reference) 1.定义引用就是给某个变量起别名&#xff0c;对引用的操作和对该变量操作完全相同。 int a 10&#xff1b; int& b a;//b就是a的别名 b; cout << a << endl;//11 2 常引用 1&#xff09;定义引用时加const修饰&#…

C++中引用和指针的区别

下面用通俗易懂的话来概述一下&#xff1a; 指针-对于一个类型T&#xff0c;T*就是指向T的指针类型&#xff0c;也即一个T*类型的变量能够保存一个T对象的地址&#xff0c;而类型T是可以加一些限定词的&#xff0c;如const、volatile等等。见下图&#xff0c;所示指针的含义&am…

C/C++引用和指针的区别

为什么C/C语言使用指针&#xff1f; 答案&#xff1a;①一方面&#xff0c;每一种编程语言都使用指针。不止C/C使用指针。 每一种编程语言都使用指针。C将指针暴露给了用户(程序员)&#xff0c;而Java和C#等语言则将指针隐藏起来了。 “Everything uses pointers. C just expo…

C++ 引用详解(引用的特点,引用与指针的区别,引用的其他使用)

目录 引用一.引入</font>二.C中较为麻烦的运算符</font>三.引用的定义</font>四.引用的特点五.对比指针与引用六.引用与指针的区别&#xff08;重点&#xff09;1.语法层面的区别2.汇编层面的区别 七.引用的其他使用 引用 一.引入 在生活中&#xff0c;我们…

Zipkin和Sleuth

“sleuth的作用是在系统中自动埋点并把数据发送给zipkin,zipkin的作用是存储这些数据并展现。” 说白了 sleuth就是为了Zipkin服务的 看了一圈 总结一下 就是微服务的链路追踪 我们看这个延迟的时间可以判断哪个服务出现了问题 进而排查出问题 简单说&#xff1a;假如服务 …

spring-cloud-sleuth分布服务跟踪式

why: 1,微服务架构微服务增多&#xff0c;一个客户端请求形成一个复杂的分布式服务调用链路&#xff0c;如果任何一个服务延迟过高或错误&#xff0c;都会引起请求失败。 how&#xff1a; 先引入了example&#xff1a; https://github.com/spring-cloud/spring-cloud-sleuth 1…

笔记:Sleuth 的底层逻辑

Sleuth 是通过打入到 Log 中的“卧底”来串联前后日志的。你集成了 Sleuth 组件之后&#xff0c; 它会向你的日志中打入三个“特殊标记”&#xff0c;其中一个标记你应该已经清楚了&#xff0c;那便是 Trace ID。剩下的两个标记分别是 Span ID 和 Parent Span ID&#xff0c;…

sleuth介绍

spring Cloud Sleuth为 spring Cloud提供了分布式跟踪的解决方案&#xff0c;它大量借用了Google Dapper、 Twitter Zipkin和 Apache HTrace的设计&#xff0c;先来了解一下 Sleuth的术语&#xff0c; Sleuth借用了 Dapper的术语。 span&#xff08;跨度&#xff09;&#xff…

sleuth 链路追踪

一&#xff1a;什么是链路追踪 对于以前的单服务器项目而言&#xff0c;如果一个地方出了问题&#xff0c;很容易去找到。可是对于微服务架构项目来说&#xff0c;因为微服务太多&#xff0c;而且往往是一个微服务调用了很多其他的微服务&#xff0c;如果一个地方出错&#xff…

java sleuth配置

java sleuth配置 springCloud学习记录SpringCloud Alibaba sleuth&#xff08;分布式请求链路跟踪&#xff09;zipkinsleuthpomyml springCloud学习记录 SpringCloud Alibaba sleuth&#xff08;分布式请求链路跟踪&#xff09; 在微服务框架种&#xff0c;一个由客户端发起的…

Spring Cloud Sleuth+Zipkin 构建微服务链路跟踪系统

什么是链路跟踪系统&#xff1f; 在微服务中&#xff0c;多个服务分布在不同物理机器上&#xff0c;各个服务之间相互调用。如何清晰地记录服务调用过程&#xff0c;并在出现问题的时候能够通过查看日志和服务之间的调用关系来定位问题&#xff0c;这样的系统就叫做链路跟踪系…

SpringCloud Sleuth入门介绍

案例代码:https://github.com/q279583842q/springcloud-e-book 一、Sleuth介绍 为什么要使用微服务跟踪?它解决了什么问题&#xff1f; 1.微服务的现状&#xff1f; 微服务的现状   随着业务的发展&#xff0c;单体架构变为微服务架构&#xff0c;并且系统规模也变得越来…

Spring Cloud Sleuth HTTP详解

目录 1版本关系 2简介 2.1术语 2.2调用链可视化 2.2.1使用Zipkin进行分布式跟踪 2.2.2错误可视化 2.2.3使用Brave进行分布式跟踪 2.2.4将Sleuth添加到项目中 3调用信息生成与上报原理 3.1初始化配置类 3.2过滤器-请求过滤 3.3调用信息拦截 3.4调用信息上报 4日志…

Sleuth链路追踪

文章目录 Spring-Cloud-Alibaba-Sleuth链路追踪一、链路追踪介绍二、Sleuth入门演示&#xff1a; 三、ZipKin介绍ZipKin服务端安装ZipKin数据持久化 Spring-Cloud-Alibaba-Sleuth链路追踪 一、链路追踪介绍 在大型的微服务项目中&#xff0c;一个系统被拆分成许多模块&#xf…