結果

問題 No.2046 Ans Mod? Mod Ans!
ユーザー umezoumezo
提出日時 2022-08-27 05:12:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 930 ms / 4,000 ms
コード長 1,664 bytes
コンパイル時間 2,062 ms
コンパイル使用メモリ 208,100 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-05-09 17:36:25
合計ジャッジ時間 6,752 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 3 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 20 ms
6,940 KB
testcase_09 AC 23 ms
6,940 KB
testcase_10 AC 26 ms
6,944 KB
testcase_11 AC 35 ms
6,940 KB
testcase_12 AC 29 ms
6,944 KB
testcase_13 AC 369 ms
6,944 KB
testcase_14 AC 267 ms
6,940 KB
testcase_15 AC 495 ms
6,940 KB
testcase_16 AC 505 ms
6,940 KB
testcase_17 AC 433 ms
6,940 KB
testcase_18 AC 329 ms
6,944 KB
testcase_19 AC 461 ms
6,948 KB
testcase_20 AC 930 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
 
#include<bits/stdc++.h>
using namespace std;

template<typename T>
struct BIT{
private:
  vector<T> A;
  const int n;
  
public:
  BIT(int _n) : A(_n+1,0), n(_n){}
  
  T sum(int i){
    T s=0;
    while(i>0){
      s+=A[i];
      i-=i&-i;
    }
    return s;
  }
  
  T sum(int i,int j){
    return sum(j)-sum(i-1);
  }

  void add(int i,T x){
    while(i<=n){
      A[i]+=x;
      i+=i&-i;
    }
  }

  void update(int i,T x){
    T tmp=sum(i,i);
    if(tmp!=x) add(i,x-tmp);
  }
};

ll sqr(ll x){
  ll ok=0,ng=x+1;
  while(ng-ok>1){
    ll mid=(ok+ng)/2;
    if(mid*mid<=x) ok=mid;
    else ng=mid;
  }
  return ok;
}

ll solve_floordiv(vector<ll> &A){
  int N=A.size();
  ll maxA=*max_element(ALL(A));
  ll sqrmaxA=sqr(maxA);
  vector<ll> L(sqrmaxA+1);
  BIT<ll> bit(maxA+1);
  ll ans=0;
  
  rep(i,N){
    if(A[i]<sqrmaxA) ans+=L[A[i]]; 
    else{
      for(int j=1;j*A[i]<=maxA;j++){
        ans+=A[i]*j*bit.sum(j*A[i],min(j*A[i]+A[i]-1,maxA));
      }
    }
    bit.add(A[i],1);
    for(int j=1;j<sqrmaxA;j++) L[j]+=A[i]/j*j; 
  }
  
  return ans; 
}

ll solve_x(vector<ll> &A){
  int N=A.size();
  ll ans=0;
  rep(i,N) ans+=(N-1-i)*A[i];
  return ans;
}

ll solve_y(vector<ll> &A){
  int N=A.size();
  ll ans=0;
  rep(i,N) ans+=i*A[i];
  return ans;
}

ll solve_mod(vector<ll> &A){
  return solve_x(A)-solve_floordiv(A);
}
 
int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  int n;
  cin>>n;
  vector<ll> A(n);
  rep(i,n) cin>>A[i];
  
  sort(ALL(A));
  reverse(ALL(A));
  
  cout<<solve_y(A)-solve_mod(A)<<endl;
    
  return 0;
}
0