結果

問題 No.752 mod数列
ユーザー tsutajtsutaj
提出日時 2019-03-30 15:56:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,340 bytes
コンパイル時間 586 ms
コンパイル使用メモリ 73,936 KB
実行使用メモリ 18,072 KB
最終ジャッジ日時 2024-04-27 14:42:58
合計ジャッジ時間 7,733 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
18,072 KB
testcase_01 AC 14 ms
11,016 KB
testcase_02 AC 14 ms
11,004 KB
testcase_03 AC 16 ms
10,988 KB
testcase_04 AC 14 ms
11,060 KB
testcase_05 AC 14 ms
11,104 KB
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 AC 187 ms
11,084 KB
testcase_16 AC 198 ms
11,016 KB
testcase_17 TLE -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
権限があれば一括ダウンロードができます

ソースコード

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 = 1000000>
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--) {
            // 商が 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