蓝桥杯考前突击

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;
}
相关推荐
W23035765732 小时前
经典算法详解:最长公共子序列 (LCS) —— 从暴力递归到动态规划完整实现
算法·动态规划·最长子序列
pzx_0012 小时前
【优化器】 随机梯度下降 SGD 详解
人工智能·python·算法
‎ദ്ദിᵔ.˛.ᵔ₎2 小时前
模板template
开发语言·c++
charlie1145141912 小时前
通用GUI编程技术——图形渲染实战(二十九)——Direct2D架构与资源体系:GPU加速2D渲染入门
开发语言·c++·学习·架构·图形渲染·win32
小肝一下2 小时前
每日两道力扣,day8
c++·算法·leetcode·哈希算法·hot100
CheerWWW2 小时前
C++学习笔记——线程、计时器、多维数组、排序
c++·笔记·学习
无限进步_2 小时前
【C++】验证回文字符串:高效算法详解与优化
java·开发语言·c++·git·算法·github·visual studio
charlie1145141913 小时前
嵌入式现代C++工程实践——第10篇:HAL_GPIO_Init —— 把引脚配置告诉芯片的仪式
开发语言·c++·stm32·单片机·c
呼啦啦5613 小时前
C++动态内存管理
c++