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)也是成立 ...
随机推荐
- ubuntu mysql5.7设置Open Files Limit
目的:解决Too many open files异常 方式:设置Open Files Limit 环境:(MySQL)Server version: 5.7.27-0ubuntu0.16.04.1 ( ...
- C++实现2048小游戏
代码如下: #define _CRT_SECURE_NO_WARNINGS//去掉编译器内部扩增问题 #include<stdio.h> #include<stdlib.h> ...
- Python+VSCode+Git【转】
Python+VSCode+Git 学习总结 - 秦无邪 - 博客园
- PB学习笔记之随笔
1.根据条件改变字体颜色.if(curdate>=bdate and curdate<edate,rgb(255,0,0),if(sex=1, if(curdate>=mdate, ...
- webpack资源加载常用配置
const path = require('path'); module.exports = { entry: './src/index.js', output: { filename: 'bundl ...
- logback配置文件模板
<?xml version="1.0" encoding="UTF-8"?> <configuration debug="false ...
- MySQL 启动、登录、退出和目录结构
一.启动 MySQL 服务器启动方式有两种: (1)通过服务的方式自动启动 (2)手动启动的方式 1.windows 服务方式启动 操作步骤: 也可以在 cmd 窗口 输入 services.msc ...
- Pandas-数据处理-基础部分
有趣的事,Python永远不会缺席! 如需转发,请注明出处:小婷儿的python https://www.cnblogs.com/xxtalhr/p/11014882.html jupyter 代码 ...
- SAP Marketing Cloud功能简述(三) 营销活动内容设计和产品推荐
Grace的前两篇文章: SAP Marketing Cloud功能简述(一) : Contacts和Profiles SAP Marketing Cloud功能简述(二) : Target Grou ...
- Ant环境安装
一:下载安装Ant,配置环境变量 1.进入http://ant.apache.org/bindownload.cgi下载ant 2.配置环境变量 新建ANT_HOME 配置path环境变量 配置cla ...