[POJ2891]Strange Way to Express Integers(扩展中国剩余定理)
来源:互联网 发布:淘宝评价赚钱 编辑:程序博客网 时间:2024/06/03 21:55
题目描述
传送门
题解
扩展中国剩余定理
注意第一个运算由于是求一个同余式的逆元所以一定要及时取模否则容易炸
代码
#include<algorithm>#include<iostream>#include<cstring>#include<cstdio>#include<cmath>using namespace std;#define LL long long#define N 1005int k;LL c[N],m[N],c1,c2,m1,m2,t;bool flag;LL gcd(LL a,LL b){ if (!b) return a; else return gcd(b,a%b);}void exgcd(LL a,LL b,LL &x,LL &y){ if (!b) x=1LL,y=0LL; else exgcd(b,a%b,y,x),y-=a/b*x;}LL inv(LL a,LL b){ LL x=0LL,y=0LL; exgcd(a,b,x,y); x=(x%b+b)%b; if (!x) x+=b; return x;}int main(){ while (~scanf("%d",&k)) { flag=true; for (int i=1;i<=k;++i) scanf("%I64d%I64d",&m[i],&c[i]); for (int i=2;i<=k;++i) { m1=m[i-1],m2=m[i],c1=c[i-1],c2=c[i]; t=gcd(m1,m2); if ((c2-c1)%t!=0) {flag=false;break;} m[i]=m1*m2/t; 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) puts("-1"); else printf("%I64d\n",c[k]); }}
0 0
- poj2891 Strange Way to Express Integers(中国剩余定理)
- poj2891 Strange Way to Express Integers(中国剩余定理)
- [POJ2891]Strange Way to Express Integers(扩展中国剩余定理)
- POJ2891 Strange Way to Express Integers 不互质中国剩余定理
- POJ2891 Strange Way to Express Integers(中国剩余定理)
- poj2891——Strange Way to Express Integers(扩展欧几里得解中国剩余定理)
- poj2891--Strange Way to Express Integers(不互素的中国剩余定理)
- 【POJ2891】Strange Way to Express Integers——中国剩余定理(非互质)
- poj2891 Strange Way to Express Integers (中国剩余定理+拓展欧几里得)
- Strange Way to Express Integers(中国剩余定理+不互质)
- POJ2891 Strange Way to Express Integers(解多元线性同余方程)(中国剩余定理非互质版)(例题)
- [POJ2891]Strange Way to Express Integers(扩展CRT)
- poj 2891 Strange Way to Express Integers 中国剩余定理
- 中国剩余定理 poj 2891 Strange Way to Express Integers
- 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 - 2785 4 Values whose Sum is 0(二分枚举)
- C++类的总结
- android 画柱状图
- 《鬼子来了-姜文》——鬼子与“我”
- [POJ2891]Strange Way to Express Integers(扩展中国剩余定理)
- 第三天-中软!
- 数组内存分配之堆、栈
- Solr字段类型field type的定义
- 怎么看越努力,越幸运?
- Python 图像读入处理(R+G+B+Gray)
- 快速排序函数模块
- Linux如何查找大文件或目录总结
- stringstream 使用时注意清空