只是套模板而已(模板其实也不懂)。

留着以后好好学的时候再改吧。

题意——

X = a[i] MOD b[i];

已知a[i],b[i],求在[1, n]中存在多少x满足条件。

输入——

第一行一个整数t,表示一共t组数据。

第二行两个整数n,m,表示在n以内寻找满足的数,一共m组方程组。

输出——

一个整数。如果存在满足的x,则输出x的数量。否则输出0。

直接给代码吧——

 #include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#define LL long long
using namespace std; const int N = ; int a[N], b[N];
int m;
int n, m1, r1, x, y, d;
int m2, r2, c, t, mod;
bool flag; void exgcd(int a, int b, int &x, int &y, int &d)
{
if(!b)
{
d = a;
x = ;
y = ;
}
else
{
exgcd(b, a%b, y, x, d);
y -= x*(a/b);
}
} void crt()
{
m1 = a[], r1 = b[];
flag = ;
for(int i = ; i < m; i++)
{
m2 = a[i];
r2 = b[i];
if(flag) continue;
exgcd(m1, m2, x, y, d);
c = r2-r1;
if(c%d)
{
flag = ;
continue;
}
mod = m2/d;
x = (c/d*x%mod+mod)%mod;
r1 = m1*x+r1;
m1 = m1*m2/d;
}
if(flag || n < r1) printf("0\n");
else
{
int ans = (n-r1)/m1+;
if(r1 == ) ans--;
printf("%d\n", ans);
}
} int main()
{
//freopen("test.txt", "r", stdin);
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &n, &m);
for(int i = ; i < m; i++) scanf("%d", &a[i]);
for(int i = ; i < m; i++) scanf("%d", &b[i]);
crt();
}
return ;
}

hdu 1573 x问题(中国剩余定理)HDU 2007-1 Programming Contest的更多相关文章

  1. 中国剩余定理 hdu 1573 X问题

    HDU 1573 X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  2. hdu X问题 (中国剩余定理不互质)

    http://acm.hdu.edu.cn/showproblem.php?pid=1573 X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory ...

  3. HDU 5768 Lucky7 (中国剩余定理 + 容斥 + 快速乘法)

    Lucky7 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 Description When ?? was born, seven crow ...

  4. 中国剩余定理 hdu 3579

    HDU 3579 Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  5. HDU 5768 Lucky7 容斥原理+中国剩余定理(互质)

    分析: 因为满足任意一组pi和ai,即可使一个“幸运数”被“污染”,我们可以想到通过容斥来处理这个问题.当我们选定了一系列pi和ai后,题意转化为求[x,y]中被7整除余0,且被这一系列pi除余ai的 ...

  6. hdu 5446 Unknown Treasure 中国剩余定理+lucas

    题目链接 求C(n, m)%p的值, n, m<=1e18, p = p1*p2*...pk. pi是质数. 先求出C(n, m)%pi的值, 然后这就是一个同余的式子. 用中国剩余定理求解. ...

  7. HDU 3579 Hello Kiki 中国剩余定理(合并方程

    题意: 给定方程 res % 14 = 5 res % 57 = 56 求res 中国剩余定理裸题 #include<stdio.h> #include<string.h> # ...

  8. hdu 3579 Hello Kiki (中国剩余定理)

    Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. POJ 2891 Strange Way to Express Integers 中国剩余定理解法

    一种不断迭代,求新的求余方程的方法运用中国剩余定理. 总的来说,假设对方程操作.和这个定理的数学思想运用的不多的话.是非常困难的. 參照了这个博客的程序写的: http://scturtle.is-p ...

  10. hdu 1573 X问题 (非互质的中国剩余定理)

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

随机推荐

  1. DOM对象和JQuery对象的区别

    DOM对象和JQuery对象的区别 jQuery对象和DOM对象使用说明,需要的朋友可以参考下.1.jQuery对象和DOM对象第一次学习jQuery,经常分辨不清哪些是jQuery对象,哪些是 DO ...

  2. Lua 的数据结构

    1. Arrays: 注意 #(data), # 加上 table名字 == size of data = {}; , do --行 , do --列 data[(y-)*+x] = (y-)*+x; ...

  3. POJ 1663

    #include<iostream>//cheng da cai zi using namespace std; int main() { int time; cin>>tim ...

  4. iOS开发中@selector的理解

    @selector 是什么? 1一种类型 SEL2代表你要发送的消息(方法), 跟字符串有点像, 也可以互转.: NSSelectorFromString() / NSSelectorFromStri ...

  5. Error building Player: Win32Exception: ApplicationName=‘xxxxxxxxxxxxxxxxxx//sdk\tools\zipalign.exe' , CommandLine='4 的解决办法

    更新了安卓SDK后,有时候Unity编译失失败,报错类似 Error building Player: Win32Exception: ApplicationName='D:/Program File ...

  6. s.charAt()

    public class ish{public static void main(String[]args){ String s="call me ishmae";System.o ...

  7. Android:文件夹显示红色叹号

    有感叹号,说明有的文件损坏或丢失了 解决方法: 右击工程,Build Path..->Configure Build Path...->Java Build Path 可以看到引用的jar ...

  8. [cocoapods]如何卸载cocoapods

    今天我们来讲一下cocoapods的删除步骤! 1.移除pod组件,打开终端执行which pod 然后输出了路径,我的是 /usr/local/bin/pod 2. 移除Cocoapods组件,继续 ...

  9. SELinux开启与关闭

    SELinux是「Security-Enhanced Linux」的简称,是美国国家安全局「NSA=The National Security Agency」 和SCC(Secure Computin ...

  10. 转Java 回调函数的理解

    所谓回调,就是客户程序C调用服务程序S中的某个函数A,然后S又在某个时候反过来调用C中的某个函数B,对于C来说,这个B便叫做回调函数.例如Win32下的窗口过程函数就是一个典型的回调函数.一般说来,C ...