1202A - You Are Given Two Binary Strings...
Идея: adedalic
Разбор
Tutorial is loading...
Решение (adedalic)
fun main(args: Array<String>) {
val T = readLine()!!.toInt()
for (tc in 1..T) {
val x = readLine()!!.reversed()
val y = readLine()!!.reversed()
val posY = y.indexOf('1')
val posX = x.indexOf('1', posY)
println(posX - posY)
}
}
1202B - You Are Given a Decimal String...
Идея: adedalic
Разбор
Tutorial is loading...
Решение (adedalic)
const val INF = 1e9.toInt()
fun main(args: Array<String>) {
val s = readLine()!!.map { it - '0' }
val cf = Array(10) {Array(10) {0}}
for (i in 1 until s.size)
cf[s[i - 1]][s[i]]++
for (x in 0..9) {
for (y in 0..9) {
val ds = Array(10) {Array(10) {INF + 7}}
for (v in 0..9) {
for (cx in 0..9) {
for (cy in 0..9) {
if (cx + cy == 0)
continue
val to = (v + cx * x + cy * y) % 10
ds[v][to] = minOf(ds[v][to], cx + cy)
}
}
}
var res = 0
for (v in 0..9) {
for (to in 0..9) {
if (ds[v][to] > INF && cf[v][to] > 0) {
res = -1
break
}
res += (ds[v][to] - 1) * cf[v][to]
}
if (res == -1)
break
}
print("$res ")
}
println()
}
}
1202C - You Are Given a WASD-string...
Идея: adedalic
Разбор
Tutorial is loading...
Решение (adedalic)
const val INF = 1e9.toInt()
fun main(args: Array<String>) {
val T = readLine()!!.toInt()
for (tc in 1..T) {
val s = readLine()!!
val alp = arrayOf("WS", "AD")
val aDir = arrayOf(
s.filter { alp[0].indexOf(it) != -1 },
s.filter { alp[1].indexOf(it) != -1 }
)
val baseW = arrayOf(INF, INF)
val bestW = arrayOf(INF, INF)
for (k in 0..1) {
val pSum = arrayListOf(0)
for (c in aDir[k]) {
val add = if (c == alp[k][0]) +1 else -1
pSum.add(pSum.last() + add)
}
for (tp in 0..1) {
val firstMin = pSum.withIndex().minBy { it.value }!!.index
val lastMax = pSum.withIndex().reversed().maxBy { it.value }!!.index
val curBase = pSum[lastMax] - pSum[firstMin] + 1
var curBest = curBase
if (curBase > 2 && lastMax < firstMin)
--curBest
baseW[k] = minOf(baseW[k], curBase)
bestW[k] = minOf(bestW[k], curBest)
for (i in pSum.indices)
pSum[i] = -pSum[i]
}
}
println("${minOf(baseW[0] * 1L * bestW[1], baseW[1] * 1L * bestW[0] )}")
}
}
1202D - Print a 1337-string...
Идея: Roms
Разбор
Tutorial is loading...
Решение (Roms)
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
for(int tc = 0; tc < t; ++tc){
int n;
cin >> n;
int len = 2;
while(len * (len + 1) / 2 <= n)
++len;
n -= len * (len - 1) / 2;
assert(n >= 0);
assert(n <= 45000);
string s = "133";
while(n > 0){
--n;
s += "7";
}
len -= 2;
while(len > 0){
--len;
s += "3";
}
s += "7";
cout << s << endl;
}
return 0;
}
1202E - You Are Given Some Strings...
Идея: Roms
Разбор
Tutorial is loading...
Решение (adedalic)
#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(int i = int(l); i < int(r); i++)
#define forn(i, n) fore(i, 0, n)
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define x first
#define y second
typedef long long li;
typedef pair<int, int> pt;
const int INF = int(1e9) + 7;
const li INF64 = li(1e18) + 7;
const int N = 500005;
const int A = 27;
const int MAG = 500;
struct node {
node *to[A];
int cnt;
} nodes[N];
int szn = 0;
typedef node* vt;
vt getNode() {
assert(szn < N);
fore(i, 0, A)
nodes[szn].to[i] = NULL;
nodes[szn].cnt = 0;
return &nodes[szn++];
}
void addWord(vt v, const string &s) {
fore(i, 0, sz(s)) {
int c = s[i] - 'a';
if (!v->to[c])
v->to[c] = getNode();
v = v->to[c];
}
v->cnt++;
}
int calcCnt(vt v, const string &s, int pos) {
assert(v->cnt == 0);
int ans = 0;
while(pos < sz(s)) {
int c = s[pos] - 'a';
if (!v->to[c])
break;
v = v->to[c];
ans += v->cnt;
pos++;
}
return ans;
}
vector<int> zf(string s) {
vector<int> z(sz(s), 0);
for (int i = 1, l = 0, r = 0; i < sz(s); ++i) {
if(i < r)
z[i] = min(r - i, z[i - l]);
while (i + z[i] < sz(s) && s[i + z[i]] == s[z[i]])
z[i]++;
if(i + z[i] > r)
l = i, r = i + z[i];
}
return z;
}
string t;
int n;
vector<string> s;
inline bool read() {
if(!(cin >> t))
return false;
cin >> n;
s.resize(n);
fore(i, 0, n)
cin >> s[i];
return true;
}
inline void solve() {
vector<int> cnt[2];
fore(k, 0, 2) {
cnt[k].assign(sz(t) + 1, 0);
szn = 0;
vt root = getNode();
forn(i, n) {
if (sz(s[i]) > MAG) {
auto z = zf(s[i] + t);
fore(j, 0, sz(t))
cnt[k][j] += (z[sz(s[i]) + j] >= sz(s[i]));
} else {
addWord(root, s[i]);
}
}
fore(i, 0, sz(t))
cnt[k][i] += calcCnt(root, t, i);
reverse(all(t));
fore(i, 0, n)
reverse(all(s[i]));
}
li ans = 0;
fore(i, 0, sz(t) + 1)
ans += cnt[0][i] * 1ll * cnt[1][sz(t) - i];
cout << ans << endl;
}
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
int tt = clock();
#endif
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cerr << fixed << setprecision(15);
if(read()) {
solve();
#ifdef _DEBUG
cerr << "TIME = " << clock() - tt << endl;
tt = clock();
#endif
}
return 0;
}
1202F - You Are Given Some Letters...
Идея: adedalic
Разбор
Tutorial is loading...
Решение (PikMike)
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
int main() {
int a, b;
scanf("%d%d", &a, &b);
int n = a + b;
int ans = 0;
int l = 1;
while (l <= n){
int g = n / l;
if (a < g || b < g){
l = n / g + 1;
continue;
}
int r = n / g;
int a_low = (a + g) / (g + 1);
int a_high = a / g;
int b_low = (b + g) / (g + 1);
int b_high = b / g;
if (a_low <= a_high && b_low <= b_high)
ans += max(0, min(r, a_high + b_high) - max(l, a_low + b_low) + 1);
l = r + 1;
}
printf("%d\n", ans);
}