日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CodeForces - 724C Ray Tracing(扩展欧几里得解方程)

發(fā)布時間:2024/4/11 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CodeForces - 724C Ray Tracing(扩展欧几里得解方程) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目鏈接:點擊查看

題目大意:在 n?mn*mn?m 的矩陣中,從點 (0,0)(0,0)(0,0) 發(fā)射一個小球,以四十五度的方向出發(fā),速度是 2\sqrt{2}2?,當碰到壁時,會進行反射;當碰到底角時,會被吸收。現(xiàn)在給出 kkk 次詢問,每次詢問需要回答點 (x,y)(x,y)(x,y) 是在什么時間第一次到達,如果無法到達輸出 ?1-1?1

題目分析:模擬也是可以做的,同樣也可以從數(shù)論的角度去思考

考慮將矩形進行平面無限展開后,每一個點 (x,y)(x,y)(x,y) 都可以被表示為 (2?n?k1±x,2?m?k2±y)(2*n*k_1 \pm x,2*m*k_2 \pm y)(2?n?k1?±x,2?m?k2?±y),其中 k1k_1k1? 表示的是穿過了多少條平行于 xxx 軸的邊,k2k_2k2? 表示的是穿過了多少條平行于 yyy 軸的邊,因為在本題中速度是 45°45\degree45°,所以最終 xxxyyy 坐標在無限展開后一定是相等的,顯然有 2?n?k1±x=2?m?k2±y2*n*k_1 \pm x = 2*m*k_2 \pm y2?n?k1?±x=2?m?k2?±y,移項得到 2?n?k1?2?m?k2=±y?±x2*n*k_1-2*m*k_2=\pm y - \pm x2?n?k1??2?m?k2?=±y?±x

然后將 (x,y)(x,y)(x,y) 分四種情況,分別用擴展歐幾里得求一下最小正整數(shù)解維護答案的最小值就好了

需要注意的是,當時間大于等于 lcm(n,m)lcm(n,m)lcm(n,m) 后會被吸收,需要特判一下

代碼:

// #pragma GCC optimize(2) // #pragma GCC optimize("Ofast","inline","-ffast-math") // #pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<bitset> #include<unordered_map> using namespace std; typedef long long LL; typedef unsigned long long ull; template<typename T> inline void read(T &x) {T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f; } template<typename T> inline void write(T x) {if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0'); } const int inf=0x3f3f3f3f; const int N=1e6+100; LL ans,limit; LL n,m,k; template<class T> void exgcd(T a,T b,T &d,T &x,T &y){if(!b) {d=a;x=1;y=0;}else {exgcd(b,a%b,d,y,x);y-=x*(a/b);} } //求解二元一次方程 a*x+b*y=c,一組解為x,y,無解則返回false template<class T> bool Solve_equation(T a,T b,T c,T &x,T& y){T gcd;exgcd(a,b,gcd,x,y);if(c%gcd) return false; //無解T k=c/gcd;x*=k;y*=k;T xplus=b/gcd,yplus=a/gcd; if(xplus<0) xplus*=-1;if(yplus<0) yplus*=-1;//此時求出的x,y即為一組解,該方程的通解形式為X=x+t*(b/gcd),Y=y-t*(a/gcd) t為任意正整數(shù)//根據(jù)題目要求我們需要構(gòu)造特殊解x=(x%xplus+xplus)%xplus;y=(c-a*x)/b; //x的最小正整數(shù)解//y=(y%yplus+yplus)%yplus;x=(c-b*y)/b; //y的最小正整數(shù)解return true; } void solve(LL dx,LL dy) {LL k1,k2;if(!Solve_equation(2*n,-2*m,dy-dx,k1,k2)) return;LL res=2*n*k1+dx;if(res>=limit) return;if(ans==-1||res>=0&&ans>res) ans=res; } int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);read(n),read(m),read(k);limit=n*m/__gcd(n,m);while(k--){int x,y;read(x),read(y);ans=-1;solve(x,y),solve(x,-y),solve(-x,y),solve(-x,-y);write(ans),putchar('\n');}return 0; }

總結(jié)

以上是生活随笔為你收集整理的CodeForces - 724C Ray Tracing(扩展欧几里得解方程)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。