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;
}
相关推荐
_dindong5 小时前
动规:回文串问题
笔记·学习·算法·leetcode·动态规划·力扣
wangwangmoon_light5 小时前
0.0 编码基础模板
java·数据结构·算法
共享家95275 小时前
Leetcode刷题
算法·leetcode·职场和发展
simon_skywalker6 小时前
第7章 n步时序差分 n步时序差分预测
人工智能·算法·强化学习
山,离天三尺三6 小时前
基于LINUX平台使用C语言实现MQTT协议连接华为云平台(IOT)(网络编程)
linux·c语言·开发语言·网络·物联网·算法·华为云
flashlight_hi7 小时前
LeetCode 分类刷题:74. 搜索二维矩阵
python·算法·leetcode·矩阵
小年糕是糕手7 小时前
【数据结构】算法复杂度
c语言·开发语言·数据结构·学习·算法·leetcode·排序算法
程序员-King.7 小时前
day86——有效的字母异位词(LeetCode-242)
算法·字符串
xxxxxxllllllshi7 小时前
Java 代理模式深度解析:从静态到动态,从原理到实战
java·开发语言·笔记·算法·代理模式
Starry_hello world8 小时前
C++ 二分算法(1)
c++·算法·有问必答