結果

問題 No.2046 Ans Mod? Mod Ans!
ユーザー umezoumezo
提出日時 2022-08-27 05:12:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 992 ms / 4,000 ms
コード長 1,664 bytes
コンパイル時間 2,213 ms
コンパイル使用メモリ 205,944 KB
実行使用メモリ 6,160 KB
最終ジャッジ日時 2023-08-22 11:54:49
合計ジャッジ時間 7,485 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 21 ms
4,544 KB
testcase_09 AC 25 ms
4,568 KB
testcase_10 AC 28 ms
4,564 KB
testcase_11 AC 37 ms
4,620 KB
testcase_12 AC 31 ms
4,688 KB
testcase_13 AC 395 ms
5,068 KB
testcase_14 AC 291 ms
5,096 KB
testcase_15 AC 531 ms
5,336 KB
testcase_16 AC 544 ms
5,472 KB
testcase_17 AC 469 ms
5,380 KB
testcase_18 AC 358 ms
4,632 KB
testcase_19 AC 498 ms
5,332 KB
testcase_20 AC 992 ms
6,160 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