题目:
http://acm.hdu.edu.cn/showproblem.php?pid=1573
题意:
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的个数。
思路:
线性同余方程组模板题,贴两个模板
模板一:此模板要求同余方程组必须是如x≡ri(modmi)的形式,若给出的同余方程是aix≡ri(modmi)这样的形式,那么可以先求出逆元a−1imodmi,两边同时乘以逆元即可转换成如上形式
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 110, INF = 0x3f3f3f3f;
int M[N], R[N];
int gcd(int a, int b)
{
return !b ? a : gcd(b, a%b);
}
int extgcd(int a, int b, int &x, int &y)
{
int d = a;
if(b)
{
d = extgcd(b, a%b, y, x);
y -= (a/b) * x;
}
else x = 1, y = 0;
return d;
}
int linear_congruence(int M[], int R[], int n, int num)
{
int m = M[1], r = R[1];
int x, y, flag = 1;
for(int i = 2; i <= n; i++)
{
int d = gcd(m, M[i]), c = R[i] - r;
if(c % d != 0)
{
flag = 0; break;
}
extgcd(m/d, M[i]/d, x, y);
int tm = M[i] / d;
x = ((c/d * x) % tm + tm) % tm;
r = r + x*m;
m = m/d * M[i];
r %= m;
}
//if(r < 0) r += m;
int ans = 0;
if(num < r || flag == 0) ans = 0;
else ans = (num - r)/m + 1;
if(ans != 0 && r == 0) ans--;
return ans;
}
int main()
{
int t, n, m;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; i++) scanf("%d", &M[i]);
for(int i = 1; i <= m; i++) scanf("%d", &R[i]);
printf("%d\n", linear_congruence(M, R, m, n));
}
return 0;
}
模板二:此模板可以解如aix≡ri(modmi)形式的同余方程组,模板一中的形式即ai=1的情况
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int N = 110;
int A[N], B[N], M[N];
int gcd(int a, int b)
{
return !b ? a : gcd(b, a % b);
}
int extgcd(int a, int b, int &x, int &y)
{
int d = a;
if(b)
{
d = extgcd(b, a%b, y, x);
y -= (a / b) * x;
}
else x = 1, y = 0;
return d;
}
int mod_inverse(int a, int m)
{
int x, y;
extgcd(a, m, x, y);
return (m + x%m) % m;
}
P linear_congruence(const int *A, const int *B, const int *M, int len)
{
int x = 0, m = 1;
for(int i = 0; i < len; i++)
{
int a = A[i] * m, b = B[i] - A[i] * x, d = gcd(M[i], a);
if(b % d != 0) return make_pair(0, -1);
int t = b / d * mod_inverse(a / d, M[i] / d) % (M[i] / d);
x = x + m * t;
m *= M[i] / d;
}
return make_pair((x+m) % m, m);
}
int main()
{
int t;
int n, m;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &n, &m);
for(int i = 0; i < m; i++) A[i] = 1;
for(int i = 0; i < m; i++) scanf("%d", &M[i]);
for(int i = 0; i < m; i++) scanf("%d", &B[i]);
P p = linear_congruence(A, B, M, m);
int ans = 0;
if(p.second == -1) ans = 0;
else if(n - p.first < 0) ans = 0;
else ans = (n - p.first) / p.second + 1;
if(p.first == 0 && ans) ans--;
printf("%d\n", ans);
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1000 + 10;
ll extgcd(ll a, ll b, ll &x, ll &y)
{
ll d = a;
if(b != 0)
{
d = extgcd(b, a%b, y, x);
y -= (a/b)*x;
}
else x = 1, y = 0;
return d;
}
ll linear_congruence(ll m[], ll r[], ll n)
{
ll M = m[1], R = r[1];
for(ll i = 2; i <= n; i++)
{
ll x, y;
int d = extgcd(M, m[i], x, y);
ll c = r[i] - R;
if(c % d) return -1;
ll mod = m[i] / d;
x = (c / d * x % mod + mod) % mod;
R = R + x * M;
M = M / d * m[i];
}
return R;
}
int main()
{
int n;
ll m[N], r[N];
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%lld%lld", &m[i], &r[i]);
ll ans = linear_congruence(m, r, n);
printf("%lld\n", ans);
return 0;
}