結果

問題 No.843 Triple Primes
ユーザー watarimaycry2watarimaycry2
提出日時 2020-10-24 17:47:47
言語 JavaScript
(node v21.7.1)
結果
AC  
実行時間 214 ms / 2,000 ms
コード長 3,042 bytes
コンパイル時間 204 ms
コンパイル使用メモリ 5,376 KB
実行使用メモリ 51,844 KB
最終ジャッジ日時 2024-07-21 15:21:32
合計ジャッジ時間 7,962 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
40,192 KB
testcase_01 AC 214 ms
50,184 KB
testcase_02 AC 82 ms
40,704 KB
testcase_03 AC 83 ms
40,704 KB
testcase_04 AC 83 ms
40,320 KB
testcase_05 AC 79 ms
40,320 KB
testcase_06 AC 82 ms
40,704 KB
testcase_07 AC 170 ms
50,560 KB
testcase_08 AC 174 ms
50,176 KB
testcase_09 AC 194 ms
50,436 KB
testcase_10 AC 180 ms
50,568 KB
testcase_11 AC 192 ms
50,436 KB
testcase_12 AC 197 ms
50,308 KB
testcase_13 AC 193 ms
50,564 KB
testcase_14 AC 192 ms
50,568 KB
testcase_15 AC 179 ms
50,308 KB
testcase_16 AC 180 ms
50,176 KB
testcase_17 AC 79 ms
40,192 KB
testcase_18 AC 79 ms
40,448 KB
testcase_19 AC 79 ms
40,192 KB
testcase_20 AC 117 ms
46,192 KB
testcase_21 AC 101 ms
45,184 KB
testcase_22 AC 146 ms
48,128 KB
testcase_23 AC 151 ms
48,252 KB
testcase_24 AC 118 ms
46,064 KB
testcase_25 AC 113 ms
45,940 KB
testcase_26 AC 201 ms
50,432 KB
testcase_27 AC 94 ms
44,928 KB
testcase_28 AC 198 ms
50,560 KB
testcase_29 AC 124 ms
46,188 KB
testcase_30 AC 204 ms
50,308 KB
testcase_31 AC 99 ms
45,184 KB
testcase_32 AC 94 ms
44,800 KB
testcase_33 AC 125 ms
48,640 KB
testcase_34 AC 143 ms
48,128 KB
testcase_35 AC 199 ms
50,432 KB
testcase_36 AC 109 ms
45,932 KB
testcase_37 AC 173 ms
51,844 KB
testcase_38 AC 163 ms
51,588 KB
testcase_39 AC 192 ms
50,312 KB
testcase_40 AC 80 ms
40,576 KB
testcase_41 AC 79 ms
40,320 KB
testcase_42 AC 181 ms
50,184 KB
testcase_43 AC 188 ms
50,432 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
	input: process.stdin, output: process.stdout
});
var obj; var inLine = [];
read.on('line', function(input){inLine.push(input);});
read.on('close', function(){
	obj = init(inLine);
	console.error('\n↑入力 ↓出力');
	Main();
});
function makeClone(obj){return (obj instanceof Set) ? new Set(Array.from(obj)) : JSON.parse(JSON.stringify(obj));}
function nextInt(){return myconv(next(),1);} function nextStrArray(){return myconv(next(),2);}
function nextIntArray(){return myconv(next(),4);} function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();}
function init(input){  
	return {
		list : input, index : 0, max : input.length,
		hasNext : function(){return (this.index < this.max);},
		next : function(){if(this.hasNext()){return this.list[this.index++];}else{throw 'ArrayIndexOutOfBoundsException ‚There is no more input';}}
	};
}
function myout(s){console.log(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
//param "no" is
//unknown or outlier : return i. 1: parseInt.
//2: split space. 4: split space and parseInt.
//6: split 1 character. 7: split 1 character and parseInt.
//8: join space. 9: join nextline. 0: join no character.
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(' ');case 4:return i.split(' ').map(Number);case 6:return i.split('');case 7:return i.split('').map(Number);case 8:return i.join(' ');case 9:return i.join('\n');case 0:return i.join('');default:return i;}}catch(e){return i;}}

//Don't have to see. end------------------------------------------
function Main(){
	var N = nextInt();
	var map = sieveOfEratos(N);
	myerr(map.size);
	var output = 0;
	var list = Array.from(map);
	for(var i = 0; i < list.length; i++){
		if(map.has(Math.sqrt(list[i] + 2))){
			output++;
		}
	}
	myout(Math.max(output * 2 - 1, 0));
}
function sieveOfEratos(val){
  var primes = new Set();
  var nums = new Set();
  var used = [2,3,5,7,11];//これらは既に素数であるとする
  var underPrime = 13;//この値から+2ごとに追加。↑の値で割り切れたものは何もしない
  if(val <= 1){
     return nums;
  }
  for(var i = 0; i < used.length; i++){if(used[i] <= val){nums.add(used[i]);}}
  for(var i = underPrime; i <= val; i = i + 2){
    var continued = false;
    for(var j = 0; j < used.length; j++){
      if(i % used[j] == 0){continued = true; break;}
    }
    if(continued){continue;}
    nums.add(i);
  }
  for(var i = 2; i <= Math.sqrt(val); (i == 2) ? i++ : i = i + 2){
    if(!nums.has(i)){continue;}
    var count = 1;
    while(i * count <= val){
      if(i <= 11 && used.indexOf(i) != -1){break;}
      if(count == 1){primes.add(i);}
      nums.delete(i * count);
      count++;
    }
  }
  var primeItr = Array.from(primes);
  for(var i = 0; i < primeItr.length; i++){
    nums.add(primeItr[i]);
  }
  return nums;
}
0