#line 1 "..\\Main.cpp" #include #include #include #include #include #line 2 "D:\\Programming\\VSCode\\competitive-cpp\\nachia\\math\\gcd.hpp" #line 4 "D:\\Programming\\VSCode\\competitive-cpp\\nachia\\math\\gcd.hpp" namespace nachia{ template Int Gcd(Int a, Int b){ if(a < 0) a = -a; if(b < 0) b = -b; if(!a || !b) return a + b; while(b){ a %= b; std::swap(a, b); } return a; } } #line 2 "D:\\Programming\\VSCode\\competitive-cpp\\nachia\\math-modulo\\dynamic-mod-supply.hpp" #include #line 2 "D:\\Programming\\VSCode\\competitive-cpp\\nachia\\math\\ext-gcd.hpp" #include #line 6 "D:\\Programming\\VSCode\\competitive-cpp\\nachia\\math\\ext-gcd.hpp" namespace nachia{ // ax + by = gcd(a,b) // return ( x, - ) std::pair ExtGcd(long long a, long long b){ long long x = 1, y = 0; while(b){ long long u = a / b; std::swap(a-=b*u, b); std::swap(x-=y*u, y); } return std::make_pair(x, a); } } // namespace nachia #line 4 "D:\\Programming\\VSCode\\competitive-cpp\\nachia\\math-modulo\\dynamic-mod-supply.hpp" namespace nachia{ class DynamicModSupplier{ using u64 = unsigned long long; using u128 = unsigned __int128; using Int = unsigned int; private: u64 imod; Int mod; // atcoder library u64 reduce2(u64 z) const noexcept { // atcoder library #ifdef _MSC_VER u64 x; _umul128(z, im, &x); #else u64 x = (u64)(((u128)(z)*imod) >> 64); #endif return z - x * mod; } Int reduce(u64 z) const noexcept { Int v = reduce2(z); if(mod <= v) v += mod; return v; } public: DynamicModSupplier(unsigned int MOD = 998244353) : mod(MOD) { assert(2 <= MOD); assert(MOD < (1u << 31)); imod = (u64)(-1) / mod + 1; } Int add(Int a, Int b) const { a += b; if(a >= mod){ a -= mod; } return a; } Int sub(Int a, Int b) const { a -= b; if(a >= mod){ a += mod; } return a; } Int mul(Int a, Int b) const { return reduce((u64)a * b); } Int muladd(Int a, Int b, Int c) const { return reduce((u64)a * b + c); } Int inv(Int a) const { Int v = ExtGcd(a, mod).first; return (v < mod) ? v : (v + mod); } Int pow(Int a, u64 i) const { Int r = a, ans = 1; while(i){ if(i & 1) ans = mul(ans, r); i /= 2; r = mul(r, r); } return ans; } Int getMod() const { return mod; } }; } // namespace nachia #line 4 "D:\\Programming\\VSCode\\competitive-cpp\\nachia\\math-modulo\\garner.hpp" namespace nachia{ template struct GarnerMod{ using Int = unsigned int; using IntLong = unsigned long long; std::vector mods; std::vector dynmods; std::vector> table_coeff; std::vector table_coeffinv; void precalc(std::vector new_mods){ mods = std::move(new_mods); dynmods.resize(mods.size()); for(size_t i=0; i(nmods, 1)); for(int j=0; j& x){ int nmods = mods.size(); std::vector table_const(nmods); FinishType res = 0; FinishType res_coeff = 1; for(int j=0; j calc(std::vector> x){ int n = x[0].size(), m = x.size(); std::vector res(n); std::vector buf(m); for(int i=0; i; i64 solve(i64 M, i64 A, i64 B, i64 K){ if(K > A) return 0; { i64 g = nachia::Gcd(A, B); if(g != 1){ if(K % g != 0) return 0; return solve(M/g, A/g, B/g, K/g); } } if(K == A){ M = M / A * A; i64 cntA = M / A; i64 cntB = M / B; i64 cntAB = M / (A*B); return cntA - 1 - cntB + cntAB; } auto garner = nachia::GarnerMod(); garner.precalc({ (u32)A, (u32)B }); i64 AB = A * B; i64 t1 = garner.calc({ (u32)0, (u32)K }); i64 t2 = garner.calc({ (u32)K, (u32)0 }); i64 ans = (M + AB - t1) / AB + (M + AB - t2) / AB; return ans; } int main(){ int T; cin >> T; rep(t,T){ i64 M, A, B, K; cin >> M >> A >> B >> K; cout << solve(M, A, B, K) << '\n'; } return 0; } struct ios_do_not_sync{ ios_do_not_sync(){ ios::sync_with_stdio(false); cin.tie(nullptr); } } ios_do_not_sync_instance;