0
点赞
收藏
分享

微信扫一扫

蓝桥杯赛前冲刺30天打卡题解(Day1)

梦为马 2022-03-12 阅读 65

一、纯质数

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n = 20210605;
    int ans = 0;
    vector<bool> isp(n+1, true);
    isp[0]=false;
    isp[1]=false;
    for (int i = 2; i <= n; i++) {
        if (isp[i]) {
            string s = to_string(i);
            if (all_of(s.begin(), s.end(), [&isp](char c) {return isp[c ^ 48]; })) {
                ans++;
            }
            long long j = (long long)i * i;
            while (j <= n) {
                isp[j] = false;
                j += i;
            }
        }
    }
    cout << ans;
    return 0;
}

二、最少砝码 

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
  int n;
  cin>>n;
  int ans;

  for(ans=1;;ans++)
  {
    if(pow(3,ans)-1>=2*n)break;
  }
  cout<<ans<<endl;
  return 0;
}

三、灌溉

 

 

#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>

using namespace std;

typedef pair<int, int> PII;

const int N = 110;

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

queue<PII> q;
int d[N][N];
int n, m, t, k;

int bfs(){
    int res = t;
    //cout << res << endl;

    while(q.size()){
        auto t = q.front();
        q.pop();

        for(int i = 0; i < 4; i ++ ){
            int a = t.first + dx[i];
            int b = t.second + dy[i];

            if(a >= 1 && a <= n && b >= 1 && b <= m && d[a][b] == -1){
                d[a][b] = d[t.first][t.second] + 1;
                res ++ ;
                if(d[a][b] < k){
                    q.push({a, b});
                }
            }
        }
    }
    return res;
}

int main(){
    freopen("in.in", "r", stdin);

    memset(d, -1, sizeof d);

    cin >> n >> m >> t;

    int tmp = t;
    while(tmp -- ){
        int x, y;
        cin >> x >> y;
        q.push({x, y});
        d[x][y] = 0;
    }
    cin >> k;

    cout << bfs();
    return 0;
}

 

 

 

 

 

 

举报

相关推荐

0 条评论