寫題解看成複習筆記
(這樣就能夠少寫一篇博客了 Yeah)函數
> Link LOJ 2721ui
「問題」spa
求解線性同餘方程組code
$$ \begin{cases} x\equiv a_1\pmod{m_1}\\ x\equiv a_2\pmod{m_2}\\ \cdots\\ x\equiv a_n\pmod{m_n} \end{cases} $$ get
$m$ 能夠不互質。input
考慮等價地合併兩個同餘方程:博客
能夠把同餘方程寫爲不定方程的形式,因而獲得參數的一些關係:string
因爲 \(m_1,m_2\) 不必定互質,因此關於 \(k_1,k_2\) 的不定方程不必定有解。具體地說,設 \((m_1,m_2)=g\),則it
等式左側 \(g\mid k_1m_1-k_2m_2\),則右側也必須知足 \(g\mid a_2-a_1\),不然無解。io
若知足上述條件,則等式兩邊同時除以 \(g\) 仍然等價,即
此時 \((m_1',m_2')=1\),能夠直接用 exGCD 解決上述問題,獲得 \(k_1\) 的一個特解爲 \(k_0\)。由
能夠知道 \(k_1\) 的通解爲 \(k_1=k_0+nm_2'(n\in\mathbb{Z})\)。
將 \(k_1\) 代回 \(x\) 的表達式,則有
因而就獲得了兩個式子合併後的等價的式子。這樣不斷合併就能夠獲得最終 \(x\) 的通解。
一開始能夠根據輸入求出「用哪一把劍攻擊第 \(i\) 條龍」,記爲 \(w_i\)。具體能夠用 multiset 實現。
multiset 內部每一個位置儲存了一個元素(並非把相同的元素合到同一個位置而且記錄次數),所以用迭代器 iterator 訪問其中的某個位置,訪問到的是單個元素。若是按迭代器順序訪問 multiset,就至關於把插入的全部元素排了個序。
multiset 內置有 lower_bound 和 upper_bound 函數,前者返回第一個大於等於給定值的元素的迭代器,後者返回第一個嚴格大於給定值的元素的迭代器。
multiset 的 delete 函數有兩類參數。第一類是給定數值,會刪除其中全部該數值;第二類是給定迭代器,會刪除迭代器對應的元素——這樣就只會刪掉一個數字。
利用這些特色,咱們能夠用 upper_bound 找到第一把攻擊力大於龍的生命值的劍,而上一把劍就是攻擊力小於等於生命值的劍。而後用 delete 刪除該劍的迭代器(不能是數值!)。
記龍的生命爲 \(h_i\),回覆力爲 \(r_i\),則有兩個限制:
問題直接轉化爲 \(n\) 對方程構成的方程組
不等式方程能夠求出 \(x\) 的下界。考慮如何求解同餘方程。
\(w_i,r_i\) 不必定互質,因而可能自己就無解。記 \((w_i,r_i)=g_i\),則必須知足 \(g_i\mid h_i\),方程纔有解。
知足有解的條件後,\(w_i,h_i,r_i\) 同時除以 \(g_i\),獲得等價的方程 \(w_i'x=h_i'\pmod {r_i'}\),此時 \((w_i',r_i')=1\),\(w_i'\) 存在逆元,因而能夠獲得
這個方程就能夠用 exCRT 了。
/*Lucky_Glass*/ #include<set> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; template<class T>T rin(T &r){ int b=1,c=getchar();r=0; while(c<'0' || '9'<c) b=c=='-'?-1:b,c=getchar(); while('0'<=c && c<='9') r=(r<<1)+(r<<3)+(c^'0'),c=getchar(); return r*=b; } typedef long long llong; const int N=1e5+10; #define con(type) const type & multiset<llong> sword; int n,m; llong ih[N],rec[N],rew[N],atk[N]; llong ina_GCD(con(llong)a,con(llong)b){return b?ina_GCD(b,a%b):a;} llong ex_GCD(con(llong)a,con(llong)b,llong &x,llong &y){ if(!b){x=1,y=0;return a;} llong ret=ex_GCD(b,a%b,x,y); swap(x,y); y-=a/b*x; return ret; } llong ina_ABS(con(llong)a){return a<0?-a:a;} llong mul(llong a,llong b,llong mod){ bool neg=(a<0)^(b<0); a=ina_ABS(a),b=ina_ABS(b); a%=mod,b%=mod; llong ret=0; while(b){ if(b&1) ret=(ret+a)>=mod? ret+a-mod:ret+a; a<<=1,b>>=1; if(a>=mod) a-=mod; } if(neg && ret) return mod-ret; return ret; } pair<llong,llong> comb_CRT(con(llong)m0,con(llong)r0,con(llong)m1,con(llong)r1){ llong g=ina_GCD(m0,m1); if((r1-r0)%g){ // printf("(%lld,%lld)=%lld %lld %lld\n",m0,m1,g,r1,r0); return make_pair(-1ll,-1ll); } llong p,q; ex_GCD(m0/g,m1/g,p,q); p=mul((r1-r0)/g,p,m1/g); llong m2=m0/g*m1,r2=(mul(p,m0,m2)+r0)%m2; if(r2<0) r2+=m2; return make_pair(m2,r2); } llong solve(){ sword.clear(); rin(n),rin(m); for(int i=1;i<=n;i++) rin(ih[i]); for(int i=1;i<=n;i++) rin(rec[i]); for(int i=1;i<=n;i++) rin(rew[i]); for(int i=1,tmp;i<=m;i++) sword.insert(rin(tmp)); for(int i=1;i<=n;i++){ multiset<llong>::iterator it=sword.upper_bound(ih[i]); if(it!=sword.begin()) it--; atk[i]=*it; sword.erase(it); sword.insert(rew[i]); } llong mnbon=0; pair<llong,llong> now(-1ll,-1ll); for(int i=1;i<=n;i++){ mnbon=max(mnbon,(ih[i]+atk[i]-1)/atk[i]); llong k=atk[i],r=ih[i],m=rec[i]; // printf("%lld x = %lld (mod %lld) -> ",k,r,m); //kx = r (mod m) k%=m,r%=m; if(!k){ if(r){ // printf("A\n"); return -1ll; } continue; } llong g=ina_GCD(k,m); if(r%g){ // printf("B\n"); return -1ll; } k/=g,m/=g,r/=g; llong invk,non; ex_GCD(k,m,invk,non); invk=(invk%m+m)%m; r=mul(r,invk,m); // printf("x = %lld (mod %lld)\n",r,m); pair<llong,llong> tmp(m,r); if(i==1) now=tmp; else now=comb_CRT(now.first,now.second,tmp.first,tmp.second); if(now.first==-1){ // printf("C\n"); return -1ll; } } if(now.first==-1) return mnbon; if(now.second>=mnbon) return now.second; else{ llong k=(mnbon-now.second+now.first-1)/now.first; return now.second+k*now.first; } } int main(){ // freopen("input.in","r",stdin); freopen("dragon.in","r",stdin); freopen("dragon.out","w",stdout); int cas;rin(cas); while(cas--) printf("%lld\n",solve()); return 0; }