A
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define has1 __builtin_popcount
bool isSubstring(string s1, string s2)
{
return s1.find(s2) != string::npos;
}
void solve()
{
int n, m;
cin >> n >> m;
string s, x;
cin >> x >> s;
int ans = 0;
if (s == x)
{
cout << 0 << endl;
}
else
{
while (x.length() <= 1000)
{
for (int i = 0; i < x.length(); i++)
{
int j = 0;
while (x[i + j] == s[j])
{
j++;
if (j == m)
{
cout << ans << "\n";
return;
}
}
}
x = x + x;
ans++;
}
cout << -1 << endl;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
B
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define has1 __builtin_popcount
void solve()
{
ll a, b, c;
cin >> a >> b >> c;
if (a == b && b == c)
{
cout << "YES"
<< "\n";
return;
}
ll minn = min(min(a, b), c);
int num = 0;
if (a != minn)
{
if (minn * 4 == a)
{
num += 3;
}
else if (a == 3 * minn)
{
num += 2;
}
else if (a == 2 * minn)
{
num += 1;
}
else
{
cout << "NO" << endl;
return;
}
}
if (b != minn)
{
if (minn * 4 == b)
{
num += 3;
}
else if (b == 3 * minn)
{
num += 2;
}
else if (b == 2 * minn)
{
num += 1;
}
else
{
cout << "NO" << endl;
return;
}
}
if (c != minn)
{
if (minn * 4 == c)
{
num += 3;
}
else if (c == 3 * minn)
{
num += 2;
}
else if (c == 2 * minn)
{
num += 1;
}
else
{
cout << "NO" << endl;
return;
}
}
if (num > 3)
{
cout << "NO\n";
}
else
{
cout << "YES\n";
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
C
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define has1 __builtin_popcount
int min_operations(vector<vector<char>> &matrix)
{
int n = matrix.size();
int operations = 0;
for (int i = 0; i < n / 2; ++i)
{
for (int j = 0; j < n / 2; ++j)
{
vector<char> chars{matrix[i][j], matrix[j][n - i - 1], matrix[n - i - 1][n - j - 1], matrix[n - j - 1][i]};
char min_char = *min_element(chars.begin(), chars.end());
char max_char = *max_element(chars.begin(), chars.end());
for (auto &c : chars)
{
if (c != max_char)
operations += (max_char - c);
}
}
}
return operations;
}
const int N = 1e3 + 5;
char a[N][N];
void solve()
{
int n;
cin >> n;
vector<vector<char>> v(n, vector<char>(n));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> v[i][j];
}
}
cout << min_operations(v) << endl;
}
int main()
{
ios_base ::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--)
solve();
return 0;
}
D
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define has1 __builtin_popcount
bool isPrime(int n)
{
if (n < 2)
{
return false;
}
for (int i = 2; i * i <= n; i++)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
void solve()
{
int n;
cin >> n;
vector<int> a(n);
map<int, int> mp;
for (int i = 0; i < n; i++)
{
cin >> a[i];
if (isPrime(a[i]))
{
mp[a[i]]++;
continue;
}
for (int j = 2; a[i] != 1; j)
{
if (isPrime(a[i]))
{
mp[a[i]]++;
break;
}
if (a[i] % j == 0)
{
a[i] /= j;
mp[j]++;
}
else
{
j++;
}
}
}
for (auto [x, y] : mp)
{
if (y % n != 0)
{
cout << "NO" << endl;
return;
}
}
cout << "YES" << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
E
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define has1 __builtin_popcount
const int INF = 1e9;
void solve()
{
int n;
cin >> n;
vector<int> a(n);
vector<int> dp(n + 1, n);
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
dp[n] = 0;
for (int i = n - 1; i >= 0; i--)
{
dp[i] = dp[i + 1] + 1;
if (i < n - a[i])
{
dp[i] = min(dp[i], dp[i + a[i] + 1]);
}
}
cout << dp[0] << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}