Hướng dẫn giải + code mẫu

Nhớ quê ra đứng đỉnh đèo, bỗng đâu thấy một chú mèo .. gâu gâu!
Dừng chân đứng lại trên cầu, bỗng đâu thấy một con trâu vàng vàng..
int root(int n) { | |
if (n >= 10) { | |
int s = 0; | |
while (n > 0) { | |
s += n % 10; | |
n /= 10; | |
} | |
return root(s); | |
} | |
return n; | |
} |
/** | |
* MDL - https://codechef.com/problems/MDL | |
* Code by @trhgquan - https://github.com/trhgquan | |
*/ | |
#include<algorithm> | |
#include<iostream> | |
#include<vector> | |
using namespace std; | |
int main() { | |
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | |
int T, N; cin >> T; | |
while (T--) { | |
cin >> N; | |
vector<int> a, b; | |
for (int i = 1; i <= N; ++i) {int u; cin >> u; a.push_back(u); b.push_back(u);} | |
sort(a.begin(), a.end()); | |
int f = 0, l = 0; | |
for (int i = 0; i <= N; ++i) | |
if (b[i] == a[0]) {f = a[0]; l = a[a.size() - 1]; break;} | |
else if (b[i] == a[a.size() - 1]) {f = a[a.size() - 1]; l = a[0]; break;} | |
cout << f << ' ' << l << endl; | |
} | |
return 0; | |
} |
/** | |
* HYPNOS - Happy Numbers I | |
* https://www.spoj.com/problems/HPYNOS/ | |
* Code by @trhgquan - https://github.com/trhgquan | |
*/ | |
#include<iostream> | |
#include<algorithm> | |
#include<vector> | |
using namespace std; | |
int main() { | |
ios_base::sync_with_stdio(false); cin.tie(NULL); | |
vector<int> e; | |
int n; cin >> n; | |
int s = 0, c = 0; | |
while (true) { | |
while (n > 0) { | |
s += (n % 10) * (n % 10); | |
n /= 10; | |
} | |
c++; | |
if (s == 1) { | |
cout << c << endl; | |
break; | |
} else { | |
// Binary search | |
sort(e.begin(), e.end()); | |
if (binary_search(e.begin(), e.end(), s)) { | |
cout << -1 << endl; | |
break; | |
} | |
e.push_back(s); | |
n = s; | |
s = 0; | |
} | |
} | |
return 0; | |
} |
/** | |
* ADASTAIR - https://www.codechef.com/problems/ADASTAIR | |
* Code by @trhgquan - https://github.com/trhgquan | |
*/ | |
#include<iostream> | |
#include<vector> | |
using namespace std; | |
int main() { | |
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | |
int T; cin >> T; | |
int N, K, S; | |
while (T--) { | |
cin >> N >> K; | |
S = 0; | |
vector<int> a; a.push_back(0); | |
for (int i = 1; i <= N; ++i) { | |
int u; cin >> u; a.push_back(u); | |
} | |
for (int i = 1; i <= N; ++i) | |
if (a[i] - a[i - 1] > K) { | |
S += (a[i] - a[i - 1]) / K; | |
if ((a[i] - a[i - 1]) % K == 0) S--; | |
} | |
cout << S << endl; | |
} | |
return 0; | |
} |
/** | |
* ZOZ - https://codechef.com/problems/ZOZ | |
* Code by @trhgquan - https://github.com/trhgquan | |
*/ | |
#include<iostream> | |
#include<vector> | |
using namespace std; | |
int main() { | |
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | |
int t; cin >> t; | |
while(t--) { | |
int n, k, s = 0, c = 0; cin >> n >> k; | |
vector<int>a; | |
for(int i = 1; i <= n; ++i) {int u; cin >> u; a.push_back(u); s += u;} | |
for (int i = 0; i < n; ++i) | |
if (a[i] + k > s - a[i]) c++; | |
cout << c << endl; | |
} | |
return 0; | |
} |
/* | |
* longestArrays.cpp - Dynamic Programming approach. | |
* Code by @trhgquan - https://github.com/trhgquan | |
*/ | |
int longestArrays(vector<int> a) { | |
vector<int> b(a.size(), 1); | |
int res = 1; | |
for (unsigned i = 1; i < a.size(); ++i){ | |
if (a[i] >= a[i - 1]) b[i] = b[i - 1] + 1; | |
res = max(b[i], res); | |
} | |
return res; | |
} |
// | |
// CMPRSS - https://codechef.com/problems/CMPRSS | |
// Code by @trhgquan - https://github.com/trhgquan | |
// | |
#include<iostream> | |
#include<vector> | |
using namespace std; | |
int main() { | |
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | |
int T, N; cin >> T; | |
while (T--) { | |
cin >> N; | |
vector<int>a, b; | |
for (int i = 1; i <= N; ++i) {int u; cin >> u; a.push_back(u);b.push_back(1);} | |
// | |
// The idea is using DP (Dynamic Programming), | |
// since we need to count every adjacent subsequence. | |
// An element is flagged "1" if it is an edge, or, a single item. | |
// | |
for (int i = 1; i < N; ++i) { | |
if (a[i] - a[i - 1] == 1) b[i] = b[i - 1] + 1; | |
if (a[i] - a[i - 1] > 1 && b[i] - b[i - 1] < 0) b[i - 1] = b[i]; | |
if (i == N - 1 && b[i] != 1) b[i] = 1; | |
} | |
// | |
// Print result | |
// | |
cout << a[0]; | |
int c = 1; | |
for (int i = 1; i < N; ++i){ | |
if (b[i] != 1) ++c; | |
if (b[i] == 1) { | |
if (c >= 2) { | |
cout << "..."; | |
c = 1; | |
} else cout << ","; | |
cout << a[i]; | |
} | |
} | |
cout << endl; | |
} | |
return 0; | |
} |
int max(int a, int b) { | |
return (a > b) ? a : b; | |
} | |
int MaximumNonAdj(vector<int> adj) { | |
int ret = INT_MIN; | |
adj.insert(adj.begin(), 0); | |
vector<int> F(adj.size(), 0); | |
F[1] = max(adj[1], 0); | |
for (int i = 2; i < adj.size(); ++i) { | |
F[i] = max(F[i - 2] + adj[i], F[i - 1]); | |
ret = max(F[i], ret); | |
} | |
return ret; | |
} |
def maximumNonAdj(arr): | |
arr.insert(0, 0) | |
brr = [0] * len(arr) | |
brr[1] = arr[1] | |
m = -100000000 | |
for i in range(1, len(arr)): | |
brr[i] = max(arr[i] + brr[i - 2], brr[i - 1]) | |
if (brr[i] > m): m = brr[i] | |
return m |
/** | |
* SC31 - https://codechef.com/problems/SC31 | |
* November Challenge 2019 - Division 2 | |
* Code by @trhgquan - https://github.com/trhgquan | |
*/ | |
#include<iostream> | |
#include<string> | |
#include<vector> | |
using namespace std; | |
int main() { | |
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | |
int t, n, sum; cin >> t; | |
string s; | |
while (t--) { | |
// | |
// So the idea is to check, if 2 string nearby has same position is '1' | |
// then the sum is 0. Then we check to another nearby string, continue to end. | |
// | |
vector<int>a(10, 0); | |
cin >> n; | |
while (n--) { | |
cin >> s; | |
for (int i = 0; i < 10; ++i) | |
if (s[i] == '1' && a[i] == 0) a[i] = 1; | |
else if (s[i] == '1' && a[i] == 1) a[i] = 0; | |
} | |
sum = 0; | |
for (int i: a) sum += i; | |
cout << sum << endl; | |
} | |
return 0; | |
} |
/** | |
* HRDSEQ - https://codechef.com/problems/HRDSEQ | |
* November Challenge 2019 - Division 2 | |
* Code by @trhgquan - https://github.com/trhgquan | |
*/ | |
#include<iostream> | |
#include<vector> | |
using namespace std; | |
int main() { | |
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | |
vector<int>a; | |
// | |
// So the idea is to build a sieve, that will cost O(n^2) | |
// the query is only cost O(n). | |
// | |
a.push_back(0); | |
a.push_back(0); | |
for (int i = 2; i <= 150; ++i) { | |
int j = i - 1; | |
bool found = false; | |
for (int k = j - 1; k >= 0; --k) { | |
if (a[k] == a[j]) { | |
a.push_back(j - k); | |
found = true; | |
break; | |
} | |
} | |
if (found) continue; | |
else a.push_back(0); | |
} | |
// Query | |
int t; cin >> t; | |
while (t--) { | |
int n; cin >> n; | |
int r = 0; | |
for (int i = 0; i < n; ++i) | |
if (a[i] == a[n - 1]) r++; | |
cout << r << endl; | |
} | |
return 0; | |
} |
/* | |
* https://www.spoj.com/problems/NUB04/ | |
* Code by @trhgquan - https://github.com/trhgquan | |
*/ | |
#include <iostream> | |
#define lli long long int | |
bool isPrime(lli a) { | |
if (a == 0 || a == 1) return false; | |
if (a == 2) return true; | |
for (lli i = 2; i * i <= a; ++i) | |
if (a % i == 0) return false; | |
return true; | |
} | |
int main() { | |
std::ios_base::sync_with_stdio(false); | |
std::cin.tie(NULL); | |
lli i; | |
while (std::cin >> i) { | |
if (isPrime(i)) std::cout << "YES" << std::endl; | |
else std::cout << "NO" << std::endl; | |
} | |
return 0; | |
} |
/** | |
* [Tutorial] PAIRS1 - Count the pairs | |
* https://www.spoj.com/problems/PAIRS1/ | |
* Code by @trhquan - https://github.com/trhgquan | |
* Solution: sorting + binary search | |
*/ | |
#include<iostream> | |
#include<algorithm> | |
#include<vector> | |
using namespace std; | |
int main() { | |
ios_base::sync_with_stdio(false); cin.tie(NULL); | |
int n, k; cin >> n >> k; | |
int ans = 0; | |
vector<int> a(n); | |
for (int i = 0; i < n; ++i) cin >> a[i]; | |
sort(a.begin(), a.end()); | |
for (int i: a) | |
if (binary_search(a.begin() , a.end(), i + k)) ans++; | |
cout << ans; | |
return 0; | |
} |
/** | |
* HIT - Codechef October Lunchtime 2019 | |
* Code by @trhgquan - https://github.com/trhgquan | |
*/ | |
#include<algorithm> | |
#include<iostream> | |
#include<vector> | |
using namespace std; | |
int main() { | |
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | |
int T; cin >> T; | |
while (T--) { | |
vector<int> a, s; | |
bool e = false; | |
int n; cin >> n; | |
for (int i = 0; i < n; ++i) {int u; cin >> u; a.push_back(u);} | |
sort(a.begin(), a.end()); | |
for (int i = n / 4; i < n; i += n / 4){ | |
if (a[i] == a[i - 1]) { | |
e = true; | |
break; | |
} else s.push_back(a[i]); | |
} | |
if (e) cout << -1 << endl; | |
else { | |
for (int i: s) cout << i << ' '; | |
cout << endl; | |
} | |
} | |
return 0; | |
} |
/* | |
* https://www.spoj.com/PTIT/problems/P166PROH/ | |
* Code by @trhgquan - https://github.com/trhgquan | |
*/ | |
#include <stdio.h> | |
int PRIME[4000]; | |
// Because maximum n is 3000, so prime list limit is bigger than 3000. | |
void generatePrimes() { | |
PRIME[1] = 0; | |
for (int i = 2; i <= 4000; ++i) PRIME[i] = 1; | |
for (int i = 2; i <= 4000; ++i) | |
for (int j = 2; i * j <= 4000; ++j) | |
if (PRIME[i * j]) PRIME[i * j] = 0; | |
} | |
int main() { | |
int n; | |
generatePrimes(); | |
scanf("%d", &n); | |
int count = 0; | |
for (int i = 2; i <= n; ++i) { | |
int can_divide = 0; | |
for (int j = 2; j < i; ++j) | |
if (PRIME[j] && i % j == 0) can_divide++; | |
if (can_divide == 2) count++; | |
} | |
printf("%d\n", count); | |
return 0; | |
} |
/* | |
* https://github.com/trhgquan | |
*/ | |
#include <stdio.h> | |
int PRIME[2000004]; | |
void generatePrimes() { | |
for (int i = 2; i <= 2000004; ++i) | |
PRIME[i] = 1; | |
for (int i = 2; i <= 2000004; ++i) | |
for (int j = 2; j * i <= 2000004; ++j) | |
PRIME[i * j] = 0; | |
} | |
int nearestPrime(int n) { | |
// Special case: | |
if (n == 1) return 2; | |
generatePrimes(); | |
int a, b; | |
for (int i = n; i >= 2; --i) | |
if (PRIME[i]) { a = i; break; } | |
for (int i = n; i <= 2000004; ++i) | |
if (PRIME[i]) { b = i; break; } | |
if (n - a > b - n) return b; | |
return a; | |
} | |
int main() { | |
int n; | |
scanf("%d", &n); | |
printf("%d\n", nearestPrime(n)); | |
return 0; | |
} |