中国剩余定理/扩展欧几里得


  题目大意:求一般模线性方程组的解(不满足模数两两互质)

  solution:对于两个方程 \[ \begin{cases} m \equiv r_1 \pmod {a_1} \\ m \equiv r_2 \pmod{a_2} \end{cases} \] 我们可以列出式子 $$ a_1x+r_1=a_2y+r_2 $$ 利用扩展欧几里得解出一个可行解$M'$。那么我们就可以将两个限制条件合为一个: $$ m \equiv M' \pmod{ lcm(a_1,a_2)} $$ 这样我们依次合并下去即可得到答案啦~(话说代码里那段处理的过程我还没看懂……

代码:(copy自http://www.cnblogs.com/Missa/archive/2013/06/01/3112536.html

 Source Code
Problem: User: sdfzyhy
Memory: 676K Time: 0MS
Language: G++ Result: Accepted Source Code //POJ 2891
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
using namespace std;
typedef long long LL;
inline LL getLL(){
LL r=,v=; char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-')r=-;
for(; isdigit(ch);ch=getchar()) v=v*+ch-'';
return r*v;
}
const int N=1e5+,INF=~0u>>;
/******************template*********************/
LL a[N],r[N],n;
void exgcd(LL a,LL b,LL &d,LL &x,LL &y){
if (!b){d=a;x=;y=;}
else{ exgcd(b,a%b,d,y,x);y-=(a/b)*x;}
}
LL ex_CRT(LL *m,LL *r,int n){
LL M=m[],R=r[],x,y,d;
F(i,,n){
exgcd(M,m[i],d,x,y);
if ((r[i]-R)%d) return -;
x = (r[i] - R) / d * x % (m[i] / d);
R += x * M;
M = M / d * m[i];
R %= M;
}
return R > ? R :R + M;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("2891.in","r",stdin);
freopen("2891.out","w",stdout);
#endif
while(scanf("%lld",&n)!=EOF){
F(i,,n) a[i]=getLL(),r[i]=getLL();
printf("%lld\n",ex_CRT(a,r,n));
}
return ;
}

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

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

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

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

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

  3. poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 9472   ...

  4. poj——2891 Strange Way to Express Integers

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 16839 ...

  5. [POJ 2891] Strange Way to Express Integers

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 10907 ...

  6. poj 2981 Strange Way to Express Integers (中国剩余定理不互质)

    http://poj.org/problem?id=2891 Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 13 ...

  7. poj Strange Way to Express Integers 中国剩余定理

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 8193   ...

  8. Strange Way to Express Integers(中国剩余定理+不互质)

    Strange Way to Express Integers Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & ...

  9. Strange Way to Express Integers

    I. Strange Way to Express Integers 题目描述 原题来自:POJ 2891 给定 2n2n2n 个正整数 a1,a2,⋯,ana_1,a_2,\cdots ,a_na​ ...

  10. POJ2891 Strange Way to Express Integers

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

随机推荐

  1. Winform开发常用控件之Checkbox和CheckedListBox

    Winform的开发基本都是基于控件事件的,也就是事件驱动型的. 多选框的放置和值的获取有很多种,这里介绍几个简单常用的方法 1.直接放置Checkbox,并获取Checkbox的值 上图 做法也非常 ...

  2. [leetcode]_Merge Two Sorted Lists

    题目:合并两个有序单链表 思路:一开始想复杂了,以为必须在原链表上修改(绕来绕去还AC了,但是思路相当绕),其实没有,按照正常地合并两个数组同样的方法也对. 代码: public ListNode m ...

  3. ES2015 ——let命令的暂时性死区

    ES6新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. 和var不同的还有,let命令不存在变量提升,所以声明前调用变量,都会报错,这就涉及到 ...

  4. MHA学习笔记

    MHA是一款开源的MySQL高可用程序,为MySQL主从复制架构提供了节点故障转移功能,当 master发生故障时MHA会自动提升拥有最新数据的slave节点成为新的主节点,还提供了master节 点 ...

  5. Java 中的抽象类及接口

    抽象类使用 abstract 关键字修饰,该类即为抽象类. 抽象类的作用: 1.某些情况下,父类约束子类必须包含哪些方法,但不知道子类如何去实现这些方法. 2.可以从多个具有相同特征的类中抽象出一个抽 ...

  6. 用户View,五大布局

    1.LinearLayout 线性布局 android:orientation="horizontal" 制定线性布局的排列方式 水平 horizontal 垂直 vertical ...

  7. DevExpress 表中数据导出

    gridView1.ExportToXlsx("SampleStock.xlsx"); if (true) { DevExpress.XtraEditors.XtraMessage ...

  8. GetProperties(BindingFlags)说明

    Instance|Public:获取公共的的实例属性(非静态的) Instance|NonPublic:获取非公共的的实例属性(非静态的).(private/protect/internal) Sta ...

  9. Python学习基础教程(learning Python)--2.2.1 Python下的变量解析

    前文提及过变量代表内存里的某个数据,这个说法有根据么? 这里我们介绍一个python内建(built-in)函数id.我们先看看id函数的帮助文档吧.在python查某个函数的帮助文档很简单,只用he ...

  10. matlab封装DLL混合编程总结

    最近做了个项目要用到matlab做些算法处理,然后用.net项目调用这个类,我把这个matlab封装dll总结了下如下: matlab是商业数学软件,优势是在算法开发上面有很强的功能,提供了很多数学算 ...