题解:CF2039C Shohag Loves XOR
给定 $x,m$,求有多少 $y$ 满足 $y \in [1,m]$ 使得 $x \oplus y$ 可以被 $x$ 或 $y$ 整除。
设 $p = x \oplus y$,分三种情况讨论:
$x | p$。设 $p = kx$,则 $y = x \oplus p \le p + x \le m$,也就是 $kx \le m - x$,进一步化简可知 $k \le \lfloor \frac{m - x}{x} \rfloor$。由于我们还有 $(m - x,m]$ 区间未检测,但可以充分利用 $x \oplus y \le x + y$ 这个性质,我们循环判断 $(m - x,m + x]$ 区间即可(当然,官方解答的做法更加简洁,但是没看懂)。
$y | p$。当 $0 < x < y$ 时,$p \le x + y < y + y = 2y$,因此 $p = ky(k \ge 2)$ 不存在解。因此只需要考虑 $y \le x$ 的情况。
$x | p$ 且 $y | p$。当 $x \neq y$ 时,$\rm{lcm(x,y)} \ge 2 \max (x,y)$,然而 $p < 2 \max (x,y)$,因此只有 $x = y$ 时才能成立。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| #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 () { 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) if (1 <= (i ^ x) && (i ^ x) <= n && i % x == 0) ++cnt; for (ll i = 1;i <= min (n,x);++i) 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; }
|