牛客周赛 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;
}
相关推荐
大锦终2 小时前
dfs解决FloodFill 算法
c++·算法·深度优先
橘颂TA2 小时前
【剑斩OFFER】算法的暴力美学——LeetCode 200 题:岛屿数量
算法·leetcode·职场和发展
苦藤新鸡2 小时前
14.合并区间(1,3)(2,5)=(1,5)
c++·算法·leetcode·动态规划
程序员-King.2 小时前
day145—递归—二叉树的右视图(LeetCode-199)
算法·leetcode·二叉树·递归
漫随流水2 小时前
leetcode算法(112.路径总和)
数据结构·算法·leetcode·二叉树
过期的秋刀鱼!2 小时前
机器学习-带正则化的成本函数-
人工智能·python·深度学习·算法·机器学习·逻辑回归
ScilogyHunter2 小时前
前馈/反馈控制是什么
算法·控制
_OP_CHEN3 小时前
【算法基础篇】(四十八)突破 IO 与数值极限:快速读写 +__int128 实战指南
c++·算法·蓝桥杯·算法竞赛·快速读写·高精度算法·acm/icpc
程序员泠零澪回家种桔子3 小时前
RAG自查询:让AI精准检索的秘密武器
人工智能·后端·算法