【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 一般模线性方程组的求解,详情请见:中国剩余定理 细节 ...
随机推荐
- vim的快捷键大全
vim是开发利器,掌握快捷可以事半功倍,这里总结下常用的快捷键,提高开发速度这里写代码片 1.vim ~/.vimrc 进入配置文件 如果不知道vimrc文件在哪,可使用 :scriptnames 来 ...
- Android截图截取弹框AlertDialog
1:效果如图 2:权限 <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="21" ...
- VHDL之concurrent之operators
Using operators Operators can be used to implement any combinational circuit. However, as will becom ...
- C++编译错误fatal error C1004: 发现意外的文件尾
出现这种情况就是类或者结构体的定义后面没有加“;”导致的. 而且这种问题好难排查.
- luogu P2422 良好的感觉 单调栈
Code: #include<bits/stdc++.h> #define maxn 1000000 #define ll long long using namespace std; v ...
- 15.6.1 【Task使用】基于任务的异步模式
C# 5异步函数特性的一大好处是,它为异步提供了一致的方案.但如果在命名异步方法以及 触发异常等方面做法存在着差异,则很容易破坏这种一致性.微软因此发布了基于任务的异步模 式(Task-based A ...
- HDU-1134 卡特兰数+java大数模板
题意: 给你一个n,然后1,2,3...2n-1,2n围一圈,让每个数都能用一条线配对并且线与线之间不能交叉,问有几种方法数. 思路: 1 可以和2,4,6...连接.假如 一共有8个数,1和2连 ...
- 洛谷P1996 约瑟夫问题【队列】
题目背景 约瑟夫是一个无聊的人!!! 题目描述 n个人(n<=100)围成一圈,从第一个人开始报数,数到m的人出列,再由下一个人重新从1开始报数,数到m的人再出圈,--依次类推,直到所有的人都出 ...
- [NOIP模拟赛]b
组合数学+容斥原理 设f[i][j]表示第i个序列中的j的倍数的个数. 然后以j为gcd的贡献就是(π(f[i][j]+1) )-1 然后从大到小枚举j,删去j的倍数的贡献即可.
- mac Gitblit安装
jdk下载传送门 gitBlit是java编写的的 第一步 需要安装java jdk 传送门 JDK6的下载地址: http://www.oracle.com/technetwork/java/jav ...