基础算法(三)#蓝桥杯

文章目录

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 小时前
自己写算法(九)网页数字动画函数——东方仙盟化神期
前端·javascript·算法·仙盟创梦ide·东方仙盟·东方仙盟算法
豐儀麟阁贵1 小时前
基本数据类型
java·算法
Larry_Yanan3 小时前
QML学习笔记(三十四)QML的GroupBox、RadioButton
c++·笔记·qt·学习·ui
程序员老舅3 小时前
干货|腾讯 Linux C/C++ 后端开发岗面试
linux·c语言·c++·编程·大厂面试题
乐迪信息3 小时前
乐迪信息:基于AI算法的煤矿作业人员安全规范智能监测与预警系统
大数据·人工智能·算法·安全·视觉检测·推荐算法
程序员Aries3 小时前
自定义网络协议与序列化/反序列化
linux·网络·c++·网络协议·程序人生
Pafey3 小时前
MFC中一个类的成员变量值自动被篡改:多重继承带来的问题
c++·mfc
hsjkdhs4 小时前
C++之多层继承、多源继承、菱形继承
开发语言·c++·算法
立志成为大牛的小牛4 小时前
数据结构——十七、线索二叉树找前驱与后继(王道408)
数据结构·笔记·学习·程序人生·考研·算法
星空下的曙光4 小时前
Node.js crypto模块所有 API 详解 + 常用 API + 使用场景
算法·node.js·哈希算法