結果

問題 No.371 ぼく悪いプライムじゃないよ
ユーザー どらら
提出日時 2016-05-14 01:09:20
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 389 ms / 1,000 ms
コード長 1,802 bytes
コンパイル時間 947 ms
コンパイル使用メモリ 82,744 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-30 18:07:11
合計ジャッジ時間 3,403 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <iostream>
#include <queue>
#include <list>
#include <stack>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;

#define MAX_PRIME 105000
bool isprime[MAX_PRIME+1]; 
vector<int> primes; 
void init_prime(){
  fill(isprime, isprime+MAX_PRIME+1, true); 
  isprime[0] = isprime[1] = false; 
  for(int i = 2; i <= MAX_PRIME; i++) { 
    if (isprime[i]) { 
      primes.push_back(i); 
      for(int j = i*2; j <= MAX_PRIME; j += i) 
      isprime[j] = false; 
    } 
  } 
}

ll findmin(ll x){
  ll d, q;
  while(x>=4 && x%2==0){ return 2; x/=2;}
  d = 3; q = x/d;
  while(q>=d){
    if(x%d==0){ return d; x=q; }else{ d+=2; }
    q=x/d;
  }
  return x;
}

ll ret;
void dfs(int i, ll now, ll L, ll H){
  if(i>=primes.size()) return;
  ll p = primes[i];
  ll q = 1;
  while(now*q<=H){
    if(L<=now*q){
      ret = max(ret, now*q);
    }
    dfs(i+1, now*q, L, H);
    q *= p;
  }
}


int main(){
  ios::sync_with_stdio(false);
  init_prime();

  ll L, H;
  cin >> L >> H;

  if(H-L<100000){
    ll mx = -1, mx_v = -1;
    for(ll x = L; x<=H; x++){
      ll res = findmin(x);
      if(res == x) continue;
      if(res >= mx){
        mx = res;
        mx_v = x;
      }
    }
    cout << mx_v << endl;
  }else{
    for(int i=primes.size()-1; i>=0; i--){
      ret = -1;
      dfs(i,primes[i],L,H);
      if(ret > 0 && ret != primes[i]){
        cout << ret << endl;
        break;
      }
    }
  }
  return 0;
}
0