結果
| 問題 |
No.144 エラトステネスのざる
|
| コンテスト | |
| ユーザー |
tottoripaper
|
| 提出日時 | 2015-03-04 23:52:16 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 97 ms / 2,000 ms |
| コード長 | 1,061 bytes |
| コンパイル時間 | 256 ms |
| コンパイル使用メモリ | 24,448 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-06-24 07:33:57 |
| 合計ジャッジ時間 | 1,694 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 17 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:19:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
19 | scanf("%d %lf", &N, &p);
| ~~~~~^~~~~~~~~~~~~~~~~~
ソースコード
#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);
}
tottoripaper