結果

問題 No.1653 Squarefree
ユーザー startcppstartcpp
提出日時 2021-08-20 22:24:44
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 27 ms / 2,000 ms
コード長 1,362 bytes
コンパイル時間 810 ms
コンパイル使用メモリ 82,420 KB
実行使用メモリ 5,480 KB
最終ジャッジ日時 2023-08-04 11:29:14
合計ジャッジ時間 3,135 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
5,248 KB
testcase_01 AC 22 ms
4,384 KB
testcase_02 AC 22 ms
4,380 KB
testcase_03 AC 25 ms
4,504 KB
testcase_04 AC 22 ms
4,388 KB
testcase_05 AC 22 ms
4,380 KB
testcase_06 AC 23 ms
4,528 KB
testcase_07 AC 22 ms
4,380 KB
testcase_08 AC 23 ms
4,384 KB
testcase_09 AC 23 ms
4,388 KB
testcase_10 AC 22 ms
4,384 KB
testcase_11 AC 25 ms
5,360 KB
testcase_12 AC 24 ms
5,224 KB
testcase_13 AC 25 ms
5,356 KB
testcase_14 AC 26 ms
5,332 KB
testcase_15 AC 25 ms
5,256 KB
testcase_16 AC 27 ms
5,340 KB
testcase_17 AC 26 ms
5,304 KB
testcase_18 AC 26 ms
5,256 KB
testcase_19 AC 25 ms
5,264 KB
testcase_20 AC 25 ms
5,300 KB
testcase_21 AC 25 ms
5,304 KB
testcase_22 AC 26 ms
5,360 KB
testcase_23 AC 26 ms
5,212 KB
testcase_24 AC 27 ms
5,272 KB
testcase_25 AC 27 ms
5,236 KB
testcase_26 AC 26 ms
5,292 KB
testcase_27 AC 25 ms
5,356 KB
testcase_28 AC 26 ms
5,256 KB
testcase_29 AC 26 ms
5,268 KB
testcase_30 AC 25 ms
5,480 KB
testcase_31 AC 27 ms
5,316 KB
testcase_32 AC 26 ms
5,276 KB
testcase_33 AC 25 ms
5,252 KB
testcase_34 AC 24 ms
5,252 KB
testcase_35 AC 26 ms
5,272 KB
testcase_36 AC 24 ms
4,384 KB
testcase_37 AC 23 ms
4,388 KB
testcase_38 AC 23 ms
4,512 KB
testcase_39 AC 25 ms
4,384 KB
testcase_40 AC 25 ms
4,508 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cmath>
#include <cassert>
#include <tuple>
#define rep(i, n) for(i = 0; i < n; i++)
#define int long long
using namespace std;

int l, r;
bool isPrime[1000001];
bool ng[1000001];	//ng[i] = l + iがsquarefreeならfalse, そうでないならtrue

signed main() {
	int i;
	
	cin >> l >> r;
	
	int unti = 1000000;
	for (i = 2; i <= unti; i++) isPrime[i] = true;
	for (i = 2; i <= unti; i++) {
		if (isPrime[i]) {
			for (int j = i * 2; j <= unti; j += i) {
				isPrime[j] = false;
			}
		}
	}
	
	for (i = 2; i <= unti; i++) {
		if (isPrime[i]) {
			int num = ((l + i * i - 1) / (i * i)) * i * i;
			while (num <= r) {
				ng[num - l] = true;
				num += i * i;
			}
		}
	}
	
	//平方数p^2として、p>10^6. l <= px <= rなるxは1つかつ、x < 10^6
	int snuke = 1000000;
	for (int x = snuke; x >= 1; x--) {
		int low = sqrt((long double)l / x) - 1;
		int high = sqrt((long double)r / x) + 1;
		for (int p = low; p <= high; p++) {
			if (p <= 1) continue;
			int num = p * p * x;
			if (l <= num && num <= r) {
				ng[num - l] = true;
			}
		}
	}
	
	int cnt = 0;
	for (i = 0; i <= r - l; i++) {
		if (ng[i] == false) {
			cnt++;
		}
	}
	cout << cnt << endl;
	return 0;
}
0