Luo Gu P4774 [NOI2018] Tulong Yong Shi

Subject to the effect:

This question and surface data range is not very good stick, see for yourself ......

First, we should be able to see that this is a \ (excrt \) title

As this limit is very sword \ (Naive \) , with \ (multiset \) exist just fine

According to the meaning of problems we can list equations

\[\begin{cases}d_1x\equiv a_1 (mod\ p_1)\\d_2x\equiv a_2 (mod\ p_2)\\……\\d_nx\equiv a_n (mod\ p_n)\end{cases}\]

Where \ (d_i \) is a sword attack, \ (the X-\) as the number of attacks, \ (a_i \) as a cloudy blood, \ (P_i \) is the ability to reply

cancer

But we seem to have \ (a_i> p_i \) case ...... according to the subject we can not seem to directly molded out ......

However, weAfter the turn the problem solutionFound that there could \ (a_i> p_i \) , the all meet \ (p_i = 1 \) then we strongly determine out just fine

specific method:

\[ans=max\{ \lceil\frac{d_i}{a_i}\rceil \}\]

General Solutions

Consider \ (d_ix \ equiv a_i (mod \ p_i) \) like and general \ (crt \) are not the same equation, simplifying it

\[x*d_i+y*p_i=- a_i\]

With \ (exgcd \) to obtain a set of \ (x_t, y_t \)

Because of \ (ecgcd \) The general solution \ (X = x_t + K * \ FRAC {B} {GCD (A, B)} \) , to give \ (x = x_t + k * \ frac {p_i} {gcd (p_i , d_i)} \)

For \ (\ frac {p_i} { gcd (p_i, d_i)} \) modulo give

\(x\equiv x_t (mod\ \frac{p_i}{gcd(p_i,d_i)})\)

Then vigorously \ (excrt \)

note

We understand how the sensibility can be drawn \ (the X-\ GE (MAXN = max \ {\ lceil \ FRAC D_i} {} {a_i \ rceil \}) \) , if the final answer is less than this number, should make up

We are introduced to the \ (ret = ret + m * \ lceil \ frac {maxn-ret} {m} \ rceil \)

#include<bits/stdc++.h>
using namespace std;
namespace red{
#define int long long
#define eps (1e-8)
    inline int read()
    {
        int x=0;char ch,f=1;
        for(ch=getchar();(ch<'0'||ch>'9')&&ch!='-';ch=getchar());
        if(ch=='-') f=0,ch=getchar();
        while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
        return f?x:-x;
    }
    const int N=1e5+10;
    int haku;
    int n,m;
    int a[N],p[N],sword[N];
    inline void exgcd(int &x,int &y,int &g,int a,int b)
    {
        if(!b){x=1,y=0,g=a;return;}
        exgcd(y,x,g,b,a%b);
        y-=a/b*x;
    }
    inline int slow(int x,int k,int p)
    {
        int ret=0;
        while(k)
        {
            if(k&1) ret=(ret+x)%p;
            x=(x+x)%p;
            k>>=1;
        }
        return ret;
    }
    multiset<int> q;
    multiset<int>::iterator it;
    inline int excrt()
    {
        int m=1,ret=0,maxn=0,x,y,g;
        for(int i=1;i<=n;++i)
        {
            it=q.upper_bound(a[i]);
            if(it!=q.begin()) --it;
            int k=*it;
            q.erase(it);
            q.insert(sword[i]);
            maxn=max(maxn,(a[i]-1)/k+1);
            k%=p[i],a[i]%=p[i];
            exgcd(x,y,g,k,p[i]);
            if(a[i]%g) return -1;
            p[i]/=g;
            a[i]=slow(a[i]/g,(x%p[i]+p[i])%p[i],p[i]);
            int c=((a[i]-ret)%p[i]+p[i])%p[i];
            exgcd(x,y,g,m,p[i]);
            if(c%g) return -1;
            x=slow(x,c/g,p[i]);
            ret+=x*m;
            m*=p[i]/g;
            ret%=m;
        }
        return ret>=maxn?ret:ret+m*((maxn-ret-1)/m+1);
    }
    inline void main()
    {
        haku=read();
        while(haku--)
        {
            n=read(),m=read();
            for(int i=1;i<=n;++i) a[i]=read();
            for(int i=1;i<=n;++i) p[i]=read();
            for(int i=1;i<=n;++i) sword[i]=read();
            for(int i=1;i<=m;++i) q.insert(read());
            printf("%lld\n",excrt());
            q.clear();
        }
    }
}
signed main()
{
    red::main();
    return 0;
}

Guess you like

Origin www.cnblogs.com/knife-rose/p/12058420.html