HDU 5768 Lucky7 Theory of Chinese remainder theorem

Source: Internet
Author: User
Tags explode modulus
The original topic see HDU 5768

In the [L,r] range is a multiple of 7 and does not satisfy the number of any given congruence. If the range is [1,100], does not meet the modulus of 3 to 2 or more than 3 of the modulus of 5 is 7,21,42,49,70,84,91, so the answer is 7.
Where the divisor is a prime number other than 7 (≤105) (\le 10^5), the product of the divisor is less than 1018 10^{18}. The same redundancy has a maximum of 15. Analysis

A multiple of 7 in all [L,r] range minus the number that satisfies any one congruence, that is, the answer is obtained. And when a number can satisfy multiple congruence, it is natural to think of the principle of tolerance to solve the problem. This collection is subtracted when the odd number of conditions are met, and vice versa.

The Chinese remainder theorem solves the solution that satisfies multiple congruence equations at the same time. Therefore 215 2^{15} within the scope of a variety of combinations of the same redundancy, you can get the answer.

Solution equation set X=ai (mod mi) mi between 22 coprime
int China (int r) {
    int M = 1, ans = 0;
    for (int i = 0; i < R; ++i)
        M *= m[i];
    for (int i = 0;i < r;i++) {
        int N = m/m[i];
        int x, y;
        Extend_euclid (N, M[i], x, y);
        x = (X%m[i]+m[i])%m[i];
        Ans = ((ans+a[i]*n%m*x%m)%M + M)%M;
    }
    return ans;
}

Pay special attention to the middle solution of each satisfied x, to take the smallest positive integer solution, or the subsequent multiplication will explode a long long (even if not explode, the answer is not, after all, M[i] is the approximate number of M, X after processing will become smaller). Ans is a constant iterative solution that is to be modeled M.
The person is still good, in the process of the ANS can also make a data explosion long long, so it is fast multiplication to engage. Note that the multiplier must be a positive number (small bin died in this hhh) code

/*--------------------------------------------* File name:hdu 5768 * Author:danliwoo * mail:danliwoo@outlook.com * Created time:2016-07-28 18:59:37--------------------------------------------*/#include <bits/stdc++.h> using
namespace Std;
#define N #define LL Long long LL a[n], m[n];
int s[n], N;
        ll Extend_euclid (ll A, ll B, LL &x, ll &y) {if (b==0) {x = 1; y = 0;
    return A;
    } LL r = extend_euclid (b, A%b, y, x);
    Y-= a/b*x;
return R;
} ll Gao (ll X, LL R, ll p) {return (x-r)/p;}
    ll Mult (ll A, LL K, ll m) {ll res = 0;
        while (k) {if (K & 1LL) res = (res + a)% m;
        K >>= 1;
    A = (a << 1)% m;
} return res;
    } ll China (ll L, ll R) {ll M = 1, ans = 0;
    for (int i = 0; I <= n; ++i) if (S[i]) {M *= m[i];
        } for (int i = 0;i <= n;i++) if (S[i]) {LL Nn = m/m[i];
        LL x, y;
        Extend_euclid (Nn, M[i], x, y); x = (X%m[i] + m[i])% m[i];
    Ans = ((Ans+mult (a[i]*nn%m, X, M))%M + m)% m;
    } LL ret = Gao (r+m, ans, m)-Gao (l-1+m, ans, M);
return ret;
    } int main () {int T, o = 0;
    scanf ("%d", &t);
        while (t--) {LL L, R;
        scanf ("%d%lld%lld", &n, &l, &r);
        memset (s, 0, sizeof (s)); M[n] = 7; A[n] = 0;
        S[n] = 1;
        for (int i = 0;i < n;i++) scanf ("%lld%lld", &m[i], &a[i]);
        LL ans = 0;
        int all = 1 << n;
            for (int i = 0;i < all;i++) {int t = i, k = 0;
                for (int j = 0;j < n;j++) {S[j] = t & 1;
                T >>= 1;
            K + = S[j]; } k = k & 1?
            -1:1; 
        Ans + = 1LL * k * China (L, R);
    } printf ("Case #%d:%lld\n", ++o, ans);
} return 0;
 }

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.