HDU3579 Hello Kiki "one-element linear congruence Equation Group"

Source: Internet
Author: User

Topic Links:

http://acm.hdu.edu.cn/showproblem.php?pid=3579


Main topic:

Kiki had x coins, she counted n times in different ways, each time she divided the coins into equal-sized groups and recorded each A group of coins

The number of MI and the last remaining coins in the number of AI. So here's the question: how many coins are there in total?


Ideas:

The typical unary linear congruence equation Group x = Ai (mod Mi) solution. The problem requires the output of the minimum positive integer solution, if the same remainder is obtained

The solution of the equations is 0, then the answer is the least common multiple of all mi.


AC Code:

#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace    STD; #define LL __int64ll GCD (ll a,ll b) {if (b = = 0) return A; Return GCD (b,a%b);}        void Exgcd (ll a,ll b,ll &d, ll &x,ll &y) {if (!b) {x = 1;        y = 0;    D = A;        } else {EXGCD (b,a%b,d,y,x);    Y-= x * (A/b);    }}ll A[15],r[15];int Main () {LL A,B,C,D,X0,Y0,LCM;    int n,m,kase = 0;    scanf ("%d", &n);        while (n--) {LCM = 1;        BOOL flag = 1;        scanf ("%d", &m);            for (int i = 1; I <= M; ++i) {scanf ("%i64d", &a[i]);        LCM = LCM/GCD (A[I],LCM) * A[i];        } for (int i = 1; I <= M; ++i) scanf ("%i64d", &r[i]);        printf ("Case%d:", ++kase);            for (int i = 2; I <= M; ++i) {a = a[1];            b = A[i];            c = r[i]-r[1];            EXGCD (A,B,D,X0,Y0);         if (c%d! = 0)   {flag = 0;            Break            } LL temp = b/d;            x0 = (x0* (C/D)%temp + temp)% temp;            R[1] = a[1]*x0 + r[1];        A[1] = a[1]* (a[i]/d);        } if (!flag) printf (" -1\n");            else {if (r[1]! = 0) printf ("%i64d\n", r[1]);        else printf ("%i64d\n", LCM); }} return 0;}



HDU3579 Hello Kiki "one-element linear congruence Equation Group"

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.