0 0 = b 0 0, X mod a 1 1 = b 1 1, X mod a 2 2 = b 2 2, …, X mod a i i = b i i, … (0 < a i i

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

Sample Output

1 0

3

中国剩余定理的简单应用

#include<set>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-8;
const int mod = 1e9 + 7;
const int N = 20;
const int INF = 0x7FFFFFFF;
int T, n, m;
LL A, B, a[N], b[N], ans;

void egcd(LL a, LL b, LL&d, LL&x, LL&y)
{
if (!b) { d = a, x = 1, y = 0; }
else
{
egcd(b, a%b, d, y, x);
y -= x*(a / b);
}
}

LL lmes() {
LL M = a[0], R = b[0], x, y, d;
for (int i = 1; i < m; i++) {
egcd(M, a[i], d, x, y);
if ((b[i] - R) % d) return 0;
x = (b[i] - R) / d*x % (a[i] / d);
R += x*M;
M = M / d*a[i];
R %= M;
}
R = (R + M) % M;
return !R ? n / M : n < R ? 0 : (n - R) / M + 1;
}

int main()
{
scanf("%d", &T);
while (T--)
{
scanf("%d%d", &n, &m);
rep(i, 0, m - 1) scanf("%lld", &a[i]);
rep(i, 0, m - 1) scanf("%lld", &b[i]);
printf("%d\n", lmes());
}
return 0;
}