2013-07-12 14:18:06Morris

[UVA][math] 10787 - Modular Equations


  Modular Equations 

The laws of modular arithmetic are among the best weapons that we have in our arsenal. We, the wannabe computer scientists, frequently use those laws to keep things manageable. For example if we are to compute the units digit of
23513714 - 24514732
we would be able to do that in a flash. However if it requires dealing with equations involving modular arithmetic, many of us may not feel just as comfortable. Fear not; were not going to daunt you with a system of gruesome modular equations we would keep it small and simple. Given the range of three integers a ( amin$ le$a$ le$amax), b ( bmin$ le$b$ le$bmax) and m ( mmin$ le$m$ le$mmax) you are to find the number of triples (a, b, c) that satisfy the equation:
(a + b) modm = (a - b) mod m
Here is a sample.
$displaystyle bf 1 le a le 2, 2 le b le 4, 3 le m le 5$
$displaystyle bf (a+b) bmod m$$displaystyle bf =$$displaystyle bf (a-b) bmod m$
$displaystyle bf (1+2) bmod 4$$displaystyle bf 3$$displaystyle bf (1-2) bmod 4$
$displaystyle bf (1+3) bmod 3$$displaystyle bf 1$$displaystyle bf (1-3) bmod 3$
$displaystyle bf (1+4) bmod 4$$displaystyle bf 1$$displaystyle bf (1-4) bmod 4$
$displaystyle bf (2+2) bmod 4$$displaystyle bf0$$displaystyle bf (2-2) bmod 4$
$displaystyle bf (2+3) bmod 3$$displaystyle bf 2$$displaystyle bf (2-3) bmod 3$
$displaystyle bf (2+4) bmod 4$$displaystyle bf 2$$displaystyle bf (2-4) bmod 4$
   

Input 

There can be multiple test cases. The first line of the input gives you the number of test cases T ( 1$ le$T$ le$20). Each of the next T line would contain the input for each test case. The input for each test is given by 3 pairs of integer amin, amax, bmin, bmax and mmin, mmax. You can assume that,
  • -1000$ le$amin$ le$amax$ le$ + 1000
  • -1000$ le$bmin$ le$bmax$ le$ + 1000
  • +1$ le$mmin$ le$mmax$ le$ + 1000.

Output 

For each of the test case you need to print the serial number of the test case first. Then on the same line you have to print the number of triples (a, b, c) that satisfy our modular equation.

Sample Input 

3
1 2 2 4 3 5
-100 100 200 350 1 1000
5 9 10 12 2 9

Sample Output 

Case 1: 6
Case 2: 318384
Case 3: 45



Miguel Revilla 2004-12-02

a+b = pm+r
a-b = qm+r
=> p-q = 2*b/m
=> a = (p+q)*m/2 + r
if has pair (b, m), then all a range has.

因此檢查有多少組 2*b mod m = 0 即可。

#include <stdio.h>

int main() {
    int testcase, cases = 0;
    scanf("%d", &testcase);
    while(testcase--) {
        int amn, amx, bmn, bmx, mmn, mmx;
        int i, j;
        scanf("%d %d", &amn, &amx);
        scanf("%d %d", &bmn, &bmx);
        scanf("%d %d", &mmn, &mmx);
        int pair = 0;
        for(i = bmn; i <= bmx; i++)//b
            for(j = mmn; j <= mmx; j++)//m
                if((2*i)%j == 0)
                    pair++;
        printf("Case %d: %d\n", ++cases, pair*(amx-amn+1));
    }
    return 0;
}
/*
a+b = pm+r
a-b = qm+r
=> p-q = 2*b/m
=> a = (p+q)*m/2 + r
if has pair (b, m), then all a range has.
*/