题解:CF2039C1 Shohag Loves XOR (Easy Version)

给定 x,mx,mx,m,求有多少 yyy 满足 y∈[1,m],x≠yy \in [1,m],x \neq yy[1,m],x=yx⊕yx \oplus yxyxxxyyy 的因子。

结论猜测:x⊕y∈[1,x)x \oplus y \in [1,x)xy[1,x),下面是证明:

对于一个 ppp 的因子 fff,满足 f≤⌊p2⌋f \le \lfloor \frac{p}{2} \rfloorf2p,也就是说 fffppp 在二进制下的最高位不同。

y≥2xy \ge 2xy2x 时,x⊕yx \oplus yxyyyy 在二进制下的最高位相同,所以 x⊕yx \oplus yxy 在此时必定不是 yyy 的因子。同时,由于最高位的不同,此时 x⊕y>xx \oplus y > xxy>x 一定成立,所以 x⊕yx \oplus yxy 同样也不是 xxx 的因子。

所以说,只有 y<2xy < 2xy<2x 时,才能满足题意,也就是说因子 x⊕y<xx \oplus y < xxy<x

代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#define init(x) memset (x,0,sizeof (x))
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
using namespace std;
const int MAX = 1e5 + 5;
const int MOD = 1e9 + 7;
inline ll read ();
ll t;
int main ()
{
	//freopen (".in","r",stdin);
	//freopen (".out","w",stdout);
	t = read ();
	while (t--)
	{
		ll x = read (),m = read ();
		ll cnt = 0;
		for (int i = 1;i <= x;++i)
		{
			ll y = x ^ i;
			if (!(1 <= y && y <= m)) continue;
			if (x % (x ^ y) == 0 || y % (x ^ y) == 0) ++cnt;
		}
		printf ("%lld\n",cnt);
	}
	return 0;
}
inline ll read ()
{
    ll s = 0;int f = 1;
    char ch = getchar ();
    while ((ch < '0' || ch > '9') && ch != EOF)
	{
        if (ch == '-') f = -1;
        ch = getchar ();
    }
    while (ch >= '0' && ch <= '9')
	{
        s = s * 10 + ch - '0';
        ch = getchar ();
    }
    return s * f;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值