POJ 1006 Biorhythms 中国剩余定理

编程入门 行业动态 更新时间:2024-10-27 15:22:52

POJ 1006 Biorhythms 中国剩余<a href=https://www.elefans.com/category/jswz/34/1769765.html style=定理"/>

POJ 1006 Biorhythms 中国剩余定理

原题链接

原题即求n满足:(n+d)%23===p, (n+d)%28=i, (n+d)%33=e 使用exgcd求逆元即可

#include <iostream>
using namespace std;
int p,e,i,d,A[3];
const int m[]={23,28,33},M=23*28*33;
void exgcd(int a,int b,int& x,int& y){if(b==0){x=1;y=0;}else{exgcd(b,a%b,y,x);y-=x*(a/b);}
}
int main(){///problem: find n satisfying (n+d)%23===p, (n+d)%28=i, (n+d)%33=eint t=1;while(cin>>p>>e>>i>>d){if(p==-1&&e==-1&&i==-1&&d==-1) break;int ans=0;A[0]=p,A[1]=e,A[2]=i;for(int k=0;k<3;k++){int x,y,Mi=M/m[k];exgcd(Mi,m[k],x,y);ans=(ans+A[k]*Mi*x)%M;}if(ans<=d) ans+=M;cout<<"Case "<<t++<<": the next triple peak occurs in "<<ans-d<<" days."<<endl;}return 0;
}


更多推荐

POJ 1006 Biorhythms 中国剩余定理

本文发布于:2024-03-06 13:12:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1715417.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:定理   中国   剩余   POJ   Biorhythms

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!