A gcd 問題
Solution
由於 \(\gcd(A,B) \le max(|A|,|B|)\)
所以直接輸出 \(n+m\) 即可
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll a, b;
cin >> a >> b;
cout << a + b << endl;
return 0;
}
B 整除問題
Solution
顯然不會超過最大的數,暴力列舉即可
Code
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
for (int x = 0; x <= max(n, m); x++) {
if ((m + x) % (n + x) == 0)
return cout << x << endl, 0;
}
cout << -1 << endl;
return 0;
}
C 數字字串問題
Solution
列舉右端點 \(i\),控制左端點 \(j\),如果不合法把左端點往右邊推,在 \(i\sim j\) 區間內的都合法
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
string s; cin >> s;
vector<int> cnt(10, 0);
int j = 0;
auto check = [&] () {
for (int i = 0; i < 10; i++)
if (cnt[i] > i) return false;
return true;
};
ll ans = 0;
for (int i= 0; i < s.size(); i++) {
cnt[s[i] - '0']++;
while (!check())
cnt[s[j++] - '0']--;
ans += i - j + 1;
}
cout << ans << endl;
return 0;
}
D 輸出答案題
威爾遜定理裸題
Solution
令 \(p=d+1\)
威爾遜定理 \((p-1)!\equiv -1 \pmod p\)
- \(p\le 4\) 時特判
- \(p\) 為質數時輸出 \(p-1\)
- 否則為 \(0\)
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool is_prime(ll x) {
if (x < 2) return false;
for (ll i = 2; i * i <= x; i++)
if (x % i == 0) return false;
return true;
}
int main() {
ll p;
cin >> p; p++;
if (p == 1)
return cout << 0 << endl, 0;
if (p == 2)
return cout << 1 << endl, 0;
if (p == 3)
return cout << 2 << endl, 0;
if (p == 4)
return cout << 2 << endl, 0;
if (is_prime(p))
return cout << p - 1 << endl, 0;
else
return cout << 0 << endl, 0;
}
E 背景題
Solution
\(\gcd(a_1,a_2,a_3,\cdots,a_n)|d\Longleftrightarrow a_1x_1+a_2x_2+a_3x_3+\cdots+a_nx_n=d\)
Code
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<int> a(n), b(m);
for (auto &x : a) cin >> x;
for (auto &x : b) cin >> x;
int g = a[0];
for (int i = 1; i < n; i++)
g = __gcd(g, a[i]);
int ans = 0;
for (int i = 0; i < m; i++)
ans += __gcd(g, b[i]) % g == 0;
cout << m - ans << endl;
return 0;
}
F 位反轉置換群問題
Solution
依舊是暴力
也可以 DP,但是我不會
Code
#include <bits/stdc++.h>
using namespace std;
int read() {
int x = 0, f = 1; char ch = getchar();
while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
void print(int x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) print(x / 10);
putchar(x % 10 + '0');
}
const int maxn = (1<<20) + 5;
int a[maxn];
int st[25];
int main() {
int n = read();
for (int i = 0; i < n; i++) a[i] = read();
int q = log2(n);
for (int i = 0; i < n; i++) {
int x = i;
for (int j = 0; j < q; j++)
st[j] = x >> j & 1;
int p = 0;
reverse(st, st + q);
for (int j = 0; j < q; j++)
p |= st[j] << j;
print(a[p]);
putchar(' ');
}
return 0;
}