ABC 387

ABC 387

A, B题都是水题,D为有限制的DFS其中DFS写法可以借鉴, C较难, EFG没做

A - Happy New Year 2025

https://atcoder.jp/contests/abc387/tasks/abc387_a

#include<bits/stdc++.h>

using namespace std;

#define endl '\n'
using i64 = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using u128 = unsigned __int128;
typedef double lf;
typedef long double Lf;
const int MAXN = 2e5 + 5, MOD = 1e9 + 7, MAXP = 31, inf = 0x3f3f3f3f;
const i64 INF = 1e18;
const double eps = 1e-6;

void solve() {
    int a, b;
    cin >> a >> b;
    cout << (int)pow(a + b, 2) << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    t = 1;
    while(t--){
        solve();
    }
    return 0;
}

B - 9x9 Sum

https://atcoder.jp/contests/abc387/tasks/abc387_b

#include<bits/stdc++.h>

using namespace std;

#define endl '\n'
using i64 = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using u128 = unsigned __int128;
typedef double lf;
typedef long double Lf;
const int MAXN = 2e5 + 5, MOD = 1e9 + 7, MAXP = 31, inf = 0x3f3f3f3f;
const i64 INF = 1e18;
const double eps = 1e-6;

void solve() {
    int x;
    cin >> x;
    int sum = 0;
    for (int i = 1; i <= 9; i++)
        for (int j = 1; j <= 9; j++) {
            if (i * j != x) {
                sum += i * j;
            }
        }
    
    cout << sum << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    t = 1;
    while(t--){
        solve();
    }
    return 0;
}

C - Snake Numbers

https://atcoder.jp/contests/abc387/tasks/abc387_c

题目数据很大考虑前缀和思想减少时间复杂度,设 f ( x ) f(x) f(x)为从 0 到整数 x 的蛇形数,那么计算从L 到 R的蛇形数就可以转化成计算 f ( R ) − f ( L − 1 ) f(R) - f(L - 1) f(R)f(L1)

接下去对从 0 到整数 x 的蛇形数考虑几种情况:

设 t 为 0 到 x 的中间数

l e n ( t ) len(t) len(t)为整数 t 的长度

l e n ( x ) len(x) len(x)为整数 x 的长度

Case 1:当 l e n ( t ) len(t) len(t) < l e n ( x ) len(x) len(x)那么只需要枚举长度 t (t >= 2)以及首位 t[0]

Case 2:当 l e n ( t ) = l e n ( x ) len(t)=len(x) len(t)=len(x)并且 t [ 0 ] ≠ x [ 0 ] t[0]{\neq}x[0] t[0]=x[0]枚举 t 从 1 到 x[0]

Case 3:当 l e n ( t ) = l e n ( x ) len(t) = len(x) len(t)=len(x)并且 t [ 0 ] = x [ 0 ] t[0]=x[0] t[0]=x[0]假设从第二位开始到第 i 位出现不同,那么t[i]应该取 m i n ( x [ 0 ] , x [ i ] ) min(x[0], x[i]) min(x[0],x[i])因为t[i]要小于x[0]并且 t 不能大于 x,如果出现x[i] > x[0]即第 i + 1 位开始会出现前 i 个数字中有数字大于等于 x[0] 所以要结束循环

Case 4:前面考虑的都是每一位可能出现 t [ i ] ≠ x [ i ] t[i] \neq x[i] t[i]=x[i]的情况,然后 t 与 x 完全相等的情况下,如果也符合蛇形数那么再+1,可以在Case 3中判断是否+1,如果Case 3中没有出现过大于等于的情况证明完全相等并且符合蛇形数那么+1

#include<bits/stdc++.h>

using namespace std;

#define endl '\n'
using ll = long long;
using ui = unsigned int;
using ull = unsigned long long;
using u128 = unsigned __int128;
typedef double lf;
typedef long double Lf;
const int MAXN = 2e5 + 5, MOD = 1e9 + 7, MAXP = 31, inf = 0x3f3f3f3f;
const ll INF = 1e18;
const double eps = 1e-6;

int get_len(ll x) {
    int ans = 0;
    while (x) {
        x /= 10;
        ans += 1;
    }
    return ans;
}

int get_hightest_digit(ll x) {
    while (x > 9) {
        x /= 10; 
    }
    return x;
}

ll Pow(ll a, ll b) {
    ll ans = 1;
    for (ll i = 1; i <= b; i++) 
        ans *= a;
    return ans; 
}

int get_digit(ll x, int i) {
    int len = get_len(x);
    for (int j = 1; j <= len - i; j++) {
        x /= 10;
    }
    return x % 10;
}

ll sum(ll R) {
    ll ans = 0;
    ll kR = get_len(R);

    if (kR <= 1) return 0;

    // case 1 k < kR
    for (ll k = 2; k < kR; k++) {
        for (int t = 1; t <= 9; t++) {
            ans += Pow(t, k - 1);
        }
    }

    // case 2
    int tR = get_hightest_digit(R);
    for (int t = 1; t < tR; t++) {
        ans += Pow(t, kR - 1);
    }

    // case 3
    bool flag = true;
    for (int i = 2; i <= kR; i++) {
        int cur_digit = get_digit(R, i);
        ans += Pow(tR, kR - i) * min(cur_digit, tR);
        if (cur_digit > tR - 1) {
            flag = false;
            break;
        }
    }

    // case 4
    if (flag) ans++;
    
    return ans;
}

void solve() {    
    ll L, R;
    cin >> L >> R;

    ll sum1 = sum(R);
    ll sum2 = sum(L - 1);

    cout << sum1 - sum2 << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    t = 1;
    while(t--){
        solve();
    }
    return 0;
}

D - Snaky Walk

https://atcoder.jp/contests/abc387/tasks/abc387_d

有点限制的DFS+最短路

假设第一步垂直走,那么奇数步就是垂直走,偶数步就是水平走,第一步水平走也一样。

#include<bits/stdc++.h>

using namespace std;

#define endl '\n'
using ll = long long;
using ui = unsigned;
using ull = unsigned long long;
using u128 = unsigned __int128;
typedef double lf;
typedef long double Lf;
const int MAXN = 2e5 + 5, MOD = 1e9 + 7, MAXP = 31, inf = 0x3f3f3f3f;
const ll INF = 1e18;
const double eps = 1e-6;

void solve() {
    int h, w;
    cin >> h >> w;
    vector<string> s(h);
    for (int i = 0; i < h; i++)
        cin >> s[i];
    int sx, sy, gx, gy;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (s[i][j] == 'S') {
                sx = i;
                sy = j;
            }
            if (s[i][j] == 'G') {
                gx = i;
                gy = j;
            }
        }
    }

    int ans = inf;
    vector<vector<pair<int, int>>> moves(2);
    moves[0] = {
        {0, 1},
        {0, -1}
    };
    moves[1] = {
        {1, 0},
        {-1, 0}
    };
    for (int p = 0; p < 2; p++) {
        vector d(h, vector<int>(w, inf));
        d[sx][sy] = 0;
        queue<pair<int, int>> q;
        q.emplace(sx, sy);
        while (!q.empty()) {
            auto [i, j] = q.front();
            q.pop();
            for (auto [di, dj] : moves[(i + j + p) & 1]) {
                int ni = i + di, nj = j + dj;
                if (ni < 0 || ni >= h || nj < 0 || nj >= w) {
                    continue;
                }
                if (s[ni][nj] == '#') continue;
                if (d[ni][nj] == inf) {
                    d[ni][nj] = d[i][j] + 1;
                    q.emplace(ni, nj);
                }
            }
        }
        ans = min(ans, d[gx][gy]);
    }
    if (ans == inf) ans = -1;
    cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    t = 1;
    while(t--){
        solve();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值