poj2891 Strange Way to Express Integers

扩展欧几里得,注意防溢出。

http://poj.org/problem?id=2891

 1 #include <cstdio>
 2 using namespace std;
 3 typedef __int64 LL;
 4 const int maxn = 1e5 + 10;
 5
 6 LL a[maxn], r[maxn];
 7 int n;
 8 LL egcd(LL a, LL b, LL& x, LL& y){
 9     if(!b){
10         x = 1, y = 0;
11         return a;
12     }
13     LL d = egcd(b, a % b, x, y);
14     LL t = x;
15     x = y;
16     y = t - a / b * y;
17     return d;
18 }
19
20 void solve(){
21     LL R = r[0], A = a[0], x, y;
22     for(int i = 1; i < n; i++){
23         LL a1 = A, b1 = a[i], m = r[i] - R;
24         LL d = egcd(a1, b1, x, y);
25         if(m % d != 0){
26             printf("-1\n");
27             return;
28         }
29         LL t = A;
30         LL B = a[i] / d;
31         R += (m / d * x) % B * A;
32         A *= B;
33         if(R < 0) R += ((-R) / A + 2)* A;
34         R %= A;
35     }
36     printf("%I64d\n", R);
37 }
38
39 int main(){
40     freopen("in.txt", "r", stdin);
41     while(~scanf("%d", &n)){
42         for(int i = 0; i < n; i++)    scanf("%I64d%I64d", &a[i], &r[i]);
43         solve();
44     }
45     return 0;
46 }

时间: 2024-12-24 20:51:01

poj2891 Strange Way to Express Integers的相关文章

解题报告 之 POJ2891 Strange Way to Express Integers

解题报告 之 POJ2891 Strange Way to Express Integers Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is described as following: Choose k different positive integers a1, a2, 

POJ2891——Strange Way to Express Integers(模线性方程组)

Strange Way to Express Integers DescriptionElina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is described as following:Choose k different positive integers a1, a2, …, ak. For some n

POJ2891 Strange Way to Express Integers 扩展欧几里德 中国剩余定理

欢迎访问~原文出处--博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2891 题意概括 给出k个同余方程组:x mod ai = ri.求x的最小正值.如果不存在这样的x,那么输出-1.不满足所有的ai互质. 题解 互质就简单,但是不互质就有些麻烦,到现在我还是不大懂. 具体证明可以求教大佬,如果我懂了,会更新的. 代码 #include <cstring> #include <cstdio> #include <algorithm> #in

POJ2891 Strange Way to Express Integers【一元线性同余方程组】

题目链接: http://poj.org/problem?id=2891 题目大意: 选择k个不同的正整数a1.a2.-.ak,对于某个整数m分别对ai求余对应整数ri,如果 适当选择a1.a2.-.ak,那么整数m可由整数对组合(ai,ri)唯一确定. 若已知a1.a2.-.ak以及m,很容易确定所有的整数对(ai,ri),但是题目是已知a1. a2.-.ak以及所有的整数对(ai,ri),求出对应的非负整数m的值. 思路: 题目可以转换为给定一系列的一元线性方程 x ≡ r1( mod a1

【POJ2891】Strange Way to Express Integers(拓展CRT)

[POJ2891]Strange Way to Express Integers(拓展CRT) 题面 Vjudge 板子题. 题解 拓展\(CRT\)模板题. #include<iostream> #include<cstdio> using namespace std; #define ll long long #define MAX 111111 ll exgcd(ll a,ll b,ll &x,ll &y) { if(!b){x=1,y=0;return a;

【EXCRT模板】POJ2891/LuoGu4777Strange Way to Express Integers拓展中国剩余定理

这道题需要exgcd的基础 POJ的题干描述十分恶心 Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 21217 Accepted: 7120 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negati

数论F - Strange Way to Express Integers(不互素的的中国剩余定理)

F - Strange Way to Express Integers Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u Submit Status Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers.

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

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