第五届xyqb题解

· · 个人记录

更了upd 26/9/15

1.A=B (ab)

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

  1. \frac {k(k+1)}2 \geqslant d

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

代码:

#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) \leqslant \min(a, b) , a \oplus b \geqslant \min(a, b) 则有 \gcd(a, b) = a - b 。

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

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

代码:

#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;
}