HDU-1573-X问题(线性同余方程组)
链接:
https://vjudge.net/problem/HDU-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+k*m \leq n\)。
解为\(1+(n-x)/m\)。
当x为0时答案要减一。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h>
#include<vector>
using namespace std;
typedef long long LL;
const int INF = 1e9;
const int MAXN = 1e6+10;
LL a, b, c, k, n, lcm;
int m;
LL M[20], A[20];
LL ExGcd(LL a, LL b, LL &x, LL &y)
{
if (b == 0)
{
x = 1, y = 0;
return a;
}
LL d = ExGcd(b, a%b, x, y);
LL tmp = x;
x = y;
y = tmp - (a/b)*y;
return d;
}
LL ExCRT()
{
LL res = A[1], mod = M[1];
LL x, y, gcd;
for (int i = 2;i <= m;i++)
{
gcd = ExGcd(mod, M[i], x, y);
if ((A[i]-res)%gcd)
return -1;
x = (x*(A[i]-res))/gcd;
x = (x%(M[i]/gcd)+(M[i]/gcd))%(M[i]/gcd);
res = res+mod*x;
mod = (mod*M[i])/gcd;
res %= mod;
}
lcm = mod;
return res;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%lld%d", &n, &m);
for (int i = 1;i <= m;i++)
scanf("%lld", &M[i]);
for (int i = 1;i <= m;i++)
scanf("%lld", &A[i]);
LL res = ExCRT();
if (res == -1 || res > n)
puts("0");
else
{
printf("%lld\n", (n-res)/lcm+(res!=0));
}
}
return 0;
}
HDU-1573-X问题(线性同余方程组)的更多相关文章
- HDU1573:X问题(解一元线性同余方程组)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1573 题目解析;HDU就是坑,就是因为n,m定义成了__int64就WAY,改成int就A了,无语. 这题 ...
- HDU1573 X问题【一元线性同余方程组】
题目链接: http://acm.hdu.edu.cn/showproblem.php? pid=1573 题目大意: 求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X ...
- HDU3579:Hello Kiki(解一元线性同余方程组)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3579 题目解析:求一元线性同余方程组的最小解X,需要注意的是如果X等于0,需要加上方程组通解的整数区间lc ...
- AcWing 204. 表达整数的奇怪方式 (线性同余方程组)打卡
给定2n个整数a1,a2,…,ana1,a2,…,an和m1,m2,…,mnm1,m2,…,mn,求一个最小的整数x,满足∀i∈[1,n],x≡mi(mod ai)∀i∈[1,n],x≡mi(mod ...
- poj3708(公式化简+大数进制装换+线性同余方程组)
刚看到这个题目,有点被吓到,毕竟自己这么弱. 分析了很久,然后发现m,k都可以唯一的用d进制表示.也就是用一个ai,和很多个bi唯一构成. 这点就是解题的关键了. 之后可以发现每次调用函数f(x),相 ...
- hdu1573(线性同余方程组)
套模板,因为要是正整数,所以处理一下x=0的情况. X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- poj2891(线性同余方程组)
一个exgcd解决一个线性同余问题,多个exgcd解决线性同余方程组. Strange Way to Express Integers Time Limit: 1000MS Memory Limi ...
- POJ2891Strange Way to Express Integers (线性同余方程组)
Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative ...
- POJ2891:Strange Way to Express Integers(解一元线性同余方程组)
写一下自己的理解,下面附上转载的:若a==b(modk);//这里的==指的是同余,我用=表示相等(a%k=b)a-b=kt(t为整数)以前理解的错误思想:以前认为上面的形式+(a-tb=k)也是成立 ...
随机推荐
- [转帖]Nginx Image Module图片缩略图 水印处理模块
Nginx Image Module图片缩略图 水印处理模块 https://www.cnblogs.com/jicki/p/5546972.html Nginx Image Module图片缩略图 ...
- (零)linux 学习 -- 从 shell 开始
The Linux Command Line 读书笔记 - 部分内容来自 http://billie66.github.io/TLCL/book/chap02.html 文章目录 前言 什么是 she ...
- 以环形角度理解php数组索引
array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = false ]] ) : ...
- 从零开始学Flask框架-001
新建项目 目录结构 Index.html <!DOCTYPE html> <html lang="en"> <head> <meta ch ...
- 在vue中使用ElementUI
完整引用ElementUI: 安装:在需要使用到的vue项目目录下,使用npm下载安装: npm/cnpm i element-ui -S/--save <!-- 引入样式 --> < ...
- [二叉树算法]关于判断是否为BST的算法
//判断是否为BST 搜索树==二叉排序树 1.递归知最大最小值.2.先中序判是否单调 bool IsValidBST(BTNode *p,int low,int high){ if(p==NULL) ...
- # RESTful登录(基于token鉴权)的设计实例
使用场景 现在很多基于restful的api接口都有个登录的设计,也就是在发起正式的请求之前先通过一个登录的请求接口,申请一个叫做token的东西.申请成功后,后面其他的支付请求都要带上这个token ...
- .net SHA-256 SHA-1
Framework 4.5 uses SHA-256 algorithm for the signature, and 4.0 uses SHA-1.
- Python查看模块
1.查看Python所有内置模块 按以下链接打开,每个模块有介绍,可以选择不同的版本 https://docs.python.org/3.6/library/index.html 2.查看Python ...
- Linux下如何挂载文件,并设置开机自动挂载
首先保证服务端安装了 查看是否安装命令: nfsstat yum install nfs-utils 安装nfs-utils 192.168.50.85(服务端)192.168.50.83(客户端) ...