给定两个整数 x , m x, m x,m,求 y y y的个数。 y y y满足 1 ≤ y ≤ m , x ≠ y 1 \leq y \leq m, x \neq y 1≤y≤m,x=y, x ⊕ y x \oplus y x⊕y是x或y的因数。
设 c = x ⊕ y c = x \oplus y c=x⊕y
则 1 ≤ y ≤ m , y ≤ 2 x , c ≤ y 2 ≤ x 1 \leq y \leq m, y \leq 2x, c \leq \frac{y}{2} \leq x 1≤y≤m,y≤2x,c≤2y≤x
由于 x , m x, m x,m大小关系不确定,因此 1 ≤ c ≤ m i n ( x , m ) 1 \leq c \leq min(x, m) 1≤c≤min(x,m)
遍历c逐个判断即可。
设 c = x ⊕ y c = x \oplus y c=x⊕y,答案即为求满足 c c c是x或y的因数的 c c c的个数。
110010110
011011110
则 1 ≤ y ≤ m , y ≤ 2 x , c ≤ y 2 ≤ x 1 \leq y \leq m, y \leq 2x, c \leq \frac{y}{2} \leq x 1≤y≤m,y≤2x,c≤2y≤x
由于 x , m x, m x,m大小关系不确定,因此 1 ≤ c ≤ m i n ( x , m ) 1 \leq c \leq min(x, m) 1≤c≤min(x,m)
#include
using namespace std;
typedef long long ll;
#define FKCF ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
#define dbg(x) cout<<"[debug] "<<#x<<" = "<<x<<endl;
// ============ BEGIN ============ //
inline void solve(){
ll x, m;
int cnt=0;
cin >> x >> m;
// 设 c = x ^ y
for (int c = 1ll; c <= (int)min(x, m); ++c) {
ll y = c ^ x;
if ((!(x % c) || !(y % c)) && x != y && y && y <= m) ++cnt;
}
cout << cnt << endl;
}
int main() {
FKCF
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
给定两个整数 x , m x, m x,m,求 y y y的个数。 y y y满足 1 ≤ y ≤ m 1 \leq y \leq m 1≤y≤m, x ⊕ y x \oplus y x⊕y是 x x x或 y y y的因数(包括0)。
设 c = x ⊕ y c = x \oplus y c=x⊕y
和上一题一样,以计数c代替计数y。
1. 求c是x的因数 的个数
设p
是c满足y范围内能取到的最后一个c。即 p = m − m % x p = m - m \% x p=m−m%x
a n s = p x ans = \frac{p}{x} ans=xp
ll p = m - m % x;
ll ans = p / x - (x < p); // c=x无法取到,c=0必取到
// y = x ^ p
// 考虑c能否取到p
if ((x ^ p) >= 1 and (x ^ p) <= m) ++ans;
p += x;
// 考虑c能否取到p+x
if ((x ^ p) >= 1 and (x ^ p) <= m) ++ans;
2. 求c是y的因数 的个数
当 y > x y>x y>x, c c c不再可能是y的因数。原因见上一题。
for (int y = 1; y <= min(1LL * x, m); y++) {
ll c = x ^ y;
if (c % y == 0) {
++ans;
}
}
3. 求c是x,y的因数 的个数
需使 c = l c m ( x , y ) c=lcm(x, y) c=lcm(x,y)
取 y = x y=x y=x,即 c = 0 c=0 c=0
if (x <= m) {
--ans;
}
#include
using namespace std;
typedef long long ll;
#define CF ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
#define dbg(x) cout<<"[debug] "<<#x<<" = "<<x<<endl;
// ============ BEGIN ============ //
void solve() {
int x; ll m; cin >> x >> m;
// divisible by x
ll p = m - m % x;
ll ans = p / x - (x < p);
if ((x ^ p) >= 1 and (x ^ p) <= m) ++ans;
p += x;
if ((x ^ p) >= 1 and (x ^ p) <= m) ++ans;
// divisibly by y
for (int y = 1; y <= min(1LL * x, m); y++) {
ll cur = x ^ y;
if (cur % y == 0) {
++ans;
}
}
// divisible by both
if (x <= m) {
--ans;
}
cout << ans << '\n';
}
int32_t main() {
CF
int t = 1;
cin >> t;
while (t--) solve();
return 0;
}