【POJ】【2891】Strange Way to Express Integers
中国剩余定理/扩展欧几里得
题目大意:求一般模线性方程组的解(不满足模数两两互质)
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的更多相关文章
- 一本通1635【例 5】Strange Way to Express Integers
1635:[例 5]Strange Way to Express Integers sol:貌似就是曹冲养猪的加强版,初看感觉非常没有思路,经过一番艰辛的***,得到以下的结果 随便解释下给以后的自己 ...
- 【POJ2891】Strange Way to Express Integers(拓展CRT)
[POJ2891]Strange Way to Express Integers(拓展CRT) 题面 Vjudge 板子题. 题解 拓展\(CRT\)模板题. #include<iostream ...
- poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 9472 ...
- poj——2891 Strange Way to Express Integers
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 16839 ...
- [POJ 2891] Strange Way to Express Integers
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 10907 ...
- poj 2981 Strange Way to Express Integers (中国剩余定理不互质)
http://poj.org/problem?id=2891 Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 13 ...
- poj Strange Way to Express Integers 中国剩余定理
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 8193 ...
- Strange Way to Express Integers(中国剩余定理+不互质)
Strange Way to Express Integers Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & ...
- Strange Way to Express Integers
I. Strange Way to Express Integers 题目描述 原题来自:POJ 2891 给定 2n2n2n 个正整数 a1,a2,⋯,ana_1,a_2,\cdots ,a_na ...
- POJ2891 Strange Way to Express Integers
题意 Language:Default Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total S ...
随机推荐
- Spark基础排序+二次排序(java+scala)
1.基础排序算法 sc.textFile()).reduceByKey(_+_,).map(pair=>(pair._2,pair._1)).sortByKey(false).map(pair= ...
- MongoDB简述
简介 MongoDB is an open-source document database that provides high performance, high availability, an ...
- 深入浅出MongoDB(二)概述
上次的博文深入浅出MongoDB(一)NoSQL中我们已经简单介绍了一下NoSQL的基本概念,这次我们来了解一下MongoDB的相关概念. 1.简介 MongoDB是一款由C++编写的高性能.开源.无 ...
- 新浪SAE URLRewrite(伪静态、重定向)详解
SAE全称Sina App Engine,真是一个好东西,他有很多优秀的特性,简单来说SAE就是一个简单高效的分布式Web服务开发.运行平台.支持现在常用的 PHP+Mysql 环境,在开发中难免会碰 ...
- Eclipse之Failed to load the JNI shared library”……\jvm.dll”的解决方案
问题描述:java环境变量配置完全正确,但是运行eclipse时提示:Failed to load the JNI shared library " xxx\jva.dll" 原因 ...
- 一个表格说明RelativeLayout中的几个重要属性【Written By KillerLegend】
RelativeLayout中几种布局属性的说明 无 无 无 无
- 《大话设计模式》ruby版代码:策略模式
需求: 商场收银软件,根据客户购买物品的单价和数量,计算费用,会有促销活动,打八折,满三百减一百之类的. 一,使用工厂模式. # -*- encoding: utf-8 -*- #现金收费抽象类 cl ...
- 数组链表下标指针map list
1.时间复杂度 (1)时间频度 一个算法执行所耗费的时间,从理论上是不能算出来的,必须上机运行测试才能知道.但我们不可能也没有必要对每个算法都上机测试,只需知道哪个算法花费的时间多,哪个算法花费的时间 ...
- iOS开发多线程篇—单例模式(ARC)
iOS开发多线程篇—单例模式(ARC) 一.简单说明: 设计模式:多年软件开发,总结出来的一套经验.方法和工具 java中有23种设计模式,在ios中最常用的是单例模式和代理模式. 二.单例模式说明 ...
- 24.task的运用
任务就是一段封装在“task-endtask”之间的程序.任务是通过调用来执行的,而且只有在调用时才执行,如果定义了任务,但是在整个过程中都没有调用它,那么这个任务是不会执行的.调用某个任务时可能需要 ...