A Polycarp and the Day of Pi
解题思路
枚举配对即可
参考代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define cy cout << "YES" << endl;
#define cn cout << "NO" << endl;
const int N = 2e5 + 5;
ll T;
string pi = "31415926535897932384626433832795028841971693993751058209749445923078164062862089986280";
void solve() {
string s;
cin >> s;
int cnt = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == pi[i]) cnt++;
else break;
}
cout << cnt << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
T = 1;
cin >> T;
while (T--) solve();
return 0;
}
B Taisia and Dice
解题思路
贪心:s-r就是最大的点数,安置在最后一个,前面的骰子只要一点点累加到r即可
参考代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define cy cout << "YES" << endl;
#define cn cout << "NO" << endl;
const int N = 2e5 + 5;
ll T;
ll n, s, r;
void solve() {
cin >> n >> s >> r;
ll ans[n + 1] = {};
ans[n] = s - r;
for (int k = 1; k <= 6; k++) {
for (int i = 1; i < n; i++) {
ans[i]++;
r--;
if (r == 0) {
for (int t = 1; t <= n; t++) cout << ans[t] << ' ';
cout << endl;
return;
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
T = 1;
cin >> T;
while (T--) solve();
return 0;
}
C Premutation
解题思路
贪心:首先可以确定第一个和最后一个数,接着找不存在最后一个的数的数列,那么直接确定中间的数
参考代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define cy cout << "YES" << endl;
#define cn cout << "NO" << endl;
const int N = 1e3 + 5;
ll T;
ll n;
int a[N][N];
int ans[N];
unordered_map<int, int>mp1, mpn;
void solve() {
mp1.clear(), mpn.clear();
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j < n; j++) {
cin >> a[i][j];
}
mp1[a[i][1]]++, mpn[a[i][n - 1]]++;
}
for (auto i : mp1) if (i.second != 1) ans[1] = i.first;
for (auto i : mpn) if (i.second != 1) ans[n] = i.first;
for (int i = 1; i <= n; i++) {
if (a[i][n - 1] != ans[n]) {
for (int j = 2; j < n; j++) ans[j] = a[i][j];
break;
}
}
for (int i = 1; i <= n; i++) cout << ans[i] << ' ';
cout << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
T = 1;
cin >> T;
while (T--) solve();
return 0;
}
D Matryoshkas
解题思路
使用哈希表,统计数字出现的次数,每一轮从前往后将连续的自然数计数-1,答案就是轮数
参考代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define cy cout << "YES" << endl;
#define cn cout << "NO" << endl;
const int N = 2e5 + 5;
ll T;
ll n;
int a[N];
map<int, int>mp;
bool cmp(int a, int b) {
return a > b;
}
void solve() {
int ans = 0;
mp.clear();
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i], mp[a[i]]++;
for (auto &i : mp) {
int f = i.first, s = i.second;
ans += s;
while (i.second) {
f = i.first;
while (mp[f]) {
mp[f]--;
f++;
}
}
}
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
T = 1;
cin >> T;
while (T--) solve();
return 0;
}
E Vlad and a Pair of Numbers
解题思路
暴力模拟发现,x为奇数一定为-1,如果有解的话,一定存在一组解, 所以如果x为偶数,就假设答案为a,b再验证即可
参考代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define cy cout << "YES" << endl;
#define cn cout << "NO" << endl;
//快速幂
ll q_pow(ll a, ll b) {
ll ans = 1;
while (b) {
if (b & 1) ans *= a;
a *= a;
b >>= 1;
}
return ans;
}
//欧拉筛
const int N = 2e5 + 5;
ll T;
ll x;
ll a, b;
void solve() {
cin >> x;
if (x % 2 == 1) {
cout << -1 << endl;
return;
}
int ok = 0;
a = x / 2, b = x + x / 2;
if ((a ^ b) == x) ok = 1;
if (ok) cout << a << ' ' << b << endl;
else cout << -1 << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
T = 1;
cin >> T;
while (T--) solve();
return 0;
}