牛客周赛 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;
}
相关推荐
触底反弹19 分钟前
一文彻底搞懂 JavaScript 栈和队列(建议收藏)
javascript·算法·面试
WL学习笔记25 分钟前
通讯录(顺序表实现)
c语言·数据结构·算法
Jerryhut44 分钟前
opencv对齐算法及其应用
人工智能·opencv·算法
果丁智能1 小时前
智慧校园一卡通深度融合方案:基于超级SIM卡的手机碰一碰智能开锁技术落地实践
数据结构·人工智能·python·科技·算法·智能家居·信息与通信
满怀冰雪1 小时前
第13篇-栈算法入门-括号匹配-表达式与单调栈基础
java·算法
TCW11211 小时前
AI底层系列:用C++实现线性代数的公式推导与算法设计-基础篇-5.矩阵方程
人工智能·线性代数·算法
叫我:松哥1 小时前
基于机器学习和flask的体育健身风险智能分析系统,系统集成DeepSeek、聚类算法、分类算法等,准确率达90%
人工智能·python·神经网络·算法·机器学习·flask·聚类
wabs6661 小时前
关于动态规划【0-1背包思想在实际问题中是怎么转化的?】
算法·动态规划
阿文的代码库1 小时前
欧拉回路与欧拉路径的算法流程演示
算法
汤姆yu1 小时前
云知声 U2 原生智能体大模型深度解析
大数据·人工智能·算法·ai·大模型·多模态·智能体