【POJ 2891】 Strange Way to Express Integers
【题目链接】
http://poj.org/problem?id=2891
【算法】
exgcd
【代码】
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
typedef long long ll; int i,k;
ll a[],m[]; inline ll exgcd(ll a,ll b,ll &x,ll &y)
{
ll g;
if (b == )
{
x = ;
y = ;
return a;
} else
{
g = exgcd(b,a%b,y,x);
y -= a / b * x;
return g;
}
}
inline ll solve()
{
int i;
ll g,x,y,M = a[],R = m[];
for (i = ; i <= k; i++)
{
g = exgcd(M,a[i],x,y);
if ((R - m[i]) % g != ) return -;
x = (R - m[i]) / g * x % a[i];
R -= x * M;
M = M / g * a[i];
R %= M;
}
return (R % M + M) % M;
}
int main()
{ while (scanf("%d",&k) != EOF)
{
for (i = ; i <= k; i++) scanf("%lld%lld",&a[i],&m[i]);
printf("%lld\n",solve());
}
return ; }
【POJ 2891】 Strange Way to Express Integers的更多相关文章
- 【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(数论--拓展欧几里德 求解同余方程组 模版题)
题意:Elina看一本刘汝佳的书(O_O*),里面介绍了一种奇怪的方法表示一个非负整数 m .也就是有 k 对 ( ai , ri ) 可以这样表示--m%ai=ri.问 m 的最小值. 解法:拓展欧 ...
- 一本通1635【例 5】Strange Way to Express Integers
1635:[例 5]Strange Way to Express Integers sol:貌似就是曹冲养猪的加强版,初看感觉非常没有思路,经过一番艰辛的***,得到以下的结果 随便解释下给以后的自己 ...
- 【POJ】【2891】Strange Way to Express Integers
中国剩余定理/扩展欧几里得 题目大意:求一般模线性方程组的解(不满足模数两两互质) solution:对于两个方程 \[ \begin{cases} m \equiv r_1 \pmod {a_1} ...
- 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; ...
- 【POJ2891】Strange Way to Express Integers(拓展CRT)
[POJ2891]Strange Way to Express Integers(拓展CRT) 题面 Vjudge 板子题. 题解 拓展\(CRT\)模板题. #include<iostream ...
- 【POJ 1958】 Strange Towers of Hanoi
[题目链接] http://poj.org/problem?id=1958 [算法] 先考虑三个塔的情况,g[i]表示在三塔情况下的移动步数,则g[i] = g[i-1] * 2 + 1 再考虑四个塔 ...
- 【POJ 3468】 A Simple Problem with Integers
[题目链接] 点击打开链接 [算法] 本题用线段树很容易写,但是,笔者为了练习树状数组,就用树状数组的方法做了一遍 我们不妨引入差分数组c, 则sum(n) = c[1] + (c[1] + c[2] ...
- 【poj2891】 Strange Way to Express Integers
http://poj.org/problem?id=2891 (题目链接) 题意 求解线性同余方程组,不保证模数一定两两互质. Solotion 一般模线性方程组的求解,详情请见:中国剩余定理 细节 ...
随机推荐
- 【转载】JavaScript中同名标识符优先级-Snandy
一,局部变量先使用后声明,不影响外部同名变量 var x = 1; // --> 外部变量x function fn(){ alert(x); // --> undefined 局部变量x ...
- JAVA 构建使用 Native 库
Java 使用Native文件,一般分解为下面几个步骤: 在Java代码中使用native关键字声明一个本地方法 运行javah,获得包含该方法声明的C语言头文件(使用jni编程中的C函数名通常是相关 ...
- 推荐系统入门:作为Rank系统的推荐系统(协同过滤)
知乎:如何学习推荐系统? 知乎:协同过滤和基于内容的推荐有什么区别? 案例:推荐系统实战? 数据准备:实现推荐栏位:重构接口:后续优化. 简书:实现实时推荐系统的三种方式?基于聚类和协同过滤:基于S ...
- 《3D建模初步》参考资料
本门课程主要从3D打印角度来介绍3D建模的简单知识.课程采用免费的3D CAD软件Autodesk 123D Design来具体演示一些简单模型的构建方法,并介绍3D打印有关的知识与方法. 课程以&l ...
- sqlserver where in 在 mysql
) tmp); 主句(select * from (从句 temp) sql的 where in 删除 要更改为 // in( select * from ((select idfrom twhe ...
- C#—接口和抽象类的区别?
一.接口 接口是指对协定进行定义的引用类型,其他类型实现接口,以保证它们支持某些操作.接口指定必须由类提供的成员或实现它的其他接口.与类相似,接口可以包含方法.属性.索引器和事件作为成员. 1.接口存 ...
- 前端面试题总结 -vue
1.active-class是哪个组件的属性? vue-router模块的router-link组件. 2.嵌套路由怎么定义? 在 VueRouter 的参数中使用 children 配置,这样就可以 ...
- 安卓 九宫格 GridView 的表格布局
首先,请大家理解一下“迭代显示”这个概念,这个好比布局嵌套,我们在一个大布局里面重复的放入一些布局相同的小布局,那些重复的部分是由图片和文字组成的小控件,图片在上方,文字在下方,之后我们只需要把这些小 ...
- C语言右移操作在汇编层面的相关解释
在 CSDN 看到帖子回复如下: x=y>>2;004122A8 mov eax,dword ptr [y] 004122AB sar eax,2 '算 ...
- Coloring Flame Graphs: Code Hues
转自:http://www.brendangregg.com/blog/2017-07-30/coloring-flamegraphs-code-type.html I recently improv ...