結果

問題 No.2032 Let's Write Multiples!!
ユーザー 👑 emthrmemthrm
提出日時 2022-08-06 17:47:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 643 ms / 3,000 ms
コード長 2,302 bytes
コンパイル時間 2,332 ms
コンパイル使用メモリ 200,140 KB
実行使用メモリ 4,356 KB
最終ジャッジ日時 2023-10-14 18:20:29
合計ジャッジ時間 13,674 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 148 ms
4,352 KB
testcase_02 AC 108 ms
4,352 KB
testcase_03 AC 99 ms
4,348 KB
testcase_04 AC 183 ms
4,348 KB
testcase_05 AC 641 ms
4,348 KB
testcase_06 AC 549 ms
4,348 KB
testcase_07 AC 129 ms
4,348 KB
testcase_08 AC 333 ms
4,348 KB
testcase_09 AC 619 ms
4,356 KB
testcase_10 AC 351 ms
4,348 KB
testcase_11 AC 347 ms
4,348 KB
testcase_12 AC 332 ms
4,348 KB
testcase_13 AC 330 ms
4,352 KB
testcase_14 AC 288 ms
4,352 KB
testcase_15 AC 182 ms
4,348 KB
testcase_16 AC 182 ms
4,348 KB
testcase_17 AC 184 ms
4,352 KB
testcase_18 AC 643 ms
4,348 KB
testcase_19 AC 257 ms
4,352 KB
testcase_20 AC 141 ms
4,352 KB
testcase_21 AC 123 ms
4,352 KB
testcase_22 AC 123 ms
4,348 KB
testcase_23 AC 125 ms
4,348 KB
testcase_24 AC 123 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

long long floor_sum(long long a, long long b, long long m, long long n) {
  long long res = 0;
  if (a < 0) {
    long long nxt_a = a % m;
    if (nxt_a < 0) nxt_a += m;
    res -= n * (n - 1) / 2 * ((nxt_a - a) / m);
    a = nxt_a;
  }
  if (b < 0) {
    long long nxt_b = b % m;
    if (nxt_b < 0) nxt_b += m;
    res -= n * ((nxt_b - b) / m);
    b = nxt_b;
  }
  while (true) {
    if (a >= m) {
      res += n * (n - 1) / 2 * (a / m);
      a %= m;
    }
    if (b >= m) {
      res += n * (b / m);
      b %= m;
    }
    const long long y_max = a * n + b;
    if (y_max < m) break;
    b = y_max % m;
    n = y_max / m;
    std::swap(a, m);
  }
  return res;
}

ll p10(int ex) {
  ll res = 1;
  for (; ex > 0; --ex) res *= 10;
  return res;
}

// |{i \in {1, 2, ..., N} | x <= iK mod D}|
int g(const int n, const ll x, const int k, const ll d) {
  return n - (floor_sum(k, k, d, n) - floor_sum(k, k - x, d, n));
}

constexpr int D = 9;
int f(const int r, const int k, const int c) {
  const int n = r / k;
  if (n <= 0) return 0;
  int ans = 0;
  REP(d, D) {
    // c * 10^d <= ik mod 10^(d + 1) < (c + 1) * 10^d
    ans += g(n, c * p10(d), k, p10(d + 1)) - g(n, (c + 1) * p10(d), k, p10(d + 1));
  }
  return ans;
}

void solve() {
  int l, r, k, c; cin >> l >> r >> k >> c;
  cout << f(r, k, c) - f(l - 1, k, c) << '\n';
}

int main() {
  int t; cin >> t;
  while (t--) solve();
  return 0;
}
0