一本通 P1801 异或

一本通原题

因为\(𝑎 = 𝑏\)时肯定无解,我们不妨设\(𝑎 > 𝑏\)
那么有\(gcd(a,b)\leq a-b\), \(a\ xor\ b\geq a-b\),很明显有\(𝑐 = 𝑎 − 𝑏\)
我们依然 枚举\(c\), \(a=i*c\),因为\(gcd(a,a-c)=c\)
所以我们只需判断\(a\ xor\ c=a-c\)即 可,时间\(O(n\ log_2n)\),因为常数小,所以可以过。

#include<cstdio>
#define re register
using namespace std;
template<typename T>
inline void read(T&x)
{
	x=0;
	char s=getchar();
	bool f=false;
	while(!(s>='0'&&s<='9'))
	{
		if(s=='-')
			f=true;
		s=getchar();
	}
	while(s>='0'&&s<='9')
	{
		x=(x<<1)+(x<<3)+s-'0';
		s=getchar();
	}
	if(f)
		x=(~x)+1;
	return;
}
int ans,n;
int main()
{
	read(n);
	for(re int i=1; i<=n; i++)
		for(re int j=i<<1; j<=n; j+=i)
			ans+=(i^j)==j-i;
	printf("%d\n",ans);
	return 0;
}
posted @ 2019-11-24 18:32  蒟蒻wjr  阅读(221)  评论(0编辑  收藏  举报