跳到正文

个人记录

第五届xyqb题解

who_r_u_2026kmh约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年9月11日
收录
2026年9月15日
更新
2026年11月6日
篇幅
约 3 分钟
获赞
1

版本

  1. v2当前
  2. v1

更了upd 26/9/15

1.A=B (ab)

易知只用差就行了。记 d=∣a−b∣d = |a - b| ,答案为 kk ,则有以下特征:

  1. 1+2+⋯+k=k(k+1)21 + 2 + \cdots + k = \frac {k(k+1)}2 与 dd 奇偶性相同
  2. k(k+1)2⩾d\frac {k(k+1)}2 \geqslant d

所以可以先二分满足第2个条件。再微微调整满足第一个条件(至多2次的)。

代码:

C++
#include<bits/stdc++.h>
#define f(i, s, e) for(int i = s; i <= e; ++i)
#define dw(i, e, s) for(int i = e; i >= s; --i)
#define int long long
using namespace std;

int n, a, b;

int er(int x){
	int l = 0, r = 1e5, mid, ans;
	while(l <= r){
		mid = (l + r) >> 1;
		if((mid + 1) * mid / 2 >= x)	
			ans = mid,
			r = mid - 1;
		else
			l = mid + 1;
	}
    return ans;
}

signed main(){
	freopen("ab.in", "r", stdin);
	freopen("ab.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> n;
	while(n--){
		cin >> a >> b;
		int d = abs(a - b);
		int k = er(d);
		while(abs(d - (k + 1) * k / 2) % 2 != 0) ++k;
		cout << k << "\n";
	}
	return 0;
}

2.gcd and xor(gcd)

易知 gcd⁡(a,b)⩽min⁡(a,b),a⊕b⩾min⁡(a,b)\gcd(a, b) \leqslant \min(a, b) , a \oplus b \geqslant \min(a, b) 则有 gcd⁡(a,b)=a−b\gcd(a, b) = a - b 。

记 d=gcd⁡(a,b)d = \gcd(a, b) 则有 ad−bd=1\frac ad - \frac bd = 1 。

枚举一下 dd 和 ad\frac ad 知 a,ba, b 后暴力看是否满足题目条件即可。时间 O(nlogn)O(n log n) 。

代码:

C++
#include<iostream>
#define f(i, s, e) for(int i = s; i <= e; ++i)
#define dw(i, e, s) for(int i = e; i >= s; --i)
#define int long long
using namespace std;

int n, ans;

signed main(){
	freopen("gcd.in", "r", stdin);
	freopen("gcd.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> n;
	f(i, 1, n / 2) f(j, 2, n / i){
		if(((i * j) ^ (i * j - i)) == i)
			++ans;
	}
	cout << ans;
	return 0;
}
原文