some programs!!!
This commit is contained in:
1 parent
71f1022dea
commit
ca6daa582c
43 files changed
+1452
-381
No files matched your search
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,29 @@
|
||||
#include <cmath>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
|
||||
constexpr float ALLOWED_DIFF = 1e-6;
|
||||
|
||||
std::tuple<int, double> sqrt_custom(double x) {
|
||||
double est_prev = -1, est_curr = x;
|
||||
int rounds = 0;
|
||||
while (std::abs(est_curr - est_prev) > ALLOWED_DIFF) {
|
||||
est_prev = est_curr;
|
||||
est_curr = 0.5 * (est_prev + x / est_prev);
|
||||
// est_curr = x / (2 * est_prev) + 0.5 * est_prev;
|
||||
rounds++;
|
||||
}
|
||||
return std::make_tuple(rounds, est_curr);
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
int x;
|
||||
while (std::cin >> x) {
|
||||
auto [rounds, ans] = sqrt_custom(x);
|
||||
std::cout << rounds << " " << std::fixed << std::setprecision(2) << ans << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
|
||||
short get_digit(int num, int digit) {
|
||||
return num / static_cast<int>(pow(10, digit)) % 10;
|
||||
}
|
||||
|
||||
std::vector<std::tuple<int, int, int>> answers;
|
||||
bool digits_bucket[10];
|
||||
int nums[3];
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
for (nums[0] = 123; nums[0] <= 333; nums[0]++) {
|
||||
memset(digits_bucket, false, sizeof(digits_bucket));
|
||||
nums[1] = 2 * nums[0], nums[2] = 3 * nums[0];
|
||||
bool success_flag = true;
|
||||
for (int num : nums) {
|
||||
for (int digit = 0; digit < 3; digit++) {
|
||||
int x = get_digit(num, digit);
|
||||
if (digits_bucket[x] || x == 0) {
|
||||
success_flag = false;
|
||||
break;
|
||||
}
|
||||
digits_bucket[x] = true;
|
||||
}
|
||||
if (!success_flag) { break; }
|
||||
}
|
||||
if (success_flag) {
|
||||
answers.emplace_back(nums[0], nums[1], nums[2]);
|
||||
}
|
||||
}
|
||||
for (const auto [a,b,c] : answers) {
|
||||
std::cout << a << " " << b << " " << c << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#include <iostream>
|
||||
|
||||
int ans;
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
int n;
|
||||
std::cin >> n;
|
||||
for (int _ = 0; _ < n; _++) {
|
||||
int x;
|
||||
std::cin >> x;
|
||||
ans ^= x;
|
||||
}
|
||||
std::cout << ans << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
|
||||
int chicken_count, coroutines;
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
std::cin >> chicken_count >> coroutines;
|
||||
if (chicken_count == 1) {
|
||||
std::cout << 2 << std::endl;
|
||||
return 0;
|
||||
}
|
||||
std::cout << static_cast<int>(ceil(1.0l * chicken_count * 2 / coroutines)) << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#include <iostream>
|
||||
|
||||
constexpr int MAX_N = 1e5 + 1;
|
||||
|
||||
int calc_factors_sum(int x) {
|
||||
int sum = 0, i = 1;
|
||||
for (i = 1; i * i < x; i++) {
|
||||
if (x % i == 0) { sum += i + x / i; }
|
||||
}
|
||||
if (i * i == x) { sum += i; }
|
||||
return sum - x;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
std::cin >> n;
|
||||
for (int a = 220; a <= n; a++) {
|
||||
int b = calc_factors_sum(a);
|
||||
if (b > a && b <= n && calc_factors_sum(b) == a) {
|
||||
std::cout << a << " " << b << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
#include <array>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
#include <chrono>
|
||||
#include <map>
|
||||
#include <set>
|
||||
|
||||
constexpr int MAX_N = 1e5 + 1;
|
||||
|
||||
std::vector<int> primes;
|
||||
std::array<bool, MAX_N> not_prime;
|
||||
|
||||
std::multiset<std::pair<long long, int>, std::greater<>> calc_times, calc_times_old;
|
||||
|
||||
void init_primes() {
|
||||
primes.reserve(MAX_N / 10);
|
||||
primes.push_back(2);
|
||||
|
||||
for (int i = 3; i < MAX_N; i++) {
|
||||
if (!not_prime[i]) {
|
||||
primes.push_back(i);
|
||||
for (int x = i + i; x < MAX_N; x += i) {
|
||||
not_prime[x] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
// int shit = 0;
|
||||
// for (int i = 0; i < 10000000; i++) {
|
||||
// shit++;
|
||||
// }
|
||||
}
|
||||
|
||||
long long calc_factors_sum(int x) {
|
||||
// std::vector<int> prime_exponents(primes.size());
|
||||
std::array<int, MAX_N / 10> prime_exponents{};
|
||||
int prime_idx = 0, old_x = x;
|
||||
while (x > 1) {
|
||||
if (x % primes[prime_idx] == 0) {
|
||||
x /= primes[prime_idx], prime_exponents[prime_idx]++;
|
||||
} else {
|
||||
prime_idx++;
|
||||
}
|
||||
}
|
||||
long long sum = 1;
|
||||
for (int i = 0; i <= prime_idx; i++) {
|
||||
if (!prime_exponents[i]) { continue; }
|
||||
if (prime_exponents[i] == 1) {
|
||||
sum *= primes[i] + 1;
|
||||
} else {
|
||||
sum *= (static_cast<long long>(std::pow(primes[i], prime_exponents[i] + 1)) - 1)
|
||||
/ (primes[i] - 1);
|
||||
}
|
||||
}
|
||||
// std::clog << sum << std::endl;
|
||||
return sum - old_x;
|
||||
}
|
||||
|
||||
|
||||
long long calc_factors_sum_old(int x) {
|
||||
long long sum = 0;
|
||||
int i = 1;
|
||||
for (i = 1; i * i < x; i++) {
|
||||
if (x % i == 0) { sum += i + x / i; }
|
||||
}
|
||||
if (i * i == x) { sum += i; }
|
||||
return sum - x;
|
||||
}
|
||||
|
||||
|
||||
template <typename Func, typename... Args>
|
||||
auto time_function(const std::string& name, Func&& func, Args&&... args) {
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
|
||||
// 完美转发参数并调用函数
|
||||
for (int i = 0; i < 100; i++) {
|
||||
auto result = std::forward<Func>(func)(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
|
||||
|
||||
// std::clog << "[" << name << "] 耗时: "
|
||||
// << duration.count() << " μs " << std::endl;
|
||||
|
||||
// return result; // 返回函数的返回值(如果有)
|
||||
return duration.count() / 100;
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
// auto s1 = std::chrono::high_resolution_clock::now();
|
||||
// init_primes();
|
||||
// auto e1 = std::chrono::high_resolution_clock::now();
|
||||
// auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(e1 - s1);
|
||||
//
|
||||
// std::clog << duration.count() << std::endl;
|
||||
|
||||
init_primes();
|
||||
|
||||
std::cout << "OLD" << std::endl;
|
||||
for (int x = 2; x < MAX_N; x++) {
|
||||
calc_times_old.insert({time_function("", calc_factors_sum_old, x), x});
|
||||
}
|
||||
|
||||
int count = 0;
|
||||
for (auto it = calc_times_old.begin(); it != calc_times_old.end() && count < 10; ++it, ++count) {
|
||||
std::cout << "OLD " << it->second << "->" << it->first << std::endl;
|
||||
}
|
||||
|
||||
|
||||
std::cout << "NEW" << std::endl;
|
||||
for (int x = 2; x < MAX_N; x++) {
|
||||
calc_times.insert({time_function("", calc_factors_sum, x), x});
|
||||
}
|
||||
|
||||
count = 0;
|
||||
for (auto it = calc_times.begin(); it != calc_times.end() && count < 10; ++it, ++count) {
|
||||
std::cout << it->second << "->" << it->first << std::endl;
|
||||
}
|
||||
|
||||
|
||||
// int n;
|
||||
// std::cin >> n;
|
||||
// for (int a = 220; a <= n; a++) {
|
||||
// long long b = calc_factors_sum(a);
|
||||
// if (b > MAX_N) { continue; }
|
||||
// if (b > a && b <= n && calc_factors_sum(static_cast<int>(b)) == a) {
|
||||
// std::cout << a << " " << b << std::endl;
|
||||
// }
|
||||
// }
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
#include <array>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
|
||||
#include <chrono>
|
||||
|
||||
constexpr int MAX_N = 1e5 + 1;
|
||||
|
||||
std::vector<int> primes;
|
||||
std::array<bool, MAX_N> not_prime;
|
||||
|
||||
void init_primes() {
|
||||
primes.reserve(MAX_N / 10);
|
||||
primes.push_back(2);
|
||||
|
||||
for (int i = 3; i < MAX_N; i++) {
|
||||
if (!not_prime[i]) {
|
||||
primes.push_back(i);
|
||||
for (int x = i + i; x < MAX_N; x += i) {
|
||||
not_prime[x] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
long long calc_factors_sum(int x) {
|
||||
std::vector<int> prime_exponents(primes.size());
|
||||
int prime_idx = 0, old_x = x;
|
||||
while (x > 1) {
|
||||
if (x % primes[prime_idx] == 0) {
|
||||
x /= primes[prime_idx], prime_exponents[prime_idx]++;
|
||||
} else {
|
||||
prime_idx++;
|
||||
}
|
||||
}
|
||||
long long sum = 1;
|
||||
for (int i = 0; i <= prime_idx; i++) {
|
||||
if (!prime_exponents[i]) { continue; }
|
||||
if (prime_exponents[i] == 1) {
|
||||
sum *= primes[i] + 1;
|
||||
} else {
|
||||
sum *= (static_cast<long long>(std::pow(primes[i], prime_exponents[i] + 1)) - 1)
|
||||
/ (primes[i] - 1);
|
||||
}
|
||||
}
|
||||
// std::clog << sum << std::endl;
|
||||
return sum - old_x;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
init_primes();
|
||||
|
||||
int n;
|
||||
std::cin >> n;
|
||||
for (int a = 220; a <= n; a++) {
|
||||
long long b = calc_factors_sum(a);
|
||||
if (b > MAX_N) { continue; }
|
||||
if (b > a && b <= n && calc_factors_sum(static_cast<int>(b)) == a) {
|
||||
std::cout << a << " " << b << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
#include <array>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
class Vector2 {
|
||||
public:
|
||||
int x, y;
|
||||
|
||||
constexpr int& operator[](const std::size_t idx) {
|
||||
switch (idx) {
|
||||
case 0: return x;
|
||||
case 1: return y;
|
||||
default: throw std::invalid_argument("Index should be 0 or 1");
|
||||
}
|
||||
}
|
||||
|
||||
constexpr Vector2 operator+(const Vector2 other) const {
|
||||
return Vector2{x + other.x, y + other.y};
|
||||
}
|
||||
|
||||
constexpr Vector2 operator-() const {
|
||||
return Vector2{-x, -y};
|
||||
}
|
||||
|
||||
constexpr Vector2 operator-(const Vector2 other) const {
|
||||
return *this + -other;
|
||||
}
|
||||
|
||||
constexpr static int cross(const Vector2 a, const Vector2 b) {
|
||||
return a.x * b.y - a.y * b.x;
|
||||
}
|
||||
};
|
||||
|
||||
bool is_convex(std::vector<Vector2> polygon) {
|
||||
for (int i = 0; i < polygon.size() - 1; i++) {
|
||||
// if (Vector2::cross(polygon[i], polygon[i + 1]) * sign <= 0) { return false; }
|
||||
// Vector2 side1 = polygon[i + 1] - polygon[i];
|
||||
int sign = 0;
|
||||
for (int j = 0; j < polygon.size(); j++) {
|
||||
if (i == j || i + 1 == j) { continue; }
|
||||
if (sign == 0) {
|
||||
sign = Vector2::cross(polygon[i] - polygon[i + 1], polygon[j] - polygon[i + 1]);
|
||||
if (sign == 0) { return false; }
|
||||
sign = sign > 0 ? 1 : -1;
|
||||
continue;
|
||||
}
|
||||
if (Vector2::cross(polygon[i] - polygon[i + 1], polygon[j] - polygon[i + 1]) * sign <= 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
std::vector<Vector2> coords(4);
|
||||
|
||||
while (std::cin >> coords[0].x >> coords[0].y) {
|
||||
for (int i = 1; i <= 3; ++i) { std::cin >> coords[i].x >> coords[i].y; }
|
||||
std::cout << (is_convex(coords) ? "yes" : "no") << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
constexpr int MAX_N = 10001, MAX_D = 50;
|
||||
|
||||
int get_digits_sum(int x) {
|
||||
int sum = 0;
|
||||
while (x) {
|
||||
sum += x % 10;
|
||||
x /= 10;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
int m, n, k;
|
||||
std::vector<int> bucket[MAX_D];
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
char temp_buffer;
|
||||
std::cin >> m >> temp_buffer >> n >> temp_buffer >> k;
|
||||
|
||||
for (int x = m + 1; x < n; x++) {
|
||||
int digits_sum = get_digits_sum(x);
|
||||
if (digits_sum % k != 0) { continue; }
|
||||
bucket[digits_sum].push_back(x);
|
||||
}
|
||||
|
||||
for (const auto& nums : bucket) {
|
||||
if (nums.empty()) { continue; }
|
||||
bool first = true;
|
||||
for (int num : nums) {
|
||||
if (first) {
|
||||
first = false;
|
||||
std::cout << num;
|
||||
} else {
|
||||
std::cout << ',' << num;
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
#include <iostream>
|
||||
|
||||
constexpr std::size_t MAX_N = 501;
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
|
||||
using ChangedCoins = std::tuple<int, int, int, int>;
|
||||
|
||||
constexpr int calc_coin_amount(const ChangedCoins& coins) {
|
||||
return std::get<0>(coins) + std::get<1>(coins) + std::get<2>(coins) + std::get<3>(coins);
|
||||
}
|
||||
|
||||
ChangedCoins dp[MAX_N];
|
||||
|
||||
|
||||
void init_dp() {
|
||||
for (int i = 1; i < MAX_N; i++) {
|
||||
dp[i] = dp[i - 1], std::get<3>(dp[i])++;
|
||||
if (i - 25 >= 0 && calc_coin_amount(dp[i]) > calc_coin_amount(dp[i - 25])) {
|
||||
dp[i] = dp[i - 25], std::get<0>(dp[i])++;
|
||||
}
|
||||
if (i - 10 >= 0 && calc_coin_amount(dp[i]) > calc_coin_amount(dp[i - 10])) {
|
||||
dp[i] = dp[i - 10], std::get<1>(dp[i])++;
|
||||
}
|
||||
if (i - 5 >= 0 && calc_coin_amount(dp[i]) > calc_coin_amount(dp[i - 5])) {
|
||||
dp[i] = dp[i - 5], std::get<2>(dp[i])++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
init_dp();
|
||||
|
||||
int cents;
|
||||
std::cin >> cents;
|
||||
for (int i = 1; std::cin >> cents; i++) {
|
||||
std::cout
|
||||
<< i << ' '
|
||||
<< std::get<0>(dp[cents]) << " QUARTER(S),"
|
||||
<< std::get<1>(dp[cents]) << " DIME(S),"
|
||||
<< std::get<2>(dp[cents]) << " NICKEL(S),"
|
||||
<< std::get<3>(dp[cents]) << " PENNY(S)" << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
||||
using IntWithBase = std::string;
|
||||
|
||||
IntWithBase dec_to_base(int x, unsigned short base) {
|
||||
IntWithBase result;
|
||||
while (x) {
|
||||
result += static_cast<char>(x % base + '0');
|
||||
x /= base;
|
||||
}
|
||||
std::reverse(result.begin(), result.end());
|
||||
return result;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
int op;
|
||||
std::cin >> op;
|
||||
|
||||
for (int x = 0; ; x++) {
|
||||
IntWithBase x7 = dec_to_base(x, 7), x9 = dec_to_base(x, 9);
|
||||
if (x7.length() != 3 || x9.length() != 3) { continue; }
|
||||
if (x7[0] == x9[2] && x7[1] == x9[1] && x7[2] == x9[0]) {
|
||||
if (op == 1) {
|
||||
std::cout << x << std::endl;
|
||||
} else {
|
||||
std::cout << (op == 2 ? x7 : x9) << std::endl;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
constexpr int DEFAULT_JUMPS = 60, STOP_PUNISHMENT = 3;
|
||||
|
||||
int get_actual_jumps(std::vector<int> counted_jumps) {
|
||||
counted_jumps.push_back(DEFAULT_JUMPS);
|
||||
for (int i = 0; i < counted_jumps.size(); i++) {
|
||||
if (counted_jumps[i] + 3 * i >= DEFAULT_JUMPS - STOP_PUNISHMENT) {
|
||||
return std::min(counted_jumps[i] + STOP_PUNISHMENT * i, DEFAULT_JUMPS)
|
||||
- STOP_PUNISHMENT * i;
|
||||
}
|
||||
}
|
||||
throw std::logic_error("Wrong algorithm");
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
int n, m;
|
||||
std::cin >> n;
|
||||
for (int _ = 0; _ < n; _++) {
|
||||
std::cin >> m;
|
||||
std::vector<int> counted_jumps(m);
|
||||
for (int i = 0; i < m; i++) {
|
||||
std::cin >> counted_jumps[i];
|
||||
}
|
||||
std::cout << get_actual_jumps(counted_jumps) << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Reference in new issue
Block a user