蓝桥杯考前突击

1.快速幂+费马小定理

在这里插入图片描述
在这里插入图片描述

#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.埃氏筛

在这里插入图片描述
在这里插入图片描述

#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背包

在这里插入图片描述

#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.差分

在这里插入图片描述

#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.前缀和

在这里插入图片描述

#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.进制转换

在这里插入图片描述

#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
在这里插入图片描述

#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
在这里插入图片描述

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值