第18场小白入门赛(蓝桥杯)

news/2024/10/4 12:23:22 标签: 蓝桥杯, 算法

第 18 场 小白入门赛

6 武功秘籍

考察进制理解。

对于第 i i i 位,设 b i t i = x bit_i=x biti=x ,每一位的最大值是 b j b_j bj ,也就是说每一位是 b j + 1 b_j+1 bj+1 进制 ,那么第 i i i 位的大小就是 x × ∑ j = i + 1 l a s ( b j + 1 ) x\times \sum_{j=i+1}^{las} (b_j+1) x×j=i+1las(bj+1)

据此推导。

#include<bits/stdc++.h>
using namespace std;
#define int long long

void solve(){
    int n, k;
    cin >> n >> k;
    if(k == 1){
        cout << "0\n";
        return ;
    }
    -- k;
    vector<int> bit;
    while(n){
        bit.push_back(n % 10);
        n /= 10;
    }
    vector<int> t(bit.size() + 1);
    // for(auto &x : bit) cout << x << ' '; cout << '\n';
    int l, r = 0, lasBit = 0;
    t[0] = 1;
    for(int i = 0; i < bit.size(); i ++){
        l = r + 1;
        r += t[i] * bit[i];
        if(r >= k){
            lasBit = i;
            break;
        }
        // cout << i << ' ' << l << ' ' << r << '\n';
        t[i + 1] = t[i] * (bit[i] + 1);
    }
    // cout << lasBit << '\n';
    for(int i = lasBit; i >= 0; i --){
        cout << (k / t[i]);
        k %= t[i];
    }
}

signed main(){
    // ios::sync_with_stdio(false);
    // cin.tie(0), cout.tie(0);

    int T = 1;
    // cin >> T;
    while (T --){
        solve();
    }

    return 0;
}

2 + 4 情报传递

思维题,技巧在于具有大量相同的区间,实际上只有左右两个特殊区间,以及中间大量相同的区间。

首先,如果不存在点不能走,那么从 x x x 走到 y y y ,贪心走 ⌈ y − x 2 ⌉ \lceil \frac {y-x}{2} \rceil 2yx 即可。

假设在 a → b a\rightarrow b ab 区间内,存在 x x x c c c 的倍数,设 d d d 是这些点的最小值, e e e 是最大值。

那么从 a → d − 1 a\rightarrow d-1 ad1 e + 1 → b e+1\rightarrow b e+1b 贪心走即可。

剩下的全是相同的走法。

O ( 1 ) O(1) O(1)

dp 思路很简答,爬楼梯问题, O ( n ) O(n) O(n)

Trick :

第一个 ≥ x \geq x x c c c 的倍数 : ⌈ x c ⌉ × c \lceil \frac x c \rceil \times c cx×c

第一个 ≤ x \leq x x c c c 的倍数 : ⌊ x c ⌋ × c \lfloor \frac x c \rfloor \times c cx×c

#include<bits/stdc++.h>
using namespace std;
#define int long long

int dis(int a, int b){
    return (b - a) / 2 + (b - a) % 2;
}

void solve(){
    int a, b, c, x = 0;
    cin >> a >> b >> c;
    int d = (a + c - 1) / c * c; // 第一个 >= a 的 c 的倍数
    int e = b / c * c; // 第一个 <= b 的 c 的倍数
    if(d > b){ 
        cout << dis(a, b) << '\n';
    }
    else{
        cout << dis(a, d - 1) + dis(e + 1, b) + 1 + dis(1, c + 1) * ((e - d) / c) << '\n';
    } 
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    int T = 1;
    cin >> T;
    while (T --){
        solve();
    }

    return 0;
}

3 村长分钱

已知 a , b a,b a,b , 求满足 a   m o d   y = b a \bmod y=b amody=b ,多少 y y y 满足要求

a = b + x y a=b+xy a=b+xy

x y = b − a xy=b-a xy=ba ,计算一下 b − a b-a ba 有多少因子即可。

注意只统计 > b >b >b 的因子。

#include <iostream>
#include <cmath>
using namespace std;

int count_solutions(int a, int b) {
    if (a < b) return 0; // 无解的情况
    int d = a - b;
    int count = 0;

    // 枚举 d 的因数
    for (int x = 1; x * x <= d; ++x) {
        if (d % x == 0) {
            if (x > b) count++;         // x 是 d 的因数,且 x > b
            if (d / x != x && d / x > b) count++; // d/x 也是因数,且 d/x > b
        }
    }

    return count;
}

int main() {
    int a, b;
    cin >> a >> b;
    cout << count_solutions(a, b) << endl;
    return 0;
}

5 好汉身份

假设先手的回合,发现选择 a + b a+b a+b 小的更优。

发现后手同样如此。

#include<bits/stdc++.h>
using namespace std;
#define int long long

struct node{
    int a, b;
    bool operator < (const node & T) const {
        return a + b < T.a + T.b;
    }
}v[2100];

int n;

void solve(){
    cin >> n;
    n <<= 1;
    for(int i = 1; i <= n; i ++){
        cin >> v[i].a;
    }
    for(int i = 1; i <= n; i ++){
        cin >> v[i].b;
    }
    sort(v + 1, v + n + 1);
    int res = 0; 
    for(int i = 1; i <= n; i ++){
        if(i & 1) res += v[i].a;
        else res -= v[i].b;
    }
    cout << res;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    int T = 1;
    // cin >> T;
    while (T --){
        solve();
    }

    return 0;
}

http://www.niftyadmin.cn/n/5689931.html

相关文章

深度学习基础—残差网络ResNets

1.残差网络结构 当网络训练的很深很深的时候&#xff0c;效果是否会很好&#xff1f;在这篇论文中&#xff0c;作者给出了答案&#xff1a;Deep Residual Learning for Image Recognitionhttps://www.cv-foundation.org/openaccess/content_cvpr_2016/papers/He_Deep_Residual_…

stm32f103调试,程序与定时器同步设置

在调试定时器相关代码时&#xff0c;注意到定时器的中断位总是置1&#xff0c;怀疑代码有问题&#xff0c;经过增大定时器的中断时间&#xff0c;发现定时器与代码调试并不同步&#xff0c;这一点对于调试涉及定时器的代码是非常不利的&#xff0c;这里给出keil调试stm32使定时…

【C语言系统编程】【第三部分:网络编程】3.1 套接字编程(TCP/UDP基础)

第三部分&#xff1a;网络编程 3.1 套接字编程&#xff08;TCP/UDP基础&#xff09; 3.1.1 套接字基础 3.1.1.1 套接字概念与类型 套接字&#xff08;Socket&#xff09;是网络编程的基础&#xff0c;它提供了一种进程间通信的机制。根据传输特点&#xff0c;套接字主要分为…

【网络安全】Cookie与ID未强绑定导致账户接管

未经许可,不得转载。 文章目录 前言正文前言 DigiLocker 是一项在线服务,旨在为公民提供一个安全的数字平台,用于存储和访问重要的文档,如 Aadhaar 卡、PAN 卡和成绩单等。DigiLocker 通过多因素身份验证(MFA)来保护用户账户安全,通常包括 6 位数的安全 PIN 和一次性密…

python爬虫 - 初识爬虫

&#x1f308;个人主页&#xff1a;https://blog.csdn.net/2401_86688088?typeblog &#x1f525; 系列专栏&#xff1a;https://blog.csdn.net/2401_86688088/category_12797772.html 目录 前言 一、爬虫的关键概念 &#xff08;一&#xff09;HTTP请求与响应 &#xff0…

问:SQL中join语法的差异?

在SQL中&#xff0c;JOIN语法用于结合来自两个或多个表的数据。不同类型的JOIN会基于不同的条件来合并表中的数据。以下是几种常见的JOIN及其差异&#xff1a; 假设我们有两个表&#xff1a;employees 和 departments。 employees 表: employee_idnamedepartment_id1Alice10…

面试速通宝典——11

188. 总结static的应用和作用 函数体内static变量的作用范围为该函数体&#xff0c;不同于auto变量&#xff0c;该变量的内存只被分配一次&#xff0c;因此其值在下次调用时仍维持上次的值。在模块内的static全局变量可以被模块内所用函数访问&#xff0c;但不能被模块外其他函…

【每天学个新注解】Day 14 Lombok注解简解(十三)—@onX(onMethod= 、onConstructor= 、onParam=)

onX&#xff08;onMethod 、onConstructor 、onParam&#xff09; 添加自定义注解 设置注解时在注解上增加注解参数&#xff0c;使生成的代码上也带有注解。 1、如何使用 Lombok注解生成的代码上自也需要注解时使用。 2、代码示例 例&#xff1a;使用Lombok官网示例。 Al…