牛客周赛 Round 127

https://ac.nowcoder.com/acm/contest/126798#question

目录

总结

[Get The Number](#Get The Number)

Sudoku

[Carry The Bit](#Carry The Bit)

[Permutation² Counting](#Permutation² Counting)


总结

注意C题:卡很久时再仔细读题,可能是题意理解偏差

Get The Number

cpp 复制代码
#include <iostream>
using namespace std;
int main()
{
    int a, b, c;
    cin >> a >> b >> c;
    bool t = false;
    if (a / b == c && c * b == a)
    {
        t = true;
    }
    if (a + b == c || a - b == c || a * b == c || t)
    {
        cout << "YES";
    }
    else
    {
        cout << "NO";
    }
    return 0;
}

优化

cpp 复制代码
#include <iostream>
using namespace std;
int main()
{
    int a, b, c;
    cin >> a >> b >> c;
    if (a + b == c || a - b == c || a * b == c || b * c == a)
    {
        cout << "YES";
    }
    else
    {
        cout << "NO";
    }
    return 0;
}

Sudoku

cpp 复制代码
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
void solve()
{
    unordered_map<int, int> map;
    vector<vector<int>> gra(5, vector<int>(5));
    for (int i = 1; i <= 4; i++)
    {
        for (int j = 1; j <= 4; j++)
        {
            cin >> gra[i][j];
        }
    }
    // 行
    for (int i = 1; i <= 4; i++)
    {
        map.clear();
        for (int j = 1; j <= 4; j++)
        {
            map[gra[i][j]]++;
        }
        if (map.size() != 4)
        {
            cout << "NO" << endl;
            return;
        }
    }
    // 列
    for (int i = 1; i <= 4; i++)
    {
        map.clear();
        for (int j = 1; j <= 4; j++)
        {
            map[gra[j][i]]++;
        }
        if (map.size() != 4)
        {
            cout << "NO" << endl;
            return;
        }
    }
    // 左上
    map.clear();
    for (int i = 1; i <= 2; i++)
    {
        for (int j = 1; j <= 2; j++)
        {
            map[gra[i][j]]++;
        }
    }
    if (map.size() != 4)
    {
        cout << "NO" << endl;
        return;
    }
    // 右上
    map.clear();
    for (int i = 1; i <= 2; i++)
    {
        for (int j = 3; j <= 4; j++)
        {
            map[gra[i][j]]++;
        }
    }
    if (map.size() != 4)
    {
        cout << "NO" << endl;
        return;
    }
    // 左下
    map.clear();
    for (int i = 3; i <= 4; i++)
    {
        for (int j = 1; j <= 2; j++)
        {
            map[gra[i][j]]++;
        }
    }
    if (map.size() != 4)
    {
        cout << "NO" << endl;
        return;
    }
    // 右下
    map.clear();
    for (int i = 3; i <= 4; i++)
    {
        for (int j = 3; j <= 4; j++)
        {
            map[gra[i][j]]++;
        }
    }
    if (map.size() != 4)
    {
        cout << "NO" << endl;
        return;
    }
    cout << "YES" << endl;
}
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

Carry The Bit

卡了很久的时间,只能拿52/150分,一直不知道问题出在哪,原来是没仔细看题目

小苯有一个正整数 nn,你必须对 nn 进行以下操作恰好一次:

原本想的是没有>=5可以不操作,原来必须操作一次

cpp 复制代码
#include <iostream>
using namespace std;
#define int long long
void solve()
{
    string s;
    cin >> s;
    if (s[0] >= '5' && s[0] <= '9')
    {
        cout << '1';
        for (int i = 0; i < s.size(); i++)
        {
            cout << '0';
        }
        cout << endl;
        return;
    }
    for (int i = 1; i < s.size(); i++)
    {
        if (s[i] >= '5' && s[i] <= '9')
        {
            s[i - 1] = s[i - 1] + '1' - '0';
            for (int j = 0; j <= i - 1; j++)
            {
                cout << s[j];
            }
            for (int j = i; j < s.size(); j++)
            {
                cout << 0;
            }
            cout << endl;
            return;
        }
    }
    for (int i = 0; i < s.size() - 1; i++)
    {
        cout << s[i];
    }
    cout << '0' << endl;
}
signed main()
{
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

Permutation² Counting

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MOD = 998244353;
const int INV2 = (MOD + 1) / 2;

void solve() {
    int n;
    cin >> n;
    unordered_map<int,int> mp;
    mp.reserve(n * 2);

    for(int i=0;i<n;i++){
        int x; cin >> x;
        if (x <= n) mp[x]++;          // 可选:只统计 <=n 的,省空间/时间
        else mp[x]++;                // 不加这行也行,但上面那行能优化
    }

    long long ans = 0;
    long long k = 1;

    for(int i=1;i<=n;i++){
        auto it = mp.find(i);
        if(it == mp.end() || it->second < 2) break;

        long long c = it->second % MOD;
        long long c2 = c * ((c - 1 + MOD) % MOD) % MOD * INV2 % MOD; // C(c,2) mod
        k = k * c2 % MOD;
        ans = (ans + k) % MOD;
    }
    cout << ans << "\n";
}

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

    int T;
    cin >> T;
    while(T--) solve();
    return 0;
}
相关推荐
大闲在人17 小时前
8. 供应链与制造过程术语:产能
算法·制造·供应链管理·智能制造·工业工程
一只小小的芙厨17 小时前
寒假集训笔记·以点为对象的树形DP
c++·算法
历程里程碑17 小时前
普通数组----合并区间
java·数据结构·python·算法·leetcode·职场和发展·tornado
执风挽^17 小时前
Python基础编程题2
开发语言·python·算法·visual studio code
Z9fish18 小时前
sse哈工大C语言编程练习20
c语言·开发语言·算法
晓131318 小时前
第六章 【C语言篇:结构体&位运算】 结构体、位运算全面解析
c语言·算法
iAkuya18 小时前
(leetcode)力扣100 61分割回文串(回溯,动归)
算法·leetcode·职场和发展
梵刹古音18 小时前
【C语言】 指针与数据结构操作
c语言·数据结构·算法
VT.馒头18 小时前
【力扣】2695. 包装数组
前端·javascript·算法·leetcode·职场和发展·typescript
CoderCodingNo19 小时前
【GESP】C++五级练习题 luogu-P1865 A % B Problem
开发语言·c++·算法