T356201 题解

· · 个人记录

神秘提,神秘做法。

发现答案是 \dfrac{P}Q,且 P,Q 范围很小,考虑直接算它的小数形式然后有理数逼近。因为值域很大所以要用科学计数法存。

判定答案可行性可以直接和小数比较,也能在 \pmod {一个大质数} 意义下比较。

时间复杂度 O(n+m+\log^2 \max(P,Q)),需要卡精度+卡时间。

码不放了。 还是贴一下场上代码吧,变量名可能有点混乱。

#include<bits/stdc++.h>
#define LL long long
#define LLL __int128
#define uint unsigned
#define ldb long double
#define uLL unsigned long long
using namespace std;
namespace FastIO{
//...
}
#define int LL
const ldb eps=1e-10;
const uLL mod1=(uLL)9e18+91;
__float128 X=1,Y=1,Inf=1;
int n,m,typ,flg,Rx,Ry;
int a,b=1,c=1,d,N=6e9;
int z1=1,w1=1;
inline bool check(int x,int y){
    if(((LLL)z1*y-(LLL)w1*x)%mod1==0)
        cout<<x<<' '<<y<<'\n',exit(0);
    return x<=y*X;
}
inline void solve(){
    int e=a+c,f=b+d,x,y,g=check(e,f);
    if(g)x=c,y=d;
    else x=a,y=b;
    int l=1,r=N;
    if(x)r=min(r,(N-e)/x);
    if(y)r=min(r,(N-f)/y);
    while(l<r){
        int mid=(l+r)>>1;
        if(check(e+x*mid,f+y*mid)==g)l=mid+1;
        else r=mid;
    }
    if(g)a=e+x*(l-1),b=f+y*(l-1),c=e+x*l,d=f+y*l;
    else c=e+x*(l-1),d=f+y*(l-1),a=e+x*l,b=f+y*l;
    check(a,b),check(c,d);
}
signed main(){
    cin.tie(0)->sync_with_stdio(0);
    read(n,m,typ);
    for(int k=500;k;--k)Inf*=10;
    while(n--){uLL x;read(x),X*=x,z1=z1*(LLL)x%mod1;while(X>=Inf)X/=Inf,Rx+=500;}
    while(m--){uLL x;read(x),Y*=x,w1=w1*(LLL)x%mod1;while(Y>=Inf)Y/=Inf,Ry+=500;}
    X/=Y,Rx-=Ry;
    while(Rx<0)X/=10,++Rx;
    while(Rx>0)X*=10,--Rx;
    //if(X<1)X=1/X,flg=1,swap(z,w);
    while(1)solve();
    return 0;
}