【2019年第十届蓝桥杯C/C++ B组国赛】 个人题解

 A: 平方序列

解题思路:

参考代码:

#include <bits/stdc++.h>  
using namespace std;  
const int N = 1e4 + 5;  
#define ll long long  
  
  
int main() {  
    ll a, b, c;  
    a = 2019 * 2019;  
    for (ll i = 2020; ; i++) {  
        for (ll j = 2020; j <= N; j++) {  
            b = i * i;  
            c = j * j;  
            if (b - a == c - b) {  
//              cout << a << ' ' << b << ' ' << c << endl;  
                cout << i + j;  
                return 0;  
            }  
        }  
    }  
    return 0;  
}  

B:质数拆分

解题思路:

参考代码:

#include <bits/stdc++.h>  
using namespace std;  
const int N = 3e3 + 5;  
#define ll long long  
vector<ll>p(1);  
bool is[N];  
  
void init() {  
    for (int i = 2; i <= 2019; i++) {  
        if (!is[i])  
            p.push_back(i);  
        for (int j = 1; j < p.size() && i * p[j] <= 2019; j++) {  
            is[i * p[j]] = 1;  
            if (i % p[j] == 0) {  
                break;  
            }  
        }  
    }  
//  for (auto i : p)  
//      cout << i << endl;  
}  
ll dp[N];  
  
int main() {  
    init();  
    dp[0] = 1;  
    for (int i = 1; i < p.size(); i++) {  
        for (int j = 2019; j >= p[i]; j--) {  
            dp[j] += dp[j - p[i]];  
        }  
    }  
    cout << dp[2019];  
    return 0;  
}  

 C: 拼接

解题思路:

参考代码:

#include <bits/stdc++.h>  
using namespace std;  
const int N = 1e1 + 5;  
#define ll long long  
bool is[N][N];  
int ans;  
  
void init() {  
    for (int i = 0; i <= 7; i++) {  
        for (int j = 0; j <= 7; j++) {  
            is[i][j] = 0;  
        }  
    }  
    for (int i = 0; i <= 7; i++) {  
        is[i][i] = 1;  
    }  
}  
  
int dx[] = {0, 0, 1, -1};  
int dy[] = {1, -1, 0, 0};  
void dfs(int x, int y, int k) {  
    if (x == 0 || y == 0 || x == 7 || y == 7) {  
        ans++;  
        return;  
    }  
    for (int i = 0; i < 4; i++) {  
        int tx = x + dx[i];  
        int ty = y + dy[i];  
        if (tx >= 0 && ty >= 0 && tx <= 7 && ty <= 7) {  
            if (is[tx][ty] == 0 && is[ty][tx] == 0) {  
                is[tx][ty] = 1;  
                is[ty][tx] = 1;  
                dfs(tx, ty, k + 1);  
                is[ty][tx] = 0;  
                is[tx][ty] = 0;  
            }  
        }  
    }  
    return;  
}  
  
int main() {  
    for (int i = 1; i <= 6 ; i++) {  
        init();  
        dfs(i, i, 0);  
    }  
    cout << ans / 2 + 2; //加上从0和7开始分的两种情况  
    return 0;  
}  

D: 求值

解题思路:

参考代码:

#include <bits/stdc++.h>  
using namespace std;  
const int N = 1e5 + 5;  
#define ll long long  
  
ll sum(ll x) {  
    ll res = 0;  
    for (int i = 1; i <= x; i++) {  
        if (x % i == 0)  
            res++;  
    }  
    return res;  
}  
  
int main() {  
    for (ll i = 1;; i++) {  
        if (sum(i) == 100) {  
            cout << i;  
            return 0;  
        }  
    }  
    return 0;  
}  

E: 路径计数

解题思路:

参考代码:

#include <bits/stdc++.h>  
using namespace std;  
const int N = 1e1 + 5;  
#define ll long long  
bool is[N][N];  
ll ans;  
int g[N][N][N][N];  
  
int dx[] = {0, 0, -1, 1};  
int dy[] = {1, -1, 0, 0};  
void dfs(int x, int y, int k) {  
    if (k > 12)  
        return;  
    if (k > 0 && x == 0 && y == 0) {  
        ans++;  
        return;  
    }  
    for (int i = 0; i < 4; i++) {  
        int tx = x + dx[i];  
        int ty = y + dy[i];  
        if (tx >= 0 && ty >= 0 && tx <= 5 && ty <= 5) {  
            if (is[tx][ty] == 0 && g[x][y][tx][ty] == 0) {  
                g[x][y][tx][ty] = 1;  
                g[tx][ty][x][y] = 1;  
                is[tx][ty] = 1;  
                dfs(tx, ty, k + 1);  
                is[tx][ty] = 0;  
                g[x][y][tx][ty] = 0;  
                g[tx][ty][x][y] = 0;  
            }  
        }  
    }  
}  
  
int main() {  
    dfs(0, 0, 0);  
    cout << ans;  
    return 0;  
}  

 F: 最优包含

解题思路:
动态规划

参考代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e3 + 5;
int inf = 0x3f3f;

string s, t;
int ls, lt;
int f[N][N];

int main() {
	cin >> s >> t;
	ls = s.size(), lt = t.size();
	s = ' ' + s, t = ' ' + t;

	memset(f, inf, sizeof f);
	for (int i = 0; i <= ls; i++) f[i][0] = 0;
	for (int i = 1; i <= ls; i++) {
		for (int j = 1; j <= lt; j++) {
			if (s[i] == t[j]) f[i][j] = f[i - 1][j - 1];
			else f[i][j] = min(f[i - 1][j], f[i - 1][j - 1] + 1);
		}
	}

	cout << f[ls][lt];
	return 0;
}

G: 排列数

解题思路:

参考代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e4 + 5;
int mod = 123456;
int n, k;
int f[N][N];
int main() {
	cin >> n >> k;
	f[1][1] = 1;
	f[2][1] = 2;
	for (int i = 3; i <= n; i ++ ) {
		for (int j = 1; j <= k && j <= i; j ++ ) {
			(f[i][j] += f[i - 1][j] * j) %= mod;
			(f[i][j] += f[i - 1][j - 1] * 2) %= mod;
			if (j > 1) (f[i][j] += f[i - 1][j - 2] * (i - j)) %= mod;
		}
	}
	cout << f[n][k];
	return 0;
}

H: 解谜游戏

解题思路:

分为四组,组内互换组间不能互换,因此每组每种颜色数量必为1/4

参考代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 5;
int n;
string a, b, c;
unordered_map<char, int>mp;
void slove() {
	cin >> a >> b >> c;
	int ok = 1;
	for (int i = 0; i < 4; i++) {
		mp[a[i]]++;
		mp[a[i + 4]]++;
		mp[a[i + 8]]++;
		mp[b[i]]++;
		mp[b[i + 4]]++;
		mp[c[i]]++;
		if (mp['G'] != 3 || mp['R'] != 2 || mp['Y'] != 1) ok = 0;
		mp.clear();
	}
	if (ok) cout << "YES" << endl;
	else cout << "NO" << endl;
}
int main() {
	cin >> n;
	while (n--) slove();
	return 0;
}

I:第八大奇迹

解题思路:

参考代码:

J: 燃烧权杖

解题思路:

参考代码:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值