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;
}

  

时间: 2024-08-08 06:55:23

HDU 1573的相关文章

HDU 1573 CRT

CRT模板题 /** @Date : 2017-09-15 13:52:21 * @FileName: HDU 1573 CRT EXGCD.cpp * @Platform: Windows * @Author : Lweleth ([email protected]) * @Link : https://github.com/ * @Version : $Id$ */ #include <bits/stdc++.h> #define LL long long #define PII pair

HDU 1573 X问题 中国剩余定理

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1573 题意:求在小于等于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). 思路:中国剩余定理的模板题,如果找不到这样的数或者最小的X大于N,输出零. 代码: #include <iostream> #include

中国剩余定理 hdu 1573 X问题

HDU 1573 X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4857    Accepted Submission(s): 1611 Problem Description 求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X mod a[1] = b[1], X mod a[2] = b[2],

hdu 1573 X问题 (非互质的中国剩余定理)

X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2980    Accepted Submission(s): 942 Problem Description 求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X mod a[1] = b[1], X mod a[2] = b[2], …, X mod

HDU 1573 X问题

http://acm.hdu.edu.cn/showproblem.php?pid=1573 解出最小解rr后,特判下其是否为0,为0的话,就直接n / lcm 否则 + 1 #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define inf (0x3f3f3f3

HDU——1573 X问题

X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6716    Accepted Submission(s): 2340 Problem Description 求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X mod a[1] = b[1], X mod a[2] = b[2], …, X mo

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

题目链接 题意 : 中文题不详述. 思路 : 中国剩余定理.求中国剩余定理中解的个数.看这里看这里 1 //1573 2 #include <stdio.h> 3 #include <iostream> 4 #include <math.h> 5 6 using namespace std ; 7 8 long long x,y ; 9 long long N,M ; 10 11 long long ext_gcd(long long a,long long b) 12

X问题 HDU - 1573(excrt入门题)

X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8365    Accepted Submission(s): 3037 Problem Description 求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X mod a[1] = b[1], X mod a[2] = b[2], -, X mod

HDU 1573 模线性方程

给定模线性方程组,求最终的值的通解.点击 两个模方程可以化解成一个模方程 x mod a1 = b1 x mod a2 = b2 a1*k1 + a2*k2 = b2 – b1 // 其中k1k2是自由元 用扩展欧几里得算出k1的解,当然它是一个解系,找出最小k1作为特解,带入x = a1 * k1 + b1得到x 然后把这个x当作特解,记作x1. 之前k1本来是一个解系,他的模是a2/gcd(a1,a2),[简单将gcd(a1,a2)记作t], 也就是说k1如果作为一个解系的话,k1 = a2