【poj2891】 Strange Way to Express Integers

poj.org/problem?id=2891 (题目链接)

题意:求解线性同余方程组,不保证模数一定两两互质。

Solotion

  用exgcd将俩个同余方程合并成一个 
   
  如合并n%M=R,n%m=r 
   
  即M*x+R=m*y+r 
   
  M*x-m*y=r-R 
   
  设a=M/t,b=m/t,c=(r-R)/t,t=gcd(a,b) 
   
  若(r-R)%t!=0则无解 
   
  用exgcd得到a*x+b*y=c的解x0, 
   
  通解x=x0+k*b,k为整数 
   
  带入M*x+R=n 
   
  M*b*k+M*x0+R=n 
   
  所以R=R+M*x0,M=M*b 
  

  蒯自hzwer。 
  注意当最后发现方程无解直接退出时,会导致有数据没有读完,然后就会Re,所以现用数组将所有数据存下来。

代码:

// poj2891
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define inf 2147483640
#define Pi 3.1415926535898
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std;

const int maxn=100010;
LL mm[maxn],rr[maxn],n;

LL gcd(LL a,LL b) {
    return a%b==0 ? b : gcd(b,a%b);
}
void exgcd(LL a,LL b,LL &x,LL &y) {
    if (!b) {x=1,y=0;return;}
    exgcd(b,a%b,y,x);
    y-=x*(a/b);   //注意此处a/b一定要到打括号,因为取的是a/b的整数部分
}
int main() {
    while (scanf("%lld",&n)!=EOF) {
        LL M,R,flag=1;
        scanf("%lld%lld",&M,&R);
        for (int i=1;i<n;i++) scanf("%lld%lld",&mm[i],&rr[i]);
        for (int i=1;i<n;i++) {
            LL m,r,x,y;
            m=mm[i],r=rr[i];
            LL d=gcd(M,m);
            if ((r-R)%d!=0) {printf("-1\n");flag=0;break;}
            exgcd(M/d,m/d,x,y);   //x*(M/d)+y*(m/d)=d,解出x,y
            x=((r-R)/d*x%(m/d)+(m/d))%(m/d);   //求出最小x
            R+=M*x;
            M*=m/d;   //lcm(M,m)
        }
        if (flag) printf("%lld\n",R);
    }
    return 0;
}

  

时间: 2024-10-19 08:32:34

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

【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;

【POJ2891】Strange Way to Express Integers

[题目大意] 给出k个模方程组:x mod ai = ri.求x的最小正值.如果不存在这样的x,那么输出-1. [题解] 模板题,练习剩余定理的模板 1 /************* 2 POJ 2891 3 by chty 4 2016.11.3 5 *************/ 6 #include<iostream> 7 #include<cstdlib> 8 #include<cstdio> 9 #include<cstring> 10 #inclu

【POJ】【2891】Strange Way to Express Integers

中国剩余定理/扩展欧几里得 题目大意:求一般模线性方程组的解(不满足模数两两互质) solution:对于两个方程 \[ \begin{cases} m \equiv r_1 \pmod {a_1} \\ m \equiv r_2 \pmod{a_2} \end{cases} \] 我们可以列出式子 $$ a_1x+r_1=a_2y+r_2 $$ 利用扩展欧几里得解出一个可行解$M'$.那么我们就可以将两个限制条件合为一个: $$ m \equiv M' \pmod{ lcm(a_1,a_2)}

【POJ 2891】Strange Way to Express Integers(扩展欧几里得)

[POJ 2891]Strange Way to Express Integers(扩展欧几里得) Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 12934   Accepted: 4130 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative in

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; void exgcd(ll a,ll b,ll &d,ll &x,ll &y) { if(b==0) { d=a; x=1; y=0; } else { exgcd(b,a%b,d,x,y); ll t=x; x=y; y=t-a/b*y; } } int main() { ll d

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

解题报告 之 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】同余方程组

同余方程组 例题1:pku2891Strange Way to Express Integers 中国剩余定理求的同余方程组mod 的数是两两互素的.然而本题(一般情况,也包括两两互素的情况,所以中国剩余定理成为了“时代的眼泪”)mod的数可能不是互素,所以要转换一下再求. P=b1(mod a1);  P / a1 ==?~~~~b1 P =b2(mod a2); P =b3(mod a3); …… P =bn(mod an); a1~an,b1~bn是给出来的. 解: 第一条:a1*x+b1

数论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.