結果
| 問題 | 
                            No.1059 素敵な集合
                             | 
                    
| コンテスト | |
| ユーザー | 
                             tko919
                         | 
                    
| 提出日時 | 2021-08-27 17:20:54 | 
| 言語 | C++17  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 84 ms / 2,000 ms | 
| コード長 | 1,325 bytes | 
| コンパイル時間 | 2,134 ms | 
| コンパイル使用メモリ | 198,740 KB | 
| 最終ジャッジ日時 | 2025-01-24 02:17:10 | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge2 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 19 | 
ソースコード
#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;
}
            
            
            
        
            
tko919