第十二届蓝桥杯c++b组(第二场)
试题A 求余
#include<iostream>
using namespace std;
int main()
{
cout << 2021 % 20;
return 0;
}
//答案:1
试题B 双阶乘
#include<iostream>
using namespace std;
int main()
{
int n = 2021, res = 1;
for(int i = 2021; i >= 1; i -= 2)
{
res = res * i % 100000;
}
cout << res;
}
//答案:59375
试题C 格点
#include<iostream>
using namespace std;
int res;
int main()
{
for(int i = 1; i <= 2021; i ++)
for(int j = 1; j <= 2021; j ++)
if(i * j <= 2021) res ++;
cout << res;
return 0;
}
//答案:15698
试题D 整数分解
思路:隔板法,可以将题目类比为2021个小球,中间放四个隔板,每个隔板变化极为一种方式,所以可以推出结果为C(2020, 4)
#include<iostream>
using namespace std;
typedef long long LL;
int main()
{
LL res = 1;
for (int i = 2020, j = 1; j <= 4; i --, j ++ )
res = res * i / j;
cout << res << endl;
return 0;
}
//答案:691677274345
试题E 城邦
思路: Kruskal算法求最小生成树
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 2030, M = N * N / 2;
int n = 2021, m;
int p[N];
struct Edge
{
int a, b, c;
bool operator< (const Edge& t) const
{
return c < t.c;
}
}e[M];
int find(int x) // 并查集
{
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
int get(int x, int y)
{
int res = 0;
while (x || y)
{
int a = x % 10, b = y % 10;
if (a != b) res += a + b;
x /= 10, y /= 10;
}
return res;
}
int main()
{
for (int i = 1; i <= n; i ++ ) p[i] = i;
for (int i = 1; i <= n; i ++ )
for (int j = i + 1; j <= n; j ++ )
e[m ++ ] = {i, j, get(i, j)};
sort(e, e + m);
int res = 0;
for (int i = 0; i < m; i ++ )
{
int a = e[i].a, b = e[i].b, c = e[i].c;
if (find(a) != find(b))
{
res += c;
p[find(a)] = find(b);
}
}
printf("%d\n", res);
return 0;
}
//答案:4046
试题F 特殊年份
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
string s;
int res = 0;
for (int i = 0; i < 5; i ++ )
{
cin >> s;
if (s[0] == s[2] && s[1] == s[3] - 1)
res ++ ;
}
cout << res << endl;
return 0;
}
试题G 小平方
#include <iostream>
using namespace std;
int main()
{
int n, ans = 0;
cin >> n;
for (int i = 1; i < n; i ++ ){
if(((i * i) % n) < n / 2.0) ans ++;
}
cout << ans;
return 0;
}
试题H 完全平方数
思路:分解质因数; 因为n*x = m^2所以分解后因数必须是偶数,例如:12 = 2^2 + 3^1;3的因数是1(奇数)所以需要加是一个3使其变成偶数;
验证:因为从最小的数开始筛所以可以保证数值最小:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
int main()
{
LL n;
cin >> n;
LL res = 1;
for (LL i = 2; i * i <= n; i ++ )
if (n % i == 0)
{
int s = 0;
while (n % i == 0) s ++, n /= i;
if (s % 2) res *= i;
}
if (n > 1) res *= n;
cout << res << endl;
return 0;
}
试题I 负载均衡
思路:模拟;
1.维护n个计算机的工作区间(从a到a+ c), 由于需要从小到达,所以可以试用小根堆。
2.如果当前需要的计算机前面的任务执行完毕则需要加上完成任务消耗的算力
3.算力不够,输出-1,不进行任何操作
#include <iostream>
#include <queue>
using namespace std;
const int N = 200010;
typedef pair<int, int> PII;
#define x first
#define y second
int s[N]; //整个计算机的算力
priority_queue<PII, vector<PII>, greater<PII>> q[N]; //first 结束时间 second 算力
int main()
{
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i ++) cin >> s[i];
while(m --)
{
int a, b, c, d;
cin >> a >> b >> c >> d;
while(q[b].size() && q[b].top().x <= a)
{
s[b] += q[b].top().y;
q[b].pop();
}
if(s[b] < d) puts("-1");
else
{
q[b].push({a + c, d});
s[b] -= d;
cout << s[b] << endl;
}
}
return 0;
}
试题J 国际象棋
数位dp
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 110, M = 1 << 6, K = 21, MOD = 1e9 + 7;
int n, m, k;
int f[N][M][M][K];
int get_count(int x)
{
int res = 0;
while (x)
{
res ++ ;
x -= x & -x;
}
return res;
}
int main()
{
cin >> n >> m >> k;
f[0][0][0][0] = 1;
int cnt = 0;
for (int i = 1; i <= m; i ++ )
for (int a = 0; a < 1 << n; a ++ )
for (int b = 0; b < 1 << n; b ++ )
{
if (a & (b << 2) || b & (a << 2)) continue;
for (int c = 0; c < 1 << n; c ++ )
{
if (c & (b << 2) || b & (c << 2)) continue;
if (c & (a << 1) || a & (c << 1)) continue;
int t = get_count(c);
for (int u = t; u <= k; u ++, cnt ++ )
f[i][b][c][u] = (f[i][b][c][u] + f[i - 1][a][b][u - t]) % MOD;
}
}
int res = 0;
for (int i = 0; i < 1 << n; i ++ )
for (int j = 0; j < 1 << n; j ++ )
res = (res + f[m][i][j][k]) % MOD;
cout << res << endl;
return 0;
}