A. A+B Again?
题目大意
给定n求数位和。
思路
?
代码实现
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define Mod 998244353
#define PI acos(-1);
#define MAXN 210000
#define debug cout<<"debug:\t"
#define debugn cout<<"debug:\n"
#define enter cout<<"\n"
void fastread() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
};
void solve() {
ll n; cin >> n;
ll ans = 0;
while (n) {
ans += n % 10;
n /= 10;
}
cout << ans << "\n";
}
int main() {
//fastread();
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ll t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
B. Card Game
题目大意
Su,Sl两个人做游戏,问Su胜利的所有局内,胜利的回合数。
注意,比赛顺序不同视为不同答案。
思路
注意题意即可。
代码实现
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define Mod 998244353
#define PI acos(-1);
#define MAXN 210000
#define debug cout<<"debug:\t"
#define debugn cout<<"debug:\n"
#define enter cout<<"\n"
void fastread() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
};
void solve() {
ll a, b, c, d; cin >> a >> b >> c >> d;
ll ans = 0;
if (a > c && b > d)ans+=2;
if (a > d && b > c)ans+=2;
if (a > c && b == d)ans += 2;
if (a > d && b == c)ans += 2;
if (a == c && b > d)ans += 2;
if (a == d && b > c)ans += 2;
cout << ans << "\n";
}
int main() {
//fastread();
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ll t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
C. Showering
题目大意
给定一个洗澡时间和n个任务时间段,问是否有时间洗澡。
思路
按照题意模拟即可,注意开头一段和最后一段。
代码实现
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define Mod 998244353
#define PI acos(-1);
#define MAXN 210000
#define debug cout<<"debug:\t"
#define debugn cout<<"debug:\n"
#define enter cout<<"\n"
void fastread() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
};
void solve() {
ll n, s, m; cin >> n >> s >> m;
ll be = 0;
bool f = false;
for (int i = 0; i < n; i++) {
ll l, r; cin >> l >> r;
if (l - be >= s) {
f = true;
}
be = r;
if (i == n - 1) {
if (m - be >= s)f = true;
}
}
if (f)cout << "YES\n";
else cout << "NO\n";
}
int main() {
//fastread();
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ll t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
D. Slavic's Exam
题目大意
给定一个包含‘?’的字符串s,问是否有子序列字符串t。
思路
注意到是子序列,贪心即可,能匹配的尽量匹配。
代码实现
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define Mod 998244353
#define PI acos(-1);
#define MAXN 210000
#define debug cout<<"debug:\t"
#define debugn cout<<"debug:\n"
#define enter cout<<"\n"
void fastread() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
};
void solve() {
string s, t; cin >> s >> t;
ll l = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == t[l] || s[i] == '?') {
s[i] = t[l];
l++;
}
if (l == t.size())break;
}
if (l == t.size()) {
cout << "YES\n";
for (auto c : s) {
if (c == '?')cout << "a";
else cout << c;
}
cout << "\n";
}
else {
cout << "No\n";
}
}
int main() {
//fastread();
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ll t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
E. Triple Operations
题目大意
在黑板上写下l,r区间内所有数。给定操作,每次选择两个数,让一个数除三,一个数乘三问什么时候。问多少次操作后所有数均为0。
思路
每次先处理出来一个0,然后再处理其他数,处理出0的过程就是ans+2。
预处理处出来所有数被三除成0的次数,然后做个前缀和。每次访问访问前缀和即可。
代码实现
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define Mod 998244353
#define PI acos(-1);
#define MAXN 210000
#define debug cout<<"debug:\t"
#define debugn cout<<"debug:\n"
#define enter cout<<"\n"
void fastread() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
};
ll dp[210000];
void dfs() {
dp[0] = 1;
ll cnt = 1;
for (int i = 3; i <= 2e5; i *= 3) {
dp[i] = ++cnt;
}
cnt = 1;
for (int i = 1; i <= 2e5; i++) {
if (dp[i] == 0) {
dp[i] = cnt;
}
else {
cnt++;
}
dp[i]+=dp[i-1];
}
}
void solve() {
ll l, r; cin >> l >> r;
ll ans = 0;
ans+=dp[r]-dp[l];
ans += (dp[l]-dp[l-1]) * 2;
cout << ans << "\n";
}
int main() {
//fastread();
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ll t = 1;
cin >> t;
dfs();
while (t--) {
solve();
}
return 0;
}
F. Expected Median
题目大意
求序列n的所有长度奇数k的子序列的中位值和,序列n只包含01。
思路
找出0、1个数。在子序列中,1的个数大于一半的时候,答案有效。
枚举,然后乘法逆元求组合数即可。
代码实现
#include<iostream>
#include<algorithm>
#include<array>
#include<string>
#include<math.h>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<deque>
#include<queue>
#include<cstdio>
#include<iomanip>
#include<unordered_set>
#include<unordered_map>
#define endl '\n'
#define ll long long
#define ld long double
#define ull unsigned long long
#define N 1000000
#define yes "YES"
#define no "NO"
#define pairl pair<ll,ll>
#define debug cout << "debug:" << endl;
const double pi = acosl(-1);
using namespace std;
//快速幂取模
ll qpow(ll a, ll n, ll p)
{
ll ans = 1;
while (n)
{
if (n & 1) ans = ans * a % p;
a = a * a % p;
n >>= 1;
}
return ans;
}
//阶乘
vector<ll>jie(N);
void jc(ll n, ll mod) {
jie[0] = 1;
for (int i = 1; i <= n; i++) {
jie[i] = i * jie[i - 1];
jie[i] %= mod;
}
}
//阶乘逆元
vector<ll>njie(N);
void njc(ll n, ll mod) {
njie[n] = qpow(jie[n], mod - 2, mod);
for (int i = n; i >= 1; i--) {
njie[i - 1] = njie[i] * i % mod;
}
}
void solve() {
ll n, k;
cin >> n >> k;
ll mod = 1e9 + 7;
vector<ll>a(n);
ll b[2] = { 0 };
for (int i = 0; i < n; i++) {
cin >> a[i];
b[a[i]]++;
}
ll ans = 0;
for (int i = max(0ll, k - b[0]); i <= min(k, b[1]); i++) {
if (i <= k / 2)continue;
ll x = (jie[b[0]] * njie[k - i]) % mod * njie[b[0] - k + i] % mod;
ll y = (jie[b[1]] * njie[i]) % mod * njie[b[1] - i] % mod;
ans += x * y % mod;
ans %= mod;
}
cout << ans << endl;
}
int main() {
ll mod = 1e9 + 7;
jc(2e5 + 10, mod);
njc(2e5 + 10, mod);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
G1,2. Ruler (easy version)
题目大意
给个尺子,尺子再2-999缺了个数,要求在n次内询问到x的值。
G1中n为10,G2中n为7。
思路
G1二分求,G2三分求。
没啥好说的,这里只给出G2代码,也能过G1,注意代码细节即可。
代码实现
#include<iostream>
#include<algorithm>
#include<array>
#include<string>
#include<math.h>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<deque>
#include<queue>
#include<cstdio>
#include<iomanip>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define Mod 998244353
#define PI acos(-1);
#define MAXN 210000
#define debug cout<<"debug:\t"
#define debugn cout<<"debug:\n"
#define enter cout<<"\n"
void fastread() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
};
void solve() {
ll bel = 1;
ll ber = 999;
ll l = (ber - bel) / 3 + bel;
ll r = (ber - bel) / 3 * 2 + bel;
ll sum = 0;
while (bel < ber) {
cout << "? " << l << " " << r << endl;
cin >> sum;
if (sum == (l + 1) * (r + 1)) {
ber = l;
l = (ber - bel) / 3 + bel;
r = (ber - bel) / 3 * 2 + bel;
}
else if (sum == l * (r + 1)) {
bel = l;
ber = r;
l = (ber - bel) / 3 + bel;
r = (ber - bel) / 3 * 2 + bel;
}
else if (sum == l * r) {
bel = r + 1;
l = (ber - bel) / 3 + bel;
r = (ber - bel) / 3 * 2 + bel;
}
}
cout << "! " << bel << endl;
}
int main() {
//fastread();
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ll t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}