更了upd 26/9/15
1.A=B (ab)
易知只用差就行了。记 ,答案为 ,则有以下特征:
- 与 奇偶性相同
所以可以先二分满足第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)
易知 则有 。
记 则有 。
枚举一下 和 知 后暴力看是否满足题目条件即可。时间 。
代码:
#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;
}