結果

問題 No.2249 GCDistance
ユーザー milanis48663220milanis48663220
提出日時 2023-03-17 22:05:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,759 ms / 5,000 ms
コード長 2,049 bytes
コンパイル時間 1,047 ms
コンパイル使用メモリ 116,296 KB
実行使用メモリ 130,624 KB
最終ジャッジ日時 2023-10-18 14:53:32
合計ジャッジ時間 22,978 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,464 ms
130,620 KB
testcase_01 AC 1,754 ms
130,624 KB
testcase_02 AC 1,742 ms
130,624 KB
testcase_03 AC 1,752 ms
130,624 KB
testcase_04 AC 1,574 ms
130,624 KB
testcase_05 AC 1,759 ms
130,624 KB
testcase_06 AC 1,743 ms
130,624 KB
testcase_07 AC 1,717 ms
130,624 KB
testcase_08 AC 1,427 ms
130,624 KB
testcase_09 AC 1,752 ms
130,624 KB
testcase_10 AC 1,711 ms
130,624 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;
}

const int N = 10000000;

int prime_fac[N+1];
bool is_prime[N+1];
ll ans[N+1];

void init(){
    for(int p = 2; p <= N; p++) is_prime[p] = true;
    for(int p = 2; p <= N; p++) {
        if(!is_prime[p]) continue;
        prime_fac[p] = p;
        for(int i = 2; i*p <= N; i++){
            prime_fac[i*p] = p;
            is_prime[i*p] = false;
        }
    }
    ans[1] = 0;
    for(int x = 2; x <= N; x++){
        int y = x;
        int tot = y;
        while(y > 1){
            int p = prime_fac[y];
            tot /= p;
            tot *= p-1;
            while(y%p == 0) y /= p;
        }
        ans[x] = ans[x-1]+(x-tot-1)+x-1;
    }
}

void solve(){
    ll n; cin >> n;
    cout << ans[n] << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    init();
    int t; cin >> t;
    while(t--) solve();
}
0