B. Mask Allocation
题意:
共n * m个口罩,装最少的箱,使得这些口罩即可以平均分给 n 个医院,也可以平均分给 m 个医院
思路:
递归。n = min(n, m),分出 n 个 n 后,还剩下n * m - n * n = n * (m - n),再把剩下的分成 n 个(m - n);同样地分(m - n)个 (m - n),剩下(m - n) * (2 * n - m),再分成(2 * n - m)个(2 * n - m) .........
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 205;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
int main() {
int t, n, m;
scanf("%d", &t);
vector<int>vec;
while(t--) {
vec.clear();
scanf("%d%d", &n, &m);
while(n != m) {
if(n > m) swap(n, m);
for(int i = 1; i <= n; ++i)
vec.push_back(n);
m -= n;
if(n == 0)
break;
}
for(int i = 1; i <= n; ++i) vec.push_back(n);
int siz = vec.size();
cout<<siz<<'\n';
for(int i = 0; i < siz; ++i) {
if(i > 0) cout<<' ';
cout<<vec[i];
}
cout<<'\n';
}
return 0;
}
D. Fake News
题意:
给定一个数 n ,判断1 ^ 2 + 2 ^ 2 + ..... + n ^ 2是否是平方数
证明:https://www.zhihu.com/question/363661682
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 205;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
int main() {
int t, n;
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
if(n == 1 || n == 24)
printf("Fake news!\n");
else
printf("Nobody knows it better than me!\n");
}
return 0;
}
J. Pointer Analysis
题意:
共26个对象,每个对象有26个指针,给定 n 条赋值 语句, 询问在以任意顺序执行每条语句无限多次的过程中, 每个指针变量可能指向的对象集合
模拟即可,建立26个set存储每个对象可能的值。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 205;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
string s1[N], s2[N];
set<char> ans[30], to[30][30];
int main() {
int n;
scanf("%d", &n);
char a, b, c;
for(int i = 1; i <= n; ++i)
cin >> s1[i] >> c >> s2[i];
for(int j = 1; j <= n; ++j) {
for(int i = 1; i <= n; ++i) {
a = s1[i][0] - 'A';
b = s2[i][0] - 'A';
if(s1[i].size() == 1 && s2[i].size() == 1) { // A = B
if(islower(s2[i][0]))
ans[a].insert(s2[i][0]);
else
for(auto k : ans[b])
ans[a].insert(k);
}
else if(s1[i].size() == 3) { // A.f = B
c = s1[i][2] - 'a';
for(auto p : ans[a])
for(auto q : ans[b])
to[p - 'a'][c].insert(q);
}
else {
c = s2[i][2] - 'a';
for(auto p : ans[b])
for(auto q : to[p - 'a'][c])
ans[a].insert(q);
}
}
}
for(int i = 0; i < 26; ++i) {
cout<<char(i + 'A')<<": ";
for(auto k : ans[i])
cout<<k;
cout<<'\n';
}
return 0;
}