zoukankan      html  css  js  c++  java
  • POJ 2891 中国剩余定理(不互素)

    Strange Way to Express Integers
    Time Limit: 1000MS   Memory Limit: 131072K
    Total Submissions: 17877   Accepted: 6021

    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模k个不同的数,得到相应的余数,求最小的n。 
    PS:一般的CRT用来解决模数互素的情况,本题不一定模数互素,因此,一般的中国剩余定理模板是不够的。 
    代码:
     1 #include "bits/stdc++.h"
     2 #define db double
     3 #define ll long long
     4 //#define vec vector<ll>
     5 #define Mt  vector<vec>
     6 #define ci(x) scanf("%d",&x)
     7 #define cd(x) scanf("%lf",&x)
     8 #define cl(x) scanf("%lld",&x)
     9 #define pi(x) printf("%d
    ",x)
    10 #define pd(x) printf("%f
    ",x)
    11 #define pl(x) printf("%lld
    ",x)
    12 #define inf 0x3f3f3f3f
    13 #define rep(i, x, y) for(int i=x;i<=y;i++)
    14 const int N   = 1e6 + 5;
    15 const int mod = 1e9 + 7;
    16 const int MOD = mod - 1;
    17 const db  eps = 1e-10;
    18 const db  PI  = acos(-1.0);
    19 using namespace std;
    20 typedef pair<ll,ll> pll;
    21 ll a[N],b[N],m[N];
    22 ll gcd(ll x,ll y) {return y==0?x:gcd(y,x%y);}
    23 ll ex_gcd(ll a,ll b,ll& x,ll& y)
    24 {
    25     if(b==0){
    26         x = 1,y = 0;
    27         return a;
    28     }
    29     ll d = ex_gcd(b,a%b,y,x);
    30     y -= a/b*x;
    31     return d;
    32 }
    33 
    34 ll inv(ll a,ll p)
    35 {
    36     ll d,x,y;
    37     d = ex_gcd(a,p,x,y);
    38     return d==1?(x%p+p)%p:-1;
    39 }
    40 pll CRT(ll A[], ll B[], ll M[], int n) {//求解A[i]x = B[i] (mod M[i]),总共n个线性方程组
    41     ll x = 0, m = 1;
    42     for(int i = 0; i < n; i ++) {
    43         ll a = A[i] * m, b = B[i] - A[i]*x, d = gcd(M[i], a);
    44         if(b % d != 0)  return pll(0, -1);//答案不存在,返回-1
    45         ll t = b/d * inv(a/d, M[i]/d)%(M[i]/d);
    46         x = x + m*t;
    47         m *= M[i]/d;
    48     }
    49     x = (x % m + m ) % m;
    50     return pll(x, m);//返回的x就是答案,m是最后的lcm值
    51 }
    52 
    53 int main()
    54 {
    55     int k;
    56     while(~scanf("%d",&k))
    57     {
    58         for(int i=0;i<k;i++)
    59         {
    60             a[i] = 1;
    61             scanf("%lld%lld",&m[i],&b[i]);
    62         }
    63         pll ans = CRT(a,b,m,k);
    64         if(ans.second==-1) puts("-1");
    65         else    pl(ans.first);
    66     }
    67 }
  • 相关阅读:
    [转发]深入理解git,从研究git目录开始
    iOS系统网络抓包方法
    charles抓包工具
    iOS多线程中performSelector: 和dispatch_time的不同
    IOS Core Animation Advanced Techniques的学习笔记(五)
    IOS Core Animation Advanced Techniques的学习笔记(四)
    IOS Core Animation Advanced Techniques的学习笔记(三)
    IOS Core Animation Advanced Techniques的学习笔记(二)
    IOS Core Animation Advanced Techniques的学习笔记(一)
    VirtualBox复制CentOS后提示Device eth0 does not seem to be present的解决方法
  • 原文地址:https://www.cnblogs.com/mj-liylho/p/8406458.html
Copyright © 2011-2022 走看看