zoukankan      html  css  js  c++  java
  • poj

    Time Limit: 1000MS   Memory Limit: 131072K
    Total Submissions: 14547   Accepted: 4759

    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 a1a2…, ak. For some non-negative m, divide it by every ai (1 ≤ i ≤ k) to find the remainder ri. If a1a2, …, ak are properly chosen, m can be determined, then the pairs (airi) can be used to express m.

    “It is easy to calculate the pairs from m, ” said Elina. “But how can I find m from the pairs?”

    Since Elina is new to programming, this problem is too difficult for her. Can you help her?

    Input

    The input contains multiple test cases. Each test cases consists of some lines.

    • Line 1: Contains the integer k.
    • Lines 2 ~ k + 1: Each contains a pair of integers airi (1 ≤ i ≤ k).

    Output

    Output the non-negative integer m on a separate line for each test case. If there are multiple possible values, output the smallest one. If there are no possible values, output -1.

    Sample Input

    2
    8 7
    11 9

    Sample Output

    31

    Hint

    All integers in the input and the output are non-negative and can be represented by 64-bit integral types.

    Source

    题意:
    给你n个线性同余方程,让你求解这个方程组。
    思路:
    对于同余方程(x≡b(mod m) 等价于 x = b1 + m1*x1;所以对于两个同余方程组,就有b1+m1*x1=b2+m2*x2。移项得到m2*y2-m1*y1=b1-b2;根据扩展欧几里得课以求出y1的值,带回就可以求出X,X是这个方程的一个特解,通解为X = X'+k*lcm(m1, m2),两边同时取模,得到X mod lcm(m1. m2)= X',这就是两个方程合并之后的结果。
    #include <map>
    #include <set>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <iostream>
    #include <stack>
    #include <cmath>
    #include <string>
    #include <vector>
    #include <cstdlib>
    //#include <bits/stdc++.h>
    //#define LOACL
    #define space " "
    using namespace std;
    typedef long long LL;
    //typedef __int64 Int;
    typedef pair<int, int> paii;
    const int INF = 0x3f3f3f3f;
    const double ESP = 1e-5;
    const double PI = acos(-1.0);
    const long long MOD = 1e9 + 7;
    const int MAXN = 1e5;
    void ext_gcd(LL a, LL b, LL& d, LL& x, LL& y) {
        if (!b){x = 1;d = a; y = 0;}
        else {
            ext_gcd(b, a%b, d, y, x);
            y -= x*(a/b);
        }
    }
    void solved(int n) {
        bool flag = false;
        LL a0, r0, a1, r1, x, y, a ,b, c, d;
        scanf("%lld%lld", &a0, &r0);
        for (int i = 1; i < n; i++) {
            scanf("%lld%lld", &a1, &r1);
            a = a0, b = a1, c = r1 - r0;
            ext_gcd(a, b, d, x, y);
            if (c%d) flag = true;
            x = ((x*c/d)%(b/d) + b/d)%(b/d);
            r0 = a0*x + r0;
            a0 = a1*a0/d;
        }
        if (flag) printf("-1
    ");
        else printf("%lld
    ", r0);
    }
    int main() {
        int n;
        while (scanf("%d", &n) != EOF) {
            solved(n);
        }
        return 0;
    }
     


  • 相关阅读:
    MongoDB系列一(查询).
    css重写checkbox样式
    python在数据处理中常用的模块之matplotlib
    第二节,TensorFlow 使用前馈神经网络实现手写数字识别
    Python数据挖掘课程
    第一节,TensorFlow基本用法
    第九节,改善深层神经网络:超参数调试、正则化及梯度下降算法(下)
    第八节,改善深层神经网络:超参数调试、正则化及梯度下降算法(中)
    theano使用
    第一节,基础知识之第一步:代数
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770778.html
Copyright © 2011-2022 走看看