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<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;

LL mod;
LL a[11];
LL b[11];
LL exgcd(LL a, LL b, LL &x, LL &y)
{
	LL d = a;
	if(b == 0)
		x = 1, y = 0;
	else
	{
		d = exgcd(b, a % b, y, x);
		y -= (a / b) * x;
	}
	return d;
}
LL md(LL x,LL y)
{
	LL res = x % y;
	if(res <= 0)
		res = res + y;
	return res;
}
int main()
{
	int T;
	cin >> T;
	while(T--)
	{
		LL n, m;
		scanf("%lld%lld", &n, &m);
		for(int i = 0; i < m; i++) scanf("%d", a + i);
		for(int j = 0; j < m; j++) scanf("%d", b + j);
		LL rem, mod;
		LL x, y;
		LL ans = 0;
		int flag = 0;
		rem = b[0], mod = a[0];
		for(int i = 1; i < m ; i++)
		{
			LL c = b[i] - rem;
			LL g = __gcd(a[i], mod);
			if(c % g!= 0)
				flag = 1;
			else
			{
				exgcd(mod, a[i], x, y);
				LL tmp = a[i] / g;
				//x = (c / g * x % tmp + tmp) % tmp;// m1x1+m2x2 = c
				x = md(c / g * x, tmp);//x =
				rem = md(rem + mod * x, mod / g * a[i]);
				//rem = mod * x + rem;//ri + mixi
				mod = mod / __gcd(mod, a[i]) * a[i];//LCM(m1, m2);
				//y mod lcm(m1,m2) = x
			}
		}
		//cout << rem <<"~" << mod << endl;
		if(flag || n < rem)
			printf("0\n");
		else printf("%lld\n", (n - rem) / mod + 1);
	}
    return 0;
}//shi·ne
时间: 2024-07-31 04:46:50

HDU 1573 CRT的相关文章

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 1930 CRT

也是很模板的一道题,给出一些数,分割,模数固定是4个互质的. /** @Date : 2017-09-16 23:54:51 * @FileName: HDU 1930 CRT.cpp * @Platform: Windows * @Author : Lweleth ([email protected]) * @Link : https://github.com/ * @Version : $Id$ */ #include <bits/stdc++.h> #define LL long lon

中国剩余定理 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

hdu 1573 x问题(中国剩余定理)

只是套模板而已(模板其实也不懂). 留着以后好好学的时候再改吧. 题意—— X = a[i] MOD b[i]; 已知a[i],b[i],求在[1, n]中存在多少x满足条件. 输入—— 第一行一个整数t,表示一共t组数据. 第二行两个整数n,m,表示在n以内寻找满足的数,一共m组方程组. 输出—— 一个整数.如果存在满足的x,则输出x的数量.否则输出0. 直接给代码吧—— 1 #include <cstdio> 2 #include <iostream> 3 #include &

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