[POJ 2891]Strange Way to Express Integers:扩展中国剩余定理
来源:互联网 发布:今日头条 微博 知乎 编辑:程序博客网 时间:2024/06/07 11:41
点击这里查看原题
这里是教程
/*User:SmallLanguage:C++Problem No.:2891*/#include<stdio.h>#include<iostream>#include<iomanip>#include<string.h>#include<algorithm>#include<queue>#include<stack>#include<math.h>#include<map>#define ll long long#define inf 999999999using namespace std;const int M=1005;ll n,c[M],m[M];ll gcd(ll a,ll b){ return b==0?a:gcd(b,a%b);}void exgcd(ll a,ll b,ll &x,ll &y){ if(b==0){ x=1; y=0; return; } exgcd(b,a%b,y,x); y-=a/b*x;}ll inv(ll a,ll b){ ll x,y; exgcd(a,b,x,y); x=(x%b+b)%b; if(!x) x+=b; return x;}int main(){ freopen("data.in","r",stdin);// ios::sync_with_stdio(false); while(cin>>n){ bool flag=0; for(int i=1;i<=n;i++) cin>>m[i]>>c[i]; for(int i=2;i<=n;i++){ ll m1=m[i-1],m2=m[i],c1=c[i-1],c2=c[i]; ll t=gcd(m1,m2); if((c2-c1)%t){ flag=1; break; } m[i]=m1/t*m2; c[i]=inv(m1/t,m2/t)*((c2-c1)/t)%(m2/t)*m1+c1; c[i]=(c[i]%m[i]+m[i])%m[i]; } if(flag) c[n]=-1; cout<<c[n]<<endl; } return 0;}
阅读全文
0 0
- poj 2891 Strange Way to Express Integers 中国剩余定理
- 中国剩余定理 poj 2891 Strange Way to Express Integers
- POJ 2891 Strange Way to Express Integers 中国剩余定理
- poj Strange Way to Express Integers 2891 (中国剩余定理)
- POJ 2891-Strange Way to Express Integers【中国剩余定理】
- POJ 2891 Strange Way to Express Integers (中国剩余定理)
- POJ 2891 Strange Way to Express Integers(中国剩余定理)
- poj 2891 Strange Way to Express Integers(中国剩余定理)
- poj 2891 Strange Way to Express Integers(扩展欧几里得,中国剩余定理)
- poj-2891(Strange Way to Express Integers)--中国剩余定理&&扩展欧几里得
- POJ 2891 Strange Way to Express Integers(中国剩余定理扩展)
- [POJ 2891]Strange Way to Express Integers:扩展中国剩余定理
- POJ-2981 Strange Way to Express Integers(中国剩余定理)
- POJ 2891 Strange Way to Express Integers 非互质的中国剩余定理
- poj 2891 Strange Way to Express Integers 模数不互素的中国剩余定理
- POJ 2891 Strange Way to Express Integers 解题报告(中国剩余定理)
- POJ 2891 Strange Way to Express Integers 中国剩余定理解法
- poj 2891 Strange Way to Express Integers(中国剩余定理)
- 完全卸载MySQL
- Linux 常用命令(二)
- 406. Queue Reconstruction by Height
- [P3518]strongbox
- zigbee路由转发,短地址是否不变?
- [POJ 2891]Strange Way to Express Integers:扩展中国剩余定理
- 图的基本概念(二)
- 修改通知
- V$LOGMNR_CONTENTS视图中的SESSION_INFO 是null的.
- java注解基础概念总结
- 欢迎使用CSDN-markdown编辑器
- Linux netstat命令详解
- 操作系统(1)
- L1和L2正则化