牛客周赛 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;
}
相关推荐
Jerry9 小时前
LeetCode 1002. 查找共用字符
算法
无限的鲜花10 小时前
协程本质是函数加状态机——零基础深入浅出 C++20 协程
c++·算法·c++20
旖-旎10 小时前
《LeetCode 64 最小路径和 || LeetCode 174 地下城游戏》
c++·算法·leetcode·动态规划
森林古猿110 小时前
再论斜率优化
c++·学习·算法
万法若空11 小时前
【数学-简单数论】同余中的逆
线性代数·算法
凯瑟琳.奥古斯特11 小时前
力扣1009补码解法C++实现
开发语言·c++·算法·leetcode·职场和发展
KaMeidebaby11 小时前
卡梅德生物技术快报|蛋白的叠氮基修饰:实操解析:核酸模板耦合蛋白的叠氮基修饰实现靶蛋白定点共价标记
前端·人工智能·物联网·算法·百度
通信仿真爱好者13 小时前
第【60期】--大规模MIMO系统信号检测算法误码率比较 --matlab完整代码+参考文章
算法·matlab·mimo·信号检测
AI科技星13 小时前
全域谱分析:无穷维超复数信息场分形统一场论——自然、量子、金融多重分形第一性原理完整体系(中英双语终稿)
人工智能·python·算法·金融·乖乖数学·全域数学
先吃饱再说14 小时前
为什么堆能 O(log n) 插入?拆解完全二叉树的数组魔法
算法·排序算法