結果
| 問題 | No.826 連絡網 |
| コンテスト | |
| ユーザー |
watarimaycry2
|
| 提出日時 | 2020-10-07 13:03:57 |
| 言語 | JavaScript (node v23.5.0) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 4,178 bytes |
| 記録 | |
| コンパイル時間 | 159 ms |
| コンパイル使用メモリ | 6,944 KB |
| 実行使用メモリ | 56,220 KB |
| 最終ジャッジ日時 | 2024-07-20 03:22:44 |
| 合計ジャッジ時間 | 5,509 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 11 RE * 19 |
ソースコード
//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 one = nextIntArray();
var N = one[0];
var P = one[1];
var uf = unionFind(N + 1);
for(var i = 2; i <= N; i++){
if(isPrime(i)){
for(var j = 2; i * j <= N; j++){
uf.doUnion(i, i * j);
}
}
}
myout(uf.getSize(P));
}
function isPrime(val){
if(val == null || val <= 1 || (val != 2 && val % 2 == 0)){
return false;
}else if(val == 2){
return true;
}
var root = Math.floor(Math.sqrt(val));
for(var i = 3; i <= root; i += 2){
if(val % i == 0){
return false;
}
}
return true;
}
function unionFind(n){
var uf = {
//全てのインデックスは「-X(親、絶対値はグループの大きさ)」「自分が属する親(≒根)」のいずれかを持つ。
//最初はみんなグループの大きさ1の親
"list" : new Array(n).fill(-1),
//同じ親を持つか
"isSame" : function(mae, ato){
return this.getRootIndex(mae) == this.getRootIndex(ato);
},
//自身の親インデックスをたどって根っこに着く
//親にたどり終えたら子に帰っていくついでに、子たちに「共通の親を持っている」ことを知らせる
"getRootIndex" : function(index){
if(this.list[index] < 0){
return index;
}else{
this.list[index] = this.getRootIndex(this.list[index]);
return this.list[index];
}
},
//異なる親同士のまとまりを一つにする(同じ親ならスルー)
//小さいグループの親が大きいグループの親の直下に着く
//グループの大きさも更新する
"doUnion" : function(mae, ato){
var maeRoot = this.getRootIndex(mae);
var atoRoot = this.getRootIndex(ato);
if(!this.isSame(maeRoot, atoRoot)){
if(maeRoot >= atoRoot){
this.list[maeRoot] += this.list[atoRoot];
this.list[atoRoot] = maeRoot;
}else{
this.list[atoRoot] += this.list[maeRoot];
this.list[maeRoot] = atoRoot;
}
}
},
//「-X(親、絶対値はグループの大きさ)」
//なので、インデックスを指定→親を知る→親の持つグループの大きさがわかる。
//ただしマイナス値なので、掛け算して返す。
"getSize" : function(index){
return -this.list[this.getRootIndex(index)];
}
}
return uf;
}
watarimaycry2