Hello Kiki & amp; http://acm.hdu.edu.cn/showproble

Source: Internet
Author: User

Problem Description
One day I was shopping in the supermarket. there was a cashier counting coins seriously when a little kid running and singing "There are a group of ducks in the lower part of the bridge in front of the door. Come and count them, October 8 ". and then the cashier put the counted coins back morosely and count again...
Hello Kiki is such a lovely girl that she loves doing counting in a different way. for example, when she is counting X coins, she count them N times. each time she divide the coins into several same sized groups and write down the group size Mi and the number of the remaining coins Ai on her note.
One day Kiki's father found her note and he wanted to know how much coins Kiki was counting.

Input
The first line is T indicating the number of test cases.
Each case contains N on the first line, Mi (1 <= I <= N) on the second line, and corresponding Ai (1 <= I <= N) on the third line.
All numbers in the input and output are integers.
1 <= T <= 100, 1 <= N <= 6, 1 <= Mi <= 50, 0 <= Ai <Mi

Output
For each case output the least positive integer X which Kiki was counting in the sample output format. If there is no solution then output-1.

Sample Input
2
2
14 57
5 56
5
19 54 40 24 80
11 2 36 20 76

Sample Output
Case 1: 341
Case 2: 5996
Question: I want to give you a variety of different ways to calculate the minimum amount of money that meets your requirements.
Idea: suddenly, it seems like a question about the Chinese surplus theorem, but the module of this question is not necessarily a matter of mutual quality. Therefore, we can use the method for solving the modulus linear equations. This requires that we must understand the Extended Euclidean algorithm. The following describes the method for solving the modulus linear equations. The idea is to constantly merge the two, to obtain. First, we can find two equations with the same remainder to set the general solution to N, N = r1 (mod (m1), N = r2 (mod (m2 )), obviously it can be converted into k1 * m1 + r1 = k2 * m2 + r2; ---> k1 * m1 + (-k2 * m2) = r2-r1; set a = m1, B = m2, x = k1, y = (-k2), c = The r2-r1 equation can be written as ax + by = c; x can be obtained by extending Euclidean, then convert x into the minimum positive integer solution of the original equation, (x * (c/d) % (B/d) + (B/d) % (B/d ); then this x is the minimum integer solution of the original equation. So N = a * (x + n * (B/d) + r1 = N = (a * B/d) * n + (a * x + r1), where n is unknown, so N = (a * x + r1) (mod (a * B/d )) then, as long as the two formulas are continuously converted into a formula, the solution of the equations can be solved.
AC code:
[Cpp]
# Include <iostream>
# Include <string. h>
# Include <string>
# Include <cstdio>
# Define N 7
Using namespace std;
Int M [N], A [N];
Int Gcd (int a, int B)
{Return B = 0? A: Gcd (B, a % B );}
Void gcd (int a, int B, int & d, int & x, int & y)
{
If (! B) x = 1, y = 0, d =;
Else gcd (B, a % B, d, y, x), y-= a/B * x;
}
Int main ()
{
Int T;
Scanf ("% d", & T );
For (int k = 1; k <= T; ++ k)
{
Int n;
Scanf ("% d", & n );
For (int I = 0; I! = N; ++ I) scanf ("% d", & M [I]);
For (int I = 0; I! = N; ++ I) scanf ("% d", & A [I]);
Int x, y, d;
Int a = M [0], c1 = A [0];
Bool flag = false;
For (int I = 1; I <n; ++ I)
{
Int B = M [I];
Int c = A [I]-c1;
Gcd (a, B, d, x, y );
If (c % d) {flag = true; break ;}
Int r = B/d;
X = (c/d * x % r + r) % r;
C1 = a * x + c1;
A = a * r;
}
If (flag) printf ("Case % d:-1 \ n", k );
Else
{
Int ans = 1;
If (c1 = 0) // in special cases, when all the remainder is 0
{
For (int I = 0; I! = N; ++ I)
Ans = M [I]/Gcd (ans, M [I]) * ans;
Printf ("Case % d: % d \ n", k, ans );
}
Else printf ("Case % d: % d \ n", k, c1 );
}
} Return 0;
}


By smallacmer

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.