結果

問題 No.2046 Ans Mod? Mod Ans!
ユーザー milanis48663220milanis48663220
提出日時 2022-08-19 22:06:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 42 ms / 4,000 ms
コード長 2,456 bytes
コンパイル時間 1,308 ms
コンパイル使用メモリ 120,428 KB
実行使用メモリ 15,372 KB
最終ジャッジ日時 2023-08-22 11:41:08
合計ジャッジ時間 2,905 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
13,952 KB
testcase_01 AC 20 ms
13,808 KB
testcase_02 AC 20 ms
13,800 KB
testcase_03 AC 20 ms
13,996 KB
testcase_04 AC 20 ms
13,908 KB
testcase_05 AC 20 ms
13,812 KB
testcase_06 AC 19 ms
13,868 KB
testcase_07 AC 20 ms
14,076 KB
testcase_08 AC 19 ms
14,024 KB
testcase_09 AC 20 ms
13,956 KB
testcase_10 AC 20 ms
13,900 KB
testcase_11 AC 20 ms
13,860 KB
testcase_12 AC 19 ms
13,912 KB
testcase_13 AC 28 ms
14,572 KB
testcase_14 AC 25 ms
14,268 KB
testcase_15 AC 30 ms
14,884 KB
testcase_16 AC 31 ms
14,856 KB
testcase_17 AC 29 ms
14,628 KB
testcase_18 AC 29 ms
14,644 KB
testcase_19 AC 30 ms
14,784 KB
testcase_20 AC 42 ms
15,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

template<typename T>
class Cumsum{
    public:
    Cumsum(vector<T> v): v(v){
        n = v.size();
        v_cumsum = vector<T>(n+1, T(0));
        for(int i = 0; i < n; i++) v_cumsum[i+1] = v_cumsum[i]+v[i];
    }
    /**
     * v[l] + ... + v[r-1]
     */ 
    T sum(int l, int r){
        if(r <= l) return T(0);
        if(r > n) r = n;
        if(l < 0) l = 0;
        return v_cumsum[r]-v_cumsum[l];
    }
    private:
    int n;
    vector<T> v;
    vector<T> v_cumsum;
};

const int N = 200000;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n; cin >> n;
    vector<ll> a(n);
    vector<ll> cnt(N+1);
    vector<ll> sum(N+1);
    for(int i = 0; i < n; i++) {
        cin >> a[i];
        cnt[a[i]]++;
        sum[a[i]] += a[i];
    }
    auto cumsum = Cumsum<ll>(cnt);
    auto cumsum_all = Cumsum<ll>(sum);
    ll ans = 0;
    for(ll x = 1; x <= N; x++){
        ll cnt_all = cumsum.sum(x+1, N+1);
        ll sum_all = cumsum_all.sum(x+1, N+1);
        ans += cnt[x]*(cnt_all*x - sum_all);
        for(ll i = 1; i*x <= N; i++){
            int l = i*x, r = (i+1)*x;
            if(i == 1) l++;
            ll c = cumsum.sum(l, r);
            ans += cnt[x]*x*i*c;
        }
    }
    cout << ans << endl;
}
0