【poj2891】 Strange Way to Express Integers
http://poj.org/problem?id=2891 (题目链接)
题意
求解线性同余方程组,不保证模数一定两两互质。
Solotion
一般模线性方程组的求解,详情请见:中国剩余定理
细节
注意当最后发现方程无解直接退出时,会导致有数据没有读完,然后就会Re,所以先用数组将所有数据存下来。
代码
- // poj2891
- #include<algorithm>
- #include<iostream>
- #include<cstdlib>
- #include<cstring>
- #include<cstdio>
- #include<cmath>
- #define LL long long
- #define inf 2147483640
- #define Pi acos(-1.0)
- #define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
- using namespace std;
- const int maxn=100010;
- LL a[maxn],r[maxn],n;
- void exgcd(LL a,LL b,LL &d,LL &x,LL &y) {
- if (b==0) {d=a;x=1;y=0;return;};
- exgcd(b,a%b,d,y,x);
- y-=a/b*x;
- }
- LL CRT() {
- LL M=a[1],R=r[1];
- LL d,x,y;
- for (int i=2;i<=n;i++) {
- LL mm=a[i],rr=r[i];
- exgcd(M,mm,d,x,y); //M*x+R=mm*y+rr
- if ((rr-R)%d!=0) return -1;
- x=((rr-R)/d*x%(mm/d)+mm/d)%(mm/d); //求出最小正整数x
- R+=M*x; //把整个M*x+R当做余数
- M*=mm/d; //lcm(M,m)
- }
- return R;
- }
- int main() {
- while (scanf("%lld",&n)!=EOF) {
- for (int i=1;i<=n;i++) scanf("%lld%lld",&a[i],&r[i]);
- printf("%lld\n",CRT());
- }
- return 0;
- }
【poj2891】 Strange Way to Express Integers的更多相关文章
- 【POJ2891】Strange Way to Express Integers(拓展CRT)
[POJ2891]Strange Way to Express Integers(拓展CRT) 题面 Vjudge 板子题. 题解 拓展\(CRT\)模板题. #include<iostream ...
- 【poj2891】Strange Way to Express Integers
题意: 给出n个模方程x=a(mod r) 求x的最小解 题解: 这就是个线性模方程组的模版题- - 但是有一些要注意的地方 extgcd算出来的解x可能负数 要让x=(x%mo+mo)%mo 而且 ...
- 【POJ】【2891】Strange Way to Express Integers
中国剩余定理/扩展欧几里得 题目大意:求一般模线性方程组的解(不满足模数两两互质) solution:对于两个方程 \[ \begin{cases} m \equiv r_1 \pmod {a_1} ...
- 一本通1635【例 5】Strange Way to Express Integers
1635:[例 5]Strange Way to Express Integers sol:貌似就是曹冲养猪的加强版,初看感觉非常没有思路,经过一番艰辛的***,得到以下的结果 随便解释下给以后的自己 ...
- 【POJ 2891】Strange Way to Express Integers(一元线性同余方程组求解)
Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...
- 【POJ 2891】 Strange Way to Express Integers
[题目链接] http://poj.org/problem?id=2891 [算法] exgcd [代码] #include <algorithm> #include <bitset ...
- 「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 ...
- 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; ...
- 【poj 2891】Strange Way to Express Integers(数论--拓展欧几里德 求解同余方程组 模版题)
题意:Elina看一本刘汝佳的书(O_O*),里面介绍了一种奇怪的方法表示一个非负整数 m .也就是有 k 对 ( ai , ri ) 可以这样表示--m%ai=ri.问 m 的最小值. 解法:拓展欧 ...
随机推荐
- mysql数据库的备份和导入
mysqldump -u root -p --default-character-set = gbk -d demo_db>c:/appserv/www/demosql/sql1.sql//将数 ...
- VS 2013 中如何自定义代码片段
1.菜单 工具->代码段管理器
- 记录使用gogs,drone搭建自动部署测试环境
使用gogs,drone,docker搭建自动部署测试环境 Gogs是一个使用go语言开发的自助git服务,支持所有平台 Docker是使用go开发的开源容器引擎 Drone是一个基于容器技术的持续集 ...
- scrapy 的 selector 练习
网页结构: <html> <head> <base href='http://example.com/' /> <title>Example websi ...
- 1从零开始学习Xamarin.iOS安装篇
安装和配置xamarin.ios 最近.net 开源新闻很火呀,于是想学习xamarin,早1年前就了解过这个东西,但是一直没有时间来学习,我这里装的是MAC上面的版本,废话不多说开始第一步安装. 概 ...
- 如何清洗 Git Repo 代码仓库
git prune 如何清洗 Git Repo 代码仓库 在腾讯云上创建您的SQL Cluster>>> » 相信不少团队的代码仓库 Git Repo 变得越来越大. ...
- html:关于表单功能的学习
比如我在某jsp页面中写了如下表单: <form action="/MavenWeb/TestFormPost" method="get"> & ...
- [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 ...
- 实验二实验报告 20135324&&20135330
北京电子科技学院(BESTI) 实 验 报 告 课程: 深入理解计算机系统 班级: 1353 姓名: 杨舒雯 张若嘉 学号: 20135324 20135330 成绩: 指导教师: 娄嘉鹏 实验日期: ...
- 学习笔记——Maven实战(三)多模块项目的POM重构
重复,还是重复 程序员应该有狗一般的嗅觉,要能嗅到重复这一最常见的坏味道,不管重复披着怎样的外衣,一旦发现,都应该毫不留情地彻底地将其干掉.不要因为POM不是产品代码而纵容重复在这里发酵,例如这样一段 ...