yt15-hdu-the least common multiple of the remainder theorem in China

Source: Internet
Author: User
Tags greatest common divisor

Problem Description

I know that some of my classmates are looking at the Chinese remainder theorem recently, and this theorem itself is relatively simple:
Assuming that the m1,m2,..., Mk 22, the following equation group is the same:
X≡A1 (mod M1)
X≡A2 (mod m2)
...
X≡ak (MoD mk)
There is a unique solution within the 0<=<m1m2...mk.
Kee Mi=m/mi (1<=i<=k), because (mi,mi) = 1, so there are two integers pi,qi meet Mipi+miqi=1, if you remember Ei=mi/pi, then there will be:
Ei≡0 (mod MJ), j!=i
Ei≡1 (mod MJ), j=i
Obviously, E1a1+e2a2+...+ekak is a solution of the equations, and this solution adds and subtract an integer multiple of M to get the least nonnegative integer solution.
This is the Chinese remainder theorem and its solution process.
Now there is a problem with this:
A positive integer n divided by M1 (m1-a), divided by M2 remainder (m2-a), divided by M3 remainder (m3-a), in short, divided by MI remainder (mi-a), where (a<mi<100 i=1,2,... I) to find the minimum number of conditions to satisfy. 

Input

The input data contains multiple sets of test instances, the first line of each instance being two integers I (1<i<10) and a, where I represents the number of m, the meaning of a as described above, followed by a line of I integer M1,m1 ... Mi,i=0 and a=0 end input, not processed.

Output

For each test instance, output the minimum number of satisfied conditions in a row. The output for each instance takes up one row.

Sample Input

2 12 30 0

Sample Output

5


The code is as follows:

#include <iostream>using namespace Std;long long gcd (long long A, long  long B)         //Request Greatest common divisor {    long long t;
   if (A < b)     {        t=a;        a=b;        b=t;    }    if (b = = 0)        return A;    t= a%b;    while (t!= 0)    {        a=b;        b=t;        t=a%b;    }    return b;} int main () {    long long i,j,a,sum;    int str[10];    while (Cin>>i>>a)    {        if (i==0| | a==0) break            ;        Sum=1;        for (j=0; j<i; j + +)        {            cin>>str[j];            Sum= (Sum*str[j])/GCD (Sum,str[j]);          The least common multiple of two numbers = the product of two numbers/greatest common divisor           }        sum-=a;                                        Originally Yu (m-a) But at this time is the M to calculate, after the end should be subtracted from a                                          cout<<sum<<endl;    }    return 0;}


Operation Result:

yt15-hdu-the least common multiple of the remainder theorem in China

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.