第 8 場 小白入門賽

value0發表於2024-03-23

第 8 場 小白入門賽

第一題:

解題思路:

乘一下。

程式碼:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define fi first
#define se second
using piii = pair<ll, pair<ll, ll>>;
const ll inf = 1ll << 60;
using ull = unsigned long long;
// using i128 = __int128_t;
const int N = 210 + 10;

void solve()
{
    int cur = 2024;
    cur *= 2.5;
    cout << (int)cur << endl;
}

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

第二題:

解題思路:

自定義排序:比較\(a + b\)\(b + a\)的字串大小,按升序排序。

程式碼:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define fi first
#define se second
using piii = pair<ll, pair<ll, ll>>;
const ll inf = 1ll << 60;
using ull = unsigned long long;
// using i128 = __int128_t;
const int N = 210 + 10;

bool cmp(string a, string b)
{
    string t1 = a + b;
    string t2 = b + a;
    return t1 < t2;
}

void solve()
{
    int n;
    cin >> n;
    vector<string> a;
    for (int i = 1; i <= n; i++)
    {
        string s;
        cin >> s;
        a.emplace_back(s);
    }
    sort(a.begin(), a.end(), cmp);
    for (auto s : a)
    {
        cout << s;
    }
    cout << endl;
}

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

第三題:

正確解題思路:

\(10\)就是求最後個位數是多少,對於這點,我們發現只要考慮\(0\sim9\)的高次冪的規律即可。打一下表能發現週期性。

賽時解法:

快速冪拆著做。

程式碼:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define fi first
#define se second
using piii = pair<ll, pair<ll, ll>>;
const ll inf = 1ll << 60;
using ull = unsigned long long;
// using i128 = __int128_t;
const int N = 210 + 10;

ll qmi(ll a, ll b)
{
    ll res = 1;
    while (b)
    {
        if (b & 1)
        {
            res = res * a % 10;
        }
        a = a * a % 10;
        b >>= 1;
    }
    return res;
}

void solve()
{
    ll x;
    cin >> x;
    string p;
    cin >> p;
    reverse(p.begin(), p.end());
    ll a = x;
    ll ans = 1;
    for (auto c : p)
    {
        int b = c - '0';
        if (b > 0)
        {
            ans = ans * qmi(a, b) % 10;
        }
        a = qmi(a, 10);
    }
    cout << ans << endl;
}

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

第四題:

解題思路:

遞推。

\(a_1 = 0\)\(a_1 = 2\)時序列唯一。

對於\(a_1 = 1\),對\(x_1 = 1和x_2 = 1\)分類討論。

注意:判掉\(x_i\)不為\(0或1\)的解法。

程式碼:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define fi first
#define se second
using piii = pair<ll, pair<ll, ll>>;
const ll inf = 1ll << 60;
using ull = unsigned long long;
// using i128 = __int128_t;
const int N = 210 + 10;

void solve()
{
    int n;
    cin >> n;
    vector<int> a(n + 1), x(n + 1, 0);
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    if (a[1] == 0)
    {
        for (int i = 3; i <= n; i++)
        {
            x[i] = a[i - 1] - (x[i - 1] + x[i - 2]);
        }
    }
    else if (a[1] == 1)
    {
        x[2] = 1;
        bool f = true;
        for (int i = 3; i <= n; i++)
        {
            x[i] = a[i - 1] - (x[i - 1] + x[i - 2]);
            if (x[i] < 0 || x[i] > 1)
            {
                f = false;
            }
        }
        if (x[n - 1] + x[n] != a[n] || !f)
        {
            x[1] = 1;
            x[2] = 0;
            for (int i = 3; i <= n; i++)
            {
                x[i] = a[i - 1] - (x[i - 1] + x[i - 2]);
            }
        }
    }
    else if (a[1] == 2)
    {
        x[1] = 1;
        x[2] = 1;
        for (int i = 3; i <= n; i++)
        {
            x[i] = a[i - 1] - (x[i - 1] + x[i - 2]);
        }
    }
    for (int i = 1; i <= n; i++)
    {
        cout << x[i] << " \n"[i == n];
    }
}

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

第五題:

解題思路:

對於\(n = 2和n = 3\)的情況特殊討論。所有元素都要變得相同。

整個序列只要按升序排序,頭兩個數字和尾兩個數字相同即合法。

程式碼:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define fi first
#define se second
using piii = pair<ll, pair<ll, ll>>;
const ll inf = 1ll << 60;
using ull = unsigned long long;
// using i128 = __int128_t;
const int N = 210 + 10;

void solve()
{
    int n;
    cin >> n;
    vector<int> a(n + 1), cnt(110, 0);
    int mx = -1;
    int sx = -1;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        cnt[a[i]]++;
    }
    for (int i = 1; i <= n; i++)
    {
        if (cnt[i] == 0)
        {
            continue;
        }
        if (cnt[i] > mx)
        {
            sx = mx;
            mx = cnt[i];
        }
        else if (cnt[i] == mx)
        {
            sx = mx;
        }
        else if (cnt[i] > sx)
        {
            sx = cnt[i];
        }
    }
    sort(a.begin() + 1, a.end());
    if (n & 1)
    {
        if (n == 3)
        {
            cout << n - mx << "\n";
        }
        else
        {
            if (a[n - 1] != a[n] && a[1] != a[2])
            {
                if (a[n - 1] == a[n - 2] || a[2] == a[3])
                {
                    cout << 1 << "\n";
                }
                else
                {
                    cout << 2 << "\n";
                }
            }
            else if (a[n - 1] == a[n] && a[1] == a[2])
            {
                cout << 0 << "\n";
            }
            else
            {
                cout << 1 << "\n";
            }
        }
    }
    else
    {
        if (n == 2)
        {
            cout << (a[1] != a[2]) << "\n";
        }
        else
        {
            if (a[n - 1] != a[n] && a[1] != a[2])
            {
                if (a[n - 1] == a[n - 2] || a[2] == a[3])
                {
                    cout << 1 << "\n";
                }
                else
                {
                    cout << 2 << "\n";
                }
            }
            else if (a[n - 1] == a[n] && a[1] == a[2])
            {
                cout << 0 << "\n";
            }
            else
            {
                cout << 1 << "\n";
            }
        }
    }
}

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

第六題:

解題思路:

可達的最小值到最大值之間的所有數都一定可以得到。

只要\(x\)在可達的最大值和最小值之間,那麼就是可以恰好得到的。

程式碼:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define fi first
#define se second
using piii = pair<ll, pair<ll, ll>>;
const ll inf = 1ll << 60;
using ull = unsigned long long;
// using i128 = __int128_t;
const int N = 210 + 10;
void solve()
{
    int n, x;
    cin >> n >> x;
    vector<int> a(n + 2);
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    vector<vector<int>> dp(n + 2, vector<int>(2));
    dp[1][0] = dp[0][0] + a[1];
    dp[1][1] = dp[0][1] + a[1];
    for (int i = 2; i <= n + 1; i++)
    {
        dp[i][0] = min(dp[i - 1][0], dp[i - 2][0]) + a[i];
    }
    for (int i = 2; i <= n + 1; i++)
    {
        dp[i][1] = max(dp[i - 1][1], dp[i - 2][1]) + a[i];
    }
    if (dp[n + 1][0] <= x && x <= dp[n + 1][1])
    {
        cout << "Yes\n";
    }
    else
    {
        cout << "No\n";
    }
}

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

相關文章