每日刷题(算法)

我们N个真是太厉害了

思路:

我们先给数组排序,如果最小的元素不为1,那么肯定是吹牛的,我们拿一个变量记录前缀和,如果当前元素大于它前面所有元素的和+1,那么sum+1是不能到达的值。

代码:

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<algorithm>
#define int long long
#define pb push_back
#define TEST int T; cin >> T; while (T--)
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define lowbit(x) x&(-x)
#define pll pair<int,int>
const int N = 1e6 + 30;
const int M = 1e3 + 10;
const int inf = 512785182741247112;
const int mod = 1e9 + 7;
using namespace std;


void solve()
{
    int n;
    cin >> n;
    vector<int>a(n + 1);
    for (int i = 1; i <= n; i++) cin >> a[i];
    sort(a.begin() + 1, a.end());
    if (a[1] != 1) {
        cout << 1 << '\n';
        return;
    }
    int sum = 1;
    for (int i = 2; i <= n; i++) {
        if (a[i] > sum + 1) {
            cout << sum + 1 << '\n';
            return;
        }
        sum += a[i];
         if (sum >= n) {
            cout << "Cool!\n";
            return;
        }
    }
    cout << "Cool!\n";
    
}
signed main() {
    ios; TEST
        solve();
    return 0;
}

折返跑

思路:

其实是一个组合数学的题目,我们只要规定每个折返至少挪一米,那么剩下的就可以随便安排,就是一个C(可用的距离,折返次数)就是答案,我们可以预处理一下1到1e6的阶乘。

代码:

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<algorithm>
#define int long long
#define pb push_back
#define TEST int T; cin >> T; while (T--)
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define lowbit(x) x&(-x)
#define pll pair<int,int>
const int N = 1e6 + 30;
const int M = 1e3 + 10;
const int inf = 512785182741247112;
const int mod = 1e9 + 7;
using namespace std;

int f[N], a[N], sum[N];
int C(int n, int m) {
    if (n < m) return 0;
    if (m == 0) return 1;
    return f[n] * qpow(f[n - m] * f[m], mod - 2, mod) % mod;
}
void solve()
{
    int n, m;
    cin >> n >> m;
    cout << C(n - 2, m - 1) << '\n';

}
signed main() {
    f[0] = 1;
    f[1] = 1;
    for (int i = 2; i <= 1e6; i++) f[i] = f[i - 1] * i % mod;
    ios; TEST
        solve();
    return 0;
}

好好好数

思路:

直接将n变成k进制数,发现答案就是最大的因为,当k为1时,直接输出1。

代码:

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<algorithm>
#define int long long
#define pb push_back
#define TEST int T; cin >> T; while (T--)
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define lowbit(x) x&(-x)
#define pll pair<int,int>
const int N = 3e5 + 30;
const int M = 1e3 + 10;
const int inf = 512785182741247112;
const int mod = 998244353;
using namespace std;

vector<int>f;
int n, k;
void solve()
{
    cin >> n >> k;
    if (k == 1) {
        cout << 1 << '\n';
        return;
    }

    int ans = 1;
    while (n) {
        ans = max(ans, n % k);
        n /= k;
    }
    cout << ans << '\n';

    
}

signed main() {

    ios; TEST
        solve();
    return 0;
}
相关推荐
liminla!10 分钟前
SQL 从入门到进阶:语法速查与实战练习(完整优化版)
数据库·sql·算法
维克兜率天1 小时前
【维克】指数调整:哪些股票会被“强制买入“?
人工智能·python·算法
罗西的思考2 小时前
【轻量化复刻大模型】线性注意力交互式演示
人工智能·算法·机器学习
神也畏惧菠萝2 小时前
Day08 Python流程控制从入门到上瘾:if/match/while/for四件套,从B站登录一路打到猜数字游戏
python·算法·数据存储·for·while·if·ifelse
揽星河..2 小时前
【C语言】分支和循环
c语言·c++·算法
Fuyo_11194 小时前
C++ AVL树保姆级详解:从平衡因子到四大旋转机制
java·c++·算法
jktantan4 小时前
每个节点 41 字节:把一棵百万节点树拆进 14 个 typed array
前端·javascript·vue.js·算法·typescript
揽星河..5 小时前
【C语言】猜数字游戏
c语言·算法·游戏
LabVIEW开发5 小时前
CTM-3 红外测温仪 + LabVIEW 温度怎么测?
算法·labview·测试·labview程序