# HDU 1573 X问题(中国剩余定理)

HDU 1573 X问题

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <climits>
#include <cmath>
#include <ctime>
#include <cassert>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;

typedef long long ll;
const int MAX_N = 15;

int T, M;
ll N, a0, m0, a[MAX_N], m[MAX_N];

ll ex_gcd(ll aa, ll bb, ll& xx, ll& yy)
{
if(bb == 0) {
xx = 1, yy = 0;
return aa;
}
ll dd = ex_gcd(bb, aa % bb, yy, xx);
yy -= aa / bb * xx;
return dd;
}

//求解: m0 * x = (aa - a0) (mod mm)
bool ModularLinearEquation(ll& m0, ll& a0, ll mm, ll aa)
{
ll x, y, d;
d = ex_gcd(m0, mm, x, y);
if(labs(aa - a0) % d != 0) return false;
mm /= d;
x = x * (aa - a0) / d % mm;
a0 += x * m0;
m0 *= mm;
a0 = (a0 % m0 + m0) % m0;
return true;
}

bool CRT(ll& m0, ll&a0)
{
bool flag = true;
m0 = 1, a0 = 0; //任意数 mod m0 = a0恒成立
for(int i = 0; i < M; i++){
if(ModularLinearEquation(m0, a0, m[i], a[i]) == false){
flag = false;
break;
}
}
return flag;
}

int main()
{
scanf("%d", &T);
while(T--){
scanf("%lld%d", &N, &M);
for(int i = 0; i < M; i++){
scanf("%lld", &m[i]);
}
for(int i = 0; i < M; i++){
scanf("%lld", &a[i]);
}
if(CRT(m0, a0) == false || N < a0) printf("0\n");
else {
//printf("a0 = %lld m0 = %lld\n", a0, m0);
printf("%lld\n", (N - a0) / m0 + (a0 == 0 ? 0 : 1));
}
}
return 0;
}


• 本文已收录于以下专栏：

举报原因： 您举报文章：HDU 1573 X问题(中国剩余定理) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)