天天看点

中国剩余定理 (求某一区间内有多少个解的问题) HDU X问题X问题

X问题

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1551    Accepted Submission(s): 443 Problem Description 求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X mod a[1] = b[1], X mod a[2] = b[2], …, X mod a[i] = b[i], … (0 < a[i] <= 10)。   Input 输入数据的第一行为一个正整数T,表示有T组测试数据。每组测试数据的第一行为两个正整数N,M (0 < N <= 1000,000,000 , 0 < M <= 10),表示X小于等于N,数组a和b中各有M个元素。接下来两行,每行各有M个正整数,分别为a和b中的元素。   Output 对应每一组输入,在独立一行中输出一个正整数,表示满足条件的X的个数。   Sample Input
3
10 3
1 2 3
0 1 2
100 7
3 4 5 6 7 8 9
1 2 3 4 5 6 7
10000 10
1 2 3 4 5 6 7 8 9 10
0 1 2 3 4 5 6 7 8 9
         
#include <iostream> #include <cstdio> #include <cstring> #include <string.h> using namespace std; typedef __int64 LL; LL GCD(LL a, LL b) {     return (b == 0) ? a : GCD(b, a % b); } LL extend_Euclid(LL a, LL b, LL &x, LL &y) {     if (b == 0) {         x = 1;         y = 0;         return a;     }     LL gcd = extend_Euclid(b, a % b, x, y);     LL temp = x;     x = y;     y = temp - (a / b) * x;     return gcd; } LL CRT_2(LL a, LL x, LL b, LL y) {     LL xx, yy, gcd;     gcd = extend_Euclid(a, b, xx, yy);     LL c = y - x;     while (c < 0) {         c += a;     }     if (c % gcd != 0) {         return -1;     }     xx *= c / gcd;     yy *= c / gcd;     LL t = yy / (a / gcd);     while (yy - t * (a / gcd) > 0) t++;     while (yy - (t - 1)*(a / gcd) <= 0) t--;     return (t * (a / gcd) - yy)*b + y; } LL CRT(LL crt[][2], int n, LL &m) {     m = crt[0][0] / GCD(crt[0][0], crt[1][0]) * crt[1][0];     LL ans = CRT_2(crt[0][0], crt[0][1], crt[1][0], crt[1][1]) % m;     for (int i = 2; i < n && ans != -1; i++) {         ans = CRT_2(m, ans, crt[i][0], crt[i][1]);         m *= crt[i][0] / GCD(m, crt[i][0]);         ans %= m;     }     return ans; } // //int main() { //    LL crt[11][2]; //    int n; //    while (scanf("%d", &n) != EOF) { //        for (int i = 0; i < n; i++) { //            scanf("%lld%lld", &crt[i][0], &crt[i][1]); //        } // //        //        if (n == 1) { //            printf("%lld\n", crt[0][1]); //        } else { //            printf("%lld\n", CRT(crt, n)); //        } //    } //    return 0; //} int main() {     int T;     int n;     LL m;     LL crt[23][2];     scanf("%d", &T);     while (T--) {         scanf("%I64d%d", &m, &n);         for (int i = 0; i < n; i++) {             scanf("%I64d", &crt[i][0]);         }         for (int i = 0; i < n; i++) {             scanf("%I64d", &crt[i][1]);         }         LL ans;                 if (n == 1) {             if (crt[0][0] == 0) {                 ans = 0;             } else {                 if (crt[0][1] > m) {                     ans = 0;                 } else if (crt[0][1] == 0) {                     ans = m / crt[0][0];                 } else {                     ans = (m - crt[0][1]) / crt[0][0] + 1;                 }             }         } else {             LL LCM;             int res = CRT(crt, n, LCM);             if (res == -1 || res > m) {                 ans = 0;             } else if (res == 0) {                 ans = m / LCM;             } else {                 ans = (m - res) / LCM + 1;             }         }         printf("%I64d\n", ans);     }     return 0; }