AcWing第 127 场周赛 - AcWing 5283. 牛棚入住+AcWing 5284. 构造矩阵 - 模拟+快速幂+数学

AcWing 5283. 牛棚入住

题目数据范围不大,直接暴力模拟即可

按照题目所说的意思即可。

cpp 复制代码
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 1e5 + 10;
#define de(x) cout << x << " ";
#define sf(x) scanf("%d", &x);
#define Pu puts("");
#define ll long long
int n, m, ans;
int a, b, c;  // 空的小栏,空的大栏,半空的大栏
int main() {
    cin >> n >> a >> b;
    c = 0;
    ans = 0;
    int x;
    while (n--) {
        cin >> x;
        // 按照题意进行简单模拟
        if (x == 1) {
            if (a > 0) {
                a--;
            } else if (b > 0) {
                b--;
                c++;
            } else if (c > 0) {
                c--;
            } else {
                ans++;
            }
        } else {
            if (b > 0) {
                b--;
            } else {
                ans += 2;
            }
        }
    }
    cout << ans << endl;
    return 0;
}

AcWing 5284. 构造矩阵

题解参考思路

上面的题解讲的很好

AC代码如下:

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define sf(x) scanf("%d", &x);
#define de(x) cout << x << " ";
#define Pu puts("");
const int N = 1e5 + 9, mod = 1e9 + 7;
ll n, m, ans;  // 注意n和m数据范围是long long
int k;
ll qmi(ll x, ll y) {  // 快速幂
    ll res = 1;
    while (y) {
        if (y & 1)
            res = (ll)(res * x) % mod;
        x = (ll)(x * x) % mod;
        y >>= 1;
    }
    return res;
}
int main() {
    cin >> n >> m >> k;
    if ((n + m & 1) && k == -1)
        cout << 0 << endl;
    else
        cout << qmi(qmi(2, n - 1), m - 1) << endl;
    return 0;
}
相关推荐
一招定胜负6 分钟前
逻辑回归核心原理与实践指南
算法·逻辑回归·线性回归
长安er16 分钟前
LeetCode 98. 验证二叉搜索树 解题总结
java·数据结构·算法·leetcode·二叉树·力扣
薛不痒17 分钟前
机器学习算法之线性回归&逻辑回归
算法·机器学习·逻辑回归
sin_hielo18 分钟前
leetcode 3433
数据结构·算法·leetcode
Swift社区25 分钟前
LeetCode 448 - 找到所有数组中消失的数字
算法·leetcode·职场和发展
OKkankan32 分钟前
二叉搜索树
c语言·数据结构·c++·算法
茶猫_37 分钟前
C++学习记录-旧题新做-字符串压缩
c语言·c++·学习·算法·leetcode
拉姆哥的小屋38 分钟前
从原子到性能:机器学习如何重塑双金属催化剂的设计范式
人工智能·python·算法·机器学习
leoufung38 分钟前
LeetCode 162:寻找峰值的二分搜索思想与区间不变式分析
算法·leetcode·职场和发展
Non importa43 分钟前
用滑动窗口代替暴力枚举:算法新手的第二道砍
java·数据结构·c++·学习·算法·leetcode·哈希算法