基础算法(三)#蓝桥杯

文章目录

11、构造

11.1、小浩的ABC

c 复制代码
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
using ll = long long;

int main(){
	IOS;
	int t;cin>>t;
	while(t--){
		ll x;cin>>x;
		// A B C  <=10^6
		if(x<2){
			cout<<"-1"<<'\n';continue;
		}
		if(x<=1000000){
			cout<<x-1<<" 1 1"<<'\n';continue;
		}
		ll a=1000000,c=x%a,b;
		if(c==0)c=a;
		b=(x-c)/a;
		cout<<a<<' '<<b<<' '<<c<<'\n';
	}
	return 0;
}

11.2、小新的质数序列挑战

c 复制代码
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
using ll = long long;

int main(){
	IOS;
	int T;cin>>T;
	while(T--){
		//质数序列,只能被1或本身整除的序列
		ll a,b;cin>>a>>b;
		if(a==1||b==1){
			cout<<"-1\n";continue;
		}
		auto gcd = [&](ll a,ll b){
			while(b!=0){
				ll t=a%b;
				a=b;
				b=t;
			}
			return a;
		};
		ll g=gcd(a,b);
		if(g==1){
			cout<<"1\n";
		}else{
			cout<<"0\n";
		}
	}
	return 0;
}

11.3、小蓝找答案

c 复制代码
#include <bits/stdc++.h>

using LL = long long;
using Pair = std::pair<int, int>;
#define inf 1'000'000'000

void solve(const int &Case) {
    int n;
    std::cin >> n;
    std::vector<int> a(n);
    for (auto &x: a)std::cin >> x;
    auto ck = [&]() {
        for (int i = 1; i < n; i++) {
            if (a[i] <= a[i - 1])return false;
        }
        return true;
    };
    if (ck()) {
        std::cout << "1\n";
        return;
    }
    int l = 2, r = n, ret = n;
    while (l <= r) {
        int mid = (l + r) >> 1;
        std::vector<Pair> A;
        int flag = 0;
        std::function<void(int)> push = [&](int x) {
            if (x <= 0) {
                flag = 1;
                return;
            }
            while (!A.empty() && A.back().first > x)A.pop_back();
            if (A.empty()) {
                A.emplace_back(x, 1);
                return;
            }
            if (A.back().first == x) {
                A.back().second++;
                if (A.back().second == mid) {
                    push(x - 1);
                    A.emplace_back(x, 0);
                }
            }
            else {
                A.emplace_back(x, 1);
            }
        };
        A.emplace_back(a[0], 0);
        for (int i = 1; i < n; i++) {
            if (a[i] > a[i - 1])A.emplace_back(a[i], 0);
            else push(a[i]);
            if (flag)break;
        }
        if (flag)l = mid + 1;
        else {
            ret = mid;
            r = mid - 1;
        }
    }
    std::cout << ret << '\n';
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    int T = 1;
//    std::cin >> T;
    for (int Case = 1; Case <= T; Case++)solve(Case);
    return 0;
}

11.4、小蓝的无限集

c 复制代码
#include <bits/stdc++.h>

using LL = long long;
using Pair = std::pair<int, int>;
#define inf 1'000'000'000

void solve(const int &Case) {
    int a, b, n;
    std::cin >> a >> b >> n;
    if (a == 1) {
        if (n % b == 1)std::cout << "Yes\n";
        else std::cout << "No\n";
        return;
    }
    // 枚举 a ^ i
    LL pw = 1;
    while (pw <= n) {
        if ((n - pw) % b == 0) {
            std::cout << "Yes\n";
            return;
        }
        pw *= a;
    }
    std::cout << "No\n";
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    int T = 1;
    std::cin >> T;
    for (int Case = 1; Case <= T; Case++)solve(Case);
    return 0;
}

12、高精度

12.1、阶乘数码(高精度*单精度)

c 复制代码
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
void mult(vector<int>& a,int b){
	int d=0;
	for(int i=0;i<a.size();i++){
		int p=a[i]*b+d;
		a[i]=p%10;
		d=p/10;
	}
	while(d){
		a.push_back(d%10);
		d/=10;
	}
}
int main() {
	IOS;
	int t;cin>>t;
	while(t--){
		int n,a1;cin>>n>>a1;
		vector<int> ans(1,1);
		for(int i=2;i<=n;i++){
			mult(ans,i);//n的阶乘
		}
		int res=0;
		for(const auto &x:ans){
			if(x==a1)res++;
		}
		cout<<res<<"\n";
	}
	return 0;
}

越来越难了。。。。。

相关推荐
程序员泡椒1 小时前
二分查找Go版本实现
数据结构·c++·算法·leetcode·go·二分
小雨下雨的雨1 小时前
Flutter鸿蒙共赢——墨染算法:柏林噪声与鸿蒙生态中的数字水墨意境
算法·flutter·华为·交互·harmonyos·鸿蒙
NAGNIP7 小时前
万字长文!回归模型最全讲解!
算法·面试
知乎的哥廷根数学学派8 小时前
面向可信机械故障诊断的自适应置信度惩罚深度校准算法(Pytorch)
人工智能·pytorch·python·深度学习·算法·机器学习·矩阵
txinyu的博客8 小时前
解析业务层的key冲突问题
开发语言·c++·分布式
666HZ6669 小时前
数据结构2.0 线性表
c语言·数据结构·算法
SmartRadio9 小时前
ESP32添加修改蓝牙名称和获取蓝牙连接状态的AT命令-完整UART BLE服务功能后的完整`main.c`代码
c语言·开发语言·c++·esp32·ble
实心儿儿10 小时前
Linux —— 基础开发工具5
linux·运维·算法
charlie11451419111 小时前
嵌入式的现代C++教程——constexpr与设计技巧
开发语言·c++·笔记·单片机·学习·算法·嵌入式
清木铎12 小时前
leetcode_day4_筑基期_《绝境求生》
算法