QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#844809 | #9962. Diminishing Fractions | ucup-team6274 | TL | 1423ms | 4368kb | C++20 | 3.4kb | 2025-01-06 11:15:34 | 2025-01-06 11:15:34 |
Judging History
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 mod){
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;
int phi[50010];
bool nsp[50010];
int tot,prm[50010];
void sieve(){
for(int i=2;i<=5e4;i++){
if(!nsp[i]){
phi[i]=i-1;
prm[++tot]=i;
}
if(i<=10)deb(i),debl(phi[i]);
for(int j=1;prm[j]*i<=5e4;j++){
nsp[i*prm[j]]=1;
if(i%prm[j]==0){
phi[i*prm[j]]=phi[i]*prm[j];
break;
}else{
phi[i*prm[j]]=phi[i]*(prm[j]-1);
}
}
}
}
int c[50010],a[50010],t[50010];
// int rec[7010][7010];
int Inv(int a,int b){
return fpm(a,phi[b]-1,b);
}
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);
(t[j]*=y+M*x*Inv(a[i],a[j]))%=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
*/
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 4064kb
input:
2 3 6
output:
1/1-1/2-1/3 1/1-1/4-1/3-2/5
result:
ok OK, t = 2
Test #2:
score: 0
Accepted
time: 1ms
memory: 4348kb
input:
1 1
output:
1/1
result:
ok OK, t = 1
Test #3:
score: 0
Accepted
time: 1ms
memory: 4156kb
input:
10 1 2 3 4 5 6 7 8 9 10
output:
1/1 1/2 1/1-1/2-1/3 -1/1+3/4+1/3 1/1-1/4-1/3-2/5 1/1-1/4-1/3-2/5 1/1-3/4-1/3-1/5+2/7 1/8+1/3-3/5+1/7 1/1-5/8-8/9+4/5-2/7 1/1-5/8-8/9+4/5-2/7
result:
ok OK, t = 10
Test #4:
score: 0
Accepted
time: 1ms
memory: 4064kb
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-1/3-1/5+2/7 -1/1+15/16+5/9+3/5-5/7+9/11-9/13-5/17-4/19 1/2 1/1-1/2-1/3 -1/1+3/4+1/3 1/1-1/4-1/3-2/5 1/1-1/4-1/3-2/5 1/1-3/4-1/3-1/5+2/7 1/8+1/3-3/5+1/7 1/1-5/8-8/9+4/5-2/7 1/1-5/8-8/9+4/5-2/7 1/1-7/8-4/9+4/5-4/7+1/11 1/1-7/8-4/9+4/5-4/7+1/11 -1/1+5/8+8/9-2/5+4/7-5/11-3/13 -1/1+5/8+8/9-2/5+4/...
result:
ok OK, t = 54
Test #5:
score: 0
Accepted
time: 4ms
memory: 4368kb
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:
-4/1+27/32+2/27+21/25+26/49-1/11+2/13-6/17-11/19+19/23-5/29+18/31+21/37+26/41-13/43+21/47 -3/1+15/32+25/27+7/25+31/49-5/11+2/13-3/17-2/19+6/23-28/29+29/31+36/37+9/41-40/43+27/47+11/53 -3/1+15/32+25/27+7/25+31/49-5/11+2/13-3/17-2/19+6/23-28/29+29/31+36/37+9/41-40/43+27/47+11/53 4/1-3/32-22/27-2/25-41...
result:
ok OK, t = 92
Test #6:
score: 0
Accepted
time: 240ms
memory: 3988kb
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:
-9/1+169/512+106/243-123/125-153/343-26/121+11/169+288/289+24/361+355/529-22/29+21/31+28/37-26/41+25/43+41/47+4/53-41/59+51/61+50/67-53/71-7/73+47/79+77/83+82/89+85/97-11/101-92/103+66/107+76/109+28/113+16/127+91/131+34/137+23/139+7/149-22/151+75/157+8/163-147/167-103/173-41/179-141/181-61/191+120/1...
result:
ok OK, t = 1000
Test #7:
score: 0
Accepted
time: 1423ms
memory: 4072kb
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:
3/1-749/1024-319/729+319/625-153/343-105/1331+162/169+114/289-192/361-227/529+370/841-954/961+299/1369+958/1681-22/43+23/47+47/53+14/59+19/61-10/67+26/71+14/73+20/79-40/83+65/89-34/97+7/101-43/103+88/107+4/109-6/113-35/127-47/131+20/137-62/139+142/149+64/151+108/157-72/163-63/167-124/173-159/179-71/...
result:
ok OK, t = 1000
Test #8:
score: -100
Time Limit Exceeded
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:
4/1+85/2048+596/729+193/625-129/343-1164/1331-151/169+103/289+124/361+275/529+487/841-429/961-282/1369+582/1681+91/1849-33/47-22/53+57/59-3/61-45/67+54/71+35/73+41/79+33/83+26/89-68/97-58/101+23/103-53/107-54/109-56/113-60/127+58/131-99/137-24/139-144/149+52/151-55/157-66/163-21/167-49/173-22/179+17...