http://poj.org/problem?id=2891 (题目链接)

题意

  求解线性同余方程组,不保证模数一定两两互质。

Solotion

  一般模线性方程组的求解,详情请见:中国剩余定理

细节

  注意当最后发现方程无解直接退出时,会导致有数据没有读完,然后就会Re,所以先用数组将所有数据存下来。

代码

  1. // poj2891
  2. #include<algorithm>
  3. #include<iostream>
  4. #include<cstdlib>
  5. #include<cstring>
  6. #include<cstdio>
  7. #include<cmath>
  8. #define LL long long
  9. #define inf 2147483640
  10. #define Pi acos(-1.0)
  11. #define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
  12. using namespace std;
  13.  
  14. const int maxn=100010;
  15. LL a[maxn],r[maxn],n;
  16.  
  17. void exgcd(LL a,LL b,LL &d,LL &x,LL &y) {
  18. if (b==0) {d=a;x=1;y=0;return;};
  19. exgcd(b,a%b,d,y,x);
  20. y-=a/b*x;
  21. }
  22. LL CRT() {
  23. LL M=a[1],R=r[1];
  24. LL d,x,y;
  25. for (int i=2;i<=n;i++) {
  26. LL mm=a[i],rr=r[i];
  27. exgcd(M,mm,d,x,y); //M*x+R=mm*y+rr
  28. if ((rr-R)%d!=0) return -1;
  29. x=((rr-R)/d*x%(mm/d)+mm/d)%(mm/d); //求出最小正整数x
  30. R+=M*x; //把整个M*x+R当做余数
  31. M*=mm/d; //lcm(M,m)
  32. }
  33. return R;
  34. }
  35. int main() {
  36. while (scanf("%lld",&n)!=EOF) {
  37. for (int i=1;i<=n;i++) scanf("%lld%lld",&a[i],&r[i]);
  38. printf("%lld\n",CRT());
  39. }
  40. return 0;
  41. }

  

  

【poj2891】 Strange Way to Express Integers的更多相关文章

  1. 【POJ2891】Strange Way to Express Integers(拓展CRT)

    [POJ2891]Strange Way to Express Integers(拓展CRT) 题面 Vjudge 板子题. 题解 拓展\(CRT\)模板题. #include<iostream ...

  2. 【poj2891】Strange Way to Express Integers

    题意: 给出n个模方程x=a(mod r) 求x的最小解 题解: 这就是个线性模方程组的模版题- - 但是有一些要注意的地方 extgcd算出来的解x可能负数  要让x=(x%mo+mo)%mo 而且 ...

  3. 【POJ】【2891】Strange Way to Express Integers

    中国剩余定理/扩展欧几里得 题目大意:求一般模线性方程组的解(不满足模数两两互质) solution:对于两个方程 \[ \begin{cases} m \equiv r_1 \pmod {a_1} ...

  4. 一本通1635【例 5】Strange Way to Express Integers

    1635:[例 5]Strange Way to Express Integers sol:貌似就是曹冲养猪的加强版,初看感觉非常没有思路,经过一番艰辛的***,得到以下的结果 随便解释下给以后的自己 ...

  5. 【POJ 2891】Strange Way to Express Integers(一元线性同余方程组求解)

    Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...

  6. 【POJ 2891】 Strange Way to Express Integers

    [题目链接] http://poj.org/problem?id=2891 [算法] exgcd [代码] #include <algorithm> #include <bitset ...

  7. 「POJ2891」Strange Way to Express Integers【数学归纳法,扩展中国剩余定理】

    题目链接 [VJ传送门] 题目描述 给你\(a_1...a_n\)和\(m_1...m_n\),求一个最小的正整数\(x\),满足\(\forall i\in[1,n] \equiv a_i(mod ...

  8. 1635:【例 5】Strange Way to Express Integers

    #include<bits/stdc++.h> #define ll long long using namespace std; ll n,m,a,lcm,now; bool flag; ...

  9. 【poj 2891】Strange Way to Express Integers(数论--拓展欧几里德 求解同余方程组 模版题)

    题意:Elina看一本刘汝佳的书(O_O*),里面介绍了一种奇怪的方法表示一个非负整数 m .也就是有 k 对 ( ai , ri ) 可以这样表示--m%ai=ri.问 m 的最小值. 解法:拓展欧 ...

随机推荐

  1. mysql数据库的备份和导入

    mysqldump -u root -p --default-character-set = gbk -d demo_db>c:/appserv/www/demosql/sql1.sql//将数 ...

  2. VS 2013 中如何自定义代码片段

    1.菜单 工具->代码段管理器

  3. 记录使用gogs,drone搭建自动部署测试环境

    使用gogs,drone,docker搭建自动部署测试环境 Gogs是一个使用go语言开发的自助git服务,支持所有平台 Docker是使用go开发的开源容器引擎 Drone是一个基于容器技术的持续集 ...

  4. scrapy 的 selector 练习

    网页结构: <html> <head> <base href='http://example.com/' /> <title>Example websi ...

  5. 1从零开始学习Xamarin.iOS安装篇

    安装和配置xamarin.ios 最近.net 开源新闻很火呀,于是想学习xamarin,早1年前就了解过这个东西,但是一直没有时间来学习,我这里装的是MAC上面的版本,废话不多说开始第一步安装. 概 ...

  6. 如何清洗 Git Repo 代码仓库

    git prune 如何清洗 Git Repo 代码仓库       在腾讯云上创建您的SQL Cluster>>> »   相信不少团队的代码仓库 Git Repo 变得越来越大. ...

  7. html:关于表单功能的学习

    比如我在某jsp页面中写了如下表单: <form action="/MavenWeb/TestFormPost" method="get">   & ...

  8. [CareerCup] 13.9 Aligned Malloc and Free Function 写一对申请和释放内存函数

    13.9 Write an aligned malloc and free function that supports allocating memory such that the memory ...

  9. 实验二实验报告 20135324&&20135330

    北京电子科技学院(BESTI) 实 验 报 告 课程: 深入理解计算机系统 班级: 1353 姓名: 杨舒雯 张若嘉 学号: 20135324 20135330 成绩: 指导教师: 娄嘉鹏 实验日期: ...

  10. 学习笔记——Maven实战(三)多模块项目的POM重构

    重复,还是重复 程序员应该有狗一般的嗅觉,要能嗅到重复这一最常见的坏味道,不管重复披着怎样的外衣,一旦发现,都应该毫不留情地彻底地将其干掉.不要因为POM不是产品代码而纵容重复在这里发酵,例如这样一段 ...