A

0,6,8,90,6,8,9 的数量。

O(s)O(|s|)

C++ Code
#include "bits/stdc++.h"

using namespace std;
using i64 = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    string s;
    cin >> s;
    int ans = 0;
    for (auto &si : s) {
        if (si == '0' || si == '6' || si == '9') {
            ans++;
        }
        if (si == '8') {
            ans += 2;
        }
    }
    cout << ans << '\n';

    return 0;
}

B

bb 的每一位乘 aa

O(b)O(\sum|b|)

C++ Code
#include "bits/stdc++.h"

using namespace std;
using i64 = long long;

void solve() {
    int a;
    string b;
    cin >> a >> b;
    i64 ans = 0;
    for (auto &bi : b) {
        ans += 1LL * a * (bi - '0');
    }
    cout << ans << '\n';
}
  
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}

C

作为答案的 xxx!x! 一定在 nn 的附近。

考虑枚举 xx,则 y{nx!1,nx!1}y\in \{\lfloor\frac{n}{x!-1}\rfloor,\lceil\frac{n}{x!-1}\rceil\}

O(50)O(50)

C++ Code
#include "bits/stdc++.h"

using namespace std;
using i64 = long long;

i64 fac[50];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    fac[1] = 1;
    for (int i = 2; i < 50; i++) {
        fac[i] = fac[i - 1] * i;
    }

    int n;
    cin >> n;
    i64 mn = 1E18;
    int x, y;

    for (int i = 1; i < 50; i++) {
        if (i == 1) {
            if (n < mn) {
                mn = n;
                x = 1;
                y = 1;
            }
        } else if (i != 2) {
            i64 y0 = n / (fac[i] - 1);
            i64 y1 = (n + fac[i] - 2) / (fac[i] - 1);

            i64 mn0 = abs(fac[i] * y0 - y0 - n);
            i64 mn1 = abs(fac[i] * y1 - y1 - n);
  
            if (y0 != 2 && y0 > 0 && mn0 < mn) {
                mn = mn0;
                x = i;
                y = y0;
            }

            if (y1 != 2 && y1 > 0 && mn1 < mn) {
                mn = mn1;
                x = i;
                y = y1;
            }
        }
    }
    cout << x << ' ' << y << '\n';

    return 0;
}

D

a0+a1++ak1=a1+a2++aka_0 + a_1 + \dots + a_{k-1}=a_1 + a_2 + \dots + a_{k},则 a0=aka_0=a_k,由此 aimodk=Ca_{i\mod k}=\text{C} (0i<n)(0\le i<n)C\text{C} 为常数。

O((n+k))O(\sum(n+ k))

C++ Code
#include "bits/stdc++.h"

using namespace std;
using i64 = long long;

void solve() {
    int n, k, x;
    cin >> n >> k >> x;

    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    vector<int> mx(k);
    for (int i = 0; i < n; i++) {
        mx[i % k] = max(mx[i % k], a[i]);
    }
    i64 cnt = 0;
    for (int i = 0; i < n; i++) {
        cnt += mx[i % k] - a[i];
    }

    if (cnt > x) {
        cout << "-1\n";
        return;
    }
    x -= cnt;
    int ans = 0;
    for (int i = 0; i < k; i++) {
        int c = n / k + (n % k > i);
        ans = max(ans, mx[i] + x / c);
    }
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}

E

qq1010 互质,根据欧拉定理 q(10ϕ(q)1)q|(10^{\phi(q)}-1)ϕ(q)\phi(q)1q\frac{1}{q} 的一个循环节,1q\frac{1}{q} 可能为 ϕ(q)\phi(q) 的某个因子,根号枚举 ϕ(q)\phi(q) 的因子,若因子 xx 满足 10x1(modq)10^{x}\equiv 1\pmod q,则 xx1q\frac{1}{q} 的一个循环节。

C++ Code
#include "bits/stdc++.h"

using namespace std;
using i64 = long long;
using i128 = __int128;

i64 phi(i64 x) {
    i64 res = x;
    for (int i = 2; i64(i) * i <= x; i++) {
        if (x % i == 0) {
            res = res / i * (i - 1);
            while (x % i == 0) {
                x /= i;
            }
        }
    }
    if (x > 1) {
        res = res / x * (x - 1);
    }
    return res;
}

i64 power(i64 a, i64 b, i64 p) {
    i64 res = 1;
    for (; b; b >>= 1, a = i128(a) * a % p) {
        if (b & 1) {
            res = i128(res) * a % p;
        }
    }
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    i64 p, q;
    cin >> p >> q;

    i64 g = gcd(p, q);
    p /= g;
    q /= g;

    int c2 = 0, c5 = 0;
    while (q % 2 == 0) {
        q /= 2;
        c2++;
    }
    while (q % 5 == 0) {
        q /= 5;
        c5++;
    }

    if (q == 1) {
        cout << "-1\n";
        return 0;
    }

    i64 x = phi(q);
    i64 ans = x;
    for (int i = 2; i64(i) * i <= x; i++) {
        if (x % i == 0) {
            i64 x0 = i;
            i64 x1 = x / i;
            if (power(10, x0, q) == 1) {
                ans = min(ans, x0);
            }
            if (power(10, x1, q) == 1) {
                ans = min(ans, x1);
            }
        }
    }
    cout << max(c2, c5) << ' ' << ans << '\n';
    
    return 0;
}