結果
| 問題 |
No.3127 Multiple of Twin Prime
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-04-25 21:34:33 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 142 ms / 2,500 ms |
| コード長 | 3,145 bytes |
| コンパイル時間 | 3,942 ms |
| コンパイル使用メモリ | 283,704 KB |
| 実行使用メモリ | 48,128 KB |
| 最終ジャッジ日時 | 2025-04-25 21:34:55 |
| 合計ジャッジ時間 | 6,704 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 12 |
ソースコード
// #include <bits/allocator.h> // Temp fix for gcc13 global pragma
// #pragma GCC target("avx2,bmi2,popcnt,lzcnt")
// #pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
// #include <x86intrin.h>
using namespace std;
#if __cplusplus >= 202002L
using namespace numbers;
#endif
#ifdef LOCAL
#include "Debug.h"
#else
#define debug_endl() 42
#define debug(...) 42
#define debug2(...) 42
#define debug_bin(...) 42
#endif
struct number_theory{
int SZ;
vector<int> lpf, prime;
number_theory(int SZ): SZ(SZ), lpf(SZ + 1){ // O(SZ)
lpf[0] = lpf[1] = numeric_limits<int>::max() / 2;
for(auto i = 2; i <= SZ; ++ i){
if(!lpf[i]) lpf[i] = i, prime.push_back(i);
for(auto j = 0; j < (int)prime.size() && prime[j] <= lpf[i] && prime[j] * i <= SZ; ++ j) lpf[prime[j] * i] = prime[j];
}
}
vector<int> precalc_mobius() const{
vector<int> mobius(SZ + 1, 1);
for(auto i = 2; i <= SZ; ++ i){
if(i / lpf[i] % lpf[i]) mobius[i] = -mobius[i / lpf[i]];
else mobius[i] = 0;
}
return mobius;
}
vector<int> precalc_phi() const{
vector<int> phi(SZ + 1, 1);
for(auto i = 2; i <= SZ; ++ i){
if(i / lpf[i] % lpf[i]) phi[i] = phi[i / lpf[i]] * (lpf[i] - 1);
else phi[i] = phi[i / lpf[i]] * lpf[i];
}
return phi;
}
// Returns {gcd(0, n), ..., gcd(SZ, n)}
vector<int> precalc_gcd(int n) const{
vector<int> res(SZ + 1, 1);
res[0] = n;
for(auto x = 2; x <= SZ; ++ x) res[x] = n % (lpf[x] * res[x / lpf[x]]) ? res[x / lpf[x]] : lpf[x] * res[x / lpf[x]];
return res;
}
bool is_prime(int x) const{
assert(0 <= x && x <= SZ);
return lpf[x] == x;
}
int mu_large(int64_t x) const{ // O(sqrt(x))
int res = 1;
for(auto i = 2LL; i * i <= x; ++ i) if(x % i == 0){
if(x / i % i) return 0;
x /= i, res = -res;
}
if(x > 1) res = -res;
return res;
}
int64_t phi_large(int64_t x) const{ // O(sqrt(x))
int64_t res = x;
for(auto i = 2LL; i * i <= x; ++ i) if(x % i == 0){
while(x % i == 0) x /= i;
res -= res / i;
}
if(x > 1) res -= res / x;
return res;
}
// returns an array is_prime of length high-low where is_prime[i] = [low+i is a prime]
vector<int> sieve(int64_t low, int64_t high) const{
assert(high - 1 <= 1LL * SZ * SZ);
vector<int> is_prime(high - low, true);
for(auto p: prime) for(auto x = max(1L * p, (low + p - 1) / p) * p; x < high; x += p) is_prime[x - low] = false;
for(auto x = 1; x >= low; -- x) is_prime[x - low] = false;
return is_prime;
}
};
int main(){
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(ios::badbit | ios::failbit);
number_theory nt{10'000'000};
vector<int64_t> cand;
for(auto i = 1; i < (int)nt.prime.size() - 1; ++ i){
if(nt.prime[i + 1] == nt.prime[i] + 2){
cand.push_back(1LL * nt.prime[i] * nt.prime[i + 1]);
}
}
auto solve_testcase = [&](auto testcase_id)->int{
int64_t n;
cin >> n;
auto it = ranges::upper_bound(cand, n);
if(it == cand.begin()){
cout << "-1\n";
}
else{
cout << *prev(it) << "\n";
}
return 0;
};
int testcase_count;
cin >> testcase_count;
for(auto testcase_id = 0; testcase_id < testcase_count; ++ testcase_id){
solve_testcase(testcase_id);
}
return 0;
}
/*
*/