3-1 饿饿 饭饭2
转换问题,每个数去掉所有
2
2
2 与
3
3
3 所有因子,比较每个数剩下的是否相等
(Ai 能到 1e9,感觉这样写应该能被卡掉才对)
#include <bits/stdc++.h>
#define For(i, a, b) for (int i = a; i <= b; i++)
#define _For(i, a, b) for (int i = a; i >= b; i--)
typedef long long ll;
using namespace std;
const int maxn = 2e5 + 5;
int T, n, k;
int a[maxn];
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> T;
while (T--) {
cin >> n;
int i, k;
bool flag = 1;
for (i = 1; i <= n; i++) {
cin >> a[i];
if (!flag) continue;
while (1) {
if (a[i] % 2 == 0) a[i] /= 2;
else if (a[i] % 3 == 0) a[i] /= 3;
else break;
}
if (i == 1) { k = a[i]; continue;}
if (a[i] != k) {
flag = 0;
continue;
}
}
if (flag) puts("YES");
else puts("NO");
}
return 0;
}
3-2 子串分值和
问题转化成 求一个字符串的所有子串中,某个字母在多少个子串中出现过
再转化成 某个字母没有在多少个子串中出现过
如果定义s的长度为int 会炸
所以多用long long
#include <bits/stdc++.h>
#define For(i, a, b) for (int i = a; i <= b; i++)
#define _For(i, a, b) for (int i = a; i >= b; i--)
typedef long long ll;
using namespace std;
const int maxn = 1e6 + 5;
string s;
ll ls;
vector <int> a[30];
signed main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> s;
ls = s.size();
For (i, 0, ls - 1) {
a[s[i] - 'a'].push_back(i);
}
ll ans = 0;
For (c, 0, 25) {
ll l = -1, len = a[c].size(), cnt = 0;
if (!len) {
continue;
}
For (i, 0, len - 1) {
ll r = a[c][i], leng = r - l - 1;
cnt += leng * (leng + 1) / 2;
l = r;
}
if (len >= 1 && s[ls - 1] != char(c + 'a')) {
ll leng = ls - 1 - l;
cnt += leng * (leng + 1) / 2;
}
ans += ls * (ls + 1) / 2 - cnt;
}
cout << ans;
return 0;
}
3-3 蒟蒻
STL
构造两个map
分别以
w
w
w 与
t
t
t 作为
k
e
y
key
key ,便于去重和查找
#include <bits/stdc++.h>
#define For(i, a, b) for (int i = a; i <= b; i++)
#define _For(i, a, b) for (int i = a; i >= b; i--)
typedef long long ll;
using namespace std;
const int maxn = 2e6 + 5;
map <int, int> mp1, mp2;
int n, op, w, t;
int main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n;
For (i, 1, n) {
cin >> op;
if (op == 1) {
cin >> w >> t;
if (mp1.find(w) != mp1.end()) continue;
if (mp2.find(t) != mp2.end()) continue;
mp1[w] = t;
mp2[t] = w;
}
else if(op == 2) {
w = mp1.begin()->first;
t = mp1.begin()->second;
auto k1 = mp1.find(w), k2 = mp2.find(t);
mp1.erase(k1), mp2.erase(k2);
}
else {
w = mp2.begin()->second;
t = mp2.begin()->first;
auto k1 = mp1.find(w), k2 = mp2.find(t);
mp1.erase(k1), mp2.erase(k2);
}
}
ll ans = 0;
for (auto i = mp1.begin(); i != mp1.end(); i++) {
ans += i->first;
}
cout << ans;
return 0;
}
3-4 循环子串
/*
分析:当整个$reverse$是循环字串时,其每个子串必然是循环字串
注意 `reverse` `substr` 的用法
*/
#include <bits/stdc++.h>
#define For(i, a, b) for (int i = a; i <= b; i++)
#define _For(i, a, b) for (int i = a; i >= b; i--)
typedef long long ll;
using namespace std;
const int maxn = 1e3 + 5;
int t, n;
string s;
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
while (t--) {
cin >> n >> s;
string cp = s + s;
reverse(s.begin(), s.end());
int i;
for (i = 0; i <= n; i++) {
string tmp = cp.substr(i, n);
if (tmp == s) { puts("YES"); break; }
}
if (i > n) { puts("NO"); }
}
return 0;
}
3-5 RSA
数论题 处理时很容易超时
去寻找
a
∗
b
a*b
a∗b 有无平方因子,可以分为两种情况讨论:
- a a a 或者 b b b 本身有平方因子
-
a
a
a 与
b
b
b 含有相同因子、凑出平方因子,即判断
a
b
a b
ab 有无大于 1 的公约数
保证操作复杂度在 O(sqrt(n, m))
#include <bits/stdc++.h>
#define For(i, a, b) for (int i = a; i <= b; i++)
#define _For(i, a, b) for (int i = a; i >= b; i--)
typedef long long ll;
using namespace std;
ll a, b;
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> a >> b;
int c1 = 0, c2 = 0;
For (i, 2, sqrt(a)) {
if (a % i == 0) c1++;
}
For (i, 2, sqrt(b)) {
if (b % i == 0) c2++;
}
if (!c1 && !c2 && a != b) puts("full credit"), exit(0);
for (ll i = 2; i <= sqrt(a); i++) {
if (a % (i * i) == 0) puts("no credit"), exit(0);
}
for (ll i = 2; i <= sqrt(b); i++) {
if (b % (i * i) == 0) puts("no credit"), exit(0);
}
if (__gcd(a, b) > 1) puts("no credit"), exit(0);
puts("partial credit");
return 0;
}
3-6 数组操作
题面较晦涩
#include <bits/stdc++.h>
#define For(i, a, b) for (int i = a; i <= b; i++)
#define _For(i, a, b) for (int i = a; i >= b; i--)
typedef long long ll;
using namespace std;
const int maxn = 2e5 + 5;
ll T, n;
ll a[maxn];
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> T;
while (T--) {
cin >> n;
_For (i, n, 1) {
cin >> a[i];
}
ll ans = 0, now = 1;
while (now < n) {
if (a[now + 1] == a[1]) now++;
else {
now *= 2;
ans++;
}
}
cout << ans << '\n';
}
return 0;
}
3-Rest 其余水题
/*
纯模拟
*/
#include <bits/stdc++.h>
#define For(i, a, b) for (int i = a; i <= b; i++)
#define _For(i, a, b) for (int i = a; i >= b; i--)
typedef long long ll;
using namespace std;
const int maxn = 1e5 + 5;
int n, k;
int a[maxn];
int main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> k;
int ans = 0;
For (i, 1, n) cin >> a[i];
sort(a + 1, a + 1 + n);
ans++;
_For (i, n - 1, 1) {
if (a[i + 1] - a[i] > k) break;
else ans++;
}
cout << ans;
return 0;
}
/*
简单DFS
*/
#include <bits/stdc++.h>
#define For(i, a, b) for (int i = a; i <= b; i++)
#define _For(i, a, b) for (int i = a; i >= b; i--)
typedef long long ll;
using namespace std;
int cnt[105], a[105];
int n, s;
int p[105];
void dfs(int k) {
if (k == s) {
For (i, 1, k) cout << p[i] << " ";
cout << '\n';
return;
}
For (i, 1, n) {
if (cnt[i] == a[i]) continue;
cnt[i]++;
p[k + 1] = i;
dfs(k + 1);
cnt[i]--;
}
}
int main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n;
For (i, 1, n) cin >> a[i], s += a[i];
For (i, 1, n) {
cnt[i]++;
p[1] = i;
dfs(1);
cnt[i]--;
}
return 0;
}
/*
纯模拟
注意开 `long long`
`int` 转 `long long` 是好文明!
*/
#include <bits/stdc++.h>
#define For(i, a, b) for (int i = a; i <= b; i++)
#define _For(i, a, b) for (int i = a; i >= b; i--)
typedef long long ll;
using namespace std;
ll n, m;
ll k, t;
string s, pr;
ll sum(char c) {
if (c >= '0' && c <= '9') return c - '0';
else if (c >= 'A' && c <= 'Z') return c - 55;
else return c - 61;
}
char str(int num) {
if (num >= 0 && num <= 9) return char(num + '0');
else if (num >= 10 && num <= 35) return char(num + 55);
else return char(num + 61);
}
int main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m;
For (i, 1, n) {
cin >> t >> s;
ll tmp = 0, base = 1, len = s.size();
tmp += sum(s[len - 1]);
_For (j, len - 2, 0) {
base *= t;
tmp += base * sum(s[j]);
}
k += tmp;
}
while (k > 0) {
pr += str(k % m);
k /= m;
}
ll len = pr.size();
_For (i, len - 1, 0) cout << pr[i];
return 0;
}
/*
转换成判断是否存在一个数的子集
因为数据范围较小 可以直接暴力
*/
#include <bits/stdc++.h>
#define For(i, a, b) for (int i = a; i <= b; i++)
#define _For(i, a, b) for (int i = a; i >= b; i--)
typedef long long ll;
using namespace std;
int t, n, k;
vector <int> a[105];
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
For (i, 1, t) {
cin >> n;
For (j, 1, n) {
cin >> k;
a[i].push_back(k);
}
}
For (i, 1, t) {
int j;
for (j = 1; j <= t; j++) {
if (i == j) continue;
int len = a[j].size();
int flag = 0;
int l;
for (l = 0; l <= len - 1; l++) {
if (find(a[i].begin(), a[i].end(), a[j][l]) == a[i].end()) {
flag = 1;
break;
}
}
if (l == len) flag = 2;
if (flag == 0 || flag == 1) continue;
else break;
}
if (j == t + 1) puts("YES");
else puts("NO");
}
return 0;
}