牛客周赛47题解
比赛链接:牛客周赛47
赛时感受
又是一场思维题,应该只有EF有点算法,E需要使用快速幂和取余,F做不出,C卡了我一下,D写完了,E不写完一半又回来看C才做掉的,E也卡了很久虽然鸽巢原理想到了,但是没想到被卡在取余问题上,一开始没想出来,去做F然后做了半个小时发现做不掉,又回来在E上做功夫。https://www.cnblogs.com/againss
A
思路
由于需要两个元素相等,其他三个元素相等,所以直接排序,只剩下有两种情况。
代码
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 10;
ll n[N];
int main() {
int a, b, c, d, e;
cin >> n[1] >> n[2] >> n[3] >> n[4] >> n[5];
sort(n + 1, n + 1 + 5);
if ((n[3] == n[5] && n[1] == n[2]) || (n[1] == n[3] && n[4] == n[5])) cout << "YES" << endl;
else cout << "NO" << endl;
return 0;
}
B
思路
由于任意公共子串都能打开密码锁,所以只需要枚举26个字母是否为所有的字符串的公共子串即可。
代码
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 10;
ll n;
int main() {
cin >> n;
string s[N];
for (int i = 1; i <= n; i++) {
cin >> s[i];
}
for (int i = 0; i < 26; i++) {
int flag = 0;
for (int j = 1; j <= n; j++) {
if (s[j].find('a' + i) == -1) {
flag = 1;
break;
}
}
if (flag)
continue;
cout << (char)('a' + i);
break;
}
return 0;
}
C
思路
代码
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 10;
ll n, a[N], sum, maxn, counts;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum += a[i];
if (a[i] > 1) counts++;
maxn = max(maxn, a[i]);
}
if (sum % 2 == 0) {
if (n == 2) {
if (a[1] == a[2]) {
cout << 0 << endl;
} else {
cout << 1 << endl;
}
} else if (sum <= maxn * 2) {
cout << 1 << endl;
} else if (maxn == 1) {
cout << 0 << endl;
} else {
cout << counts << endl;
}
} else {
if (sum <= maxn * 2) {
cout << 1 << endl;
} else {
cout << n << endl;
}
}
return 0;
}
D
思路
找规律发现18个好数为一组,则下一组b数组的数组值可以由当前组a数组的数组值推理得到,即为b[i] = a[i] + 30。
代码
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 10;
ll n, a[N] = { 1, 2, 4, 5, 7, 8, 10, 11, 14, 16, 17, 19, 20, 22, 25, 26, 28, 29 };
int main() {
int t;
cin >> t;
while (t--) {
ll n;
cin >> n;
cout << ((n - 1) / 18) * 30 + a[((n - 1) % 18)] << endl;
}
return 0;
}
E
思路
分类讨论横轴对称,纵轴对称和中心堆成,结果为横轴对称 + 纵轴堆成 - 中心对称,因为中心堆成的部分都包含于横轴对称和纵轴堆成,若不减去则会计算两次。 **纵轴对称:**n为奇数时,纵轴所在的元素只能选择0,8,纵轴左边的元素只能选择0,8,2,5,当左边选择0,8时右边对应选择0,8,左边选择2,5时,右边对应选择5,2,所以n为奇数时纵轴对称的情况种数为:((pow(4, n / 2) * 2)。n为偶数时,纵轴左边的元素和右边元素的选择情况和n为奇数时一致,只是不需要考虑纵轴所在的元素的选择情况,所以n为偶数时纵轴对称的情况种数为:(pow(4, n / 2) * 1),综合讨论得到纵轴对称的种数为:((pow(4, n / 2) * (n % 2 == 1 ? 2 : 1))。 **横轴对称:**n不用考虑奇偶性,计算器上能显示的数字为1,3,8,0,所以横轴对称的情况种数为:pow(4, n)。 **中心对称:**中心对称时,计算器上显示的数字只能是0,8,所以会被横轴对称和纵轴对称都计算了一遍。但是同时又需要是横轴对称又要是纵轴对称,所以中心对称的情况种数为:pow(2, ((n + 1) / 2))。
代码
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 10;
const ll mod = 1e9 + 7;
ll pow(ll x, ll y) {
ll res = 1;
while (y) {
if (y & 1) {
res *= x;
res %= mod;
}
y >>= 1;
x *= x;
x %= mod;
}
return res;
}
int main() {
ll n;
cin >> n;
if (n == 1) {
cout << 4 << endl;
return 0;
}
ll res = (((pow(4ll, n / 2) * (n % 2 == 1 ? 2ll : 1ll)) % mod + pow(4ll, n)) % mod - pow(2ll, ((n + 1) / 2)) + mod);
cout << (res % mod);
return 0;
}
F

查看5道真题和解析
