結果

問題 No.1262 グラフを作ろう!
ユーザー fine
提出日時 2020-10-16 23:53:57
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 318 ms / 3,000 ms
コード長 1,462 bytes
コンパイル時間 1,356 ms
コンパイル使用メモリ 165,900 KB
実行使用メモリ 19,200 KB
最終ジャッジ日時 2024-07-21 01:05:44
合計ジャッジ時間 23,805 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 96
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

constexpr char newl = '\n';
  
#define MAX 1000001 
  
// phi[i] stores euler totient function for i 
// result[j] stores result for value j 
long long phi[MAX], result[MAX]; 
  
// Precomputation of phi[] numbers. Refer below link 
// for details : https://goo.gl/LUqdtY 
void computeTotient() 
{ 
    // Refer https://goo.gl/LUqdtY 
    phi[1] = 1; 
    for (int i=2; i<MAX; i++) 
    { 
        if (!phi[i]) 
        { 
            phi[i] = i-1; 
            for (int j = (i<<1); j<MAX; j+=i) 
            { 
                if (!phi[j]) 
                    phi[j] = j; 
  
                phi[j] = (phi[j]/i)*(i-1); 
            } 
        } 
    } 
}

// Precomputes result for all numbers till MAX 
void sumOfGcdPairs() 
{ 
    // Precompute all phi value 
    computeTotient(); 
  
    for (int i=1; i<MAX; ++i) 
    { 
        // Iterate throght all the divisors 
        // of i. 
        for (int j=2; i*j<MAX; ++j) 
            result[i*j] += i*phi[j]; 
    } 
  
    // Add summation of previous calculated sum 
    for (int i=2; i<MAX; i++) 
        result[i] += result[i-1]; 
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int n, m;
    cin >> n >> m;

    sumOfGcdPairs();

    ll ans = 0;
    for (int i = 0; i < m; i++) {
        int a;
        cin >> a;
        ans += result[a] - result[a - 1];
    }
    cout << ans << newl;

    return 0;
}
0