Idea: fcspartakm
Tutorial
Tutorial is loading...
Solution
for i in range(int(input())):
n, x = map(int, input().split())
print(1 if n <= 2 else (n - 3) // x + 2)
Idea: BledDest
Tutorial
Tutorial is loading...
Solution
for i in range(int(input())):
n, m = map(int, input().split())
a = []
for i in range(n):
a.append([[int(x) for x in input().split()] for i in range(2)])
ok = False
for i in range(n):
ok |= a[i][0][1] == a[i][1][0]
ok &= m % 2 == 0
print("YES" if ok else "NO")
Idea: vovuh
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int ans = 1e9;
for (int x = 1; x * x <= n; ++x) {
ans = min(ans, x - 1 + ((n - x) + x - 1) / x);
}
cout << ans << endl;
}
return 0;
}
Solution 2
#include<bits/stdc++.h>
using namespace std;
const long double EPS = 1e-9;
long long f(long long x)
{
long long z = sqrtl(x);
long long ans = 1e18;
for(int i = -5; i <= 5; i++)
{
long long z2 = z - i;
if(z2 > x || z2 < 1)
continue;
ans = min(ans, z2 - 2 + (x + z2 - 1) / z2);
}
return ans;
}
int main()
{
int t;
cin >> t;
for(int i = 0; i < t; i++)
{
long long x;
cin >> x;
cout << f(x) << endl;
}
}
Idea: BledDest
Tutorial
Tutorial is loading...
Solution
n = int(input())
a = [int(x) for x in input().split()]
d = set()
d.add(0)
cur = 0
ans = 0
for i in range(n):
cur += a[i]
if cur in d:
ans += 1
d = set()
d.add(0)
cur = a[i]
d.add(cur)
print(ans)
Idea: fcspartakm
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
#define sz(v) int(v.size())
#define all(v) v.begin(), v.end()
#define pb push_back
#define ft first
#define sc second
using namespace std;
int n;
vector<int> a, b;
inline void read() {
cin >> n;
a.resize(3);
b.resize(3);
for (int i = 0; i < 3; i++) cin >> a[i];
for (int i = 0; i < 3; i++) cin >> b[i];
}
inline void solve() {
int ans1 = INT_MAX;
vector<pair<int, int> > ord;
ord.pb({0, 0});
ord.pb({0, 2});
ord.pb({1, 1});
ord.pb({1, 0});
ord.pb({2, 2});
ord.pb({2, 1});
sort(all(ord));
do {
vector<int> a1 = a, b1 = b;
for (int i = 0; i < sz(ord); i++) {
int cnt = min(a1[ord[i].ft], b1[ord[i].sc]);
a1[ord[i].ft] -= cnt;
b1[ord[i].sc] -= cnt;
}
int cur = min(a1[0], b1[1]) + min(a1[1], b1[2]) + min(a1[2], b1[0]);
ans1 = min(ans1, cur);
} while(next_permutation(all(ord)));
int ans2 = min(a[0], b[1]) + min(a[1], b[2]) + min(a[2], b[0]);
cout << ans1 << ' ' << ans2 << endl;
}
int main () {
read();
solve();
}
1426F - Number of Subsequences
Idea: fcspartakm
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
const int MOD = int(1e9) + 7;
const int N = 200043;
const int K = 4;
int add(int x, int y)
{
x += y;
while(x >= MOD) x -= MOD;
while(x < 0) x += MOD;
return x;
}
int mul(int x, int y)
{
return (x * 1ll * y) % MOD;
}
int n;
string s;
int dp[N][K][K];
char buf[N];
int pow3[N];
int main() {
scanf("%d", &n);
scanf("%s", buf);
s = buf;
int cntQ = 0;
for(auto c : s)
if(c == '?')
cntQ++;
pow3[0] = 1;
for(int i = 1; i < N; i++)
pow3[i] = mul(pow3[i - 1], 3);
dp[0][0][0] = 1;
for(int i = 0; i < n; i++)
for(int j = 0; j <= 3; j++)
for(int k = 0; k <= 3; k++)
{
if(!dp[i][j][k]) continue;
dp[i + 1][j][k] = add(dp[i + 1][j][k], dp[i][j][k]);
if(j < 3 && (s[i] == '?' || s[i] - 'a' == j))
{
int nk = (s[i] == '?' ? k + 1 : k);
dp[i + 1][j + 1][nk] = add(dp[i + 1][j + 1][nk], dp[i][j][k]);
}
}
int ans = 0;
for(int i = 0; i <= 3; i++)
if(cntQ >= i)
ans = add(ans, mul(dp[n][3][i], pow3[cntQ - i]));
printf("%d\n", ans);
}