基础算法(三)#蓝桥杯

文章目录

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;
}

越来越难了。。。。。

相关推荐
草莓熊Lotso20 分钟前
【Linux网络】UDP Socket 编程全解析:从回显服务到通用字典服务,从零实现工业级代码
linux·运维·服务器·数据库·c++·单片机·udp
飞鸿踏雪(蓝屏选手)6 小时前
137 ≤ Chrome 主密钥获取研究
c++·chrome·windows·网络安全·逆向分析
洛水水7 小时前
【力扣100题】18.随机链表的复制
算法·leetcode·链表
南宫萧幕7 小时前
规则基 EMS 仿真实战:SOC 区间划分与 Simulink 闭环建模全解
算法·matlab·控制
多加点辣也没关系7 小时前
数据结构与算法|第二十三章:高级数据结构
数据结构·算法
hoiii1879 小时前
孤立森林 (Isolation Forest) 快速异常检测系统
算法
c++之路11 小时前
适配器模式(Adapter Pattern)
java·算法·适配器模式
吴声子夜歌11 小时前
Java——接口的细节
java·开发语言·算法
myheartgo-on11 小时前
Java—方 法
java·开发语言·算法·青少年编程
汉克老师12 小时前
GESP6级C++考试语法知识(四、图与树(四))
c++·贪心算法·优先队列·哈夫曼编码·哈夫曼树·gesp6级·gesp六级