結果

問題 No.144 エラトステネスのざる
ユーザー bal4ubal4u
提出日時 2019-07-11 22:47:47
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 624 bytes
コンパイル時間 570 ms
コンパイル使用メモリ 33,536 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-26 05:37:30
合計ジャッジ時間 1,538 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 28 ms
6,940 KB
testcase_14 AC 31 ms
6,940 KB
testcase_15 AC 29 ms
6,944 KB
testcase_16 AC 29 ms
6,940 KB
testcase_17 AC 29 ms
6,944 KB
testcase_18 AC 29 ms
6,940 KB
testcase_19 AC 29 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// yukicoder: No.144 エラトステネスのざる
// 2019.7.11 bal4u

#include <stdio.h>
#include <math.h>

#define EQ(a, b) (fabs((a)-(b)) < 1e-8)

double p;
int f[1000005];
double memo[300]; char sw[300];

int main()
{
	int i, j, n, N;
	double p, ans;
	
	scanf("%d%lf", &N, &p); n = N >> 1, p = 1-p;
	for (i = 2; i <= n; i++) for (j = i << 1; j <= N; j += i) f[j]++;
	
	if (EQ(p, 1)) ans = N-1;
	else if (EQ(p, 0)) while (N > 1) ans += (f[N--] == 0);
	else {
		ans = 0;
		while (N > 1) {
			n = f[N];
			if (!sw[n]) memo[n] = pow(p, n), sw[n] = 1;
			ans += memo[n];
			N--;
		}
	}
	printf("%.10lf\n", ans);
	return 0;
}
0