QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#844794#9962. Diminishing Fractionsucup-team6274TL 1442ms31000kbC++203.2kb2025-01-06 11:08:252025-01-06 11:08:26

Judging History

你现在查看的是最新测评结果

  • [2025-01-06 11:08:26]
  • 评测
  • 测评结果:TL
  • 用时:1442ms
  • 内存:31000kb
  • [2025-01-06 11:08:25]
  • 提交

answer

#pragma GCC optimize("Ofast")


#include<bits/stdc++.h>
bool Mbg;
using namespace std;
#define vec vector
#define pb push_back
#define eb emplace_back
#define siz(vec) ((int)(vec).size())
#define all(vec) (vec).begin(),(vec).end()
template<class T>
void operator +=(vec<T> &a,T b){a.push_back(b);}
template<class T>
void operator --(vec<T> &a){a.pop_back();}
#define pii pair<int,int>
#define x first
#define y second
#define mp make_pair
#define exc(exp) if(exp)continue;
#define stop(exp) if(exp)break;
#define ret(exp) if(exp)return;
#define deb(var) cerr<<#var<<'='<<(var)<<"; "
#define debl(var) cerr<<#var<<'='<<(var)<<";\n"
#define ins insert
#define era erase
#define lb lower_bound
#define ub upper_bound
#define int long long
#define inf (long long)(1e9)
template<class T>
bool Min(T &x,T y){return x>y?x=y,1:0;}
template<class T>
bool Max(T &x,T y){return x<y?x=y,1:0;}
const int mod=1e9+7;
void Add(int &x,int y){x=x+y<mod?x+y:x+y-mod;}
void Dec(int &x,int y){x=x>=y?x-y:x-y+mod;}
int fpm(int x,int y){
    int ans=1;for(;y;y>>=1,(x*=x)%=mod)if(y&1)(ans*=x)%=mod;return ans;
}
void exgcd(int a,int b,int &x,int &y){
	if(!b){
        x=1,y=0;
    }else{
        exgcd(b,a%b,y,x);y-=a/b*x;
    }
}
int n;
bool nsp[50010];
void sieve(){
    for(int i=2;i<=5e4;i++){
        if(!nsp[i]){
            for(int j=i+i;j<=5e4;j+=i){
                nsp[j]=1;
            }
        }
    }
}


int c[50010],a[50010],t[50010];

int rec[7010][7010];
int Inv(int a,int b){
    if(max(a,b)<=7000&&rec[a][b]!=0){
        return rec[a][b];
    }
    int x,y;
    exgcd(a,b,x,y);

    if(max(a,b)<=7000)rec[a][b]=x;
    return x;
}
void work(){
    cin>>n;
    if(n==1){
        cout<<"1/1\n";
        return;
    }

    vec<int> ps;
    for(int i=2;i<=n;i++){
        exc(nsp[i]);
        ps+=i;

        c[i]=0,a[i]=1;
        while(a[i]*i<=n){
            a[i]*=i,++c[i];
        }
    }

    
    for(int i=2;i<=n;i++){
        exc(nsp[i]);
        
        int M=1,T=0;
        for(auto j:ps){
            stop(j>=i);
            M=M*a[j];
            if(((T++)&3)==3){
                M=M%a[i];
            }
        }
        M=M%a[i];


        int x,y;
        exgcd(a[i],M,y,x);

        t[i]=x%a[i];


        for(auto j:ps){
            stop(j>=i);

            int iv=Inv(a[i],a[j]);

            (t[j]*=y+M*x*iv)%=a[j];
        }
    }

    long double ext=0;
    for(int i=1;i<=n;i++){
        exc(nsp[i]||!t[i]);
        ext+=(long double)t[i]/a[i];
    }


    int x=-floor(ext+0.3);
    
    bool fir=1;
    if(x!=0){
        cout<<x<<"/1";
        fir=0;
    }
    
    for(int i=2;i<=n;i++){
        exc(nsp[i]||t[i]==0);
        if(!fir&&t[i]>0)cout<<'+';
        fir=0;
        cout<<t[i]<<'/'<<a[i];
    }
    cout<<'\n';
}
bool Med;
signed main(){
    sieve();
    ios::sync_with_stdio(0),
    cin.tie(0),cout.tie(0);
    int T=1;cin>>T;while(T--)work();
    // cerr<<"Time: "<<clock()<<" ms;\n";
    // cerr<<"Memory: "<<abs(&Med-&Mbg)/1024.0/1024.0<<" MiB.\n";
}
/*
- CONTINUE, NON-STOPPING, FOR THE FAITH
- START TYPING IF YOU DON'T KNOW WHAT TO DO
- STOP TYPING IF YOU DON'T KNOW WHAT YOU'RE DOING
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5744kb

input:

2
3
6

output:

1/1-1/2-1/3
-1/1+3/4+2/3-2/5

result:

ok OK, t = 2

Test #2:

score: 0
Accepted
time: 1ms
memory: 3880kb

input:

1
1

output:

1/1

result:

ok OK, t = 1

Test #3:

score: 0
Accepted
time: 1ms
memory: 3640kb

input:

10
1
2
3
4
5
6
7
8
9
10

output:

1/1
1/2
1/1-1/2-1/3
-1/4+1/3
-1/1+3/4+2/3-2/5
-1/1+3/4+2/3-2/5
-1/1-3/4+2/3+4/5+2/7
1/8-2/3+2/5+1/7
2/1-5/8-8/9-1/5-2/7
2/1-5/8-8/9-1/5-2/7

result:

ok OK, t = 10

Test #4:

score: 0
Accepted
time: 1ms
memory: 3700kb

input:

54
7
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
3
47
81

output:

-1/1-3/4+2/3+4/5+2/7
1/1-1/16-4/9-2/5-5/7+9/11-9/13+12/17-4/19
1/2
1/1-1/2-1/3
-1/4+1/3
-1/1+3/4+2/3-2/5
-1/1+3/4+2/3-2/5
-1/1-3/4+2/3+4/5+2/7
1/8-2/3+2/5+1/7
2/1-5/8-8/9-1/5-2/7
2/1-5/8-8/9-1/5-2/7
1/1-7/8+5/9-1/5-4/7+1/11
1/1-7/8+5/9-1/5-4/7+1/11
-1/1-3/8+8/9+3/5-3/7+6/11-3/13
-1/1-3/8+8/9+3/5-3/7...

result:

ok OK, t = 54

Test #5:

score: 0
Accepted
time: 0ms
memory: 6184kb

input:

92
51
54
57
60
63
66
69
72
75
78
81
84
87
90
93
96
99
102
105
108
111
114
117
120
123
126
129
132
135
138
141
144
147
150
153
156
159
162
165
168
171
174
177
180
183
186
189
192
195
198
201
204
207
210
213
216
219
222
225
228
231
234
237
240
243
246
249
252
255
258
261
264
267
270
273
276
279
282
28...

output:

-1/1-5/32-25/27-4/25+26/49+10/11+2/13+11/17+8/19-4/23-5/29+18/31-16/37-15/41-13/43+21/47
-1/1+15/32+25/27+7/25-18/49+6/11+2/13-3/17-2/19-17/23+1/29-2/31-1/37+9/41-40/43+27/47+11/53
-1/1+15/32+25/27+7/25-18/49+6/11+2/13-3/17-2/19-17/23+1/29-2/31-1/37+9/41-40/43+27/47+11/53
5/1+29/32-22/27+23/25+8/49-...

result:

ok OK, t = 92

Test #6:

score: 0
Accepted
time: 46ms
memory: 5720kb

input:

1000
622
149
839
472
292
345
799
941
449
15
907
494
48
430
505
66
83
97
10
548
286
644
528
249
573
860
557
932
746
262
971
157
603
715
984
333
53
916
784
649
70
768
780
64
118
616
979
466
24
2
517
774
566
419
182
222
40
169
951
830
977
383
355
770
134
973
917
3
854
31
35
825
109
945
671
536
952
888
...

output:

3/1-343/512+106/243-123/125+190/343+95/121-158/169-1/289-337/361+355/529-22/29+21/31+28/37-26/41+25/43-6/47+4/53-41/59-10/61+50/67-53/71-7/73-32/79-6/83+82/89-12/97+90/101+11/103-41/107-33/109+28/113-111/127-40/131+34/137-116/139-142/149+129/151+75/157+8/163+20/167+70/173+138/179+40/181-61/191+120/1...

result:

ok OK, t = 1000

Test #7:

score: 0
Accepted
time: 216ms
memory: 11212kb

input:

1000
1748
1741
1576
1940
1648
1825
1183
1447
1318
1277
1913
1208
1417
1388
1143
1581
1222
1904
1407
1006
1175
1218
1734
1934
1003
1704
1984
1399
1333
1840
1317
1233
1133
1232
1776
1881
1476
1712
1401
1231
1978
1964
1419
1644
1103
1498
1454
1480
1377
1352
1837
1616
1009
1413
1199
1977
1477
1579
1920
...

output:

-2/1-749/1024-319/729+319/625+190/343-105/1331-7/169-175/289-192/361-227/529-471/841+7/961-1070/1369+958/1681+21/43+23/47-6/53+14/59+19/61+57/67-45/71-59/73+20/79-40/83+65/89-34/97+7/101-43/103+88/107-105/109-6/113-35/127+84/131-117/137+77/139-7/149-87/151+108/157-72/163+104/167-124/173-159/179-71/1...

result:

ok OK, t = 1000

Test #8:

score: 0
Accepted
time: 516ms
memory: 14896kb

input:

1000
2107
2115
2985
2832
2564
2529
2971
2637
2666
2172
2496
2191
2465
2199
2260
2161
2402
2369
2762
2674
2734
2252
2488
2185
2652
2014
2018
2960
2313
2063
2696
2976
2457
2247
2180
2647
2907
2901
2912
2538
2512
2623
2267
2986
2348
2170
2131
2166
2563
2111
2860
2254
2462
2080
2054
2803
2397
2778
2411
...

output:

8/1-1963/2048+596/729+193/625-129/343+167/1331+18/169-186/289-237/361-254/529-354/841-429/961-282/1369+582/1681-1758/1849-33/47-22/53-2/59+58/61-45/67-17/71-38/73-38/79-50/83-63/89+29/97+43/101-80/103+54/107-54/109-56/113-60/127-73/131+38/137+115/139-144/149-99/151+102/157-66/163+146/167-49/173+157/...

result:

ok OK, t = 1000

Test #9:

score: 0
Accepted
time: 944ms
memory: 23492kb

input:

1000
3416
3784
3793
3610
3982
3174
3253
3088
3197
3926
3664
3669
3431
3821
3340
3298
3498
3627
3194
3354
3362
3512
3865
3529
3988
3973
3852
3552
3672
3282
3035
3639
3998
3090
3771
3618
3402
3139
3903
3110
3452
3947
3941
3225
3187
3283
3243
3722
3808
3491
3309
3935
3937
3259
3909
3665
3809
3390
3285
...

output:

-5/1+921/2048+1583/2187+3038/3125+2063/2401+101/1331+1412/2197+73/289+102/361-522/529+24/841+637/961+849/1369+1339/1681+931/1849-1887/2209+2764/2809+30/59-3/61+10/67-70/71-36/73-69/79-72/83+81/89-60/97-72/101+54/103-87/107+20/109-90/113+21/127+106/131+102/137-96/139+17/149-110/151+22/157+48/163+104/...

result:

ok OK, t = 1000

Test #10:

score: 0
Accepted
time: 1442ms
memory: 31000kb

input:

899
4695
4616
4545
4771
4315
4850
4821
4722
4493
4338
4566
4144
4721
4334
4536
4313
4264
4669
4648
4780
4551
4044
4506
4798
4483
4372
4371
4636
4650
4590
4340
4383
4756
4104
4077
4067
4692
4008
4141
4610
4532
4751
4345
4498
4214
4621
4519
4505
4681
4726
4011
4879
4103
4283
4470
4844
4520
4740
4333
4...

output:

1/1+2597/4096+1553/2187+487/3125-659/2401-680/1331-2053/2197+275/289+177/361-277/529+439/841-424/961-309/1369+627/1681-1302/1849-1622/2209-962/2809-2927/3481-2057/3721-2269/4489+10/71+44/73+65/79+13/83-68/89+71/97-62/101+91/103-52/107+90/109+101/113+45/127+98/131-67/137-38/139+70/149+87/151+10/157-1...

result:

ok OK, t = 899

Test #11:

score: -100
Time Limit Exceeded

input:

1000
3798
4097
3356
3197
4364
3360
3927
4704
4072
3229
3681
3276
4187
4013
4014
3134
3743
3208
4792
3235
4788
3953
3313
4147
3230
3497
3181
4376
4631
3710
3602
4191
3405
3381
4722
3607
4374
3037
3149
3682
3557
3338
4471
3242
4470
4753
4637
3343
3625
4516
3505
3553
3626
3073
3222
4514
4402
4063
3372
...

output:

-5/1-11/2048-1123/2187+3074/3125+527/2401+772/1331+1793/2197+172/289+60/361-500/529-50/841+827/961-215/1369+1633/1681+1084/1849-1882/2209-2327/2809+122/3481+2786/3721+53/67-16/71+43/73-24/79-24/83+5/89-40/97+99/101+17/103-51/107-92/109+51/113-12/127+58/131+19/137-10/139-145/149-96/151-101/157+1/163+...

result: