牛客周赛 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;
}
相关推荐
Tisfy6 小时前
LeetCode 2333.最小差值平方和:排序 + 数学计算(不用二分)
数学·算法·leetcode·题解·贪心·排序
aqiu1111116 小时前
【C++算法打怪专栏】LeetCode 217. 存在重复元素(解法总结)
c++·算法·leetcode
yi0118 小时前
DAY28: LeetCode 111. 二叉树的最小深度
人工智能·笔记·python·算法·leetcode·二叉树·递归
wuyk5558 小时前
27.LeetCode 2265. 统计值等于子树平均值的节点数 —— 后序遍历实战
stm32·单片机·嵌入式硬件·算法
xiangyun618 小时前
C++ list 模拟实现(二):const迭代器与反向迭代器,模板的黑魔法
开发语言·数据库·c++·算法·stl·list
xxwl5858 小时前
三、区域赛算法模版第三章---字符串相关算法
笔记·学习·算法
信奥卷王9 小时前
[GESP202509 六级] 划分字符串(含视频)
数据结构·算法
酒醉的胡铁9 小时前
相似度搜索的常见算法
算法·ai
局部有解9 小时前
Leetcode Hot100 - 最长连续序列
算法·leetcode·职场和发展
行者全栈架构师9 小时前
给开源 Rust 工具加一个新检查项,TRAE 帮我从想法跑到构建产物:全链路实战
前端·算法·架构