【题目描述】

求在小于等于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. HAWQ取代传统数仓实践(十三)——事实表技术之周期快照

    一.周期快照简介 周期快照事实表中的每行汇总了发生在某一标准周期,如一天.一周或一月的多个度量.其粒度是周期性的时间段,而不是单个事务.周期快照事实表通常包含许多数据的总计,因为任何与事实表时间范围一 ...

  2. poj 3517

    题目链接  http://poj.org/problem?id=3517 题意        约瑟夫环  要求最后删掉的那个人是谁: 方法        理解递推公式就行了  考虑这样一组数据  k ...

  3. 剑指offer-第七章面试案例1(字符串转换为整型)

    //将字符串转换为整型 //思路:特殊的输入测试: //1,考虑字符串是否为空.2.字符串问空的时候的返回0,和真实的返回0直键的区别.3,字符串中出现0~9的字符处理 //4.字符串中出现*,¥等一 ...

  4. return super(ParamValueInline,self).formfield_for_foreignkey(db_field,request,**kwargs)自己返回自己的父类

    作者:刘强胜链接:https://www.zhihu.com/question/30361435/answer/83940591来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  5. #51单片机#蓝牙模块(ATK-SPP-HC06从机串口)的使用方法

    #include <AT89X51.H> #include <intrins.h> // 函数原形定义 #define uchar unsigned char #define ...

  6. Delphi 连接 Paradox

    应用TDataBase控件把DataBase的DriveName设为STANDARD,然后设置DataBase的Params中设置PATH=*.db文件地点目录DEFAULT DRIVER=PARAD ...

  7. ajax()函数传值中文乱码解决方法介绍

    jquery的ajax()函数传值中文乱码解决方法介绍,需要的朋友可以参考下 复制代码 代码如下: $.ajax({ dataType : ‘json',type : ‘POST',url : ‘ht ...

  8. MySQL-5.7中InnoDB表数据文件存储位置

    学习地址:https://www.cnblogs.com/tongxiaoda/p/7874535.html

  9. vue参考

    https://github.com/taylorchen709/vue-admin http://element-cn.eleme.io/#/zh-CN/component/layout https ...

  10. MVC FileDownLoad

    public ActionResult MatDownload() { string ShopId = Session["ShopId"].ToString(); var self ...