結果

問題 No.752 mod数列
ユーザー tsutajtsutaj
提出日時 2019-03-30 15:59:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,362 ms / 2,000 ms
コード長 2,886 bytes
コンパイル時間 616 ms
コンパイル使用メモリ 74,916 KB
実行使用メモリ 42,368 KB
最終ジャッジ日時 2024-04-27 14:53:12
合計ジャッジ時間 10,451 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
42,368 KB
testcase_01 AC 63 ms
42,240 KB
testcase_02 AC 63 ms
42,240 KB
testcase_03 AC 64 ms
42,240 KB
testcase_04 AC 64 ms
42,360 KB
testcase_05 AC 63 ms
42,240 KB
testcase_06 AC 64 ms
42,240 KB
testcase_07 AC 63 ms
42,240 KB
testcase_08 AC 63 ms
42,240 KB
testcase_09 AC 64 ms
42,240 KB
testcase_10 AC 65 ms
42,240 KB
testcase_11 AC 66 ms
42,240 KB
testcase_12 AC 66 ms
42,368 KB
testcase_13 AC 67 ms
42,240 KB
testcase_14 AC 66 ms
42,240 KB
testcase_15 AC 239 ms
42,240 KB
testcase_16 AC 247 ms
42,332 KB
testcase_17 AC 1,362 ms
42,240 KB
testcase_18 AC 267 ms
42,240 KB
testcase_19 AC 269 ms
42,284 KB
testcase_20 AC 271 ms
42,248 KB
testcase_21 AC 284 ms
42,280 KB
testcase_22 AC 275 ms
42,300 KB
testcase_23 AC 286 ms
42,220 KB
testcase_24 AC 273 ms
42,324 KB
testcase_25 AC 130 ms
42,260 KB
testcase_26 AC 722 ms
42,284 KB
testcase_27 AC 433 ms
42,256 KB
testcase_28 AC 641 ms
42,084 KB
testcase_29 AC 561 ms
42,248 KB
testcase_30 AC 274 ms
42,240 KB
testcase_31 AC 63 ms
42,308 KB
testcase_32 AC 62 ms
42,220 KB
testcase_33 AC 65 ms
42,252 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

// F(i+1) := P (mod 1) + P (mod 2) + ... + P (mod i) を計算
// 答えは NumType 型で返ってくる
using lint = long long;
template <typename NumType, int K = 5000000>
struct RemainderSum {
    lint P;
    vector<NumType> small_case;
    RemainderSum(lint P_) {
        P = P_;
        build();
    }

    // 小さいケースに対する答えを覚える
    void build() {
        small_case.resize(K + 1, NumType(0));
        for(lint i=1; i<=K; i++) {
            NumType mod(P % i);
            small_case[i] = small_case[i-1] + mod;
        }
    }

    // [1, x] (閉区間であることに注意!!)
    // F(x) を計算 
    NumType sum_func(lint x) {
        if(x <= K) return small_case[x];
        return small_case[K] + sum_func(K+1, x);
    }

    // [l, r] (閉区間であることに注意!!)
    // P (mod l) + P (mod l+1) + ... + P (mod r) を計算
    NumType sum_func(lint l, lint r) {
        if(r <= K) return small_case[r] - small_case[l-1];
        if(l <= K) return small_case[K] - small_case[l-1] + sum_func(K+1, r);

        NumType ans(0);
        // l も r も、 K を超える値である
        lint dmax = P / l, dmin = P / r, num = l;
        for(lint div=dmax; div>=dmin; div--) {
            if(div == 0) {
                // 商が div になるような要素のインデックス (閉区間)
                lint idx_l = max(l, P + 1);
                lint idx_r = r;
                NumType range(idx_r - idx_l + 1);

                // このときの剰余の値は全て P に等しい
                NumType sum = range * NumType(P);
                ans += sum;
            }
            else {
                // 商が div になるような要素のインデックス (閉区間)
                lint idx_l = max(l, P / (div + 1) + 1);
                lint idx_r = min(r, P / div);
                NumType range(idx_r - idx_l + 1);
                
                // 左端と右端について、その剰余の値
                NumType mod_l(P % idx_l);
                NumType mod_r(P % idx_r);
                
                // 等差数列の和
                NumType sum = range * (mod_l + mod_r) / NumType(2);
                ans += sum;
                
                // 次の div を求める (1 ずつ減らない場合がある)
                if(P / num != P / (num + 1)) {
                    div = P / (num + 1) + 1;
                    num++;
                }
            }
        }
        return ans;
    }
};

void ECR005_E() {

}

void yuki_752() {
    lint P, Q; cin >> P >> Q;
    RemainderSum<long long int> rs(P);
    while(Q--) {
        lint l, r; cin >> l >> r;
        cout << rs.sum_func(l, r) << endl;
    }
}

int main() {
    // ECR005_E();
    yuki_752();
    return 0;
}
0