結果

問題 No.2552 Not Coprime, Not Divisor
ユーザー InTheBloomInTheBloom
提出日時 2023-11-20 20:40:56
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 395 ms / 2,000 ms
コード長 1,412 bytes
コンパイル時間 1,598 ms
コンパイル使用メモリ 160,996 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2023-11-20 20:41:03
合計ジャッジ時間 6,592 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 146 ms
6,676 KB
testcase_01 AC 291 ms
6,676 KB
testcase_02 AC 120 ms
6,676 KB
testcase_03 AC 289 ms
6,676 KB
testcase_04 AC 290 ms
6,676 KB
testcase_05 AC 41 ms
6,676 KB
testcase_06 AC 317 ms
6,676 KB
testcase_07 AC 363 ms
6,676 KB
testcase_08 AC 233 ms
6,676 KB
testcase_09 AC 395 ms
6,676 KB
testcase_10 AC 105 ms
6,676 KB
testcase_11 AC 200 ms
6,676 KB
testcase_12 AC 373 ms
6,676 KB
testcase_13 AC 133 ms
6,676 KB
testcase_14 AC 82 ms
6,676 KB
testcase_15 AC 1 ms
6,676 KB
testcase_16 AC 1 ms
6,676 KB
testcase_17 AC 69 ms
6,676 KB
testcase_18 AC 2 ms
6,676 KB
testcase_19 AC 1 ms
6,676 KB
testcase_20 AC 1 ms
6,676 KB
testcase_21 AC 1 ms
6,676 KB
testcase_22 AC 1 ms
6,676 KB
testcase_23 AC 1 ms
6,676 KB
testcase_24 AC 1 ms
6,676 KB
testcase_25 AC 1 ms
6,676 KB
testcase_26 AC 1 ms
6,676 KB
testcase_27 AC 1 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

void main () {
    int N = readln.chomp.to!int;
    solve(N);
}

void solve (int N) {
    /* そのまま考えるのはきつそうなので、余事象を考える */
    /* オイラーのファイ関数に突っ込めば解けそうだが、そのままだとO(Nsqrt(N))でまずそう -> 篩で前計算しておく */
    bool[] isPrime = new bool[](N+1);
    isPrime[] = true;
    isPrime[0] = isPrime[1] = false;
    for (int i = 2; i <= N; i++) {
        if (N < i*i) break;
        int j = i*i;
        while (j <= N) {
            isPrime[j] = false;
            j += i;
        }
    }

    int[] primes;
    foreach (i, val; isPrime) if (val) primes ~= cast(int) i;

    long ans = 1L*N*(N-1)/2;

    // x < y かつ gcd(x, y) = xとなるペアの数え上げ
    for (int i = 2; i <= N; i++) {
        ans -= N/i - 1;
    }

    // x < y かつ gcd(x, y) = 1となるペアの数え上げ
    for (int i = 2; i <= N; i++) {
        ans -= euler_phi(i, primes);
    }

    writeln(ans);
}

long euler_phi (int n, int[] primes) {
    long res = n;
    foreach (p; primes) {
        if (n == 1 || n < 1L*p*p) break;
        if (n % p == 0) res -= res/p;
        while (n % p == 0) n /= p;
    }
    if (1 < n) res -= res/n;
    return res;
}

void read(T...)(string S, ref T args) {
    auto buf = S.split;
    foreach (i, ref arg; args) {
        arg = buf[i].to!(typeof(arg));
    }
}
0