HDU 1573

特判r1=0时的情况,因为0是不能模的。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int MaxM=11;

int a[MaxM],b[MaxM];

void exgcd(int a,int b,int &d,int &x,int &y){
	if(b==0){
		x=1; y=0;
		d=a;
	}
	else{
		exgcd(b,a%b,d,x,y);
		int tmp=x;
		x=y;
		y=tmp-(a/b)*y;
	}
}

int gcd(int a,int b){
	if(b==0) return a;
	return gcd(b,a%b);
}

int main(){
	int t; int a1,r1,a2,r2; int aa,bb,cc,dd; int x,y; int g;
	scanf("%d",&t);
	int n,m;
	bool ifhave;
	while(t--){
		scanf("%d%d",&n,&m);
		g=1;
		for(int i=0;i<m;i++){
			scanf("%d",&a[i]);
			if(i==0){ g=a[i]; continue; }
			g=g*a[i]/gcd(g,a[i]);
		}
		for(int i=0;i<m;i++)
		scanf("%d",&b[i]);
		a1=a[0]; r1=b[0];
		ifhave=true;
		for(int i=1;i<m;i++){
			a2=a[i]; r2=b[i];
			aa=a1; bb=a2; cc=r2-r1;
			exgcd(aa,bb,dd,x,y);
			if(cc%dd!=0){
				ifhave=false;
				break;
			}
			int t=bb/dd;
			x=(x*(cc/dd)%t+t)%t;
			r1=a1*x+r1;
			a1=a1*(a2/dd);
		}
		if(!ifhave)
		printf("0\n");
		else{
			int tmp=r1;
			int i;
			for(i=1;;i++){
				if(tmp+(i-1)*g>n) break;
			}
			if(tmp==0) i--;
			printf("%d\n",i-1);
		}
	}
	return 0;
}

  

转载于:https://www.cnblogs.com/jie-dcai/p/3945723.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值