結果

問題 No.144 エラトステネスのざる
ユーザー tottoripapertottoripaper
提出日時 2015-03-04 23:52:16
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 96 ms / 2,000 ms
コード長 1,061 bytes
コンパイル時間 173 ms
コンパイル使用メモリ 27,080 KB
実行使用メモリ 7,928 KB
最終ジャッジ日時 2023-09-06 12:57:05
合計ジャッジ時間 1,855 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,000 KB
testcase_01 AC 2 ms
4,976 KB
testcase_02 AC 2 ms
5,056 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
5,028 KB
testcase_07 AC 2 ms
5,028 KB
testcase_08 AC 2 ms
5,060 KB
testcase_09 AC 2 ms
5,028 KB
testcase_10 AC 2 ms
5,068 KB
testcase_11 AC 2 ms
5,036 KB
testcase_12 AC 2 ms
5,036 KB
testcase_13 AC 90 ms
7,908 KB
testcase_14 AC 90 ms
7,924 KB
testcase_15 AC 91 ms
7,892 KB
testcase_16 AC 91 ms
7,864 KB
testcase_17 AC 91 ms
7,868 KB
testcase_18 AC 96 ms
7,928 KB
testcase_19 AC 91 ms
7,892 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>

bool isNotPrime[1000001];
int minFactor[1000001];

double pow(double a, int n){
    double res = 1.0f;
    while(n > 0){
        if(n & 1){res *= a;}
        a *= a;
        n >>= 1;
    }
    return res;
}

int main(){
    int N;
    double p;
    scanf("%d %lf", &N, &p);
    
    for(int i=4;i<=N;i+=2){
        isNotPrime[i] = true;
        minFactor[i] = 2;
    }
    minFactor[2] = 2;
    for(int i=3;i<=N;i+=2){
        if(!isNotPrime[i]){
            minFactor[i] = i;
            for(int j=2*i;j<=N;j+=i){
                isNotPrime[j] = true;
                minFactor[j] = i;                
            }
        }
    }

    double res = 0.0f;
    for(int i=2;i<=N;i++){
        if(!isNotPrime[i]){res += 1.0f; continue;}
        
        int x = i, count = 1;
        while(x > 1){
            int mF = minFactor[x], c = 0;
            while(x % mF == 0){c++; x /= mF;}
            count *= c + 1;
        }

        // printf("%d: %d\n", i, count-2);
        res += pow(1-p, count-2);
    }

    printf("%.10f\n", res);
    
}
0