2019 ICPC Asia Xuzhou A - Cat 计蒜客 - 42540

本文通过一个具体的编程挑战,探讨了如何使用位运算解决特定数学问题的方法。通过对不同输入范围的讨论,展示了位运算在效率和资源消耗方面的优势。适用于对位运算感兴趣或希望深入了解其应用的读者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#pragma warning (disable:4996)
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <fstream>
#include <vector>
#include <set>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int main() {
	int T;
	scanf("%d", &T);
	while (T--) {
		ull l, r, s;
		scanf("%llu%llu%llu", &l, &r, &s);
		if (l == r) {
			if (s >= l || s >= r)
				printf("1\n");
			else
				printf("-1\n");
		}
		else if (r - l + 1 == 2) {
			if (s >= (r ^ l))
				printf("2\n");
			else if (s >= r || s >= l)
				printf("1\n");
			else
				printf("-1\n");
		}
		else {
			if ((r - l + 1) % 2 == 0) {//长度偶
				if (l % 2 == 0) {//偶开始 
					ull tmp = (r - l + 1) / 2;
					if (tmp % 2 == 0)
						printf("%llu\n", r - l + 1);
					else {
						if (s >= 1)
							printf("%llu\n", r - l + 1);
						else {//s==0
							if (s >= (1 ^ (l + 1)) || s >= (1 ^ (r - 1)))
								printf("%llu\n", r - l + 1 - 1);
							else
								printf("%llu\n", r - l + 1 - 2);
						}
					}
				}
				else {//奇开始
					ull tmp = (r - l + 1 - 2) / 2;
					if (tmp % 2 == 0) {
						if (s >= (r ^ l))
							printf("%llu\n", r - l + 1);
						else if (s >= r || s >= l)
							printf("%llu\n", r - l + 1 - 1);
						else
							printf("%llu\n", r - l + 1 - 2);
					}
					else {
						if (s >= 1) {
							if (s >= (1 ^ r ^ l))
								printf("%llu\n", r - l + 1);
							else if (s >= (1 ^ r) || s >= (1 ^ l))
								printf("%llu\n", r - l + 1 - 1);
							else
								printf("%llu\n", r - l + 1 - 2);
						}
						else { //s==0
							if (s >= (1 ^ l) || s >= (1 ^ r))
								printf("%llu\n", r - l + 1 - 1);
							else if (r - l + 1 - 2 - 2 > 0)
								printf("%llu\n", r - l + 1 - 2 - 2);
							else
								printf("-1\n");
						}
					}
				}
			}
			else {//长度奇
				if (l % 2 == 0) {//偶开始
					ull tmp = (r - l + 1 - 1) / 2;
					if (tmp % 2 == 0) {
						if (s >= r)
							printf("%llu\n", r - l + 1);
						else
							printf("%llu\n", r - l + 1 - 1);
					}
					else {
						if (s >= 1) {
							if (s >= (1 ^ r))
								printf("%llu\n", r - l + 1);
							else
								printf("%llu\n", r - l + 1 - 1);
						}
						else { //s==0
							if (s >= (1 ^ r))
								printf("%llu\n", r - l + 1);
							else if (r - l + 1 - 3 > 0)
								printf("%llu\n", r - l + 1 - 3);
							else
								printf("-1\n");
						}
					}
				}
				else {//奇开始
					ull tmp = (r - l + 1 - 1) / 2;
					if (tmp % 2 == 0) {
						if (s >= l)
							printf("%llu\n", r - l + 1);
						else
							printf("%llu\n", r - l + 1 - 1);
					}
					else {
						if (s >= 1) {
							if (s >= (1 ^ l))
								printf("%llu\n", r - l + 1);
							else
								printf("%llu\n", r - l + 1 - 1);
						}
						else { //s==0
							if (s >= (1 ^ l))
								printf("%llu\n", r - l + 1);
							else if (r - l + 1 - 3 > 0)
								printf("%llu\n", r - l + 1 - 3);
							else
								printf("-1\n");
						}
					}
				}
			}
		}
	}
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值