HDU 1573 CRT
CRT模板题
/** @Date : 2017-09-15 13:52:21
* @FileName: HDU 1573 CRT EXGCD.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @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
HDU 1573 CRT的更多相关文章
- HDU 1930 CRT
也是很模板的一道题,给出一些数,分割,模数固定是4个互质的. /** @Date : 2017-09-16 23:54:51 * @FileName: HDU 1930 CRT.cpp * @Plat ...
- 中国剩余定理 hdu 1573 X问题
HDU 1573 X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 1573 X问题 中国剩余定理
链接:pid=1573">http://acm.hdu.edu.cn/showproblem.php? pid=1573 题意:求在小于等于N的正整数中有多少个X满足:X mod a[ ...
- X问题 HDU - 1573(excrt入门题)
X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- BZOJ 2976: [Poi2002]出圈游戏 HDU 5668 CRT
2976: [Poi2002]出圈游戏 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2976 Description Input 中第一 ...
- hdu 1573 X问题 (非互质的中国剩余定理)
X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- hdu 1573 X问题
数论题,本想用中国剩余定理,可是取模的数之间不一定互质,用不了,看到网上有篇文章写得很好的:数论——中国剩余定理(互质与非互质),主要是采用合并方程的思想: 大致理解并参考他的代码后便去试试hdu上这 ...
- hdu 1573 x问题(中国剩余定理)HDU 2007-1 Programming Contest
只是套模板而已(模板其实也不懂). 留着以后好好学的时候再改吧. 题意—— X = a[i] MOD b[i]; 已知a[i],b[i],求在[1, n]中存在多少x满足条件. 输入—— 第一行一个整 ...
- hdu 1573 A/B (扩展欧几里得)
Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973)= 1). Input 数据的第一行 ...
随机推荐
- 20172305 暑假作业 之 TimeCalculate & Save Iron Man
20172305 暑假作业 之 TimeCalculate & Save Iron Man TimeCalculate 项目介绍 项目名称: TimeCalculate 项目简介: 本项目基于 ...
- AVL树 算法思想与代码实现
AVL树是高度平衡的二叉搜索树,按照二叉搜索树(Binary Search Tree)的性质,AVL首先要满足: 若它的左子树不为空,则左子树上所有结点的值均小于它的根结点的值: 若它的右子树不为空, ...
- 2016-2017 ACM-ICPC, NEERC, Moscow Subregional Contest Problem L. Lazy Coordinator
题目来源:http://codeforces.com/group/aUVPeyEnI2/contest/229511 时间限制:1s 空间限制:512MB 题目大意: 给定一个n 随后跟着2n行输入 ...
- 使用命令virsh管理网络设备,创建桥设备 和 使用virt-manager创建虚拟机
# 1:查看网卡接口 virsh iface-list Name State MAC Address ------------------------------------------------- ...
- 成功解决JSP和Servlet的中文乱码问题
表单提交时出现乱码: 在进行表单提交的时候,经常提交一些中文,自然就避免不了出现中文乱码的情况,对于表单来说有两种提交方式:get和post提交方式.所以请求的时候便有get请求和post请求.以前我 ...
- virtualenv是什么?virtualenv的安装及pycharm的配置和使用
virtualenv是什么? virtualenv是一个创建隔绝的Python环境的工具.virtualenv创建一个包含所有必要的可执行文件的文件夹,用来使用Python工程所需的包.简单的说就是一 ...
- Building microservices with ASP.NET Core (without MVC)(转)
There are several reasons why it makes sense to build super-lightweight HTTP services (or, despite a ...
- Collections带有的排序方法 传入的元素类型 需是子类或者这个类的实例
- APIO/CTSC2017游记
5.10开坑,别问我为啥今天才开始写,前几天玩得太开心了233 5.7 坐火车坐火车,坐地铁坐地铁.其实是第一次坐地铁233.解锁了在地铁上双手玩手机不扶东西站立的姿势? 全程烧流量上QQ,拜大佬约面 ...
- 超链接提示效果jQuery+CSS+html
我们知道浏览器自带了超链接提示, 只需要在超链接中加入 title 属性就可以了. <a href="#" title="吉大砍人案致1死1伤 受害者死前大喊他手里 ...