\(a\) va \(b\) nuqtalar orasidagi nuqtalar soni: \(|a - b| + 1\)
Tomonlari a va b bo'lgan to'g'ri to'rtburchakning diagonali uzunli \(\sqrt{a^2 + b ^ 2}\) teng. Agar gugurt donasi uzunligi \(\sqrt{a^2 + b ^ 2}\) dan kichik yok teng bo'lsa javob BOX aks holda TRASH.
Pikselni enini \(b\) martaga oshiradigan bo'lsa bu piksel \(b\) marta ketma ket yoziladi. Agar qatorni bo'yini \(a\) martaga oshiradigan bo'lsak qator \(a\) marta chiqadi. Yaxshiroq tushunish uchun rasmga qarang.
Kiritilgan telefon nomeridagi har bir raqam yonma yun tursa javob YES bo'ladi, aks holda NO.
Bu masala dynamic-programming bilan yordamida osonlikcha hal qilish mumkin. Keling \(dp[n][k]\) deb uzunligi \(n\) ga teng bo'lgan permutatsiyalar ichida inversiyalar soni \(k\) bo'lgan massivlar soni (boshqacharoq qilib aytganda (n, k) kiritilgandagi javob). 3 hil holat bor:
\(dp[0][k] = 0\), massiv bo'sh
\(dp[n][0] = 1\), inversiyasi soni 0 ta bo'lgan yagona massiv bor.
\(dp[n][k] = dp[n - 1][k - n + 1] + dp[n - 1][k - n + 2] + ... + dp[n - 1][k]\), shunday bo'lishini isbotlaymiz. Misol uchun \(dp[n - 1][k - n + 2]\) bo'lgan holat javobga qo'shilayabdi, lekin nega? Chunki uzunligi \((n - 1)\)bo'lgan va inversiyalar soni \((k - n + 2)\) bo'lgan permutatsiyalarni 2-o'rniga n sonini qo'shsak undagi inversiyalar soni \(k\) ta bo'lib qoladi va uzunligi ham \(n\) bo'ladi.
C++ dasturlash tilida masalalarning yechimlari:
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << b - a + 1;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a, b;
cin >> n >> a >> b;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
cout << (x <= sqrt(a * a + b * b) ? "BOX\n" : "TRASH\n");
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n, m, a, b;
cin >> n >> m >> a >> b;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
for (int j = 0; j < a; j++) {
for (int k = 0; k < m; k++) {
for (int l = 0; l < b; l++) cout << s[k];
}
cout << '\n';
}
}
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
int main()
{
string s;
cin >> s;
vector<pair<int, int>> order = {{4, 2}, {1, 1}, {1, 2}, {1, 3}, {2, 1}, {2, 2}, {2, 3}, {3, 1}, {3, 2}, {3, 3}};
for (int i = 1; i < s.size(); i++)
{
if (abs(order[s[i - 1] - '0'].first - order[s[i] - '0'].first) +
abs(order[s[i - 1] - '0'].second - order[s[i] - '0'].second) !=
1)
{
cout << "NO";
return 0;
}
}
cout << "YES";
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
const int mod = 1e9 + 7;
int main() {
int n, k;
cin >> n >> k;
vector<vector<int>> dp(n + 1, vector<int>(k + 1));
for (int i = 0; i <= n; i++) {
ll sum = 0;
for (int j = 0; j <= k; j++) {
if (i == 0) continue;
sum += dp[i - 1][j];
if (j == 0) {
dp[i][0] = 1;
continue;
}
// dp[i][j - i] qo'shilib qolgan bo'lsa chiqarvorish kerak
if (j - i >= 0) sum -= dp[i - 1][j - i];
dp[i][j] = sum % mod;
}
}
cout << dp[n][k];
return 0;
}
Python dasturlash tilidagi yechimlar:
a = int(input())
b = int(input())
print(b - a + 1)
n, a, b = map(int, input().split())
for i in range(n):
x = int(input())
print("BOX" if x ** 2 <= (a ** 2 + b ** 2) else "TRASH")
n, m, a, b = map(int, input().split())
for i in range(n):
s = input()
for j in range(a):
for k in range(m):
for l in range(b):
print(s[k], end='')
print()
s = input()
order = [[4, 2], [1, 1], [1, 2], [1, 3], [2, 1],
[2, 2], [2, 3], [3, 1], [3, 2], [3, 3]]
for i in range(1, len(s)):
if abs(order[int(s[i - 1])][0] - order[int(s[i])][0]) + abs(order[int(s[i - 1])][1] - order[int(s[i])][1]) != 1:
print("NO")
exit()
print("YES")
TL = False
ML = False
if TL or ML:
print("Learn cpp")
else:
n, k = map(int, input().split())
dp = [[0 for _ in range(k + 2)] for _ in range(n + 2)]
mod = 10 ** 9 + 7
for i in range(n + 1):
sum = 0
for j in range(k + 1):
if i == 0:
continue
sum += dp[i - 1][j]
if j == 0:
dp[i][0] = 1
continue
if j - i >= 0:
sum -= dp[i - 1][j - i]
dp[i][j] = sum % mod
print(dp[n][k])