博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SOJ - 11598
阅读量:7238 次
发布时间:2019-06-29

本文共 2347 字,大约阅读时间需要 7 分钟。

Constraints

Time Limit: 1 secs, Memory Limit: 256 MB

Description

 

Given two integers S and F, what is the XOR (exclusive-or) of all numbers between S andF (inclusive)?

 

Input

 

The first line of input is the integer T, which is the number of test cases (1 ≤ T ≤ 1000). Tlines follow, with each line containing two integers S and F (1 ≤ S ≤ F ≤ 1 000 000 000).

 

Output

 

For each test case, output the (decimal) value of the XOR of all numbers between S and F, inclusive.

 

Sample Input

53 105 513 42666 13371234567 89101112

Sample Output

8539089998783

Problem Source

2014年每周一赛第八场

 

题意:计算区间[S,F]所有整数的异或和。

思路:先讨论S==1时的情况:若F为奇数,则看F/2是否为奇数,若是则结果为0,否则为1;若F为偶数,则看F/2是否为奇数,若是则结果为F+1,否则为F。

   S^...^F == (1^...^F) ^ (1^...^(S - 1))

 

1 // Problem#: 11598 2 // Submission#: 3058633 3 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License 4 // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ 5 // All Copyright reserved by Informatic Lab of Sun Yat-sen University 6 #include
7 using namespace std; 8 int main() 9 {10 int t,s,f,ss,ff;11 scanf("%d",&t);12 while(t--)13 {14 scanf("%d%d",&s,&f);15 if(f&1)ff=!((f>>1)&1);else ff=f+((f>>1)&1);16 s--;17 if(s&1)ss=!((s>>1)&1);else ss=s+((s>>1)&1);18 printf("%d\n",ff^ss);19 }20 return 0;21 }

 

今天又研究出另一种解法:

先来观察一组二元序列:

00,01,10,11;

100,101,110,111;

1000,1001,1010,1011;

……

可见每组元素的异或和一定为0,即只要F的二进制表示以11结尾,那么区间[0,F]内所有整数的异或和一定为0.

于是有下面这个公式:

设sumofxor(x)为区间[0,x]内所有整数的异或和,则有

 

1 // Problem#: 11598 2 // Submission#: 3063400 3 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License 4 // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ 5 // All Copyright reserved by Informatic Lab of Sun Yat-sen University 6 #include
7 using namespace std; 8 int sumofxor(int x) 9 {10 int i,j,s;11 for(i=0;i<=3;i++)12 if((x & 0x3) == i)13 {14 s = 0;15 for(j=3-i;j>=1;j--)s ^= (x + j);16 return s;17 }18 return 0;19 }20 int main()21 {22 int t,s,f;23 scanf("%d",&t);24 while(t--)25 {26 scanf("%d%d",&s,&f);27 printf("%d\n",sumofxor(f) ^ sumofxor(s-1));28 }29 return 0;30 }

 

转载于:https://www.cnblogs.com/gangduo-shangjinlieren/p/4041760.html

你可能感兴趣的文章
[精讲]15-Winodws Server 2012 工作文件夹
查看>>
java.lang.ClassCastException: org.apache.catalina.util.DefaultAnnotationProcessor 访问jsp报错-过滤器报错...
查看>>
如何更改Exchange服务器的传输队列数据库路径
查看>>
未来图灵发布《AI明星企业家热搜榜》
查看>>
Linux存储管理及硬盘分区、格式化、挂载
查看>>
Linux服务器时间不准确
查看>>
【AD】清楚windows下的不同凭据缓存
查看>>
没有如果,只需要去尝试
查看>>
LINUX下删除用户与主目录
查看>>
Remote Listener Server side Connect-Time Load Balancing
查看>>
程序开发时编写sql语句的注意事项
查看>>
Oracle 12c新特性对于业务上的一些影响总结
查看>>
基于redis的缓存机制的思考和优化
查看>>
IBM DS 5300存储硬盘故障数据恢复详解
查看>>
企业生产环境不同业务,系统分区建议(自定义分区布局)
查看>>
使用Verilog实现FPGA双列电梯控制系统
查看>>
编写安装配置mail服务脚本
查看>>
<Power Shell>13 powershell三个实用特性和功能实例
查看>>
spring cloud使用Feign实现远程接口的调用
查看>>
Delphi 中使用 ADO 方法打开 MySQL5.0 数据库并避免汉字乱码
查看>>