为什么错了?
原题链接:1.A+B problem2026-02-28 09:21:24
发布于:浙江
我已经做出来了哈:https://www.acgo.cn/discuss/study/73822
我不懂错在哪:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <locale>
#include <map>
#include <memory>
#include <mutex>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define ENABLE_VERBOSE_LOGGING 1
#define ENABLE_TIMING 1
#define ENABLE_THREAD_SAFETY 1
#define USE_MULTITHREADING 0
#define MAX_ITERATIONS 1
#define START_OF_PROGRAM do {
#define END_OF_PROGRAM } while(0)
#define BEGIN_NAMESPACE namespace {
#define END_NAMESPACE }
#define UNUSED(x) ((void)(x))
#define IGNORE_RETURN_VALUE(x) if (x) {}
#define LIKELY(x) (__builtin_expect(!!(x), 1))
#define UNLIKELY(x) (__builtin_expect(!!(x), 0))
#define PROGRAM_NAME "A+B Solver"
#define PROGRAM_VERSION "9.9.9"
#define PROGRAM_AUTHOR "Dr. Overkill"
#define PROGRAM_LICENSE "GPLv3+ with extra verbosity clause"
const char* const kProgramName = PROGRAM_NAME;
const char* const kProgramVersion = PROGRAM_VERSION;
const char* const kProgramAuthor = PROGRAM_AUTHOR;
const char* const kProgramLicense = PROGRAM_LICENSE;
const double kPi = 3.14159265358979323846;
const double kEulerNumber = 2.71828182845904523536;
const double kGoldenRatio = 1.61803398874989484820;
const float kFloatZero = 0.0f;
const int kMagicNumber = 42;
const char kGreetingMessage[] = "Welcome to the most over-engineered A+B solver!\n";
const char kFarewellMessage[] = "Thank you for using our software. Have a nice day!\n";
static bool g_initialized = false;
static std::mutex g_global_mutex;
static int g_log_level = 0;
using Integer = int;
using InputPair = std::pair<Integer, Integer>;
using ResultType = Integer;
using Logger = std::ostream;
using ChronoClock = std::chrono::high_resolution_clock;
using TimePoint = ChronoClock::time_point;
using DurationMs = std::chrono::duration<double, std::milli>;
enum class ErrorCode : int {
SUCCESS = 0,
INVALID_INPUT = 1,
DIVISION_BY_ZERO = 2,
OUT_OF_MEMORY = 3,
UNKNOWN_ERROR = 99
};
enum class AdditionStrategyType : uint8_t {
SIMPLE,
BITWISE,
TEMPLATE_META,
MULTITHREADED,
RECURSIVE,
FUNCTIONAL,
NUM_STRATEGIES
};
struct MathConstants {
static constexpr double PI = 3.141592653589793;
static constexpr double E = 2.718281828459045;
static constexpr double GOLDEN_RATIO = 1.618033988749895;
static constexpr int MAX_INT = std::numeric_limits<int>::max();
static constexpr int MIN_INT = std::numeric_limits<int>::min();
};
enum class Weekday {
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
};
enum class Month {
January, February, March, April, May, June, July, August, September, October, November, December
};
enum class Color {
Red, Green, Blue, Yellow, Cyan, Magenta, Black, White
};
struct Point2D { double x, y; };
struct Point3D { double x, y, z; };
struct LineSegment { Point2D start, end; };
namespace OverkillSoft {
namespace APlusB {
namespace Core {
namespace Arithmetic {
namespace details {
template<typename T>
struct IsInteger : std::false_type {};
template<> struct IsInteger<char> : std::true_type {};
template<> struct IsInteger<signed char> : std::true_type {};
template<> struct IsInteger<unsigned char> : std::true_type {};
template<> struct IsInteger<short> : std::true_type {};
template<> struct IsInteger<unsigned short> : std::true_type {};
template<> struct IsInteger<int> : std::true_type {};
template<> struct IsInteger<unsigned int> : std::true_type {};
template<> struct IsInteger<long> : std::true_type {};
template<> struct IsInteger<unsigned long> : std::true_type {};
template<> struct IsInteger<long long> : std::true_type {};
template<> struct IsInteger<unsigned long long> : std::true_type {};
#ifdef __cpp_char8_t
template<> struct IsInteger<char8_t> : std::true_type {};
#endif
#ifdef __cpp_lib_byte
template<> struct IsInteger<std::byte> : std::true_type {};
#endif
} // namespace details
class AdditionStrategy {
public:
virtual ~AdditionStrategy() = default;
virtual ResultType execute(Integer a, Integer b) const = 0;
virtual std::string name() const = 0;
};
class SimpleAdditionStrategy : public AdditionStrategy {
public:
ResultType execute(Integer a, Integer b) const override {
#if ENABLE_VERBOSE_LOGGING
std::cerr << "[SimpleAdditionStrategy] Adding " << a << " and " << b << " using operator+." << std::endl;
#endif
return a + b;
}
std::string name() const override { return "SimpleAddition"; }
};
class BitwiseAdditionStrategy : public AdditionStrategy {
public:
ResultType execute(Integer a, Integer b) const override {
#if ENABLE_VERBOSE_LOGGING
std::cerr << "[BitwiseAdditionStrategy] Adding " << a << " and " << b << " using bitwise operations." << std::endl;
#endif
while (b != 0) {
Integer carry = a & b;
a = a ^ b;
b = carry << 1;
}
return a;
}
std::string name() const override { return "BitwiseAddition"; }
};
class RecursiveAdditionStrategy : public AdditionStrategy {
private:
ResultType add_recursive(Integer a, Integer b) const {
if (b == 0) return a;
#if ENABLE_VERBOSE_LOGGING
std::cerr << "[RecursiveAdditionStrategy] Recursion depth: " << (b > 0 ? b : -b) << std::endl;
#endif
if (b > 0)
return add_recursive(a + 1, b - 1);
else if (b < 0)
return add_recursive(a - 1, b + 1);
return a;
}
public:
ResultType execute(Integer a, Integer b) const override {
#if ENABLE_VERBOSE_LOGGING
std::cerr << "[RecursiveAdditionStrategy] Starting recursive addition." << std::endl;
#endif
return add_recursive(a, b);
}
std::string name() const override { return "RecursiveAddition"; }
};
class MultithreadedAdditionStrategy : public AdditionStrategy {
private:
mutable std::mutex m_mutex;
mutable ResultType m_result;
mutable bool m_completed;
void thread_func(Integer a, Integer b) const {
#if ENABLE_VERBOSE_LOGGING
std::cerr << "[MultithreadedAdditionStrategy] Worker thread started." << std::endl;
#endif
ResultType res = a + b;
{
std::lock_guard<std::mutex> lock(m_mutex);
m_result = res;
m_completed = true;
}
#if ENABLE_VERBOSE_LOGGING
std::cerr << "[MultithreadedAdditionStrategy] Worker thread finished." << std::endl;
#endif
}
public:
MultithreadedAdditionStrategy() : m_result(0), m_completed(false) {}
ResultType execute(Integer a, Integer b) const override {
#if ENABLE_VERBOSE_LOGGING
std::cerr << "[MultithreadedAdditionStrategy] Creating worker thread." << std::endl;
#endif
m_completed = false;
std::thread worker(&MultithreadedAdditionStrategy::thread_func, this, a, b);
worker.join();
#if ENABLE_VERBOSE_LOGGING
std::cerr << "[MultithreadedAdditionStrategy] Worker thread joined." << std::endl;
#endif
return m_result;
}
std::string name() const override { return "MultithreadedAddition"; }
};
template<int A, int B>
struct TemplateMetaAdder {
static constexpr int value = A + B;
};
class TemplateMetaAdditionStrategy : public AdditionStrategy {
public:
ResultType execute(Integer a, Integer b) const override {
#if ENABLE_VERBOSE_LOGGING
std::cerr << "[TemplateMetaAdditionStrategy] This strategy only works at compile time. Falling back to simple addition." << std::endl;
#endif
return a + b;
}
std::string name() const override { return "TemplateMetaAddition"; }
};
} // namespace Arithmetic
class Adder {
private:
std::unique_ptr<Arithmetic::AdditionStrategy> m_strategy;
mutable std::mutex m_mutex;
public:
explicit Adder(Arithmetic::AdditionStrategy* strategy) : m_strategy(strategy) {
#if ENABLE_VERBOSE_LOGGING
std::cerr << "[Adder] Constructed with strategy: " << (strategy ? strategy->name() : "nullptr") << std::endl;
#endif
if (!m_strategy) {
throw std::invalid_argument("Addition strategy cannot be null");
}
}
Adder(const Adder&) = delete;
Adder(Adder&& other) noexcept : m_strategy(std::move(other.m_strategy)) {
#if ENABLE_VERBOSE_LOGGING
std::cerr << "[Adder] Move constructed." << std::endl;
#endif
}
~Adder() {
#if ENABLE_VERBOSE_LOGGING
std::cerr << "[Adder] Destructor called." << std::endl;
#endif
}
Adder& operator=(const Adder&) = delete;
Adder& operator=(Adder&& other) noexcept {
if (this != &other) {
m_strategy = std::move(other.m_strategy);
#if ENABLE_VERBOSE_LOGGING
std::cerr << "[Adder] Move assignment." << std::endl;
#endif
}
return *this;
}
ResultType add(Integer a, Integer b) const {
#if ENABLE_THREAD_SAFETY
std::lock_guard<std::mutex> lock(m_mutex);
#endif
#if ENABLE_VERBOSE_LOGGING
std::cerr << "[Adder] Performing addition using strategy: " << m_strategy->name() << std::endl;
#endif
return m_strategy->execute(a, b);
}
void setStrategy(Arithmetic::AdditionStrategy* strategy) {
if (!strategy) {
throw std::invalid_argument("New strategy cannot be null");
}
#if ENABLE_VERBOSE_LOGGING
std::cerr << "[Adder] Changing strategy from "
<< (m_strategy ? m_strategy->name() : "null")
<< " to " << strategy->name() << std::endl;
#endif
m_strategy.reset(strategy);
}
};
namespace Input {
class InputReader {
public:
virtual ~InputReader() = default;
virtual std::string readLine() = 0;
virtual bool isGood() const = 0;
};
class StdinReader : public InputReader {
private:
std::istream& m_inputStream;
public:
explicit StdinReader(std::istream& is = std::cin) : m_inputStream(is) {
#if ENABLE_VERBOSE_LOGGING
std::cerr << "[StdinReader] Created, using stream: " << (void*)&m_inputStream << std::endl;
#endif
}
std::string readLine() override {
std::string line;
std::getline(m_inputStream, line);
#if ENABLE_VERBOSE_LOGGING
std::cerr << "[StdinReader] Read line: \"" << line << "\"" << std::endl;
#endif
return line;
}
bool isGood() const override {
return m_inputStream.good();
}
};
class FixedStringReader : public InputReader {
private:
std::string m_fixedLine;
bool m_good;
public:
explicit FixedStringReader(const std::string& line) : m_fixedLine(line), m_good(true) {
#if ENABLE_VERBOSE_LOGGING
std::cerr << "[FixedStringReader] Created with fixed line: \"" << line << "\"" << std::endl;
#endif
}
std::string readLine() override {
#if ENABLE_VERBOSE_LOGGING
std::cerr << "[FixedStringReader] Returning fixed line: \"" << m_fixedLine << "\"" << std::endl;
#endif
m_good = false;
return m_fixedLine;
}
bool isGood() const override {
return m_good;
}
};
class CompositeReader : public InputReader {
private:
std::vector<std::unique_ptr<InputReader>> m_readers;
size_t m_currentIndex = 0;
public:
void addReader(std::unique_ptr<InputReader> reader) {
m_readers.push_back(std::move(reader));
}
std::string readLine() override {
while (m_currentIndex < m_readers.size()) {
if (m_readers[m_currentIndex]->isGood()) {
return m_readers[m_currentIndex]->readLine();
} else {
m_currentIndex++;
}
}
return "";
}
bool isGood() const override {
for (size_t i = m_currentIndex; i < m_readers.size(); ++i) {
if (m_readers[i]->isGood()) return true;
}
return false;
}
};
} // namespace Input
namespace Parsing {
class Parser {
public:
virtual ~Parser() = default;
virtual InputPair parse(const std::string& input) = 0;
};
class WhitespaceParser : public Parser {
private:
std::string trim(const std::string& s) const {
size_t start = s.find_first_not_of(" \t\n\r");
size_t end = s.find_last_not_of(" \t\n\r");
if (start == std::string::npos) return "";
return s.substr(start, end - start + 1);
}
public:
InputPair parse(const std::string& input) override {
#if ENABLE_VERBOSE_LOGGING
std::cerr << "[WhitespaceParser] Parsing string: \"" << input << "\"" << std::endl;
#endif
std::istringstream iss(input);
Integer a, b;
if (!(iss >> a >> b)) {
#if ENABLE_VERBOSE_LOGGING
std::cerr << "[WhitespaceParser] Parsing failed." << std::endl;
#endif
throw std::invalid_argument("Input does not contain two integers");
}
#if ENABLE_VERBOSE_LOGGING
std::cerr << "[WhitespaceParser] Parsed numbers: " << a << " and " << b << std::endl;
#endif
return std::make_pair(a, b);
}
};
template<char Delimiter = ' '>
class DelimitedParser : public Parser {
public:
InputPair parse(const std::string& input) override {
#if ENABLE_VERBOSE_LOGGING
std::cerr << "[DelimitedParser] Parsing with delimiter '" << Delimiter << "'" << std::endl;
#endif
std::stringstream ss(input);
std::string token;
std::vector<std::string> tokens;
while (std::getline(ss, token, Delimiter)) {
if (!token.empty()) {
tokens.push_back(token);
}
}
if (tokens.size() != 2) {
throw std::invalid_argument("Expected exactly two numbers");
}
Integer a = std::stoi(tokens[0]);
Integer b = std::stoi(tokens[1]);
return std::make_pair(a, b);
}
};
#ifdef __cpp_lib_regex
#include <regex>
class RegexParser : public Parser {
private:
std::regex m_pattern;
public:
RegexParser() : m_pattern(R"(^\s*([+-]?\d+)\s+([+-]?\d+)\s*$)") {
#if ENABLE_VERBOSE_LOGGING
std::cerr << "[RegexParser] Constructed with regex pattern." << std::endl;
#endif
}
InputPair parse(const std::string& input) override {
std::smatch match;
if (!std::regex_match(input, match, m_pattern) || match.size() != 3) {
throw std::invalid_argument("Input does not match expected pattern");
}
Integer a = std::stoi(match[1].str());
Integer b = std::stoi(match[2].str());
return std::make_pair(a, b);
}
};
#endif
} // namespace Parsing
namespace Validation {
class Validator {
public:
virtual ~Validator() = default;
virtual bool validate(const InputPair& pair) const = 0;
virtual std::string errorMessage() const = 0;
};
class IntRangeValidator : public Validator {
private:
mutable std::string m_lastError;
public:
bool validate(const InputPair& pair) const override {
Integer a = pair.first;
Integer b = pair.second;
if (a < MathConstants::MIN_INT || a > MathConstants::MAX_INT) {
m_lastError = "First number out of range";
return false;
}
if (b < MathConstants::MIN_INT || b > MathConstants::MAX_INT) {
m_lastError = "Second number out of range";
return false;
}
return true;
}
std::string errorMessage() const override { return m_lastError; }
};
class PositiveNumberValidator : public Validator {
private:
mutable std::string m_lastError;
public:
bool validate(const InputPair& pair) const override {
if (pair.first < 0) {
m_lastError = "First number is negative";
return false;
}
if (pair.second < 0) {
m_lastError = "Second number is negative";
return false;
}
return true;
}
std::string errorMessage() const override { return m_lastError; }
};
class CompositeValidator : public Validator {
private:
std::vector<std::unique_ptr<Validator>> m_validators;
mutable std::string m_lastError;
public:
void addValidator(std::unique_ptr<Validator> validator) {
m_validators.push_back(std::move(validator));
}
bool validate(const InputPair& pair) const override {
for (const auto& v : m_validators) {
if (!v->validate(pair)) {
m_lastError = v->errorMessage();
return false;
}
}
return true;
}
std::string errorMessage() const override { return m_lastError; }
};
} // namespace Validation
namespace Output {
class OutputFormatter {
public:
virtual ~OutputFormatter() = default;
virtual std::string format(ResultType result) const = 0;
};
class SimpleFormatter : public OutputFormatter {
public:
std::string format(ResultType result) const override {
#if ENABLE_VERBOSE_LOGGING
std::cerr << "[SimpleFormatter] Formatting result: " << result << std::endl;
#endif
return std::to_string(result);
}
};
class DecoratedFormatter : public OutputFormatter {
private:
std::string m_prefix;
std::string m_suffix;
public:
DecoratedFormatter(const std::string& prefix = "", const std::string& suffix = "")
: m_prefix(prefix), m_suffix(suffix) {
#if ENABLE_VERBOSE_LOGGING
std::cerr << "[DecoratedFormatter] Created with prefix \"" << prefix << "\", suffix \"" << suffix << "\"" << std::endl;
#endif
}
std::string format(ResultType result) const override {
return m_prefix + std::to_string(result) + m_suffix;
}
};
class JsonFormatter : public OutputFormatter {
public:
std::string format(ResultType result) const override {
std::ostringstream oss;
oss << "{ \"result\": " << result << " }";
return oss.str();
}
};
class XmlFormatter : public OutputFormatter {
public:
std::string format(ResultType result) const override {
std::ostringstream oss;
oss << "<result>" << result << "</result>";
return oss.str();
}
};
} // namespace Output
namespace Logging {
enum class LogLevel {
DEBUG,
INFO,
WARNING,
ERROR
};
class Logger {
private:
LogLevel m_threshold;
std::ostream& m_outputStream;
public:
explicit Logger(LogLevel threshold = LogLevel::INFO, std::ostream& os = std::cerr)
: m_threshold(threshold), m_outputStream(os) {}
template<typename... Args>
void log(LogLevel level, const Args&... args) const {
if (level >= m_threshold) {
std::lock_guard<std::mutex> lock(g_global_mutex);
(m_outputStream << ... << args) << std::endl;
}
}
};
static Logger g_logger(LogLevel::DEBUG, std::cerr);
#define LOG_DEBUG(...) g_logger.log(LogLevel::DEBUG, "[DEBUG] ", __VA_ARGS__)
#define LOG_INFO(...) g_logger.log(LogLevel::INFO, "[INFO] ", __VA_ARGS__)
#define LOG_WARNING(...) g_logger.log(LogLevel::WARNING, "[WARNING] ", __VA_ARGS__)
#define LOG_ERROR(...) g_logger.log(LogLevel::ERROR, "[ERROR] ", __VA_ARGS__)
} // namespace Logging
namespace Utility {
void nop() {}
int random_number() {
static std::random_device rd;
static std::mt19937 gen(rd());
static std::uniform_int_distribution<> dis(0, 1000);
return dis(gen);
}
void print_banner() {
std::cerr << R"(
╔═══════════════════════════════════════════════════════════════════╗
║ A+B SOLVER - ULTIMATE EDITION ║
║ (with extra verbosity) ║
╚═══════════════════════════════════════════════════════════════════╝
)" << std::endl;
}
} // namespace Utility
class Application {
private:
std::unique_ptr<Input::InputReader> m_reader;
std::unique_ptr<Parsing::Parser> m_parser;
std::unique_ptr<Validation::Validator> m_validator;
std::unique_ptr<Adder> m_adder;
std::unique_ptr<Output::OutputFormatter> m_formatter;
TimePoint m_startTime;
TimePoint m_endTime;
public:
Application(std::unique_ptr<Input::InputReader> reader,
std::unique_ptr<Parsing::Parser> parser,
std::unique_ptr<Validation::Validator> validator,
std::unique_ptr<Adder> adder,
std::unique_ptr<Output::OutputFormatter> formatter)
: m_reader(std::move(reader))
, m_parser(std::move(parser))
, m_validator(std::move(validator))
, m_adder(std::move(adder))
, m_formatter(std::move(formatter)) {
LOG_INFO("Application constructed with all dependencies.");
}
~Application() {
LOG_INFO("Application destroyed.");
}
int run() {
int dummy1 = 0;
int dummy2 = 0;
double dummy3 = 0.0;
std::string dummy4 = "unused";
bool dummy5 = false;
UNUSED(dummy1); UNUSED(dummy2); UNUSED(dummy3); UNUSED(dummy4); UNUSED(dummy5);
static_assert(MathConstants::PI > 3.0, "PI is not greater than 3?");
static_assert(MathConstants::E > 2.7, "E is too small");
#if ENABLE_TIMING
m_startTime = ChronoClock::now();
#endif
LOG_INFO("Application started.");
LOG_INFO("Reading input...");
std::string inputLine;
try {
inputLine = m_reader->readLine();
} catch (const std::exception& ex) {
LOG_ERROR("Exception while reading input: ", ex.what());
return static_cast<int>(ErrorCode::INVALID_INPUT);
}
LOG_INFO("Parsing input...");
InputPair numbers;
try {
numbers = m_parser->parse(inputLine);
} catch (const std::exception& ex) {
LOG_ERROR("Exception while parsing input: ", ex.what());
return static_cast<int>(ErrorCode::INVALID_INPUT);
}
LOG_INFO("Validating numbers...");
if (!m_validator->validate(numbers)) {
LOG_ERROR("Validation failed: ", m_validator->errorMessage());
return static_cast<int>(ErrorCode::INVALID_INPUT);
}
LOG_INFO("Performing addition...");
ResultType result;
try {
result = m_adder->add(numbers.first, numbers.second);
} catch (const std::exception& ex) {
LOG_ERROR("Exception during addition: ", ex.what());
return static_cast<int>(ErrorCode::UNKNOWN_ERROR);
}
LOG_INFO("Formatting result...");
std::string outputStr = m_formatter->format(result);
#if ENABLE_TIMING
m_endTime = ChronoClock::now();
DurationMs elapsed = m_endTime - m_startTime;
LOG_INFO("Elapsed time: ", elapsed.count(), " ms");
#endif
std::cout << outputStr << std::endl;
LOG_INFO("Application finished successfully.");
return static_cast<int>(ErrorCode::SUCCESS);
}
};
} // namespace Core
} // namespace APlusB
} // namespace OverkillSoft
namespace {
class GlobalInitializer {
public:
GlobalInitializer() {
std::cerr << "[GlobalInitializer] Program started initializing globals." << std::endl;
}
~GlobalInitializer() {
std::cerr << "[GlobalInitializer] Program cleaning up globals." << std::endl;
}
} g_initializer;
struct StaticData {
int value;
StaticData() : value(42) {
std::cerr << "[StaticData] Constructed with value " << value << std::endl;
}
} g_static_data;
int g_dummy_array[1000] = {0};
}
template<int N>
constexpr int sum_upto() {
return N + sum_upto<N-1>();
}
template<> constexpr int sum_upto<0>() { return 0; }
static_assert(sum_upto<5>() == 15, "Compile-time sum works");
template<typename... Ts>
struct TypeList {};
using DummyTypes = TypeList<int, double, char, float, long, short, bool>;
template int sum_upto<10>();
template int sum_upto<20>();
int main(int argc, char* argv[]) {
using namespace OverkillSoft::APlusB::Core;
using namespace OverkillSoft::APlusB::Core::Logging;
Utility::print_banner();
std::cerr << "=================================================================" << std::endl;
std::cerr << kGreetingMessage;
std::cerr << "Program: " << kProgramName << ", Version " << kProgramVersion << std::endl;
std::cerr << "Author: " << kProgramAuthor << std::endl;
std::cerr << "License: " << kProgramLicense << std::endl;
std::cerr << "=================================================================" << std::endl;
if (argc > 1) {
std::cerr << "Command line arguments detected (" << argc-1 << "):" << std::endl;
for (int i = 1; i < argc; ++i) {
std::cerr << " argv[" << i << "] = \"" << argv[i] << "\"" << std::endl;
}
} else {
std::cerr << "No command line arguments provided. Proceeding with default behavior." << std::endl;
}
const char* env_log = std::getenv("LOG_LEVEL");
if (env_log) {
std::cerr << "LOG_LEVEL environment variable set to: " << env_log << std::endl;
}
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(1, 100);
int random_dummy = dis(gen);
std::cerr << "Random dummy number generated: " << random_dummy << " (unused)" << std::endl;
UNUSED(random_dummy);
std::cerr << "Loading";
for (int i = 0; i < 5; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(200));
std::cerr << ".";
}
std::cerr << " done!" << std::endl;
std::unique_ptr<Input::InputReader> reader;
#if 1
reader = std::make_unique<Input::StdinReader>();
#else
reader = std::make_unique<Input::FixedStringReader>("42 42");
#endif
std::unique_ptr<Parsing::Parser> parser;
parser = std::make_unique<Parsing::WhitespaceParser>();
#ifdef __cpp_lib_regex
#endif
std::unique_ptr<Validation::Validator> validator;
auto compositeValidator = std::make_unique<Validation::CompositeValidator>();
compositeValidator->addValidator(std::make_unique<Validation::IntRangeValidator>());
validator = std::move(compositeValidator);
std::uniform_int_distribution<> strat_dis(0, static_cast<int>(AdditionStrategyType::NUM_STRATEGIES)-1);
int strategy_choice = strat_dis(gen);
std::cerr << "Randomly selected addition strategy index: " << strategy_choice << std::endl;
std::unique_ptr<Arithmetic::AdditionStrategy> strategy;
switch (strategy_choice) {
case 0: strategy = std::make_unique<Arithmetic::SimpleAdditionStrategy>(); break;
case 1: strategy = std::make_unique<Arithmetic::BitwiseAdditionStrategy>(); break;
case 2: strategy = std::make_unique<Arithmetic::TemplateMetaAdditionStrategy>(); break;
case 3: strategy = std::make_unique<Arithmetic::MultithreadedAdditionStrategy>(); break;
case 4: strategy = std::make_unique<Arithmetic::RecursiveAdditionStrategy>(); break;
default:
std::cerr << "Invalid strategy index, using simple." << std::endl;
strategy = std::make_unique<Arithmetic::SimpleAdditionStrategy>();
break;
}
auto adder = std::make_unique<Adder>(strategy.release());
std::unique_ptr<Output::OutputFormatter> formatter;
formatter = std::make_unique<Output::SimpleFormatter>();
Application app(std::move(reader), std::move(parser), std::move(validator),
std::move(adder), std::move(formatter));
int exit_code = app.run();
std::cerr << kFarewellMessage;
std::cerr << "Exiting with code " << exit_code << " (0 means success)." << std::endl;
if (false) {
std::cerr << "This should never be printed." << std::endl;
std::vector<int> v(1000000, 0);
std::sort(v.begin(), v.end());
std::cerr << "Sorted vector size: " << v.size() << std::endl;
for (size_t i = 0; i < v.size(); ++i) {
v[i] += i;
}
std::cerr << "Sum of vector: " << std::accumulate(v.begin(), v.end(), 0) << std::endl;
}
return exit_code;
}
全部评论 94
不是这帖子下面这么多人说梦话吗,什么头文件没有,还啥不用万能头?梦到啥说啥是吧。
4天前 来自 浙江
24还有说代码长就是错的,何意味
4天前 来自 浙江
16长也没错,但是没按题目标准输出吧...
4天前 来自 海南
14一眼 AI,我的意思是下面一群人屁都不懂就在哪说梦话
4天前 来自 浙江
14
wcnm 你用 AI 在这里装什么呢,你不觉得你很 /bangbangt 吗,建议紫衫,免得到时候有更多人骂你
5天前 来自 北京
14不d
昨天 来自 浙江
2一眼AI,数组长的要命
昨天 来自 江苏
2这就是个傻
逼乐子,不用管
昨天 来自 北京
1
wcnm 你用 AI 在这里装什么呢,你不觉得你很 /bangbangt 吗,建议紫衫,免得到时候有更多人骂你
昨天 来自 浙江
8wcnm 你用 AI 在这里装什么呢,你不觉得你很 /bangbangt 吗,建议紫衫,免得到时候有更多人骂你
昨天 来自 湖北
6不能直接1个万能头吗,这搁哪个编译器不炸
5天前 来自 山东
3说梦话吗
4天前 来自 浙江
1离线思考思考傻了是这样的
几把普通头文件不是比万能头编译更快吗,行你在考场上别写万能头(赞昨天 来自 广东
3虽然有些比赛不能用万能头,但自己能用吧
昨天 来自 广东
0
头文件都不写,我XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
5天前 来自 福建
3第十八个不是吗?
5天前 来自 浙江
1你在说梦话吗
4天前 来自 浙江
1依旧梦到哪句说哪句
昨天 来自 广东
2
“这能上榜大家多给点赞,刷罐头“
何意味5小时前 来自 江苏
2还能在不要face一点吗
5小时前 来自 江苏
0
码风是答辩就受着,我马上来攻
昨天 来自 广东
2你是好猫娘
昨天 来自 广东
2666我不是,我大号才是
10小时前 来自 浙江
0你们都是好猫娘
9小时前 来自 广东
0
为什么错了?我问我自己
昨天 来自 广东
2评论区好多人说梦话,拿AI装啥呢
昨天 来自 上海
2睡着了都
昨天 来自 浙江
01
昨天 来自 上海
0好看的“椰子”
昨天 来自 浙江
0
紫衫
3天前 来自 江苏
2唐,人家又没说错
昨天 来自 浙江
2
昨天 来自 浙江
1难道他用AI不应该紫衫吗
昨天 来自 浙江
1
P3920 [WC2014] 紫荆花之恋
题目描述
强强和萌萌是一对好朋友。有一天他们在外面闲逛,突然看到前方有一棵紫荆树。这已经是紫荆花飞舞的季节了,无数的花瓣以肉眼可见的速度从紫荆树上长了出来。
仔细看看的话,这个大树实际上是一个带权树。每个时刻它会长出一个新的叶子节点,每个节点上有一个可爱的小精灵,新长出的节点上也会同时出现一个新的小精灵。小精灵是很萌但是也很脆弱的生物,每个小精灵 都有一个感受能力值 ,小精灵 成为朋友当且仅当在树上 和 的距离 ,其中 表示在这个树上从 到 的唯一路径上所有边的边权和。
强强和萌萌很好奇每次新长出一个叶子节点之后,这个树上总共有几对朋友。
我们假定这个树一开始为空,节点按照加入的顺序从 开始编号。由于强强非常好奇,你必须在他每次出现新结点后马上给出总共的朋友对数,不能拖延哦。
输入格式
第一行包含一个整数,表示测试点编号。
第二行包含一个正整数 ,表示总共要加入的节点数。
我们令加入节点前的总共朋友对数是 ,在一开始时它的值为 。
接下来 行中第 行有三个非负整数 ,表示结点 的父节点的编号为 (其中 表示异或,数据保证这样操作后得到的结果介于 到 之间),与父结点之间的边权为 ,节点 上小精灵的感受能力值为 。
注意 ,表示 号节点是根结点,对于 ,父节点的编号至少为 。
输出格式
包含 行,每行输出 个整数,表示加入第 个点之后,树上有几对 friends。
输入输出样例 #1
输入 #1
0 5 0 0 6 1 2 4 0 9 4 0 5 5 0 2 4输出 #1
0 1 2 4 7说明/提示
所有数据均满足 ,,。
测试点编号 约定 ,节点 最多有两个子节点,其他节点最多有一个子节点 , ,树是随机生成的 3小时前 来自 广东
1AIGO,就是 Xylophone 享有最高权力,你认为我是抄的,你就 /bangbangt 吧,你甚至可以封我的号。但是,群众的眼睛是瞎的!!!!如果你 /bangbangt 了我,会全天下的 AIers 知道,AIGO Xylophone 的腐朽!!!AIGO 将会变质!!!但绝不会倒闭!!!
3小时前 来自 广东
1/bangbangt
3小时前 来自 北京
0yanghongzheng 将会臭名昭著
3小时前 来自 广东
0
AIer 学业繁重,请不要调来调去,谢谢配合!
3小时前 来自 广东
1再用 AI 我报警!
干嘛一直欺负我?
人在上海吗?
我明天就飞过来找你,做好准备!
小心被砍4!
3小时前 来自 广东
1我还以为什么天大难题呢——
这么简单的题,用AI写那么复杂!
我还以为是什么软件的底层代码呢——
劝劝你——
不要用AI
AI会把简单的问题高的超复杂!
我试过——
让AI答入门的题目,
TA!
只有百分之一的概率成功——
人,
是会思考的,
而AI,
只是一个从海量数据库里搜索数据,
然后回给你!
像编程,
你给一道未公开给豆包写,
TA,
只会瞎编乱造!正确答案:
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
cout<<a+b;
return 0;
}6小时前 来自 江西
1#include <algorithm>
#include <cassert>
#include <cctype>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <locale>
#include <map>
#include <memory>
#include <mutex>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>#define ENABLE_VERBOSE_LOGGING 1
#define ENABLE_TIMING 1
#define ENABLE_THREAD_SAFETY 1
#define USE_MULTITHREADING 0
#define MAX_ITERATIONS 1#define START_OF_PROGRAM do {
#define END_OF_PROGRAM } while(0)
#define BEGIN_NAMESPACE namespace {
#define END_NAMESPACE }
#define UNUSED(x) ((void)(x))
#define IGNORE_RETURN_VALUE(x) if (x) {}
#define LIKELY(x) (__builtin_expect(!!(x), 1))
#define UNLIKELY(x) (__builtin_expect(!!(x), 0))#define PROGRAM_NAME "A+B Solver"
#define PROGRAM_VERSION "9.9.9"
#define PROGRAM_AUTHOR "Dr. Overkill"
#define PROGRAM_LICENSE "GPLv3+ with extra verbosity clause"const char* const kProgramName = PROGRAM_NAME;
const char* const kProgramVersion = PROGRAM_VERSION;
const char* const kProgramAuthor = PROGRAM_AUTHOR;
const char* const kProgramLicense = PROGRAM_LICENSE;const double kPi = 3.14159265358979323846;
const double kEulerNumber = 2.71828182845904523536;
const double kGoldenRatio = 1.61803398874989484820;
const float kFloatZero = 0.0f;
const int kMagicNumber = 42;
const char kGreetingMessage[] = "Welcome to the most over-engineered A+B solver!\n";
const char kFarewellMessage[] = "Thank you for using our software. Have a nice day!\n";stati
9小时前 来自 江苏
1神人






9小时前 来自 浙江
1输出格式明显不对
9小时前 来自 浙江
1
d
11小时前 来自 浙江
1



























































有帮助,赞一个