【POJ】2891 Strange Way to Express Integers

1. 题意描述
已知数$X$模$n$个$m_i$的结果分别为$r_i$,求$X$的值。

2. 基本思路
因为$m_i$并不一定互质,所以此题不能直接用CRT解,不过解法基本是类似的。模板题,解一般模线型方程组。

3. 代码

  1 /*  */
  2 #include <iostream>
  3 #include <sstream>
  4 #include <string>
  5 #include <map>
  6 #include <queue>
  7 #include <set>
  8 #include <stack>
  9 #include <vector>
 10 #include <deque>
 11 #include <bitset>
 12 #include <algorithm>
 13 #include <cstdio>
 14 #include <cmath>
 15 #include <ctime>
 16 #include <cstring>
 17 #include <climits>
 18 #include <cctype>
 19 #include <cassert>
 20 #include <functional>
 21 #include <iterator>
 22 #include <iomanip>
 23 using namespace std;
 24 //#pragma comment(linker,"/STACK:102400000,1024000")
 25
 26 #define sti                set<int>
 27 #define stpii            set<pair<int, int> >
 28 #define mpii            map<int,int>
 29 #define vi                vector<int>
 30 #define pii                pair<int,int>
 31 #define vpii            vector<pair<int,int> >
 32 #define rep(i, a, n)     for (int i=a;i<n;++i)
 33 #define per(i, a, n)     for (int i=n-1;i>=a;--i)
 34 #define clr                clear
 35 #define pb                 push_back
 36 #define mp                 make_pair
 37 #define fir                first
 38 #define sec                second
 39 #define all(x)             (x).begin(),(x).end()
 40 #define SZ(x)             ((int)(x).size())
 41 #define lson            l, mid, rt<<1
 42 #define rson            mid+1, r, rt<<1|1
 43
 44 typedef long long LL;
 45 const int maxn = 1e5+5;
 46 LL a[maxn], b[maxn];
 47 int n;
 48 bool flag;
 49
 50 void egcd(LL a, LL b, LL& d, LL& x, LL& y) {
 51     if (!b) {
 52         d = a;
 53         x = 1;
 54         y = 0;
 55     } else {
 56         egcd(b, a%b, d, y, x);
 57         y -= a/b*x;
 58     }
 59 }
 60
 61 LL china(int n, LL *r, LL *m) {
 62     LL M = m[0], R = r[0], x, y, d;
 63
 64     rep(i, 1, n) {
 65         egcd(M, m[i], d, x, y);
 66         if ((r[i] - R) % d)    return -1;
 67         x = (r[i] - R) / d * x % (m[i] / d);
 68         R += x * M;
 69         M = M / d * m[i];
 70         R %= M;
 71     }
 72
 73     return (R + M) % M;
 74 }
 75
 76 void solve() {
 77     LL ans = china(n, b, a);
 78     printf("%I64d\n", ans);
 79 }
 80
 81 int main() {
 82     cin.tie(0);
 83     ios::sync_with_stdio(false);
 84     #ifndef ONLINE_JUDGE
 85         freopen("data.in", "r", stdin);
 86         freopen("data.out", "w", stdout);
 87     #endif
 88
 89     while (scanf("%d", &n)!=EOF) {
 90         rep(i, 0, n)
 91             scanf("%I64d%I64d", &a[i],&b[i]);
 92         solve();
 93     }
 94
 95     #ifndef ONLINE_JUDGE
 96         printf("time = %ldms.\n", clock());
 97     #endif
 98
 99     return 0;
100 }
时间: 2024-11-06 05:35:36

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

POJ——T 2891 Strange Way to Express Integers

http://poj.org/problem?id=2891 Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 16849   Accepted: 5630 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is

poj 2891 Strange Way to Express Integers

http://poj.org/problem?id=2891 这道题的题意是:给你多个模性方程组:m mod ai=ri 求最小的m: 中国剩余定理 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define ll long long 5 using namespace std; 6 7 ll gcd(ll a,ll b,ll &x,ll &y) 8 { 9 if(!

poj 2891 Strange Way to Express Integers (扩展gcd)

题目链接 题意:给k对数,每对ai, ri.求一个最小的m值,令m%ai = ri; 分析:由于ai并不是两两互质的, 所以不能用中国剩余定理. 只能两个两个的求. a1*x+r1=m=a2*y+r2联立得:a1*x-a2*y=r2-r1;设r=r2-r2; 互质的模线性方程组m=r[i](mod a[i]).两个方程可以合并为一个,新的a1为lcm(a1,a2), 新的r为关于当前两个方程的解m,然后再和下一个方程合并--.(r2-r1)不能被gcd(a1,a2)整除时无解. 怎么推出的看了好

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

Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 9472   Accepted: 2873 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is

【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记

A Simple Problem with Integers Time Limit:5000MS   Memory Limit:131072K Case Time Limit:2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each

poj——2891 Strange Way to Express Integers

Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 16839   Accepted: 5625 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is

POJ 2891 Strange Way to Express Integers【扩展欧几里德】【模线性方程组】

求解方程组 X%m1=r1 X%m2=r2 .... X%mn=rn 首先看下两个式子的情况 X%m1=r1 X%m2=r2 联立可得 m1*x+m2*y=r2-r1 用ex_gcd求得一个特解x' 得到X=x'*m1+r2 X的通解X'=X+k*LCM(m1,m2) 上式可化为:X'%LCM(m1,m2)=X 到此即完成了两个式子的合并,再将此式子与后边的式子合并,最后的得到的X'即为答案的通解,求最小整数解即可. #include<stdio.h> #include<string.h

POJ 2891 Strange Way to Express Integers 中国剩余定理MOD不互质数字方法

http://poj.org/problem?id=2891 711323 97935537 475421538 1090116118 2032082 120922929 951016541 15898 418373 161478614 149488440 1748022751 21618619576 810918992 241779667 1772616743 1953316358 125248280 2273149397 3849022001 2509433771 3885219405 35

poj 2891 Strange Way to Express Integers 2012-09-05

http://poj.org/problem?id=2891 解线性模方程组. 比较坑爹,数据比较大,很容易溢出. 1 Program poj2891; 2 3 var m:int64; 4 5 a,r:array[1..30000000]of int64; 6 7 ans,x,y,lcm:int64; 8 9 10 Procedure init; 11 12 var i,j:longint; 13 14 begin 15 16 m:=0; 17 18 readln(m); 19 20 for