// https://atcoder.jp/contests/adt_easy_20240807_1/tasks/abc237_a #include #include using namespace std; // #include // using namespace atcoder; // ==================== // デバッグユーティリティ // ==================== #ifdef DEBUG #define COLOR_RESET "\033[0m" #define COLOR_YELLOW "\033[33m" #define COLOR_CYAN "\033[36m" #define COLOR_MAGENTA "\033[35m" #define COLOR_BLACK "\033[30m" #define COLOR_RED "\033[31m" #define COLOR_GREEN "\033[32m" #define COLOR_BLUE "\033[34m" #define COLOR_WHITE "\033[37m" // ==================== // 型情報出力 // ==================== // 型情報出力(AtCoder用) template string format_int_with_exp(T x); string format_float_with_exp(long double x); template void debug_typeinfo(const string& name) { cout << COLOR_CYAN << "Type info for " << name << " ----------" << COLOR_RESET << "\n"; cout << COLOR_YELLOW << " sizeof : " << COLOR_WHITE << sizeof(T) << " bytes\n"; if constexpr (std::numeric_limits::is_specialized) { if constexpr (std::is_integral_v) { cout << COLOR_YELLOW << " min : " << COLOR_GREEN << format_int_with_exp(numeric_limits::min()) << COLOR_RESET << "\n"; cout << COLOR_YELLOW << " max : " << COLOR_GREEN << format_int_with_exp(numeric_limits::max()) << COLOR_RESET << "\n"; } else if constexpr (std::is_floating_point_v) { cout << COLOR_YELLOW << " min : " << COLOR_GREEN << format_float_with_exp(numeric_limits::min()) << COLOR_RESET << "\n"; cout << COLOR_YELLOW << " max : " << COLOR_GREEN << format_float_with_exp(numeric_limits::max()) << COLOR_RESET << "\n"; } } else { cout << " min : (not available)\n"; cout << " max : (not available)\n"; } cout << "----------------------------------\n"; } // よく使う型リスト void debug_type_list() { debug_typeinfo("int"); debug_typeinfo("long long"); debug_typeinfo("unsigned long long"); debug_typeinfo<__int128>("__int128"); debug_typeinfo("unsigned __int128"); debug_typeinfo("float"); debug_typeinfo("double"); debug_typeinfo("long double"); debug_typeinfo("char"); debug_typeinfo("bool"); debug_typeinfo("string"); } string trim(const string& s) { size_t first = s.find_first_not_of(" \t"); size_t last = s.find_last_not_of(" \t"); if (first == string::npos) return ""; return s.substr(first, (last - first + 1)); } // pair template ostream& operator<<(ostream& os, const pair& p) { return os << "(" << p.first << ", " << p.second << ")"; } // tuple template void tuple_out_impl(ostream& os, const Tuple& t, index_sequence) { ((os << (Is == 0 ? "" : ", ") << get(t)), ...); } template ostream& operator<<(ostream& os, const tuple& t) { os << "("; tuple_out_impl(os, t, index_sequence_for{}); os << ")"; return os; } // vector template ostream& operator<<(ostream& os, const vector& v) { os << "[ "; for (size_t i = 0; i < v.size(); i++) { os << v[i]; if (i + 1 < v.size()) os << ", "; } os << " ]"; return os; } // set template ostream& operator<<(ostream& os, const set& s) { os << "{ "; size_t i = 0; for (auto& x : s) { os << x; if (++i < s.size()) os << ", "; } os << " }"; return os; } template ostream& operator<<(ostream& os, const unordered_set& s) { os << "{ "; size_t i = 0; for (auto& x : s) { os << x; if (++i < s.size()) os << ", "; } os << " }"; return os; } // map template ostream& operator<<(ostream& os, const map& m) { os << "{ "; size_t i = 0; for (auto& [k,v] : m) { os << k << ": " << v; if (++i < m.size()) os << ", "; } os << " }"; return os; } template ostream& operator<<(ostream& os, const unordered_map& m) { os << "{ "; size_t i = 0; for (auto& [k,v] : m) { os << k << ": " << v; if (++i < m.size()) os << ", "; } os << " }"; return os; } // 通常変数 template void debug_dispatch(const char* name, const T& x) { cerr << COLOR_YELLOW << name << COLOR_WHITE << " : " << x << COLOR_RESET << "\n"; } // 複数変数 template void debug_single(const char* name, const T& x) { debug_dispatch(name, x); } template void debug_multi(const char* names, Args&&... args) { stringstream ss(names); string name; ((getline(ss, name, ','), debug_single(trim(name).c_str(), args)), ...); } // メインマクロ #define debug(...) \ do { \ cerr << COLOR_CYAN << "debug ----------<< start" << COLOR_RESET << "\n"; \ debug_multi(#__VA_ARGS__, __VA_ARGS__); \ } while (0) // queue #define debug_queue(q) \ do { \ auto temp = q; int idx = 0; \ cerr << COLOR_MAGENTA << #q << " ----------" << COLOR_RESET << "\n"; \ while (!temp.empty()) { \ cerr << COLOR_CYAN << idx++ << COLOR_WHITE << " : " \ << temp.front() << COLOR_RESET << "\n"; \ temp.pop(); \ } \ } while (0) // stack #define debug_stack(s) \ do { \ auto temp = s; int idx = 0; \ cerr << COLOR_MAGENTA << #s << " ----------" << COLOR_RESET << "\n"; \ while (!temp.empty()) { \ cerr << COLOR_CYAN << idx++ << COLOR_WHITE << " : " \ << temp.top() << COLOR_RESET << "\n"; \ temp.pop(); \ } \ } while (0) // priority_queue #define debug_pq(pq) \ do { \ auto temp = pq; int idx = 0; \ cerr << COLOR_MAGENTA << #pq << " ----------" << COLOR_RESET << "\n"; \ while (!temp.empty()) { \ cerr << COLOR_CYAN << idx++ << COLOR_WHITE << " : " \ << temp.top() << COLOR_RESET << "\n"; \ temp.pop(); \ } \ } while (0) #else #define debug(...) #define debug_queue(q) #define debug_stack(s) #define debug_pq(pq) #endif // 整数をカンマ区切りに string format_with_commas(unsigned __int128 x) { if (x == 0) return "0"; string s; while (x > 0) { s.push_back('0' + (x % 10)); x /= 10; } reverse(s.begin(), s.end()); string out; int n = s.size(), cnt = 0; for (int i = n-1; i >= 0; --i) { out.push_back(s[i]); if (++cnt % 3 == 0 && i != 0) out.push_back(','); } reverse(out.begin(), out.end()); return out; } // 整数型 min/max をカンマ区切り+10^n 表記 プロトタイプ宣言★ template string format_int_with_exp(T x) { using U = unsigned __int128; // --- bool 特別処理 --- if constexpr (std::is_same_v) { return string(x ? "1 (true)" : "0 (false)"); } // --- 符号あり整数 --- else if constexpr (std::is_signed_v) { // __int128 の最小値対策 if (x == std::numeric_limits::min()) { // 最小値は直接文字列化 string base = "-" + format_with_commas((U)( (unsigned __int128)std::numeric_limits::max() ) + 1); int exp = base.size() - 2; // "-"があるので-1補正 // 先頭3桁だけ科学的表記 string digits = base.substr(1, 3); // "-"抜き ostringstream oss; oss << base << " (≈ " << digits[0] << "." << digits.substr(1) << " × 10^" << exp << ")"; return oss.str(); } bool neg = (x < 0); U val = neg ? U(-( __int128)x) : U(x); string base = format_with_commas(val); int exp = base.size() - 1; // 先頭3桁を抽出して科学的表記 string digits = base.substr(0, min(3, (int)base.size())); ostringstream oss; oss << base << " (≈ " << digits[0] << "." << digits.substr(1) << " × 10^" << exp << ")"; return (neg ? "-" : "") + oss.str(); } // --- 符号なし整数 --- else { U val = U(x); string base = format_with_commas(val); int exp = base.size() - 1; string digits = base.substr(0, min(3, (int)base.size())); ostringstream oss; oss << base << " (≈ " << digits[0] << "." << digits.substr(1) << " × 10^" << exp << ")"; return oss.str(); } } // 浮動小数型 → 科学的記法 プロトタイプ宣言★ string format_float_with_exp(long double x) { if (x == 0.0L) return "0"; int exp = floor(log10(fabsl(x))); long double base = x / powl(10.0L, exp); ostringstream oss; oss << fixed << setprecision(5) << base; return oss.str() + " × 10^" + to_string(exp); } // // 型情報出力 // #include // template // void debug_typeinfo(const string& name) { // using boost::typeindex::type_id_with_cvr; // cout << COLOR_CYAN << "Type info for " << name << " ----------" << COLOR_RESET << "\n"; // // cout << " type : " << type_id_with_cvr().pretty_name() << "\n"; // // cout << " sizeof : " << sizeof(T) << " bytes\n"; // cout << COLOR_YELLOW << " type : " << COLOR_WHITE << type_id_with_cvr().pretty_name() << "\n"; // cout << COLOR_YELLOW << " sizeof : " << COLOR_WHITE << sizeof(T) << " bytes\n"; // if constexpr (std::numeric_limits::is_specialized) { // if constexpr (std::is_integral_v) { // cout << COLOR_YELLOW << " min : " << COLOR_GREEN << format_int_with_exp(numeric_limits::min()) << COLOR_RESET << "\n"; // cout << COLOR_YELLOW << " max : " << COLOR_GREEN << format_int_with_exp(numeric_limits::max()) << COLOR_RESET << "\n"; // // cout << " min : " << format_int_with_exp(numeric_limits::min()) << "\n"; // // cout << " max : " << format_int_with_exp(numeric_limits::max()) << "\n"; // } else if constexpr (std::is_floating_point_v) { // cout << COLOR_YELLOW << " min : " << COLOR_GREEN << format_float_with_exp(numeric_limits::min()) << COLOR_RESET << "\n"; // cout << COLOR_YELLOW << " max : " << COLOR_GREEN << format_float_with_exp(numeric_limits::max()) << COLOR_RESET << "\n"; // // cout << " min : " << format_float_with_exp(numeric_limits::min()) << "\n"; // // cout << " max : " << format_float_with_exp(numeric_limits::max()) << "\n"; // } // } else { // cout << " min : (not available)\n"; // cout << " max : (not available)\n"; // } // cout << "----------------------------------\n"; // } using ll = long long; [[maybe_unused]] const int INF = 1e9; // 非常に大きい値(= 到達不能な値) [[maybe_unused]] const long long LINF = 1e18; // long long 用の無限 [[maybe_unused]] const int NINF = -INF; // 非常に小さい値(最小化の比較など) // #include // const int INF = std::numeric_limits::max()/2; // +∞ 代用 // const int NEG = std::numeric_limits::min()/2; // −∞ 代用 // const ll LINF = std::numeric_limits::max()/4; // 64bit 版 // /2 /4 しておくと 加算してもオーバーフローしない // https://atcoder.jp/contests/tessoku-book/tasks/tessoku_book_ai // ==================== // main 部分 // ==================== int main() { ios::sync_with_stdio(false); cin.tie(nullptr); long long X, Y, N; cin >> X >> Y >> N; for(int i = 0; i < N; ++i){ long long U, V; cin >> U >> V; // どの腕にいるのか long long uA = 0; long long vA = 0; if(U != 0)uA = (U-1) / Y; if(V != 0)vA = (V-1) / Y; // 頂点0 までの距離 long long uB = U - (Y * uA); long long vB = V - (Y * vA); // // 頂点0 から 通る辺の数 // long long xU = U - uB; // long long xV = V - vB; debug(uA, vA, uB, vB); if(uA == vA){ // 同じ腕なら // cout << xV - xU << endl; cout << vB - uB << endl; }else{ // 違う腕なら // cout << xV + xU << endl; cout << vB + uB << endl; } } // --- 型情報の確認 --- // debug_type_list(); // debug_typeinfo("int"); return 0; } // // --- 基本型のデバッグ --- // debug(x, y, d, s); // int, long long, double, string // // --- コンテナのデバッグ --- // debug(v, st, mp); // vector, set, map // debug_queue(q); // debug_stack(stc); // debug_pq(pq); // // --- 型情報の確認 --- // debug_typeinfo("int"); // debug_typeinfo("double"); // debug_type_list(); /* ---------------------------------------------- x が増えれば y も増えるとき,その関数は単調増加と言う 広義単調増加 x1 < x2 ならば f(x1) <= f(x2) 狭義単調増加 x1 < x2 ならば f(x1) < f(x2) */ /* 区間表記 --------------------------------------- [l, r] l 以上 r 以下(両端を含む) l <= x && x <= r [l, r) l 以上 r 未満(左端含む、右端含まない) l <= x && x < r (l, r] l より大きい r 以下(左端含まない、右端含む) l < x && x <= r (l, r) l より大きい r 未満(両端を含まない) l < x && x < r */ //dbg- //ord- // ./a << in1.txt