結果
| 問題 | No.3101 Range Eratosthenes Query |
| コンテスト | |
| ユーザー |
ryoku
|
| 提出日時 | 2026-08-25 22:28:57 |
| 言語 | C++23(gcc16) (gcc 16.1.0 + boost 1.90.0) |
| 結果 |
AC
|
| 実行時間 | 466 ms / 3,000 ms |
| + 589µs | |
| コード長 | 14,105 bytes |
| 記録 | |
| コンパイル時間 | 4,795 ms |
| コンパイル使用メモリ | 313,912 KB |
| 実行使用メモリ | 43,044 KB |
| 最終ジャッジ日時 | 2026-08-25 22:29:16 |
| 合計ジャッジ時間 | 16,002 ms |
|
ジャッジサーバーID (参考情報) |
judge3_0 / judge1_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 24 |
ソースコード
#define CP_BUNDLED_SOURCE
#ifdef TEMPLATE
#else
#define TEMPLATE
# pragma GCC optimize("O3")
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <climits>
#include <cassert>
#include <functional>
#include <iterator>
#include <utility>
#include <complex>
#include <bitset>
#include <chrono>
#include <random>
#include <limits>
#include <optional>
#include <variant>
#include <any>
#include <array>
#include <bit>
#include <compare>
#include <concepts>
#include <numbers>
#include <ranges>
#include <span>
#include <string_view>
#include <tuple>
#include <type_traits>
#include <version>
using namespace std;
using uint=unsigned;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using i128=__int128;
using u128=unsigned __int128;
template<class T>using vc=vector<T>;
template<class T>using vvc=vc<vc<T>>;
template<class T>using vvvc=vvc<vc<T>>;
template<class T>using smpq=priority_queue<T,vector<T>,greater<T>>;
template<class T>using bipq=priority_queue<T>;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define REP(i,j,n) for(ll i=(j);i<(ll)(n);i++)
#define DREP(i,n,m) for(ll i=(n);i>=(m);i--)
#define drep(i,n) for(ll i=((n)-1);i>=0;i--)
#define rall(x) x.rbegin(),x.rend()
#define mp(...) make_pair(__VA_ARGS__)
#define pb push_back
#define fi first
#define se second
#define is insert
#define bg begin()
#define ed end()
#define all(x) x.begin(),x.end()
void scan(int&a) { cin >> a; }
void scan(ll&a) { cin >> a; }
void scan(string&a) { cin >> a; }
void scan(char&a) { cin >> a; }
void scan(uint&a) { cin >> a; }
void scan(ull&a) { cin >> a; }
void scan(bool&a) { cin >> a; }
void scan(ld&a){ cin>> a;}
template<class T> void scan(vector<T>&a) { for(auto&x:a) scan(x); }
void read() {}
template<class Head, class... Tail> void read(Head&head, Tail&... tail) { scan(head); read(tail...); }
#define INT(...) int __VA_ARGS__; read(__VA_ARGS__);
#define LL(...) ll __VA_ARGS__; read(__VA_ARGS__);
#define ULL(...) ull __VA_ARGS__; read(__VA_ARGS__);
#define STR(...) string __VA_ARGS__; read(__VA_ARGS__);
#define VC(type, name, ...) vector<type> name(__VA_ARGS__); read(name);
#define VVC(type, name, size, ...) vector<vector<type>> name(size, vector<type>(__VA_ARGS__)); read(name);
template<class T>void print(T a) { cout << a; }
template<class T> void print(vector<T>a) { for(int i=0;i<(int)a.size();i++){if(i)cout<<" ";print(a[i]);}cout<<endl;}
void PRT() { cout <<endl; return ; }
template<class T> void PRT(T a) { print(a); cout <<endl; return; }
template<class Head, class... Tail> void PRT(Head head, Tail ... tail) { print(head); cout << " "; PRT(tail...); return; }
template<class T,class F>
bool chmin(T &x, F y){
if(x>y){
x=y;
return true;
}
return false;
}
template<class T, class F>
bool chmax(T &x, F y){
if(x<y){
x=y;
return true;
}
return false;
}
template <typename T>
T floor(T a, T b) {
return a / b - (a % b && (a ^ b) < 0);
}
template <typename T>
T ceil(T x, T y) {
return floor(x + y - 1, y);
}
template <typename T>
T bmod(T x, T y) {
return x - y * floor(x, y);
}
template <typename T>
pair<T, T> divmod(T x, T y) {
T q = floor(x, y);
return {q, x - q * y};
}
void YesNo(bool b){
cout<<(b?"Yes":"No")<<endl;
}
void YESNO(bool b){
cout<<(b?"YES":"NO")<<endl;
}
void AliceBob(bool b){
cout<<(b?"Alice":"Bob")<<endl;
}
void TakahashiAoki(bool b){
cout<<(b?"Takahashi":"Aoki")<<endl;
}
void Yes(){
cout<<"Yes"<<endl;
}
void No(){
cout<<"No"<<endl;
}
vc<int>stovi(const string&s,const string&S){
vc<int>v(s.size());
rep(i,s.size()){
auto t=S.find(s[i]);
assert(t!=string::npos);
v[i]=t;
}
return v;
}
template<class T=ll>
T isqrt(T x){
T F=sqrtl(x);
while((F+1)*(F+1)<=x)F++;
while(F*F>x)F--;
return F;
}
template<class T>
vvc<T>trans(const vvc<T>&a){
assert(a.size()&&a[0].size());
vvc<T>b(a[0].size(),vc<T>(a.size()));
rep(i,a.size())rep(j,a[0].size()){
b[j][i]=a[i][j];
}
return b;
}
template<class T>
vc<string>trans(const vc<string>&a){
assert(a.size()&&a[0].size());
vc<string>b(a[0].size(),string(a.size(),0));
rep(i,a.size())rep(j,a[0].size()){
b[j][i]=a[i][j];
}
return b;
}
template<class T>
int popcount(T n){
return __builtin_popcountll(n);
}
template<class T,class L=ll>
L sum(vc<T>&a){
return accumulate(all(a),L(0));
}
template<class T>
vc<T>subset(T S){
vc<T>ans;
for(T x=S;x>0;x=(x-1)&S)ans.pb(x);
ans.pb(0);
return ans;
}
template<class T>
T max(vc<T>&a){
return *max_element(all(a));
}
template<class T>
T min(vc<T>&a){
return *min_element(all(a));
}
#ifndef COMPRESSER_STRUCT
#define COMPRESSER_STRUCT
template<class T>
struct compresser{
vc<T>x;
compresser(int n=0){x.reserve(n);}
compresser(const vc<T>&xs){
x=xs;
}
void push(T p){built=false;x.pb(p);}
bool built=false;
void build(){
if(!chmax(built,1))return;
sort(all(x));
x.erase(unique(all(x)),x.end());
}
int find(T v){
build();
auto itr=lower_bound(all(x),v)-x.begin();
if(itr==x.size()||x[itr]!=v)return -1;
return itr;
}
int find_next(T v){
build();
return lower_bound(all(x),v)-x.begin();
}
int size(){
build();
return x.size();
}
T operator[](int i)const{
assert(0<=i&&i<x.size());
return x[i];
}
};
#endif
template<class T,class L=ll>
vc<L> presum(vc<T> &a){
vc<L> ret(a.size()+1);
rep(i,a.size())ret[i+1]=ret[i]+a[i];
return ret;
}
template<class T, class F>
vc<T> &operator+=(vc<T> &a,F b){
for (auto&v:a)v += b;
return a;
}
template<class T, class F>
vc<T> &operator-=(vc<T>&a,F b){
for (auto&v:a)v-=b;
return a;
}
template<class T, class F>
vc<T> &operator*=(vc<T>&a,F b){
for (auto&v:a)v*=b;
return a;
}
template<class T=ll>
constexpr T pow(T a,T b){
T res=1;
while(b){
if(b&1)res*=a;
a*=a;
b/=2;
}
return res;
}
constexpr ll ten(ll a){
return pow<ll>(10,a);
}
template<typename T>constexpr T inf=numeric_limits<T>::max()/2-1;
template<class T>
int tbit(T x){
using U=make_unsigned_t<T>;
U y=(U)x;
return y?(int)bit_width(y)-1:-1;
}
template<class T>
int lbit(T x){
using U=make_unsigned_t<T>;
U y=(U)x;
return y?(int)countr_zero(y):-1;
}
template<class T>
int tbit(T x,int p){
using U=make_unsigned_t<T>;
constexpr int W=numeric_limits<U>::digits;
U y=(U)x;
if(p<0)return -1;
if(p>=W-1)return tbit(y);
return tbit(y&((U(1)<<(p+1))-1));
}
template<class T>
int lbit(T x,int p){
using U=make_unsigned_t<T>;
constexpr int W=numeric_limits<U>::digits;
U y=(U)x;
if(p<0)return lbit(y);
if(p>=W)return -1;
return lbit(y&(~U(0)<<p));
}
istream& operator>>(istream&is,i128&x){
string s;is>>s;
x=0;
int i=0,neg=0;
if(s[0]=='-')neg=1,i=1;
for(;i<(int)s.size();i++)x=x*10+s[i]-'0';
if(neg)x=-x;
return is;
}
ostream& operator<<(ostream&os,i128 x){
if(x==0)return os<<0;
if(x<0)os<<"-";
u128 y=x<0?-(u128)x:(u128)x;
string s;
while(y)s.pb('0'+y%10),y/=10;
reverse(all(s));
return os<<s;
}
#ifdef LOCAL
#include "debug/debug.hpp"
#else
#define dbg(...) ((void)0)
#endif
struct template_setup{
template_setup(){
#ifdef LOCAL
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
cin.tie(0)->sync_with_stdio(0);
#ifdef LOCAL
cout<<fixed<<setprecision(6);
dbg("==============="s);
#else
cout<<fixed<<setprecision(20);
#endif
}
};
inline template_setup template_setup;
#endif
//n<=2^32
struct bit_vector{
using u64=uint64_t;
vc<u64>big_block;
vc<u64>pattern;
constexpr static inline int B=64;
int n;
bit_vector()=default;
bit_vector(const vc<int>&v):n(v.size()){
big_block.assign((n+B-1)/B+1,0);
pattern.assign(big_block.size()+1,0);
REP(i,1,big_block.size()){
u64 val=0;
uint shift=0;
uint lim=min<uint>(v.size(),i*B);
REP(l,(i-1)*B,lim){
assert(v[l]==0||v[l]==1);
val+=(u64)v[l]<<(shift++);
}
big_block[i]=big_block[i-1]+__builtin_popcountll(val);
pattern[i-1]=val;
}
}
//[0,r)
int rank1(int r)const{
assert(0<=r&&r<=n);
int res=big_block[r/B];
res+=__builtin_popcountll(pattern[r/B]&((u64(1)<<(r%B))-1));
return res;
}
int rank1(int l,int r)const{
assert(0<=l&&l<=r&&r<=n);
return rank1(r)-rank1(l);
}
int rank0(int r)const{
assert(0<=r&&r<=n);
return r-rank1(r);
}
int rank0(int l,int r)const{
assert(0<=l&&l<=r&&r<=n);
return r-l-rank1(l,r);
}
};
template<class T,int D>
struct wm_base{
using U=make_unsigned_t<T>;
int n;
array<bit_vector,D>bv;
array<int,D>mid;
vc<T>gi;
void build(vc<T>a){
gi=a,n=a.size();
vc<int>send(n);
drep(bit,D){
rep(i,n)send[i]=a[i]>>bit&1;
bv[bit]=bit_vector(send);
vc<T>l,r;
rep(i,n){
if(a[i]>>bit&1)r.pb(a[i]);
else l.pb(a[i]);
}
mid[bit]=l.size();
l.insert(l.end(),all(r));
a=move(l);
}
}
template<class F>
void build_info(F f){
auto a=gi;
vc<int>idx(n);
rep(i,n)idx[i]=i;
f(a,idx,D);
drep(bit,D){
vc<T>l,r;
vc<int>li,ri;
rep(i,n){
if(a[i]>>bit&1)r.pb(a[i]),ri.pb(idx[i]);
else l.pb(a[i]),li.pb(idx[i]);
}
l.insert(l.end(),all(r));
li.insert(li.end(),all(ri));
a=move(l);
idx=move(li);
f(a,idx,bit);
}
}
template<class L>
static array<vc<L>,D+1>& lower_pref(){
static array<vc<L>,D+1>pref;
return pref;
}
template<class L>
void buildlowersum(const vc<L>&W){
auto&pref=lower_pref<L>();
build_info([&](const vc<T>&a,const vc<int>&idx,int bit){
pref[bit]=vc<L>(n+1);
rep(i,n)pref[bit][i+1]=pref[bit][i]+W[idx[i]];
});
}
struct node{
int bit,l,r;
int size()const{return r-l;}
};
struct child_type{
bool has;
node zero,one;
};
child_type child(node x)const{
if(x.bit==-1)return {false,{},{}};
int a0=bv[x.bit].rank0(x.l);
int b0=bv[x.bit].rank0(x.r);
int a1=x.l-a0,b1=x.r-b0;
return {true,{x.bit-1,a0,b0},{x.bit-1,mid[x.bit]+a1,mid[x.bit]+b1}};
}
T kth_smallest(int l,int r,int k)const{
T ans=0;
node x={D-1,l,r};
while(x.bit!=-1){
child_type ch=child(x);
if(ch.zero.size()<=k){
k-=ch.zero.size();
ans+=T(1)<<x.bit;
x=ch.one;
}else x=ch.zero;
}
return ans;
}
ll count_less(int l,int r,T k)const{
ll ans=0;
node x={D-1,l,r};
while(x.bit!=-1){
child_type ch=child(x);
if(k>>x.bit&1)ans+=ch.zero.size(),x=ch.one;
else x=ch.zero;
}
return ans;
}
//[l,r) [d,u)
ll count(int l,int r,T d,T u)const{
return count_less(l,r,u)-count_less(l,r,d);
}
template<class L>
L lower_sum(int l,int r,T u)const{
auto&pref=lower_pref<L>();
L ans=0;
node x={D-1,l,r};
while(x.bit!=-1){
child_type ch=child(x);
if(u>>x.bit&1){
ans+=pref[x.bit][ch.zero.r]-pref[x.bit][ch.zero.l];
x=ch.one;
}else x=ch.zero;
}
return ans;
}
template<class L>
L lower_sum(int l,int r,T d,T u)const{
return lower_sum<L>(l,r,u)-lower_sum<L>(l,r,d);
}
};
template<int N>
struct ninfo{
array<int,N>mf_;
bool built_=0;
void buildp(){
if(built_)return;
built_=1;
REP(i,1,N)mf_[i]=i;
REP(i,2,N){
if(mf_[i]==i&&i*i<N)for(int j=i*i;j<N;j+=i){
chmin(mf_[j],i);
}
}
}
vc<pair<int,int>>factorize(int n){
assert(0<n&&n<N);
buildp();
vc<pair<int,int>>res;
while(n>1){
int tar=mf_[n];
res.pb({tar,0});
while(n%tar==0){
n/=tar;
res.back().second++;
}
}
return res;
}
vc<int>div(int n){
vc<int>res{1};
for(auto [p,e]:factorize(n)){
int sz=res.size(),pw=1;
rep(i,e){
pw*=p;
rep(j,sz)res.pb(res[j]*pw);
}
}
sort(all(res));
return res;
}
int mobius(int n){
auto pf=factorize(n);
for(auto [p,e]:pf)if(e>1)return 0;
return pf.size()%2?-1:1;
}
bool is_prime(int n){
assert(0<=n&&n<N);
buildp();
return n>=2&&mf_[n]==n;
}
int phi(int n){
ll res=n;
while(n>1){
res*=mf_[n]-1;
res/=mf_[n];
int M=mf_[n];
while(n%M==0)n/=M;
}
return res;
}
};
void solve(){
INT(q);
const int N=ten(6)+10;
ninfo<N>nin;
nin.buildp();
vc<int>vs(N);
REP(i,1,N){
vs[i]=i/nin.mf_[i];
}
vs[1]=0;
wm_base<int,30>wms(N);wms.build(vs);
while(q--){
LL(l,r);
++r;
PRT(wms.count_less(l,r,l));
}
}
signed main(){
int t=1;
// cin >> t;
while(t--)solve();
}
ryoku