結果

問題 No.1059 素敵な集合
ユーザー tko919tko919
提出日時 2021-08-27 17:20:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 1,325 bytes
コンパイル時間 2,312 ms
コンパイル使用メモリ 204,464 KB
実行使用メモリ 37,440 KB
最終ジャッジ日時 2023-08-13 02:17:41
合計ジャッジ時間 3,848 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 50 ms
36,728 KB
testcase_02 AC 8 ms
5,344 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 7 ms
5,384 KB
testcase_07 AC 8 ms
5,440 KB
testcase_08 AC 9 ms
5,336 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 14 ms
8,980 KB
testcase_11 AC 8 ms
5,436 KB
testcase_12 AC 4 ms
4,384 KB
testcase_13 AC 14 ms
8,872 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 26 ms
12,552 KB
testcase_16 AC 9 ms
5,484 KB
testcase_17 AC 8 ms
5,488 KB
testcase_18 AC 5 ms
4,568 KB
testcase_19 AC 48 ms
37,288 KB
testcase_20 AC 62 ms
37,440 KB
testcase_21 AC 23 ms
12,788 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

//template
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
using ll=long long int;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}

struct UnionFind{
   vector<int> par; int n;
   UnionFind(){}
   UnionFind(int _n):par(_n,-1),n(_n){}
   int root(int x){return par[x]<0?x:par[x]=root(par[x]);}
   bool same(int x,int y){return root(x)==root(y);}
   int size(int x){return -par[root(x)];}
   bool unite(int x,int y){
      x=root(x),y=root(y); if(x==y)return false;
      if(size(x)>size(y))swap(x,y);
      par[y]+=par[x]; par[x]=y; n--; return true;
   }
};

int main(){
   int L,R;
   cin>>L>>R;

   int n=R-L+1;
   UnionFind uni(n);
   using P=pair<int,int>;
   vector<P> es;
   rep(x,L,R+1){
      int y=x;
      while(y<=R){
         uni.unite(x-L,y-L);
         y+=x;
      }
      y=x+1;
      while(y<=R){
         es.push_back({x-L,y-L});
         y+=x;
      }
   }
   int res=0;
   for(auto& [u,v]:es){
      if(uni.unite(u,v))res++;
      if(uni.n==1)break;
   }
   cout<<res<<'\n';
   return 0;
}
0