結果

問題 No.2032 Let's Write Multiples!!
ユーザー t98slidert98slider
提出日時 2022-12-25 18:43:09
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 642 ms / 3,000 ms
コード長 1,368 bytes
コンパイル時間 1,583 ms
コンパイル使用メモリ 163,720 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-12 09:04:12
合計ジャッジ時間 11,144 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 154 ms
4,376 KB
testcase_02 AC 102 ms
4,380 KB
testcase_03 AC 74 ms
4,380 KB
testcase_04 AC 232 ms
4,380 KB
testcase_05 AC 641 ms
4,380 KB
testcase_06 AC 533 ms
4,380 KB
testcase_07 AC 184 ms
4,376 KB
testcase_08 AC 367 ms
4,380 KB
testcase_09 AC 642 ms
4,376 KB
testcase_10 AC 384 ms
4,376 KB
testcase_11 AC 377 ms
4,376 KB
testcase_12 AC 401 ms
4,380 KB
testcase_13 AC 403 ms
4,376 KB
testcase_14 AC 347 ms
4,376 KB
testcase_15 AC 229 ms
4,380 KB
testcase_16 AC 230 ms
4,376 KB
testcase_17 AC 232 ms
4,376 KB
testcase_18 AC 642 ms
4,380 KB
testcase_19 AC 227 ms
4,380 KB
testcase_20 AC 104 ms
4,376 KB
testcase_21 AC 144 ms
4,380 KB
testcase_22 AC 143 ms
4,376 KB
testcase_23 AC 143 ms
4,380 KB
testcase_24 AC 144 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

long long floor_sum(long long n, long long m, long long a, long long b) {
    /*if(b < 0){
        ll up = (abs(b) + a - 1) / a;
        if(n < up)return 0;
        return floor_sum(n - up, m, a, b + up * a);
    }*/
    long long ans = 0;
    if (a >= m) {
        ans += (n - 1) * n * (a / m) / 2;
        a %= m;
    }
    if (b >= m) {
        ans += n * (b / m);
        b %= m;
    }

    long long y_max = (a * n + b) / m, x_max = (y_max * m - b);
    if (y_max == 0) return ans;
    ans += (n - (x_max + a - 1) / a) * y_max;
    ans += floor_sum(y_max, a, m, (a - x_max % a) % a);
    return ans;
}

//0 ~ nの範囲でc * 10 ^ e <= i * k < (c + 1) * 10 ^ e となる i の個数を求める
ll f(ll n, ll k, ll c){
    ll d = 1, ans = 0;
    for(int i = 0; i < 9; i++){
        ll x = c * d;
        ll y = (c + 1) * d;
        d *= 10;
        ans += floor_sum(n, d, k, d - x) - floor_sum(n, d, k, d - y);
        //両方にdが加算されているため影響しない
    }
    return ans;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin >> T;
    while(T--){
        ll L, R, K, C, ans = 0, l, r, d = 1;
        cin >> L >> R >> K >> C;
        ans += f(R / K + 1, K, C);
        ans -= f((L - 1) / K + 1, K, C);
        cout << ans << '\n';
    }
}
0