soj 11598. XOR

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 Sand F (inclusive)?

Input

The first line of input is the integer T, which is the number of test cases (1 ≤ T ≤ 1000).T lines 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 andF, inclusive.

Sample Input

5
3 10
5 5
13 42
666 1337
1234567 89101112

Sample Output

8
5
39
0
89998783

Problem Source

2014年每周一赛第八场

题目意思很简单,就是要求出从s到f之间所有数的按位异或的值,因为中间的数可能很多,所以我们不可能暴力。就需要换一种其他的思路。
异或有一个性质,就是对某一位来说,假如说这些数中的这一位有奇数个1,那么异或起来这以位就是1,有偶数个1,异或起来就是0,这很好理解。我们就可以利用这个性质来做这道题目。
对于一个数n来说,计算出来从0到n的第i位上有多少个1,那么我们就可以求得从s到f之间的数有多少个1。现在问题就转变成了求0到n的第i位上有多少个1。先考虑一个特殊的情况,当n的二进制形式为全1时,即n等于2^k-1,第i位为1的个数就是(n+1)/2,因为第i位不是0,就是1,当然占总个数的一半。得到这个结论就离答案更近了,由于我们只关心第i位是0还是1,那么高于第i位的我们都不关心,我们截取第1位一直到第i位获得它的十进制值,这里的截取相当于对2^i取模,这样我们就把0到n这n+1个数分成了2^i类,即模2^i为0,1,2,... ,2^i-1的,而只有模2^i的余数大于或等于2^(i-1)的数第i位才是1,我们先计算从0到n有多少个完整的模2^i余数为0到2^i-1的,这个数就是n/(2^i),再乘以2^(i-1),就是1的个数的一部分,另一部分是模2^i余数是0到m,m小于2^i-1,如果m大于等于2^(i-1),那么这部分第i位为1的个数为m-2^(i-1)+1,否则,就是0。这样就求出来了第i位上1的个数,即:n/(2^i)*(2^(i-1)),如果n%(2^i)大于等于2^(i-1),那么再加上n%(2^i)-2^(i-1)+1,将这个最后的数记为cnt,如果cnt为奇数则结果的第i位为1,否则为0。
注意到异或后的结果不可能超过右边界,所以只需要看右边界有多少位,就能确定需要计算多少位,复杂度为log级别的。
我当时写的时候出了好多错误,其中一个是括号扩错了,找了好半天才找到,真坑,看来以后还是要细心。
代码如下:
/*************************************************************************
	> File Name: a.cpp
	> Author: gwq
	> Mail: gwq5210@qq.com 
	> Created Time: 2014年10月21日 星期二 19时18分10秒
 ************************************************************************/

#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>

#define INF (INT_MAX / 10)
#define SQR(x) ((x) * (x))
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repf(i, a, b) for (int i = (a); i <= (b); ++i)
#define repd(i, a, b) for (int i = (a); i >= (b); --i)
#define clr(arr, val) memset(arr, val, sizeof(arr))
#define pb push_back
#define sz(a) ((int)(a).size())
#define middle(x, y) ((x + y) >> 1)

using namespace std;
typedef set<int> si;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef long long ll;

const double esp = 1e-5;

//获取从0到n的第cnt+1位上的1的个数。
int getcnt(int n, int cnt)
{
	int ret = n / (1 << (cnt + 1)) * (1 << cnt);
	//括号括错了,坑了好半天
	if (n % (1 << (cnt + 1)) >= (1 << cnt)) {
		ret += n % (1 << (cnt + 1)) - (1 << cnt) + 1;
	}
	return ret;
}

int main(int argc, char *argv[])
{
	int t;

	scanf("%d", &t);
	while (t--) {
		int l, r;
		scanf("%d%d", &l, &r);
		int cnt = 0;
		int ans = 0;
		while ((r >> cnt) != 0) {
			int tmp = (getcnt(r, cnt) - getcnt(l - 1, cnt)) % 2;
			ans |= (tmp << cnt);
			++cnt;
		}

		printf("%d\n", ans);
	}

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
代码下载:完整代码,可直接运行 ;运行版本:2022a或2019b或2014a;若运行有问题,可私信博主; **仿真咨询 1 各类智能优化算法改进及应用** 生产调度、经济调度、装配线调度、充电优化、车间调度、发车优化、水库调度、三维装箱、物流选址、货位优化、公交排班优化、充电桩布局优化、车间布局优化、集装箱船配载优化、水泵组合优化、解医疗资源分配优化、设施布局优化、可视域基站和无人机选址优化 **2 机器学习和深度学习方面** 卷积神经网络(CNN)、LSTM、支持向量机(SVM)、最小二乘支持向量机(LSSVM)、极限学习机(ELM)、核极限学习机(KELM)、BP、RBF、宽度学习、DBN、RF、RBF、DELM、XGBOOST、TCN实现风电预测、光伏预测、电池寿命预测、辐射源识别、交通流预测、负荷预测、股价预测、PM2.5浓度预测、电池健康状态预测、水体光学参数反演、NLOS信号识别、地铁停车精准预测、变压器故障诊断 **3 图像处理方面** 图像识别、图像分割、图像检测、图像隐藏、图像配准、图像拼接、图像融合、图像增强、图像压缩感知 **4 路径规划方面** 旅行商问题(TSP)、车辆路径问题(VRP、MVRP、CVRP、VRPTW等)、无人机三维路径规划、无人机协同、无人机编队、机器人路径规划、栅格地图路径规划、多式联运运输问题、车辆协同无人机路径规划、天线线性阵列分布优化、车间布局优化 **5 无人机应用方面** 无人机路径规划、无人机控制、无人机编队、无人机协同、无人机任务分配 **6 无线传感器定位及布局方面** 传感器部署优化、通信协议优化、路由优化、目标定位优化、Dv-Hop定位优化、Leach协议优化、WSN覆盖优化、组播优化、RSSI定位优化 **7 信号处理方面** 信号识别、信号加密、信号去噪、信号增强、雷达信号处理、信号水印嵌入提取、肌电信号、脑电信号、信号配时优化 **8 电力系统方面** 微电网优化、无功优化、配电网重构、储能配置 **9 元胞自动机方面** 交通流 人群疏散 病毒扩散 晶体生长 **10 雷达方面** 卡尔曼滤波跟踪、航迹关联、航迹融合
详细设计: 1. 框图 ``` +----------------+ +------------------------+ | | | | | 用户操作界面 |<--------->| 服务器处理 | | | | | +----------------+ +------------------------+ ``` 2. 程序实现 (1)添加学生信息 ``` void add_student_info(char* file_name, char* student_id, char* student_name, float student_score) { FILE* fp = fopen(file_name, "a"); fprintf(fp, "%s\t%s\t%.2f\n", student_id, student_name, student_score); fclose(fp); } ``` (2)修改学生信息 ``` void modify_student_info(char* file_name, char* student_id, char* student_name, float student_score) { FILE* fp = fopen(file_name, "r"); FILE* tmp_fp = fopen("tmp.txt", "w"); char buf[1024]; int found = 0; while (fgets(buf, sizeof(buf), fp) != NULL) { char id[10], name[20]; float score; sscanf(buf, "%s\t%s\t%f", id, name, &score); if (strcmp(id, student_id) == 0) { fprintf(tmp_fp, "%s\t%s\t%.2f\n", student_id, student_name, student_score); found = 1; } else { fprintf(tmp_fp, "%s", buf); } } fclose(fp); fclose(tmp_fp); if (found) { remove(file_name); rename("tmp.txt", file_name); } else { remove("tmp.txt"); } } ``` (3)删除学生信息 ``` void delete_student_info(char* file_name, char* student_id) { FILE* fp = fopen(file_name, "r"); FILE* tmp_fp = fopen("tmp.txt", "w"); char buf[1024]; int found = 0; while (fgets(buf, sizeof(buf), fp) != NULL) { char id[10]; sscanf(buf, "%s", id); if (strcmp(id, student_id) == 0) { found = 1; } else { fprintf(tmp_fp, "%s", buf); } } fclose(fp); fclose(tmp_fp); if (found) { remove(file_name); rename("tmp.txt", file_name); } else { remove("tmp.txt"); } } ``` (4)查询学生信息 ``` void query_student_info(char* file_name, char* student_id) { FILE* fp = fopen(file_name, "r"); char buf[1024]; int found = 0; while (fgets(buf, sizeof(buf), fp) != NULL) { char id[10], name[20]; float score; sscanf(buf, "%s\t%s\t%f", id, name, &score); if (strcmp(id, student_id) == 0) { printf("%s\t%s\t%.2f\n", id, name, score); found = 1; break; } } fclose(fp); if (!found) { printf("Student info not found!\n"); } } ``` (5)显示所有学生信息 ``` void display_all_student_info(char* file_name) { FILE* fp = fopen(file_name, "r"); char buf[1024]; while (fgets(buf, sizeof(buf), fp) != NULL) { char id[10], name[20]; float score; sscanf(buf, "%s\t%s\t%f", id, name, &score); printf("%s\t%s\t%.2f\n", id, name, score); } fclose(fp); } ``` 3. 核心步骤图 (1)添加学生信息 ![add_student_info](https://i.imgur.com/0xZ8JYf.png) (2)修改学生信息 ![modify_student_info](https://i.imgur.com/8fO8mY0.png) (3)删除学生信息 ![delete_student_info](https://i.imgur.com/FXiw3xq.png) (4)查询学生信息 ![query_student_info](https://i.imgur.com/1RUK2gX.png) (5)显示所有学生信息 ![display_all_student_info](https://i.imgur.com/SOJ8LhL.png)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值