結果
| 問題 | 
                            No.1059 素敵な集合
                             | 
                    
| コンテスト | |
| ユーザー | 
                             snow39
                         | 
                    
| 提出日時 | 2020-05-22 22:28:03 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 12 ms / 2,000 ms | 
| コード長 | 1,780 bytes | 
| コンパイル時間 | 1,017 ms | 
| コンパイル使用メモリ | 101,092 KB | 
| 実行使用メモリ | 6,944 KB | 
| 最終ジャッジ日時 | 2024-07-23 09:31:19 | 
| 合計ジャッジ時間 | 1,816 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 19 | 
ソースコード
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}
class DisjointSet{
public:
  vector<int> rank,p;
  vector<int> sz;
 
  DisjointSet(){}
  DisjointSet(int size){
    rank.resize(size,0);
    p.resize(size,0);
    sz.resize(size,0);
    for(int i=0;i<size;i++) makeSet(i);
  }
 
  void makeSet(int x){
    p[x]=x;
    rank[x]=0;
    sz[x]=1;
  }
 
  bool same(int x,int y){
    return findSet(x)==findSet(y);
  }
 
  void unite(int x,int y){
    if(same(x,y)) return;
    link(findSet(x),findSet(y));
  }
 
  void link(int x,int y){
    if(rank[x]>rank[y]){
      p[y]=x;
      sz[x]+=sz[y];
    }else{
      p[x]=y;
      sz[y]+=sz[x];
      if(rank[x]==rank[y]){
        rank[y]++;
      }
    }
  }
 
  int findSet(int x){
    if(x!=p[x]){
      p[x]=findSet(p[x]);
    }
    return p[x];
  }
 
  int findSize(int x){
    return sz[findSet(x)];
  }
};
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int L,R;
  cin>>L>>R;
  vector<int> isprime(R+1,1);
  vector<ll> primes;
  for(ll i=2;i<=R;i++){
    if(isprime[i]==0) continue;
    primes.push_back(i);
    for(ll j=2;i*j<=R;j++) isprime[i*j]=0;
  }
  DisjointSet us(R+1);
  for(int i=L;i<=R;i++){
    for(int j=0;j<primes.size();j++){
      if(i*primes[j]>R) break;
      us.unite(i,i*primes[j]);
    }
  }
  int ans=0;
  for(int i=L;i<=R;i++) if(us.findSet(i)==i) ans++;
  cout<<ans-1<<endl;
  return 0;
}
            
            
            
        
            
snow39