http://poj.org/problem?id=2891

题意:与中国剩余定理不同,p%ai=bi,此处的ai(i=1 2 3 ……)是不一定互质的,所以要用到的是同余方程组,在网上看到有人称为拓展中国剩余定理。

具体讲解可以看我昨天的博文:http://www.cnblogs.com/KonjakJuruo/p/5176417.html

  1. //poj2891
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<iostream>
  6. using namespace std;
  7.  
  8. typedef long long LL;
  9. const LL N=;
  10. LL a[N],b[N];
  11. LL tx,ty;
  12.  
  13. LL exgcd(LL aa,LL bb)
  14. {
  15. if(bb==) {tx=,ty=;return aa;}
  16. LL d=exgcd(bb,aa%bb);
  17. LL x=ty,y=tx-(aa/bb)*ty;
  18. tx=x;ty=y;
  19. return d;
  20. }
  21.  
  22. LL lcu(LL aa,LL bb)
  23. {
  24. LL d=exgcd(aa,bb);
  25. return aa*bb/d;
  26. }
  27.  
  28. int main()
  29. {
  30. freopen("a.in","r",stdin);
  31. freopen("a.out","w",stdout);
  32. // printf("%d\n",(-5)%3);
  33. LL n,a1,b1,x;
  34. while(scanf("%I64d",&n)!=EOF)
  35. {
  36. bool bk=;
  37. for(LL i=;i<=n;i++)
  38. scanf("%I64d%I64d",&a[i],&b[i]);
  39. LL A=a[],B=a[],C=b[]-b[];
  40. LL g=exgcd(A,B);
  41. if(C%g) bk=;
  42. else {
  43. x=((tx*C/g)%(B/g)+(B/g))%(B/g);
  44. b1=a[]*x+b[];
  45. a1=lcu(a[],a[]);
  46. }
  47. for(LL i=;i<=n;i++)
  48. {
  49. A=a1,B=a[i],C=b[i]-b1;
  50. g=exgcd(A,B);
  51. if(C%g) {bk=;break;}
  52. x=((tx*C/g)%(B/g)+(B/g))%(B/g);
  53. b1=a1*x+b1;
  54. a1=lcu(a1,a[i]);
  55. }
  56. if(bk) printf("%I64d\n",b1);
  57. else printf("-1\n");
  58. }
  59. return ;
  60. }

【poj2891-Strange Way to Express Integers】拓展欧几里得-同余方程组的更多相关文章

  1. HDU-3579-Hello Kiki (利用拓展欧几里得求同余方程组)

    设 ans 为满足前 n - 1个同余方程的解,lcm是前n - 1个同余方程模的最小公倍数,求前n个同余方程组的解的过程如下: ①设lcm * x + ans为前n个同余方程组的解,lcm * x ...

  2. 【hdu3579-Hello Kiki】拓展欧几里得-同余方程组

    http://acm.hdu.edu.cn/showproblem.php?pid=3579 题解:同余方程组的裸题.注意输出是最小的正整数,不包括0. #include<cstdio> ...

  3. 【hdu1573-X问题】拓展欧几里得-同余方程组

    http://acm.hdu.edu.cn/showproblem.php?pid=1573 求小于等于N的正整数中有多少个X满足: X mod a0 = b0 X mod a1 = b1 …… X  ...

  4. 中国剩余定理+扩展中国剩余定理 讲解+例题(HDU1370 Biorhythms + POJ2891 Strange Way to Express Integers)

    0.引子 每一个讲中国剩余定理的人,都会从孙子的一道例题讲起 有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二.问物几何? 1.中国剩余定理 引子里的例题实际上是求一个最小的x满足 关键是,其中 ...

  5. POJ2891——Strange Way to Express Integers(模线性方程组)

    Strange Way to Express Integers DescriptionElina is reading a book written by Rujia Liu, which intro ...

  6. POJ2891 Strange Way to Express Integers

    题意 Language:Default Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total S ...

  7. POJ2891 Strange Way to Express Integers 扩展欧几里德 中国剩余定理

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2891 题意概括 给出k个同余方程组:x mod ai = ri.求x的最小正值.如果不存在这样的x, ...

  8. 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 ...

  9. POJ-2891 Strange Way to Express Integers(拓展中国剩余定理)

    放一个写的不错的博客:https://www.cnblogs.com/zwfymqz/p/8425731.html POJ好像不能用__int128. #include <iostream> ...

随机推荐

  1. 横屏下的ImagePickerController

    Try this way.... As per Apple Document, ImagePicker Controller never Rotate in Landscape mode. You h ...

  2. Swift function how to return nil

    这两天在学习Stanford出品的iOS7的课程,这个课程去年也看过,但是看到第3课就不行了,满篇的OC,把人都搞晕了.这段时间因为要写个iOS的App,正好赶上了Swift问世,所以趁着这股劲继续学 ...

  3. [转]NHibernate之映射文件配置说明

    1. hibernate-mapping 这个元素包括以下可选的属性.schema属性,指明了这个映射所引用的表所在的schema名称.假若指定了这个属性, 表名会加上所指定的schema的名字扩展为 ...

  4. openSUSE install failed

    install openSUSE 13.1 with vmware play (version 6.0.0)  calling the yast module 'inst_autoinit has f ...

  5. python djange输入中文错误的解决办法

    UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128) ...

  6. 我的VS2013中,用Ado.net给SQLParameter赋值的时候,当赋值null的时候,生成的sql语句是default

    /// <summary> /// 增加一条数据 /// </summary> public bool Add(Model.WechatDocuments model) { S ...

  7. C++string的使用

    在这里总结一下string的用法 String是可变长字符串,使用的时候要包含string头文件. 要想使用标准C++中string类,必须要包含 #include <string>// ...

  8. 如何查看系统内置的shell命令及帮助信息

    1.打开终端,输入help命令可以查看系统默认的shell(通常是bash)的内置的shell命令列表: [tansheng@localhost ~]$ help GNU bash, version ...

  9. HashCode作用

    作用: 1.HashCode的存在主要是为了查找的快捷性,HashCode是用来在散列存储结构中确定镀锡的存储地址的 2.如果两个对象的equals相等,那么HashCode一定相等,反之不行 3.如 ...

  10. Python中数据的保存和读取

    在科学计算的过程中,往往需要保存一些数据,也经常需要把保存的这些数据加载到程序中,在 Matlab 中我们可以用 save 和 lood 函数很方便的实现.类似的在 Python 中,我们可以用 nu ...