【poj2891-Strange Way to Express Integers】拓展欧几里得-同余方程组
http://poj.org/problem?id=2891
题意:与中国剩余定理不同,p%ai=bi,此处的ai(i=1 2 3 ……)是不一定互质的,所以要用到的是同余方程组,在网上看到有人称为拓展中国剩余定理。
具体讲解可以看我昨天的博文:http://www.cnblogs.com/KonjakJuruo/p/5176417.html
- //poj2891
- #include<cstdio>
- #include<cstdlib>
- #include<cstring>
- #include<iostream>
- using namespace std;
- typedef long long LL;
- const LL N=;
- LL a[N],b[N];
- LL tx,ty;
- LL exgcd(LL aa,LL bb)
- {
- if(bb==) {tx=,ty=;return aa;}
- LL d=exgcd(bb,aa%bb);
- LL x=ty,y=tx-(aa/bb)*ty;
- tx=x;ty=y;
- return d;
- }
- LL lcu(LL aa,LL bb)
- {
- LL d=exgcd(aa,bb);
- return aa*bb/d;
- }
- int main()
- {
- freopen("a.in","r",stdin);
- freopen("a.out","w",stdout);
- // printf("%d\n",(-5)%3);
- LL n,a1,b1,x;
- while(scanf("%I64d",&n)!=EOF)
- {
- bool bk=;
- for(LL i=;i<=n;i++)
- scanf("%I64d%I64d",&a[i],&b[i]);
- LL A=a[],B=a[],C=b[]-b[];
- LL g=exgcd(A,B);
- if(C%g) bk=;
- else {
- x=((tx*C/g)%(B/g)+(B/g))%(B/g);
- b1=a[]*x+b[];
- a1=lcu(a[],a[]);
- }
- for(LL i=;i<=n;i++)
- {
- A=a1,B=a[i],C=b[i]-b1;
- g=exgcd(A,B);
- if(C%g) {bk=;break;}
- x=((tx*C/g)%(B/g)+(B/g))%(B/g);
- b1=a1*x+b1;
- a1=lcu(a1,a[i]);
- }
- if(bk) printf("%I64d\n",b1);
- else printf("-1\n");
- }
- return ;
- }
【poj2891-Strange Way to Express Integers】拓展欧几里得-同余方程组的更多相关文章
- HDU-3579-Hello Kiki (利用拓展欧几里得求同余方程组)
设 ans 为满足前 n - 1个同余方程的解,lcm是前n - 1个同余方程模的最小公倍数,求前n个同余方程组的解的过程如下: ①设lcm * x + ans为前n个同余方程组的解,lcm * x ...
- 【hdu3579-Hello Kiki】拓展欧几里得-同余方程组
http://acm.hdu.edu.cn/showproblem.php?pid=3579 题解:同余方程组的裸题.注意输出是最小的正整数,不包括0. #include<cstdio> ...
- 【hdu1573-X问题】拓展欧几里得-同余方程组
http://acm.hdu.edu.cn/showproblem.php?pid=1573 求小于等于N的正整数中有多少个X满足: X mod a0 = b0 X mod a1 = b1 …… X ...
- 中国剩余定理+扩展中国剩余定理 讲解+例题(HDU1370 Biorhythms + POJ2891 Strange Way to Express Integers)
0.引子 每一个讲中国剩余定理的人,都会从孙子的一道例题讲起 有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二.问物几何? 1.中国剩余定理 引子里的例题实际上是求一个最小的x满足 关键是,其中 ...
- POJ2891——Strange Way to Express Integers(模线性方程组)
Strange Way to Express Integers DescriptionElina is reading a book written by Rujia Liu, which intro ...
- POJ2891 Strange Way to Express Integers
题意 Language:Default Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total S ...
- POJ2891 Strange Way to Express Integers 扩展欧几里德 中国剩余定理
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2891 题意概括 给出k个同余方程组:x mod ai = ri.求x的最小正值.如果不存在这样的x, ...
- poj2891 Strange Way to Express Integers poj1006 Biorhythms 同余方程组
怎样求同余方程组?如: \[\begin{cases} x \equiv a_1 \pmod {m_1} \\ x \equiv a_2 \pmod {m_2} \\ \cdots \\ x \equ ...
- POJ-2891 Strange Way to Express Integers(拓展中国剩余定理)
放一个写的不错的博客:https://www.cnblogs.com/zwfymqz/p/8425731.html POJ好像不能用__int128. #include <iostream> ...
随机推荐
- 横屏下的ImagePickerController
Try this way.... As per Apple Document, ImagePicker Controller never Rotate in Landscape mode. You h ...
- Swift function how to return nil
这两天在学习Stanford出品的iOS7的课程,这个课程去年也看过,但是看到第3课就不行了,满篇的OC,把人都搞晕了.这段时间因为要写个iOS的App,正好赶上了Swift问世,所以趁着这股劲继续学 ...
- [转]NHibernate之映射文件配置说明
1. hibernate-mapping 这个元素包括以下可选的属性.schema属性,指明了这个映射所引用的表所在的schema名称.假若指定了这个属性, 表名会加上所指定的schema的名字扩展为 ...
- openSUSE install failed
install openSUSE 13.1 with vmware play (version 6.0.0) calling the yast module 'inst_autoinit has f ...
- python djange输入中文错误的解决办法
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128) ...
- 我的VS2013中,用Ado.net给SQLParameter赋值的时候,当赋值null的时候,生成的sql语句是default
/// <summary> /// 增加一条数据 /// </summary> public bool Add(Model.WechatDocuments model) { S ...
- C++string的使用
在这里总结一下string的用法 String是可变长字符串,使用的时候要包含string头文件. 要想使用标准C++中string类,必须要包含 #include <string>// ...
- 如何查看系统内置的shell命令及帮助信息
1.打开终端,输入help命令可以查看系统默认的shell(通常是bash)的内置的shell命令列表: [tansheng@localhost ~]$ help GNU bash, version ...
- HashCode作用
作用: 1.HashCode的存在主要是为了查找的快捷性,HashCode是用来在散列存储结构中确定镀锡的存储地址的 2.如果两个对象的equals相等,那么HashCode一定相等,反之不行 3.如 ...
- Python中数据的保存和读取
在科学计算的过程中,往往需要保存一些数据,也经常需要把保存的这些数据加载到程序中,在 Matlab 中我们可以用 save 和 lood 函数很方便的实现.类似的在 Python 中,我们可以用 nu ...