蓝桥杯考前突击

1.快速幂+费马小定理


cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 1e9+7;
ll qpow(ll a,ll b){//快速幂
  ll res = 1;
  a %= mod;
  while(b >0){
    if(b & 1)//判断是否为奇数
      res = res * a % mod;
      a = a * a % mod;
      b >>= 1;//除以2
  }
  return res;
}


int main(){
  int t; cin>>t;
  while(t--){
    ll n; cin>>n;
    cout<<qpow(n,mod - 2)<<'\n';//最核心的一步,费马小定理
  }
  return 0;
}

2.埃氏筛


cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
const int N = 3e6+5;
using ll = long long;
vector<bool> prime(N,true);
vector<ll> pre(N,0);
void sieve(int n){
  prime[0] = prime[1] = false;
  for(ll i = 2; i <= n; i++){
    if(prime[i]){
      pre[i] = i;
      for(ll j = i * i; j <= n; j += i){
        prime[j] = false;
        if(pre[j] == 0) pre[j] = i;
      }
    }
  }
  for(ll i = 2; i <= n; i++){
    pre[i] += pre[i - 1];
  }
}
int main(){ 
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  sieve(N);
  ll t ; cin>>t;
  while(t--){
    ll n; cin>>n;
    cout<<pre[n]<<'\n';
  }
  return 0;
}

3.0-1背包

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
const int N = 105;
int a[N];
vector<int> v(N);
vector<int> w(N);
int n;
int bag(int n,int m){
  vector<int> dp(m+1);
  for(int i = 0; i < n; i++){
    for(int j = m; j >= w[i]; j--){
      dp[j] = max(dp[j],dp[j-w[i]]+v[i]);
    }
  }
  return dp[m];
}
int main(){
  int m; cin>>n>>m;
  int res = 0;
  for(int i = 0; i <n ;i ++){
    cin>>w[i]>>v[i];
  }
  res = bag(n,m);
  cout<<res;
  return 0;
}

4.差分

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
  ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  int n, q; cin>>n>>q;
  vector<ll> df(n + 2, 0);
  vector<ll> a(n + 1, 0);
  for(int i = 1; i <= n ;i ++){
    cin>>a[i];
  }
  for(int i = 1; i <= n;i++){
    df[i] = a[i] - a[i - 1];//构造差分数组
  }

  while(q--){
    ll l, r, x;
    cin>>l>>r>>x;
    df[l] += x;
    df[r+1] -= x;
  }
  for(int i = 1; i <= n ; i++){
    df[i] += df[i - 1];//求前缀和
    ll ans = df[i];
    if(ans < 0) ans = 0;
    if(i == n) cout<<ans<<'\n';
    else cout<<ans<<" ";
  }
  return 0;
}

5.前缀和

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 5;
ll a[N];
int main(){
  int n; cin>>n;
  ll sum = 0;
  for(int i = 0; i < n; i++){
    cin>>a[i];
    sum+=a[i];
  }
  ll res = 0;
  for(int i = 0; i < n;i++){
    sum -= a[i];
    res += a[i] * sum;
  }
  cout<<res<<'\n';
  return 0;
}

6.进制转换

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
  string s = "2022";
  int base = 9;
  ll res = 0;
  for(char c : s){
    int val = isdigit(c) ? c - '0' : toupper(c) - 'A' + 10;
    res = res * base + val;
  }
  cout<<res;
  return 0;
}

7.LIS---最长上升子序列(贪心+二分)

8.DFS

P1036

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
int a[21];
int n, k;
int res = 0;
bool prime(int x){
	if(x < 2) return false;
	for(int i = 2; i * i <= x; i++){
		if(x % i == 0) return false;
	}
	return true;
}
void dfs(int pos,int sum,int cnt){
	if(cnt == k){
		if(prime(sum)) res++;
		return ;
	}
	for(int i = pos; i < n ; i++){
		dfs(i + 1, sum + a[i], cnt + 1);
	}
	return ;
}
int main(){
	cin>>n>>k;
	for(int i = 0; i < n; i++) cin>>a[i];
	dfs(0,0,0);
	cout<<res;
	return 0;
}

P9241

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
struct plane{
	ll t,d,l;
};
vector<plane> p;
vector<bool> vis;
int n;
bool success;
void dfs(ll cur, int cnt){
	if(cnt == n) {
	  success = true;
	  return;	
	}
	if(success) return ;
	for(int i = 0; i < n;i++){
		if(vis[i]) continue;
		ll start = max(cur,p[i].t);
		if(start > p[i].t + p[i].d) continue;
		vis[i] = true;
		dfs(start+p[i].l,cnt+1);
		vis[i] = false;	
	}
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	ll t; cin>>t;
	while(t--){
	cin>>n;
	p.resize(n);
	vis.assign(n,false);
	for(int i =0 ; i < n; i++){
		cin>>p[i].t>>p[i].d>>p[i].l;
	}
	success = false;
	dfs(0,0);
	cout<<(success?"YES":"NO")<<"\n";
 }

	return 0;
}
相关推荐
MartinYeung519 分钟前
[论文学习]DP2Unlearning:高效且具保证的大型语言模型遗忘框架(基于差分隐私的 LLM Unlearning 方法)
学习·算法·语言模型
Tian_Hang32 分钟前
C++原型模式(Protype)
开发语言·c++·算法
bIo7lyA8v32 分钟前
算法复杂度的渐进分析与实际运行时间的差异的技术8
算法
yuan199971 小时前
欧拉梁静力与屈曲计算的 MATLAB 实现(有限差分法 + 解析解)
开发语言·算法·matlab
FL16238631292 小时前
[cmake]基于C++使用纯opencv部署ppocrv5v6的onnx模型
开发语言·c++·opencv
玖玥拾2 小时前
C/C++ 数据结构(六)链表迭代器与底层
c语言·数据结构·c++·链表·stl库
牛油果子哥q2 小时前
AVL平衡树与红黑树深度精讲对比,平衡因子、四大旋转原理、着色规则、平衡策略、性能差异与面试手撕全解
数据结构·c++·面试
汉克老师2 小时前
GESP7级C++考试语法知识(二、指数函数(3、综合练习)
c++·算法·数学建模·指数函数·gesp7级·复利
C++ 老炮儿的技术栈2 小时前
Ubuntu root账号自动登陆
linux·运维·服务器·c语言·c++·ubuntu·visual studio
林间码客3 小时前
04 ROC曲线与AUC:从零开始手动计算
大数据·人工智能·算法