题目链接:

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)。

思路:

先求出数组b[]中全部数的最小公倍数lcm,再求解出该一元线性同余方程组在lcm范围内的解为a。题目要

求解x是小于等于N的正整数,则可列不等式:a + lcm * x <= N。

那么,假设a = 0,则答案为x-1。假设

a != 0,则答案为x。

AC代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std; __int64 GCD(__int64 a,__int64 b)
{
if(b == 0)
return a;
else
return GCD(b,a%b);
} void ExGCD(__int64 a,__int64 b,__int64 &d,__int64 &x,__int64 &y)
{
if( !b )
{
x = 1;
y = 0;
d = a;
}
else
{
ExGCD(b,a%b,d,y,x);
y -= x * (a/b);
}
} __int64 A[15],B[15]; int main()
{
int T,N,M;
__int64 a,b,c,d,x0,y0,lcm;
cin >> T;
while(T--)
{
cin >> N >> M;
bool flag = 1;
lcm = 1; for(int i = 1; i <= M; ++i)
{
cin >> A[i];
lcm = lcm / GCD(lcm,A[i]) * A[i];
} for(int i = 1; i <= M; ++i)
cin >> B[i]; for(int i = 2; i <= M; ++i)
{
a = A[1];
b = A[i];
c = B[i] - B[1];
ExGCD(a,b,d,x0,y0);
if(c % d != 0)
{
flag = 0;
break;
}
__int64 temp = b / d;
x0 = (x0*(c/d)%temp + temp) % temp;
B[1] = A[1] * x0 + B[1];
A[1] = A[1] * (A[i]/d);
}
if( !flag )
{
cout << "0" << endl;
continue;
}
__int64 Ans = 0;
if(B[1] <= N)
Ans = 1 + (N - B[1])/lcm;
if(Ans && B[1] == 0)
Ans--;
cout << Ans << endl;
} return 0;
}

HDU1573 X问题【一元线性同余方程组】的更多相关文章

  1. HDU1573:X问题(解一元线性同余方程组)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1573 题目解析;HDU就是坑,就是因为n,m定义成了__int64就WAY,改成int就A了,无语. 这题 ...

  2. HDU3579:Hello Kiki(解一元线性同余方程组)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3579 题目解析:求一元线性同余方程组的最小解X,需要注意的是如果X等于0,需要加上方程组通解的整数区间lc ...

  3. POJ2891:Strange Way to Express Integers(解一元线性同余方程组)

    写一下自己的理解,下面附上转载的:若a==b(modk);//这里的==指的是同余,我用=表示相等(a%k=b)a-b=kt(t为整数)以前理解的错误思想:以前认为上面的形式+(a-tb=k)也是成立 ...

  4. 【POJ 2891】Strange Way to Express Integers(一元线性同余方程组求解)

    Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...

  5. POJ 1061 - 青蛙的约会 - [exgcd求解一元线性同余方程]

    先上干货: 定理1: 如果d = gcd(a,b),则必能找到正的或负的整数k和l,使ax + by = d. (参考exgcd:http://www.cnblogs.com/dilthey/p/68 ...

  6. POJ2115:C Looooops(一元线性同余方程)

    题目: http://poj.org/problem?id=2115 要求: 会求最优解,会求这d个解,即(x+(i-1)*b/d)modm;(看最后那个博客的链接地址) 前两天用二元一次线性方程解过 ...

  7. 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  ...

  8. poj3708(公式化简+大数进制装换+线性同余方程组)

    刚看到这个题目,有点被吓到,毕竟自己这么弱. 分析了很久,然后发现m,k都可以唯一的用d进制表示.也就是用一个ai,和很多个bi唯一构成. 这点就是解题的关键了. 之后可以发现每次调用函数f(x),相 ...

  9. hdu1573(线性同余方程组)

    套模板,因为要是正整数,所以处理一下x=0的情况. X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

随机推荐

  1. java定时器和实时查询数据库

    定时器: Timer timer = new Timer();                    timer.schedule(new TimerTask() {                  ...

  2. 检查阿里云ssl证书到期情况

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019-06-10 16:00 # @Author : Anthony.long # ...

  3. Ubuntu 系统的常用快捷键

    Ubuntu操作基本快捷键 ibus-setup :设置系统输入法 scp filename username@serverIp:/home/xxx/xxx/filename   回车输入该usern ...

  4. vue-clickoutside d

    js文件 export default { bind(el, binding, vnode) { function documentHandler(e) { if (el.contains(e.tar ...

  5. 【译】x86程序员手册15-5.2页转换

    5.2 Page Translation 页转换 In the second phase of address transformation, the 80386 transforms a linea ...

  6. VHDL_ADC之cic_diffcell

    library IEEE; use ieee.std_logic_1164.all; use ieee.numeric_std.all; library edclib; use edclib.pkg_ ...

  7. Python开发工具搭建-Pycharm

    PyCharm2017. 3.X专业版 安装使用. 注册码激活 本文以 Windows系统 为例: 1.开发工具获取及下载 Anaconda(Python 的集成工具 ) 下载地址: https:// ...

  8. 05--QT常用的类

    http://blog.csdn.net/HMSIWTV/article/category/1128561/2 Qt常用类(1)—— 开端       使用Qt进行编程必须对 Qt 中常用的类有一定的 ...

  9. Centos6.4 安装fail2ban防暴力破解

    Centos6.4 安装fail2ban防暴力破解 一. 安装 curl -O https://codeload.github.com/fail2ban/fail2ban/tar.gz/0.9.0 m ...

  10. Map 键值对 set get delete