【POJ】2891 Strange Way to Express Integers
2016-06-17 22:04
363 查看
1. 题意描述
已知数$X$模$n$个$m_i$的结果分别为$r_i$,求$X$的值。
2. 基本思路
因为$m_i$并不一定互质,所以此题不能直接用CRT解,不过解法基本是类似的。模板题,解一般模线型方程组。
3. 代码
已知数$X$模$n$个$m_i$的结果分别为$r_i$,求$X$的值。
2. 基本思路
因为$m_i$并不一定互质,所以此题不能直接用CRT解,不过解法基本是类似的。模板题,解一般模线型方程组。
3. 代码
/* */ #include <iostream> #include <sstream> #include <string> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #include <deque> #include <bitset> #include <algorithm> #include <cstdio> #include <cmath> #include <ctime> #include <cstring> #include <climits> #include <cctype> #include <cassert> #include <functional> #include <iterator> #include <iomanip> using namespace std; //#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int> #define stpii set<pair<int, int> > #define mpii map<int,int> #define vi vector<int> #define pii pair<int,int> #define vpii vector<pair<int,int> > #define rep(i, a, n) for (int i=a;i<n;++i) #define per(i, a, n) for (int i=n-1;i>=a;--i) #define clr clear #define pb push_back #define mp make_pair #define fir first #define sec second #define all(x) (x).begin(),(x).end() #define SZ(x) ((int)(x).size()) #define lson l, mid, rt<<1 #define rson mid+1, r, rt<<1|1 typedef long long LL; const int maxn = 1e5+5; LL a[maxn], b[maxn]; int n; bool flag; void egcd(LL a, LL b, LL& d, LL& x, LL& y) { if (!b) { d = a; x = 1; y = 0; } else { egcd(b, a%b, d, y, x); y -= a/b*x; } } LL china(int n, LL *r, LL *m) { LL M = m[0], R = r[0], x, y, d; rep(i, 1, n) { egcd(M, m[i], d, x, y); if ((r[i] - R) % d) return -1; x = (r[i] - R) / d * x % (m[i] / d); R += x * M; M = M / d * m[i]; R %= M; } return (R + M) % M; } void solve() { LL ans = china(n, b, a); printf("%I64d\n", ans); } int main() { cin.tie(0); ios::sync_with_stdio(false); #ifndef ONLINE_JUDGE freopen("data.in", "r", stdin); freopen("data.out", "w", stdout); #endif while (scanf("%d", &n)!=EOF) { rep(i, 0, n) scanf("%I64d%I64d", &a[i],&b[i]); solve(); } #ifndef ONLINE_JUDGE printf("time = %ldms.\n", clock()); #endif return 0; }
相关文章推荐
- 嵌入式系统通用驱动程序接口及其实现-EERPOM存取位置标准化(视频教学时的同步输入文件)
- 使用struts1回显数据,防重复提交
- 【设计模式】过滤器模式
- 关于MYSQL的一些命令
- 基于C++的简单的FSM实现
- BZOJ3489 A simple rmq problem
- 补c++作业2
- 再次拾回android-修改系统定义的style
- SQLServer附加数据库失败
- 彩电市场竞争格局巨变, 创维“修炼内功”逆势增长
- exit和_exit,atexit的浅析
- 【翻译】Tweepy 3.5.0 Doc (2) Authentication Tutorial
- selenium常用命令之操作页面元素及获取元素内容的事件整理
- java学习笔记之ORM注解
- Deep Learning(深度学习)学习笔记整理系列之(四)
- 大道至简阅读笔记02
- 【转】Linux CentOS内核编译:下载CentOS源码、编译2.6.32-220的错误(apic.c:819 error 'numi_watchdog' undeclared)
- android 多渠道打包
- 数据结构(树,点分治):POJ 1741 Tree
- 跳出多重循环 JS