https://vjudge.net/contest/753101
A.
vjudge
CF
给 a、b、d,求 x 使 \(a or x\) 与 \(b or x\) 是 d 的因数,\(a,b,d<2^{30}\),要求 \(x<2^{60}\)
考虑让 \(a or x = x\),\(b or x = x\),使 x 是 d 的倍数即可。
首先让 d 为奇数,直接右移 lowbit(d),同时 a、b 也要右移 lowbit d,但是如果 lowbit(a(或b)) < lowbit(d) 就无解(因为是或,这个1一定去不掉)。
所以 \(a or b\) 的每一个 1 的位 x 也为 1。所以拆位,如果 \(a or b\) 的该位为 1,但是我们当前的答案这位为 0,就把答案加上 \(d*2^i\),由于 \(a,b,d<2^30\),所以答案必定在范围内。
代码:
点击查看代码
#include <bits/stdc++.h>
#define dbg(x) cout << #x << '=' << x << endl
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define frep(i, r, l) for (int i = (r); i >= (l); i--)
#define int long long
using namespace std;void work()
{int a, b, d; cin >> a >> b >> d;int num = 0; a |= b;while (!(d & 1)) num++, d >>= 1;if (a & ((1 << num) - 1)) {cout << "-1\n"; return ;}a >>= num;int k = 0;for (int i = 0; i <= 30; i++) {if (a & (1 << i) && !(k & (1 << i)))k += d << i;}cout << (k << num) << "\n";
}signed main()
{std::ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);int T = 1, opinput = 1;if (opinput) cin >> T;while (T--) work();return 0;
}
B
咕咕咕。
C
中国剩余定理板子
vjudge
洛谷
一个板子……就不写题解了,感觉很好理解……
挂个代码:
点击查看代码
#include <bits/stdc++.h>
#define dbg(x) cout << #x << '=' << x << endl
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define frep(i, r, l) for (int i = (r); i >= (l); i--)
#define int __int128
using namespace std;const int N = 15;int n;
int a[N], b[N];void Exgcd(int a, int b, int &x, int &y) {if (a == 1 && b == 0) {x = 1, y = 0; return;}Exgcd(b, a % b, y, x);y -= a / b * x;
}int read()
{int f = 1, s = 0; char ch = getchar();while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}while (ch >= '0' && ch <= '9') {s = s * 10 + ch - '0'; ch = getchar();}return f * s;
}void write(int x) {if (x > 9) write(x / 10);putchar(x % 10 + '0');
}signed main()
{std::ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);n = read();int res = 1, ans = 0;rep(i, 1, n) a[i] = read(), b[i] = read(), res *= a[i];rep(i, 1, n) {int k = res / a[i];int x, y;Exgcd(k, a[i], x, y);ans = (ans + k * b[i] * x % res) % res;}ans = (ans % res + res) % res;write(ans);return 0;
}