【题目描述】

求在小于等于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)。
 
【题解】
模板题,为读者提供一个中国剩余定理(非互质版)的模板
 
 /*************
HDU 1573
by chty
2016.11.2
*************/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
using namespace std;
int T,n,s,M,ans,flag,a[],m[];
inline int read()
{
int x=,f=; char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-; ch=getchar();}
while(isdigit(ch)) {x=x*+ch-''; ch=getchar();}
return x*f;
}
void exgcd(int a,int b,int &g,int &x,int &y)
{
if(b==) {x=; y=; g=a; return;}
exgcd(b,a%b,g,x,y);
int t=x;x=y;y=t-a/b*y;
}
void China()
{
int A=a[],k,y; M=m[];
for(int i=;i<=n;i++)
{
int da=a[i]-A,g;
exgcd(M,m[i],g,k,y);
if(da%g) {flag=; return;}
int t=m[i]/g;
k*=da/g;
k=(k+t)%t;
A+=k*M;
M=M*m[i]/g;
A=(A+M)%M;
}
ans=A;
}
int main()
{
freopen("cin.in","r",stdin);
freopen("cout.out","w",stdout);
T=read();
while(T--)
{
s=read(); n=read(); flag=;
for(int i=;i<=n;i++) m[i]=read();
for(int i=;i<=n;i++) a[i]=read();
China();
if(ans>s||flag) {printf("0\n"); continue;}
int sum=(s-ans)/M+;
if(ans==) sum--;
printf("%d\n",sum);
}
return ;
}

【HDU1573】X问题的更多相关文章

  1. hdu1573:数论,线性同余方程组

    题目大意: 给定一个N ,m 找到小于N的  对于i=1....m,满足  x mod ai=bi  的 x 的数量. 分析 先求出 同余方程组 的最小解x0,然后 每增加lcm(a1...,am)都 ...

  2. hdu-1573 Robot Motion

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10219   Accepted: 4977 Des ...

  3. X问题(中国剩余定理+不互质版应用)hdu1573

    X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  4. hdu1573 X问题【中国剩余定理】

    <题目链接> X问题 Problem Description 求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X mod a[1] = b[1], X mod ...

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

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

  6. HDU1573 线性同余方程(解的个数)

    X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

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

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

  8. HDU1573 X问题【一元线性同余方程组】

    题目链接: http://acm.hdu.edu.cn/showproblem.php? pid=1573 题目大意: 求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X ...

  9. HDU Math Problems

    1576 const int mod = 9973; n = a - a / mod * mod; a / b = ans; ans * b = a = a / mod * mod + n; n = ...

随机推荐

  1. 深入理解UE4宏定义—— GENERATED_BODY

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/72834164 作者:car ...

  2. 深度学习(六十二)SqueezeNet网络设计思想笔记

  3. win7C盘不够用怎么办

    Windows 7 是由微软公司(Microsoft)开发的操作系统,核心版本号为Windows NT 6.1.Windows 7 可供家庭及商业工作环境.笔记本电脑.平板电脑.多媒体中心等使用. 工 ...

  4. nmap 扫描工具

    Nmap 7.30 ( https://nmap.org ) 使用方法: nmap [扫描类型(s)] [选项] {目标说明}目标说明:通过主机名称, IP 地址, 网段, 等等.协议: scanme ...

  5. EMQ (Erlang/Enterprise/Elastic MQTT Broker)

    EMQ (Erlang/Enterprise/Elastic MQTT Broker) https://www.cnblogs.com/SteveLee/p/9843215.html MQ介绍 EMQ ...

  6. Redis设计与实现 (三): 字典

     哈希表 结构定义dict.h/dictht /* * 哈希表 * * 每个字典都使用两个哈希表,从而实现渐进式 rehash . */ typedef struct dictht { // 哈希表数 ...

  7. 实现一个scnprinf

    #include <stdio.h> #include <stdarg.h> /* 该函数ret = min(size, 实际写入长度) - 1,即ret永远小于size * ...

  8. 迭代器.NET实现—IEnumerable和IEnumerator (foreach实现)

    能用foreach遍历访问的对象需要实现什么接口或声明什么方法的类型? 答案:能用foreach遍历访问的对象必须是集合或数组对象,而这些都是靠实现超级接口IEnumerable或被声明 GetEnu ...

  9. 学大伟业DAY2模拟赛

    T1忍者钩爪 题目描述 小Q是一名酷爱钩爪的忍者,最喜欢飞檐走壁的感觉,有一天小Q发现一个练习使用钩爪的好地方,决定在这里大显身手. 场景的天花板可以被描述为一个无穷长的数轴,初始小Q挂在原点上.数轴 ...

  10. 洛谷 P3225 [HNOI2012]矿场搭建

    传送门 题目大意:建设几个出口,使得图上无论哪个点被破坏,都可以与出口联通. 题解:tarjian求割点 首先出口不能建在割点上,找出割点,图就被分成了几个联通块. 每个联通块,建出口.如果割点数为0 ...