結果

問題 No.1059 素敵な集合
ユーザー umezoumezo
提出日時 2024-01-24 03:20:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 20 ms / 2,000 ms
コード長 1,120 bytes
コンパイル時間 4,256 ms
コンパイル使用メモリ 207,920 KB
実行使用メモリ 7,936 KB
最終ジャッジ日時 2024-01-24 03:20:55
合計ジャッジ時間 6,362 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 20 ms
7,936 KB
testcase_02 AC 6 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 4 ms
6,676 KB
testcase_07 AC 4 ms
6,676 KB
testcase_08 AC 5 ms
6,676 KB
testcase_09 AC 3 ms
6,676 KB
testcase_10 AC 6 ms
6,676 KB
testcase_11 AC 5 ms
6,676 KB
testcase_12 AC 4 ms
6,676 KB
testcase_13 AC 8 ms
6,676 KB
testcase_14 AC 2 ms
6,676 KB
testcase_15 AC 13 ms
7,296 KB
testcase_16 AC 5 ms
6,676 KB
testcase_17 AC 5 ms
6,676 KB
testcase_18 AC 7 ms
7,936 KB
testcase_19 AC 19 ms
7,936 KB
testcase_20 AC 19 ms
7,936 KB
testcase_21 AC 13 ms
7,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
 
#include<bits/stdc++.h>
using namespace std;

class Dis{
  public:
  vector<ll> rank,p,siz;
  
  Dis(int s){
    rank.resize(s,0);
    p.resize(s,0);
    siz.resize(s,1);
    rep(i,s) makeSet(i);
  }
  
  void makeSet(int x){
    p[x]=x;
    rank[x]=0;
  }
  
  bool same(int x,int y){
    return root(x)==root(y);
  }
  
  void unite(int x,int y){
    if(same(x,y)) return;
    link(root(x),root(y));
  }
  
  void link(int x,int y){
    if(rank[x]>rank[y]){
      p[y]=x;
      siz[x]+=siz[y];
    }
    else{
      p[x]=y;
      siz[y]+=siz[x];
      if(rank[x]==rank[y]) rank[y]++;
    }
  }
  
  int root(int x){
    if(x != p[x]) p[x]=root(p[x]);
    return p[x];
  }
  
  int size(int x){
    return siz[root(x)];
  }
};
 
int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  int l,r;
  cin>>l>>r;
  Dis ds=Dis(r+1);
  for(int i=l;i<=r;i++){
    for(int j=2*i;j<=r;j+=i) ds.unite(i,j);
  }
  int cnt=0;
  for(int i=l;i<=r;i++) if(ds.root(i)==i) cnt++;
  cout<<cnt-1<<endl;
    
  return 0;
}
0