QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#843357 | #9962. Diminishing Fractions | ucup-team3586# | WA | 955ms | 6560kb | C++23 | 4.5kb | 2025-01-04 18:12:23 | 2025-01-04 18:12:32 |
Judging History
answer
//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define ull unsigned ll
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define i128 __int128_t
void die(string S){puts(S.c_str());exit(0);}
char buf[1<<23],*p1=buf,*p2=buf,obuf[1<<24],*O=obuf;
void print(long long x) {
if(x>9) print(x/10);
*O++=x%10+'0';
}
void print(long long x,string &Cur) {
if(x>9) print(x/10);
Cur+=(char)(x%10)+'0';
*O++=x%10+'0';
}
const ll base=1e13;
using Int=vector<ll>;
Int div(Int A,ll p)
{
for(int i=sz(A)-1;i>=0;i--)
{
ll rem=(A[i]%p);
A[i]/=p;
if(i) A[i-1]+=rem*base;
}
while(sz(A)&&!A.back()) A.pop_back();
return A;
}
Int mul(Int A,ll p)
{
ll cur=0;
for(int i=0;i<sz(A);i++)
{
cur=cur+A[i]*p;
A[i]=cur%base;
cur=cur/base;
}
while(cur)
{
A.pb(cur%base);
cur/=base;
}
return A;
}
ll mod(Int A,ll p)
{
ll ans=0;
for(int i=sz(A)-1;i>=0;i--)
ans=(ans*base+A[i])%p;
return ans;
}
Int add(Int A,Int B)
{
int m=max(sz(A),sz(B));
ll cur=0;
A.resize(m);
B.resize(m);
for(int i=0;i<m;i++)
{
cur=cur+A[i]+B[i];
A[i]=cur%base;
cur=cur/base;
}
while(cur)
{
A.pb(cur%base);
cur/=base;
}
return A;
}
Int sub(Int A,Int B)
{
int m=max(sz(A),sz(B));
ll cur=0;
A.resize(m);
B.resize(m);
for(int i=0;i<m;i++)
{
cur=A[i]-B[i]-cur;
if(cur>=0)
{
A[i]=cur;
cur=0;
}
else
{
A[i]=base+cur;
cur=-1;
}
}
assert(cur==0);
return A;
}
bool cmp(Int A,Int B)
{
if(sz(A)!=sz(B)) return sz(A)<sz(B);
for(int i=sz(A)-1;i>=0;i--)
if(A[i]!=B[i])
return A[i]<B[i];
return 0;
}
int pr[50005],ppr[50005],c;
int isp[50005];
int ans[50005];
void exgcd(ll a,ll b,ll &x,ll &y)
{
if(!b)
{
x=1;
y=0;
return ;
}
exgcd(b,a%b,y,x);
y-=x*(a/b);
}
ll getinv(ll a,ll b)
{
ll x,y;
exgcd(a,b,x,y);
return (x%b+b)%b;
}
Int A[50005];
vector<int> vv,vv2;
void gen()
{
int cnt=0;
for(int i=2;i<=50000;i++)
{
int pr=2;
while(pr*pr<=i&&i%pr) pr++;
if(pr*pr>i)
{
vv.pb(i);
vv2.pb(i);
}
else
{
int ok=1;
int tmp=i;
while(tmp%pr==0)
{
tmp/=pr;
}
if(tmp==1)
{
vv.pb(i);
vv2.pb(pr);
}
}
}
}
vector<int> vq[10005];
string Answer[10005];
long double sim(Int B,Int A)
{
B.resize(sz(A));
if(sz(A)==1) return 1.L*B.back()/A.back();
i128 B1=((i128)(B.back())*base+(B[sz(B)-2]));
i128 A1=((i128)(A.back())*base+(A[sz(A)-2]));
return 1.L*B1/A1;
}
void solve()
{
Int A={2};
int m=sz(vv);
ans[0]=1;
for(int i=1;i<m;i++)
{
ll remA=mod(A,vv[i])/(vv[i]/vv2[i]);
ll x=getinv(remA,vv2[i]);
ans[i]=(x-vv2[i]);
Int B=add(mul(A,vv2[i]-x),{vv[i]/vv2[i]});
B=div(B,vv[i]);
for(int j=0;j<i;j++)
ans[j]=(ans[j]*getinv(vv2[i],vv[j]))%vv[j];
long double sm1=0,sm2=0;
for(int j=0;j<i;j++)
if(ans[j]>0)
sm1+=(long double)(ans[j])/vv[j];
else
sm2+=(long double)(-ans[j])/vv[j];
sm1-=sm2;
sm1-=sim(B,A);
const long double eps=1e-2;
int sm3=0;
int cc=max(sm1,-sm1)+10;
while(fabs(sm1)>eps&&cc)
{
cc--;
if(sm1<0)
{
sm3--;
sm1+=1;
}
else
{
sm3++;
sm1-=1;
}
}
for(int j=0;j<i;j++)
{
if(ans[j]<=0&&sm3<0)
{
sm3++;
ans[j]+=vv[j];
}
else if(ans[j]>=0&&sm3>0)
{
sm3--;
ans[j]-=vv[j];
}
}
if(sz(vq[i]))
{
string res;
int flag=0;
for(int j=0;j<=i;j++) if(ans[j])
{
if(ans[j]>0&&flag) res+='+';
flag=1;
ll out=ans[j];
if(out<0)
{
res+='-';
out=-out;
}
print(out,res);
res+='/';
print(vv[j],res);
}
for(auto ind:vq[i])
Answer[ind]=res;
}
A=mul(A,vv2[i]);
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
gen();
int t;
cin>>t;
for(int i=1;i<=t;i++)
{
int n;
cin>>n;
if(n==1) Answer[i]="1/1";
else if(n==2) Answer[i]="1/2";
else
{
int p=ub(vv,n)-1;
vq[p].pb(i);
}
}
solve();
for(int i=1;i<=t;i++)
cout<<Answer[i]<<'\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 955ms
memory: 6488kb
input:
2 3 6
output:
1/2-1/3 2/2-1/3-1/4-2/5
result:
ok OK, t = 2
Test #2:
score: 0
Accepted
time: 951ms
memory: 6560kb
input:
1 1
output:
1/1
result:
ok OK, t = 1
Test #3:
score: -100
Wrong Answer
time: 947ms
memory: 6212kb
input:
10 1 2 3 4 5 6 7 8 9 10
output:
1/1 1/2 1/2-1/3 2/2-2/3-1/4 2/2-1/3-1/4-2/5 2/2-1/3-1/4-2/5 2/2+2/3-3/4-1/5-5/7 2/2+1/3+1/4-3/5-6/7-1/8 3/4-1/5-2/7-3/8-2/9 3/4-1/5-2/7-3/8-2/9
result:
wrong answer Sums do not match for modulus 8809877585262195773