给定 x,mx,mx,m,求有多少 yyy 满足 y∈[1,m]y \in [1,m]y∈[1,m] 使得 x⊕yx \oplus yx⊕y 可以被 xxx 或 yyy 整除。
设 p=x⊕yp = x \oplus yp=x⊕y,分三种情况讨论:
-
x∣px | px∣p。设 p=kxp = kxp=kx,则 y=x⊕p≤p+x≤my = x \oplus p \le p + x \le my=x⊕p≤p+x≤m,也就是 kx≤m−xkx \le m - xkx≤m−x,进一步化简可知 k≤⌊m−xx⌋k \le \lfloor \frac{m - x}{x} \rfloork≤⌊xm−x⌋。由于我们还有 (m−x,m](m - x,m](m−x,m] 区间未检测,但可以充分利用 x⊕y≤x+yx \oplus y \le x + yx⊕y≤x+y 这个性质,我们循环判断 (m−x,m+x](m - x,m + x](m−x,m+x] 区间即可(当然,官方解答的做法更加简洁,但是没看懂)。
-
y∣py | py∣p。当 0<x<y0 < x < y0<x<y 时,p≤x+y<y+y=2yp \le x + y < y + y = 2yp≤x+y<y+y=2y,因此 p=ky(k≥2)p = ky(k \ge 2)p=ky(k≥2) 不存在解。因此只需要考虑 y≤xy \le xy≤x 的情况。
-
x∣px | px∣p 且 y∣py | py∣p。当 x≠yx \neq yx=y 时,lcm(x,y)≥2max(x,y)\rm{lcm(x,y)} \ge 2 \max (x,y)lcm(x,y)≥2max(x,y),然而 p<2max(x,y)p < 2 \max (x,y)p<2max(x,y),因此只有 x=yx = yx=y 时才能成立。
代码如下:
#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 (),n = read (),cnt = (n - x) / x + (n < 2 * x && x <= n);
for (ll i = (n - x) / x * x + 1;i <= n + x;++i) //枚举 x ^ y
if (1 <= (i ^ x) && (i ^ x) <= n && i % x == 0) ++cnt;
for (ll i = 1;i <= min (n,x);++i) // 枚举 y
if ((x ^ i) % i == 0) ++cnt;
printf ("%lld\n",cnt - (n >= x));
}
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;
}
989

被折叠的 条评论
为什么被折叠?



