2019第十届蓝桥杯大赛软件类B组C/C++国赛目录
试题 A:平方序列(结果填空)
题意:
做法:双重循环暴力找一找,把N定义成了1e4(赌不会很大)。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e4+10;
int main() {
LL x = 2019*2019, y, z;
LL rx = N, ry = N;
for(int i = 2020; i < N; ++i) {
for(int j = i+1; j < N; ++j) {
y = i*i; z = j*j;
if(z-y == y-x && i+j < rx+ry) rx = i, ry = j;
}
}
cout << rx << " " << ry << " " << rx + ry << endl;
return 0;
}
答案:7020
试题 B:质数拆分(结果填空)
题意:
做法:有坑,我一开始以为是两个素数相加,其实可以是多个。先素数筛出2019以内的素数,然后可以转化成01背包求方案数,也可以用记忆化搜索。答案有很多种,记得开long long。
代码:
1、01背包求方案数:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e4+10;
int p[N], v[N], res, tot;
LL dp[N];
int main() {
for(int i = 2; i <= 2019; ++i) {
if(v[i]) continue;
p[++tot] = i;
for(int j = i+i; j <= 2019; j += i) {
v[j] = 1;
}
}
dp[0] = 1;
for(int i = 1; i <= tot; ++i) {
for(int j = 2019; j >= p[i]; --j)
dp[j] += dp[j-p[i]];
}
cout << dp[2019] << endl;
return 0;
}
2、记忆化搜索
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 3000;
int p[N], v[N], res, tot;
LL dp[400][N];
LL dfs(int pos, int sum) {
if(pos == tot + 2 || sum > 2019) return 0