X问题
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3847 Accepted Submission(s): 1226
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
Sample Output
1 0 3
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
ll Read()
{
char ch;
ll a = 0;
while((ch = getchar()) == ' ' | ch == '\n');
a += ch - '0';
while((ch = getchar()) != ' ' && ch != '\n')
{
a *= 10;
a += ch - '0';
}
return a;
}
void Prll(ll a) //输出外挂
{
if(a>9)
Prll(a/10);
putchar(a%10+'0');
}
void Exgcd(ll a,ll b,ll &d,ll &x,ll &y)
{
if(!b)
{
x=1; y=0; d=a;
}
else
{
Exgcd(b,a%b,d,y,x);
y-=x*(a/b);
}
}
int aa[20],r[20];
int main()
{
// fread;
int tc;
scanf("%d",&tc);
while(tc--)
{
ll n,m;
scanf("%I64d%I64d",&n,&m);
for(ll i=1;i<=m;i++)
scanf("%I64d",&aa[i]);
for(ll i=1;i<=m;i++)
scanf("%I64d",&r[i]);
bool ifhave=1;
ll a1,a2,r1,r2,ans,a,b,c,d,x0,y0;
a1=aa[1];r1=r[1];
for(int i=2;i<=m;i++)
{
a2=aa[i]; r2=r[i];
a=a1; b=a2; c=r2-r1;
Exgcd(a,b,d,x0,y0);
if(c%d!=0)
ifhave=0;
ll t=b/d;
x0=(x0*(c/d)%t+t)%t;
r1=a1*x0+r1;
a1=a1*(a2/d);
// r1=(r1%a1+a1)%a1;
}
if(!ifhave||n<r1)
{
puts("0");
continue;
}
ll num=(n-r1)/a1+1;
if(r1==0)
num--;
printf("%I64d\n",num);
}
return 0;
}