G.Do You Like XOR?---异或的运用(前缀+规律)

博客围绕一个异或加密算法展开,该算法用三个整数 a、b、c 计算密码。Backlight 忘记密码,需根据这三个数算出。解题利用异或性质,当 c 为偶数结果为 0,奇数时为本身,还用到前缀思想,通过找规律解决数据大的问题,并给出 AC 代码。

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

Do You Like XOR?

Time Limit: 1 Sec Memory Limit: 128 Mb

题目链接http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=2324

Description

Do you like XOR? Whatever your answer is, Backlight like XOR very much. Thus, he uses a XOR problem as an encryption algorithm, and apply it to his personal computer. The algorithm is as follow:

1.choose 3 integers a,b,c and a,b (1 <= a < b<= 1018),c (1 <= c <= 1018) .

2.calculate password for
pi = i ⊕ i ⊕ i⋯i(a total of c times)

在这里插入图片描述

here, ⊕ means XOR(exclusive OR).

One day, Backlight forget his password. But he remember the 3 integers he use to encrypt. Given the 3 integers, could you help him to calculate out his password?

Input

Input contains multiple test cases. Each test case consists of a pair of integers a, b and c , separated by a space, one pair of integers per line.

Output

For each test case, print a single integer on its own line denoting the password.

Sample Input

1 2 3
6 66 666

Sample Output

3
0

Hint
1⊕1⊕1=1,2⊕2⊕2=2,1⊕2=3


题目大意:给你一个区间[a,b]求a到b的每个数的c次异或的异或值。比如第一个样例:
1异或三次⊕2异或三次。。。

emmm,看起来有点恐怖,毕竟数据有1018。。。但想想异或的性质。。。c为偶数的情况就直接过了。。。(全都是0。。。)c为计数的时候他一定可以拆成偶数+1,所以奇数的时候就是它本身。

那么也就是说我们只要求解a⊕(a+1)⊕(a+2)……⊕b就行了。。。但似乎也不是那么好求。。。

这里就用到前缀的思想了。。。关于a⊕(a+1)⊕(a+2)……⊕b的答案我们只要求f[1,a-1]和f[1,b]就行了,而f[a,b]=f[1,a-1]^ f[1,b]。但这数据颇大,所以我们要找一下规律,(一般而言异或的运算都很有规律。。。):

int sum=0;
for (int i=a; i<=b; i++) sum^=i;
printf ("%d ",sum);

直接写个小程序打表就会发现一个规律:

//求1-a的异或值
if (a%4==1) return 1;
if (a%4==2) return a+1;
if (a%4==3 || a==0) return 0;
return a;

emmm,所以问题就解决了。。。
以下是AC代码:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll getans(ll a)
{
	if (a%4==1) return 1;
	if (a%4==2) return a+1;
	if (a%4==3 || a==0) return 0;
	return a;
}
int main()
{
	ll a,b,c;
	while (~scanf ("%lld%lld%lld",&a,&b,&c)){
		if (c%2==0) printf ("0\n");
    	else {
		    ll a1=getans(a-1);
		    ll a2=getans(b);
		    ll ans=a1^a2;
		    printf ("%lld\n",ans);
	    }
	}	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值