結果

問題 No.144 エラトステネスのざる
ユーザー a0171610a0171610
提出日時 2020-05-19 21:47:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 52 ms / 2,000 ms
コード長 790 bytes
コンパイル時間 1,468 ms
コンパイル使用メモリ 163,112 KB
実行使用メモリ 10,912 KB
最終ジャッジ日時 2024-04-09 22:28:42
合計ジャッジ時間 2,528 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,880 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
7,880 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 3 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,948 KB
testcase_13 AC 47 ms
10,100 KB
testcase_14 AC 49 ms
9,164 KB
testcase_15 AC 47 ms
10,348 KB
testcase_16 AC 49 ms
10,392 KB
testcase_17 AC 47 ms
9,256 KB
testcase_18 AC 52 ms
10,912 KB
testcase_19 AC 48 ms
9,228 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:26:7: warning: 'ans' is used uninitialized [-Wuninitialized]
   26 |   ans += double(p);
      |   ~~~~^~~~~~~~~~~~
main.cpp:25:10: note: 'ans' was declared here
   25 |   double ans;
      |          ^~~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
const int MAX_N = 1e6+10;
int prime[MAX_N], dataset[MAX_N];
bool is_prime[MAX_N];
int p = 0;

void sieve(int n){
  for(int i = 0; i <= n; i++) is_prime[i] = true;
  is_prime[0] = false; is_prime[1] = false;
  for(int i = 2; i <= n; i++){
    if(is_prime[i]){
      prime[p++] = i;
      for(int j = 2 * i; j <= n; j += i) is_prime[j] = false;
    }
    for(int j = 2 * i; j <= n; j += i) dataset[j]++;
  }
}

signed main(){
  int n; cin >> n;
  double P; cin >> P;
  sieve(n);
  double ans;
  ans += double(p);
  for(int i = 2; i <= n; i++){
    if(dataset[i]){
      double ans1 = 1;
      for(int j = 0; j < dataset[i]; j++) ans1 *= (1.0 - P);
      ans += ans1;
    }
  }
  cout << setprecision(10) << ans << endl;
}
0