2024年10月8日下午总结

· · 个人记录

T1B3944 [语言月赛 202403] 传染病

思路

过了。

代码:

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
const int MOD = 722733748;
int n, k, m, num;
int MUL(int a, int b)
{
    return ( (a % MOD) * (b % MOD) ) % MOD;
}
int ADD(int a, int b)
{
    return ( (a % MOD) * (b % MOD) ) % MOD;
}
signed main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> n >> m >> k;
    int ans = m;
    num = m;
    for (int i = 2;i <= n;i ++)
    {
        ans = MUL(ans, k);
        num = MUL(ans, num);
    } 
    cout << num << endl;
    return 0;
}

T2P1802 5 倍经验日

思路

这道题其实是一个 dp,一个 01 背包问题,判断选与不选,然后就是一个正常的背包板子就可以了。

代码:

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
const int INF = 1e9 + 5;
int lose, win, use, n, m, dp[1001][1001];
signed main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> n >> m;
    for (int i = 1;i <= n;i ++)
    {
        cin >> lose >> win >> use;
        for (int j = 0;j <= m;j ++)
        {
            dp[i][j] = dp[i - 1][j] + lose;
            if(j >= use)
            {
                dp[i][j] = max(dp[i][j], dp[i - 1][j - use] + win);
            }
        }
    }
    cout << dp[n][m] * 5ll << endl;
    return 0;
}

T3 P1508 Likecloud-吃、吃、吃

思路

这道题其实是以个递推的过程,用 f[i][j] 来记录所有的答案,之后的转移式方程就是

f[i][j] = max(f[i - 1][j], max(f[i - 1][j - 1], f[i - 1][j + 1])) + a[i][j];

所以答案就出来了。

代码:

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e3 + 5;
const int INF = 1e9 + 5;
int f[N][N], n, m, a[N][N];
signed main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> n >> m;
    for (int i = 1;i <= n;i ++)
    {
        for (int j = 1;j <= m;j ++)
        {
            cin >> a[i][j]; 
        }
    }
    for (int i = 1;i <= n;i ++)
    {
        for (int j = 1;j <= m;j ++)
        {
            f[i][j] = max(f[i - 1][j], max(f[i - 1][j - 1], f[i - 1][j + 1])) + a[i][j];
        }
    }
    cout << max (max (f[n][(m + 1) / 2], f[n][(m + 1) / 2 + 1]), f[n][(m + 1) / 2 - 1]) << endl;
    return 0;
}

T4P10095 [ROIR 2023 Day 1] 斐波那契乘积

思路

这道题看上去是数学,实际上则是暴搜!!!。

这道题 n 的数据范围很小,所以我们直接枚举暴力搜索即可,现预处理出所有的斐波那契数列,然后看是否能整除,判断选与不选即可。

代码:

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
const int INF = 1e9 + 5;
int fb[105] = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073, 4807526976, 7778742049, 12586269025, 20365011074, 32951280099, 53316291173, 86267571272, 139583862445, 225851433717, 365435296162, 591286729879, 956722026041, 1548008755920, 2504730781961, 4052739537881, 6557470319842, 10610209857723, 17167680177565, 27777890035288, 44945570212853, 72723460248141, 117669030460994, 190392490709135, 308061521170129, 498454011879264, 806515533049393, 1304969544928657, 2111485077978050, 3416454622906707, 5527939700884757, 8944394323791464, 14472334024676221, 23416728348467685, 37889062373143906, 61305790721611591, 99194853094755497, 160500643816367088, 259695496911122585, 420196140727489673, 679891637638612258, 1100087778366101931}; // 预处理斐波那契数
int dfs(int n,  int x)
{
    if (n == 1) return 1;
    if (x == 1) return 0;
    while(n < fb[x])
    {
        x --;
    }
    int ans = 0;
    if (!(n % fb[x]))
    {
        ans += dfs(n / fb[x],  x);
    }
    return ans + dfs(n,  x - 1);
}
signed main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int T;
    cin >> T;
    while (T --)
    {
        int n;
        cin >> n;
        cout << dfs(n,  86) << endl;
    }
    return 0;
}

T5P1148 拱猪计分

呜呜呜,大模拟。

这道题在考场上真的是做了一个小时,烦死了。

思路

其实非常简单,但是有几个点都需要注意。

如果一个人有 C10,但是没有其他排,就要变成50分。

就是这样,就挂了50分。

代码:

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
const int INF = 1e9 + 5;
int a, b, c, d;
string a1[N], a2[N], a3[N], a4[N];
int ans1 = 0, ans2 = 0, ans3 = 0, ans4 = 0;
map<string, int> mp;
bool check(){
    int u = 0;
    for (int i = 1;i <= a;i ++)
    {
        if (a1[i][0] == 'H')
        {
            u ++;
        }
    }
    if (u == 13)
    {
        return true;
    }
    u = 0;
    for (int i = 1;i <= b;i ++)
    {
        if (a2[i][0] == 'H')
        {
            u ++;
        }
    }
    if ( u == 13)
    {
        return true;
    }
    u = 0;
    for (int i = 1;i <= c;i ++)
    {
        if (a3[i][0] == 'H')
        {
            u ++;
        }
    }
    if (u == 13)
    {
        return true;
    }
    u = 0;
    for (int i = 1;i <= d;i ++)
    {
        if (a4[i][0] == 'H')
        {
            u ++;
        }
    }
    if (u == 13)
    {
        return true;
    }
    return false;
}
void init1 (){
    mp["H1"] = -50; 
    mp["H2"] = -2;  
    mp["H3"] = -3;  
    mp["H4"] = -4;  
    mp["H5"] = -5;  
    mp["H6"] = -6;  
    mp["H7"] = -7;  
    mp["H8"] = -8;  
    mp["H9"] = -9;  
    mp["H10"] = -10;    
    mp["H11"] = -20;    
    mp["H12"] = -30;    
    mp["H13"] = -40;
    mp["S12"] = -100;
    mp["D11"] = 100;
} 
void init2(){
    mp["H1"] = 200; 
    mp["H2"] = 0;   
    mp["H3"] = 0;   
    mp["H4"] = 0;   
    mp["H5"] = 0;   
    mp["H6"] = 0;   
    mp["H7"] = 0;   
    mp["H8"] = 0;   
    mp["H9"] = 0;   
    mp["H10"] = 0;  
    mp["H11"] = 0;  
    mp["H12"] = 0;  
    mp["H13"] = 0;
    mp["S12"] = -100;
    mp["D11"] = 100;
}
bool check2 ()
{
    bool flag = true;
    int u = 0;
    for (int i = 1;i <= a;i ++)
    {
        if (a1[i][0] != 'H' && a1[i] != "D11" && a1[i] != "S12" && a1[i] != "C10")
        {
            flag = false;
        }
        else if (a1[i] == "D11")
        {
            u ++;
        }
        else if (a1[i] == "S12")
        {
            u ++;
        }
    }
    if (flag && u == 2)
    {
        ans1 += 500; 
        return true;
    }
    flag = true;
    u = 0;
    for (int i = 1;i <= b;i ++)
    {
        if (a2[i][0] != 'H' && a2[i] != "D11" && a2[i] != "S12" && a2[i] != "C10")
        {
            flag = false;
        }
        else if (a2[i] == "D11")
        {
            u ++;
        }
        else if (a2[i] == "S12")
        {
            u ++;
        }
    }
    if (flag && u == 2)
    {
        ans2 += 500;
        return true;
    }
    flag = true;
    u = 0;
    for (int i = 1;i <= c;i ++)
    {
        if (a3[i][0] != 'H' && a3[i] != "D11" && a3[i] != "S12" && a3[i] != "C10")
        {
            flag = false;
        }
        else if (a3[i] == "D11")
        {
            u ++;
        }
        else if (a3[i] == "S12")
        {
            u ++;
        }
    }
    if (flag && u == 2)
    {
        ans3 += 500;
        return true;
    }
    flag = true;
    u = 0;
    for (int i = 1;i <= d;i ++)
    {
        if (a4[i][0] != 'H' && a4[i] != "D11" && a4[i] != "S12" && a4[i] != "C10")
        {
            flag = false;
        }
        else if (a4[i] == "D11")
        {
            u ++;
        }
        else if (a4[i] == "S12")
        {
            u ++;
        }
    }
    if (flag && u == 2)
    {
        ans4 += 500;
        return true;
    }
    return false;
}
int to_c10()
{
    for (int i = 1;i <= a;i ++) if (a1[i] == "C10") return 1;
    for (int i = 1;i <= b;i ++) if (a2[i] == "C10") return 2;
    for (int i = 1;i <= c;i ++) if (a3[i] == "C10") return 3;
    for (int i = 1;i <= d;i ++) if (a4[i] == "C10") return 4;
    return 5;
}
signed main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    while(true)
    {
        ans1 = 0, ans2 = 0, ans3 = 0, ans4 = 0;
        cin >> a;
        for (int i = 1;i <= a;i ++) cin >> a1[i];
        cin >> b;
        for (int i = 1;i <= b;i ++) cin >> a2[i];
        cin >> c;
        for (int i = 1;i <= c;i ++) cin >> a3[i];
        cin >> d;
        for (int i = 1;i <= d;i ++) cin >> a4[i];
        if (!a && !b && !c && !d)
        {
            return 0;
        }
        if (check() == false)
        {
            init1();
            int num = to_c10();
            for (int i = 1;i <= a;i ++)ans1 += mp[a1[i]];
            for (int j = 1;j <= b;j ++)ans2 += mp[a2[j]];
            for (int i = 1;i <= c;i ++)ans3 += mp[a3[i]];
            for (int i = 1;i <= d;i ++)ans4 += mp[a4[i]];
            if (num == 1) ans1 *= 2;
            if (num == 2) ans2 *= 2;
            if (num == 3) ans3 *= 2;
            if (num == 4) ans4 *= 2;
                if (num == 1 && a == 1) ans1 = 50;
                if (num == 2 && b == 1) ans2 = 50;
                if (num == 3 && c == 1) ans3 = 50;
                if (num == 4 && d == 1) ans4 = 50;
        }
        else
        {
            int num = to_c10();
            init2();
            if (check2() == true)
            {
                if (num == 1) ans1 *= 2;
                if (num == 2) ans2 *= 2;
                if (num == 3) ans3 *= 2;
                if (num == 4) ans4 *= 2;
                if (num == 1 && a == 1) ans1 = 50;
                if (num == 2 && b == 1) ans2 = 50;
                if (num == 3 && c == 1) ans3 = 50;
                if (num == 4 && d == 1) ans4 = 50;
            }
            else
            {
                int num = to_c10();
                for (int i = 1;i <= a;i ++)ans1 += mp[a1[i]];
                for (int j = 1;j <= b;j ++)ans2 += mp[a2[j]];
                for (int i = 1;i <= c;i ++)ans3 += mp[a3[i]];
                for (int i = 1;i <= d;i ++)ans4 += mp[a4[i]];
                if (num == 1 && a > 1) ans1 *= 2;
                if (num == 2 && b > 1) ans2 *= 2;
                if (num == 3 && c > 1) ans3 *= 2;
                if (num == 4 && d > 1) ans4 *= 2;
                if (num == 1 && a == 1) ans1 = 50;
                if (num == 2 && b == 1) ans2 = 50;
                if (num == 3 && c == 1) ans3 = 50;
                if (num == 4 && d == 1) ans4 = 50;
            } 
        }
        if (ans1 > 0) cout << '+' << ans1 << ' ';
        else if (ans1 < 0) cout << ans1 << ' ';
        else cout << 0 << ' ';
        if (ans2 > 0) cout << '+' << ans2 << ' ';
        else if (ans2 < 0) cout << ans2 << ' ';
        else cout << 0 << ' ';
        if (ans3 > 0) cout << '+' << ans3 << ' ';
        else if (ans3 < 0) cout << ans3 << ' ';
        else cout << 0 << ' ';
        if (ans4 > 0) cout << '+' << ans4 << ' ';
        else if (ans4 < 0) cout << ans4 << ' ';
        else cout << 0 << ' ';
        cout << endl;
    }
    return 0;
}

T6 P5195 [USACO05DEC] Knights of Ni S

思路

过了。

代码:

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e3 + 5;
const int INF = 1e9 + 5;
char a[N][N];
int n, m, d[N][N], d1[N][N];
struct node
{
    int x,y;    
};
int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0};
void bfs1(int x, int y)
{
    memset(d, -1, sizeof d);
    queue<node> q;
    q.push({x, y});
    d[x][y] = 0;
    while (!q.empty())
    {
        node t = q.front();
        q.pop();
        for (int i = 0 ;i < 4;i ++)
        {
            int tx = t.x + dx[i], ty = t.y + dy[i];
            if (tx <= 0 || ty <= 0 || tx > n || ty > m) continue;
            if (d[tx][ty] != -1) continue;
            if (a[tx][ty] == '1') continue;
            d[tx][ty] = d[t.x][t.y] + 1;
            q.push({tx, ty});
        }
    }
}
void bfs2(int x, int y)
{
    memset(d1, -1, sizeof d);
    queue<node> q;
    q.push({x, y});
    d1[x][y] = 0;
    while (!q.empty())
    {
        node t = q.front();
        q.pop();
        for (int i = 0 ;i < 4;i ++)
        {
            int tx = t.x + dx[i], ty = t.y + dy[i];
            if (tx <= 0 || ty <= 0 || tx > n || ty > m) continue;
            if (d1[tx][ty] != -1) continue;
            if (a[tx][ty] == '1') continue;
            d1[tx][ty] = d1[t.x][t.y] + 1;
            q.push({tx, ty});
        }
    }
}
signed main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> m >> n;
    int s1, s2, s3, s4, s5, s6;
    for (int i = 1;i <= n;i ++)
    {
        for (int j = 1;j <= m;j ++)
        {
            cin >> a[i][j];
            if (a[i][j] == '3')
            {
                s3 = i, s4 = j;
            }
            else if (a[i][j] == '2')
            {
                s5 = i, s6 = j;
            }
        }
    }
    bfs1(s5, s6);
    bfs2(s3, s4);
    int cnt = INF;
    for (int i = 1;i <= n;i ++)
    {
        for (int j = 1;j <= m;j ++)
        {
            if (a[i][j] == '4' && d[i][j] != -1 && d1[i][j] != -1)
            {
                cnt = min(cnt, d[i][j] + d1[i][j]);
            }
        }
    }
    cout << cnt << endl;
    return 0;
}