QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#844890 | #9962. Diminishing Fractions | ucup-team6274 | WA | 40ms | 8716kb | C++20 | 3.9kb | 2025-01-06 12:01:38 | 2025-01-06 12:01:38 |
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
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],pr[50010],TOT,Id[50010];
struct Barrett{
int m,p;
void init(int pp) {
m=(1ll<<32)/pp;
p=pp;
}
int64_t operator()(int64_t x) {
return x-((__int128)(x*m)>>32)*p;
}
}Mod[50010];
void sieve(){
for(int i=2;i<=5e4;i++){
if(!nsp[i]){
pr[i]=i;
phi[i]=i-1;
prm[++tot]=i;
}
for(int j=1;prm[j]*i<=5e4;j++){
nsp[i*prm[j]]=1;
pr[i*prm[j]]=prm[j];
if(i%prm[j]==0){
phi[i*prm[j]]=phi[i]*prm[j];
break;
}else{
phi[i*prm[j]]=phi[i]*(prm[j]-1);
}
}
if(phi[i]==i/pr[i]*(pr[i]-1)){
Id[i]=++TOT;
Mod[i].init(i);
}
}
}
int rec[5218][5218];
int Inv(int a,int b){
if(rec[Id[a]][Id[b]]!=0)return rec[Id[a]][Id[b]];
int ans=fpm(a,phi[b]-1,b);
return rec[Id[a]][Id[b]]=ans;
}
int c[50010],a[50010],t[50010];
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){
T=0;
M=Mod[a[i]](M);
// M=M%a[i];
}
}
M=Mod[a[i]](M);
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]);
t[j]=Mod[a[j]](t[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.49);
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: 0ms
memory: 7012kb
input:
2 3 6
output:
1/2-1/3 -1/1+3/4+2/3-2/5
result:
ok OK, t = 2
Test #2:
score: 0
Accepted
time: 2ms
memory: 5548kb
input:
1 1
output:
1/1
result:
ok OK, t = 1
Test #3:
score: 0
Accepted
time: 0ms
memory: 7548kb
input:
10 1 2 3 4 5 6 7 8 9 10
output:
1/1 1/2 1/2-1/3 -1/1+3/4+1/3 -1/1+3/4+2/3-2/5 -1/1+3/4+2/3-2/5 -2/1+1/4+2/3+4/5+2/7 -1/1+1/8+1/3+2/5+1/7 -1/1+3/8+1/9+4/5-2/7 -1/1+3/8+1/9+4/5-2/7
result:
ok OK, t = 10
Test #4:
score: 0
Accepted
time: 0ms
memory: 7348kb
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:
-2/1+1/4+2/3+4/5+2/7 -4/1+15/16+5/9+3/5+2/7+9/11+4/13+12/17-4/19 1/2 1/2-1/3 -1/1+3/4+1/3 -1/1+3/4+2/3-2/5 -1/1+3/4+2/3-2/5 -2/1+1/4+2/3+4/5+2/7 -1/1+1/8+1/3+2/5+1/7 -1/1+3/8+1/9+4/5-2/7 -1/1+3/8+1/9+4/5-2/7 -2/1+1/8+5/9+4/5+3/7+1/11 -2/1+1/8+5/9+4/5+3/7+1/11 -3/1+5/8+8/9+3/5+4/7+6/11-3/13 -3/1+5/8+...
result:
ok OK, t = 54
Test #5:
score: 0
Accepted
time: 0ms
memory: 8040kb
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:
-9/1+27/32+2/27+21/25+26/49+10/11+2/13+11/17+8/19+19/23+24/29+18/31+21/37+26/41+30/43+21/47 -8/1+15/32+25/27+7/25+31/49+6/11+2/13+14/17+17/19+6/23+1/29+29/31+36/37+9/41+3/43+27/47+11/53 -8/1+15/32+25/27+7/25+31/49+6/11+2/13+14/17+17/19+6/23+1/29+29/31+36/37+9/41+3/43+27/47+11/53 -7/1+29/32+5/27+23/2...
result:
ok OK, t = 92
Test #6:
score: -100
Wrong Answer
time: 40ms
memory: 8716kb
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:
-226035490248376552/1-5671107253231419223/512+5858917184962903784/243+4407165847616904435/125-3932295806434895237/343-8300080342602394904/121+140905532657994857/169-4997989575010399917/289-3913965460731713310/361+1991268566325949908/529+364970898946142532/29+7585417941249902090/31+389915754315069072...
result:
wrong answer Numerator too large