結果

問題 No.2262 Fractions
ユーザー RubikunRubikun
提出日時 2023-04-07 22:51:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 318 ms / 2,000 ms
コード長 10,656 bytes
コンパイル時間 2,283 ms
コンパイル使用メモリ 218,416 KB
実行使用メモリ 35,540 KB
最終ジャッジ日時 2024-05-05 07:23:30
合計ジャッジ時間 14,743 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 226 ms
35,404 KB
testcase_01 AC 140 ms
35,408 KB
testcase_02 AC 138 ms
35,536 KB
testcase_03 AC 137 ms
35,408 KB
testcase_04 AC 139 ms
35,408 KB
testcase_05 AC 135 ms
35,404 KB
testcase_06 AC 143 ms
35,536 KB
testcase_07 AC 141 ms
35,536 KB
testcase_08 AC 138 ms
35,408 KB
testcase_09 AC 142 ms
35,408 KB
testcase_10 AC 139 ms
35,412 KB
testcase_11 AC 226 ms
35,536 KB
testcase_12 AC 230 ms
35,408 KB
testcase_13 AC 232 ms
35,408 KB
testcase_14 AC 232 ms
35,412 KB
testcase_15 AC 228 ms
35,536 KB
testcase_16 AC 173 ms
35,404 KB
testcase_17 AC 167 ms
35,404 KB
testcase_18 AC 163 ms
35,412 KB
testcase_19 AC 288 ms
35,540 KB
testcase_20 AC 279 ms
35,408 KB
testcase_21 AC 292 ms
35,412 KB
testcase_22 AC 277 ms
35,404 KB
testcase_23 AC 257 ms
35,532 KB
testcase_24 AC 307 ms
35,408 KB
testcase_25 AC 297 ms
35,532 KB
testcase_26 AC 299 ms
35,408 KB
testcase_27 AC 305 ms
35,536 KB
testcase_28 AC 293 ms
35,536 KB
testcase_29 AC 298 ms
35,412 KB
testcase_30 AC 298 ms
35,404 KB
testcase_31 AC 300 ms
35,408 KB
testcase_32 AC 311 ms
35,404 KB
testcase_33 AC 316 ms
35,412 KB
testcase_34 AC 310 ms
35,536 KB
testcase_35 AC 318 ms
35,404 KB
testcase_36 AC 312 ms
35,408 KB
testcase_37 AC 99 ms
35,536 KB
testcase_38 AC 101 ms
35,532 KB
testcase_39 AC 289 ms
35,400 KB
testcase_40 AC 292 ms
35,404 KB
testcase_41 AC 302 ms
35,532 KB
testcase_42 AC 291 ms
35,540 KB
testcase_43 AC 287 ms
35,444 KB
testcase_44 AC 302 ms
35,412 KB
testcase_45 AC 299 ms
35,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//https://atcoder.jp/contests/arc123/submissions/24171616

#include <bits/stdc++.h>
using namespace std;

using ll=long long;
#define int ll

#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define si(x) int(x.size())
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif

template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}

template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;

using pi=pair<int,int>;
using vi=vc<int>;

template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
    return os<<"{"<<p.a<<","<<p.b<<"}";
}

template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
    os<<"{";
    for(auto e:v)os<<e<<",";
    return os<<"}";
}

#define mp make_pair
#define mt make_tuple
#define one(x) memset(x,-1,sizeof(x))
#define zero(x) memset(x,0,sizeof(x))
#ifdef LOCAL
void dmpr(ostream&os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
    os<<t<<" ";
    dmpr(os,args...);
}
#define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__)
#else
#define dmp2(...) void(0)
#endif

using uint=unsigned;
using ull=unsigned long long;

template<class t,size_t n>
ostream& operator<<(ostream&os,const array<t,n>&a){
    return os<<vc<t>(all(a));
}

template<int i,class T>
void print_tuple(ostream&,const T&){
}

template<int i,class T,class H,class ...Args>
void print_tuple(ostream&os,const T&t){
    if(i)os<<",";
    os<<get<i>(t);
    print_tuple<i+1,T,Args...>(os,t);
}

template<class ...Args>
ostream& operator<<(ostream&os,const tuple<Args...>&t){
    os<<"{";
    print_tuple<0,tuple<Args...>,Args...>(os,t);
    return os<<"}";
}

template<class t>
void print(t x,int suc=1){
    cout<<x;
    if(suc==1)
        cout<<"\n";
    if(suc==2)
        cout<<" ";
}

ll read(){
    ll i;
    cin>>i;
    return i;
}

vi readvi(int n,int off=0){
    vi v(n);
    rep(i,n)v[i]=read()+off;
    return v;
}

pi readpi(int off=0){
    int a,b;cin>>a>>b;
    return pi(a+off,b+off);
}

template<class t,class u>
void print(const pair<t,u>&p,int suc=1){
    print(p.a,2);
    print(p.b,suc);
}

template<class T>
void print(const vector<T>&v,int suc=1){
    rep(i,v.size())
    print(v[i],i==int(v.size())-1?suc:2);
}

string readString(){
    string s;
    cin>>s;
    return s;
}

template<class T>
T sq(const T& t){
    return t*t;
}

//#define CAPITAL
void yes(bool ex=true){
#ifdef CAPITAL
    cout<<"YES"<<"\n";
#else
    cout<<"Yes"<<"\n";
#endif
    if(ex)exit(0);
#ifdef LOCAL
    cout.flush();
#endif
}
void no(bool ex=true){
#ifdef CAPITAL
    cout<<"NO"<<"\n";
#else
    cout<<"No"<<"\n";
#endif
    if(ex)exit(0);
#ifdef LOCAL
    cout.flush();
#endif
}
void possible(bool ex=true){
#ifdef CAPITAL
    cout<<"POSSIBLE"<<"\n";
#else
    cout<<"Possible"<<"\n";
#endif
    if(ex)exit(0);
#ifdef LOCAL
    cout.flush();
#endif
}
void impossible(bool ex=true){
#ifdef CAPITAL
    cout<<"IMPOSSIBLE"<<"\n";
#else
    cout<<"Impossible"<<"\n";
#endif
    if(ex)exit(0);
#ifdef LOCAL
    cout.flush();
#endif
}

constexpr ll ten(int n){
    return n==0?1:ten(n-1)*10;
}

const ll infLL=LLONG_MAX/3;

#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif

int topbit(signed t){
    return t==0?-1:31-__builtin_clz(t);
}
int topbit(ll t){
    return t==0?-1:63-__builtin_clzll(t);
}
int botbit(signed a){
    return a==0?32:__builtin_ctz(a);
}
int botbit(ll a){
    return a==0?64:__builtin_ctzll(a);
}
int popcount(signed t){
    return __builtin_popcount(t);
}
int popcount(ll t){
    return __builtin_popcountll(t);
}
bool ispow2(int i){
    return i&&(i&-i)==i;
}
ll mask(int i){
    return (ll(1)<<i)-1;
}

bool inc(int a,int b,int c){
    return a<=b&&b<=c;
}

template<class t> void mkuni(vc<t>&v){
    sort(all(v));
    v.erase(unique(all(v)),v.ed);
}

ll rand_int(ll l, ll r) { //[l, r]
#ifdef LOCAL
    static mt19937_64 gen;
#else
    static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
#endif
    return uniform_int_distribution<ll>(l, r)(gen);
}

template<class t>
void myshuffle(vc<t>&a){
    rep(i,si(a))swap(a[i],a[rand_int(0,i)]);
}

template<class t>
int lwb(const vc<t>&v,const t&a){
    return lower_bound(all(v),a)-v.bg;
}

vvc<int> readGraph(int n,int m){
    vvc<int> g(n);
    rep(i,m){
        int a,b;
        cin>>a>>b;
        //sc.read(a,b);
        a--;b--;
        g[a].pb(b);
        g[b].pb(a);
    }
    return g;
}

vvc<int> readTree(int n){
    return readGraph(n,n-1);
}

template<class t=ll>
t extgcd(t a,t b,t&x,t&y){
    if(b==0){
        x=1;
        y=0;
        return a;
    }else{
        t g=extgcd(b,a%b,y,x);
        y-=a/b*x;
        return g;
    }
}


const int vmax=ten(6)+10;
bool pri[vmax];
vi ps;
int sf[vmax]; //smallest factor, sf[1] is undefined(0)
void linear_sieve(){
    rng(i,2,vmax)
    pri[i]=1;
    rng(i,2,vmax){
        if(pri[i]){
            ps.pb(i);
            sf[i]=i;
        }
        for(int j=0;i*ps[j]<vmax;j++){
            pri[i*ps[j]]=0;
            sf[i*ps[j]]=ps[j];
            if(i%ps[j]==0)break;
        }
    }
}

int mu[vmax];
vi ms;//all m s.t. mu[m] is non-zero

ll phi[vmax];
void initmu(){
    mu[1]=1;
    ms.pb(1);
    for(int i=1;i<vmax;i++) phi[i]=i;
    rng(i,2,vmax){
        if(pri[i]){
            for(int j=i;j<vmax;j+=i){
                phi[j]/=i;
                phi[j]*=(i-1);
            }
        }
        if(i%(sf[i]*sf[i])==0)continue;
        mu[i]=-mu[i/sf[i]];
        if(mu[i])ms.pb(i);
    }
    for(int i=1;i<vmax;i++) phi[i]+=phi[i-1];
}

ll fdiv(ll a, ll b) { // floored division
    return a / b - ((a ^ b) < 0 && a % b); }

ll cdiv(ll a, ll b) { // ceiled division
    return a / b + ((a ^ b) > 0 && a % b); }

bool cmpf(pi a,pi b){
    return a.a*b.b<a.b*b.a;
}

//p/q <= num/den, 0<=p, 1<=q<=n
//の形で表される有理数の個数を返す
//O(N)
int count_frac(int n,int num,int den){
    int ans=0;
    rng(v,1,n+1){
        int w=max<int>((num*v+den)/den,0);
        int s=0;
        for(auto m:ms){
            if(v*m>n)break;
            s+=mu[m];
        }
        ans+=w*s;
    }
    dmp2(n,num,den,ans);
    return ans;
}

//num/den < p/q, (0<=p,1<=q<=n)
//の形で表される既約分数を小さい方から len 個返す
//O(N+Len)
vc<pi> enum_frac(int len,int n,int num,int den){
    vc<pi> res;
    res.reserve(len);
    auto dfs=[&](auto self,pi l,pi r)->void{
        if(si(res)==len)return;
        pi m(l.a+r.a,l.b+r.b);
        if(m.b>n)return;
        self(self,l,m);
        if(si(res)==len)return;
        res.pb(m);
        self(self,m,r);
    };
    pi l(fdiv(num,den),1);
    pi r(l.a+1,1);
    rng(v,2,n+1){
        pi w(fdiv(num*v,den),v);
        if(cmpf(l,w))l=w;
        w.a++;
        if(cmpf(w,r))r=w;
    }
    while(si(res)<len){
        if(l.b<r.b){
            pi z(r.a-l.a,r.b-l.b);
            res.pb(r);
            dfs(dfs,r,z);
            r=z;
        }else if(l.b>r.b){
            l.a-=r.a;
            l.b-=r.b;
        }else{
            assert(l.b==1);
            l.a++;
            r.a++;
            res.pb(l);
            dfs(dfs,l,r);
        }
    }
    return res;
}

/*
 int gcd(int a,int b){
 if(a<0)a=-a;
 if(b<0)b=-b;
 if(a==0)return b;
 if(b==0)return a;
 int s=botbit(a|b);
 a>>=botbit(a);
 do{
 b>>=botbit(b);
 if(a>b)swap(a,b);
 b-=a;
 }while(b);
 return a<<s;
 }
 
 vc<pi> enum_frac(int len,int n,int num,int den){
 struct picmp{
 bool operator()(const pi&a,const pi&b)const{
 return a.a*b.b>a.b*b.a;
 }
 };
 priority_queue<pi,vc<pi>,picmp> pq;
 rng(v,1,n+1){
 int w=fdiv(num*v,den);
 do{w++;
 }while(gcd(w,v)!=1);
 pq.emplace(w,v);
 }
 vc<pi> res;
 while(si(res)<len){
 pi z=pq.top();pq.pop();
 res.pb(z);
 do{z.a++;
 }while(gcd(z.a,z.b)!=1);
 pq.push(z);
 }
 return res;
 }
 */

void slv(){
    int a,b,mx,l,r;
    a=1;b=1;
    cin>>mx>>l;
    r=l+1;
    if(phi[mx]==l){
        cout<<"1/1\n";
    }else if(phi[mx]>l){
        int x,y;
        int g=extgcd(a,b,x,y);
        dmp2(a,b,g);
        a/=g;
        b/=g;
        mx/=g;
        if(y>0){
            x+=b;
            y-=a;
        }
        assert(y<=0);
        y=-y;
        
        int den=sq(mx+1);
        int num=cdiv(y*den,a)-1;
        
        dmp2(num,den);
        
        {
            int tmp=count_frac(mx,num,den);
            l+=tmp;
            r+=tmp;
        }
        
        dmp2(l,r);
        
        int lw=-1,up=den;
        while(up-lw>1){
            const int mid=(lw+up)/2;
            int cnt=count_frac(mx,mid,den);
            if(cnt<=l)lw=mid;
            else up=mid;
        }
        
        dmp2(up,den);
        
        dmp2(r-l,mx,lw,den);
        auto ans=enum_frac(r-l,mx,lw,den);
        dmp(ans);
        vi res;
        for(auto [p,q]:ans){
            //res.pb(q*g);
            cout<<p<<"/"<<q<<"\n";
        }
    }else{
        l-=(phi[mx]-1);
        r-=(phi[mx]-1);
        
        if(l>phi[mx]){
            cout<<-1<<"\n";
        }else{
            l=phi[mx]+1-l;
            r=l+1;
            int x,y;
            int g=extgcd(a,b,x,y);
            dmp2(a,b,g);
            a/=g;
            b/=g;
            mx/=g;
            if(y>0){
                x+=b;
                y-=a;
            }
            assert(y<=0);
            y=-y;
            
            int den=sq(mx+1);
            int num=cdiv(y*den,a)-1;
            
            dmp2(num,den);
            
            {
                int tmp=count_frac(mx,num,den);
                l+=tmp;
                r+=tmp;
            }
            
            dmp2(l,r);
            
            int lw=-1,up=den;
            while(up-lw>1){
                const int mid=(lw+up)/2;
                int cnt=count_frac(mx,mid,den);
                if(cnt<=l)lw=mid;
                else up=mid;
            }
            
            dmp2(up,den);
            
            dmp2(r-l,mx,lw,den);
            auto ans=enum_frac(r-l,mx,lw,den);
            dmp(ans);
            vi res;
            for(auto [p,q]:ans){
                //res.pb(q*g);
                cout<<q<<"/"<<p<<endl;
            }
        }
    }
    
}

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout<<fixed<<setprecision(20);
    
    linear_sieve();
    initmu();
    
    int t;cin>>t;
    rep(_,t) slv();
}

0