中国剩余定理 (求某一区间内有多少个解的问题) HDU 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

/*
 * File:   main.cpp
 * Author: hit-acm
 *
 * Created on 2012年5月25日, 上午12:42
 */

#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;
}

/*只有两个方程的式子,n % a = x && n % b = y*/
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;
}

/*中国剩余定理的主体*/

/*参数说明:crt[i][0]存的是除数,crt[i][1]存的是余数,(0<= i <n). n为方程的个数*/
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;
}

///*如需使用最大公约数,上面函数中m即是,防止越界,可以用JAVA大数写,ans=-1时代表无解!!!*/
//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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值