X问题

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Problem Description
求在小于等于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)。
 
Input
输入数据的第一行为一个正整数T,表示有T组测试数据。每组测试数据的第一行为两个正整数N,M (0 < N <= 1000,000,000 , 0 < M <= 10),表示X小于等于N,数组a和b中各有M个元素。接下来两行,每行各有M个正整数,分别为a和b中的元素。
 
Output
对应每一组输入,在独立一行中输出一个正整数,表示满足条件的X的个数。
 
Sample Input
3
10 3
1 2 3
0 1 2
100 7
3 4 5 6 7 8 9
1 2 3 4 5 6 7
10000 10
1 2 3 4 5 6 7 8 9 10
0 1 2 3 4 5 6 7 8 9
 
Sample Output
1
0
3
 
Author
lwg
思路:不互质的中国剩余定理(和题目中的a,b数组反了,不要在乎这些细节)
    x≡ a1(mod b1)
    x≡ a2(mod b2)
    a1+b1*m1=a2+b2*m2扩展欧几里德搞下
代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define mod 1000000007
#define inf 999999999
//#pragma comment(linker, "/STACK:102400000,102400000")
int scan()
{
int res = , ch ;
while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
{
if( ch == EOF ) return << ;
}
res = ch - '' ;
while( ( ch = getchar() ) >= '' && ch <= '' )
res = res * + ( ch - '' ) ;
return res ;
}
int a[];
int b[];
int gcd(int x,int y)
{
if(x%y==)
return y;
else
return gcd(y,x%y);
}
void exgcd(int a, int b, int &x, int &y)
{
if(b == )
{
x = ;
y = ;
return;
}
exgcd(b, a % b, x, y);
int tmp = x;
x = y;
y = tmp - (a / b) * y;
}
int main()
{
int x,y,z,i,t;
scanf("%d",&x);
while(x--)
{
scanf("%d%d",&y,&z);
for(i=;i<z;i++)
scanf("%d",&b[i]);
for(i=;i<z;i++)
scanf("%d",&a[i]);
int a1=a[],b1=b[];
int jie=;
for(i=;i<z;i++)
{
int a2=a[i],b2=b[i];
int xx,yy;
int gys=gcd(b1,b2);
if((a2-a1)%gys)
{
jie=;
break;
}
exgcd(b1,b2,xx,yy);
xx=(xx*(a2-a1))/gys;
int gbs=b1*b2/gys;
a1=(((xx*b1+a1)%gbs)+gbs)%gbs;
b1=gbs;
}
if(!jie||y<a1)
printf("0\n");
else
printf("%d\n",(y-a1)/b1+-((a1==)?:));
}
return ;
}

hdu 1573 X问题 两两可能不互质的中国剩余定理的更多相关文章

  1. hdu 1573 X问题 不互质的中国剩余定理

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

  2. hdu 3579 Hello Kiki 不互质的中国剩余定理

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

  3. HDU 1573 X问题 中国剩余定理

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

  4. 牛客小白月赛9H论如何出一道水题(两个连续自然数互质)

    题面 记录一下...连续得两个自然数互质,这题再特判一下1的情况 #include<bits/stdc++.h> using namespace std; int main() { lon ...

  5. hdu 1370 Biorthythms 中国剩余定理

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

  6. HDU 5446 Unknown Treasure Lucas+中国剩余定理

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 Unknown Treasure 问题描述 On the way to the next se ...

  7. hdu 2891 中国剩余定理

    从6点看到10点,硬是没算出来,早知道玩游戏去了,艹,明天继续看 不爽,起来再看,终于算是弄懂了,以后超过一个小时的题不会再看了,不是题目看不懂,是水平不够 #include<cstdio> ...

  8. hdu 5446 Unknown Treasure Lucas定理+中国剩余定理

    Unknown Treasure Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  9. hdu 5446 Unknown Treasure 卢卡斯+中国剩余定理

    Unknown Treasure Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

随机推荐

  1. maven工程插件配置

    <build> <!-- 该级工程会加载插件,放在父工程里 --> <plugins> <!-- 资源文件拷贝插件 --> <plugin> ...

  2. AWS邮件通知服务:实时监控邮件状态

    以下为译文: 订阅“AWS中文技术社区”微信公众号,实时掌握AWS技术及产品消息! AWS中文技术社区为广大开发者提供了一个Amazon Web Service技术交流平台,推送AWS最新资讯.技术视 ...

  3. 机器学习理论基础学习9--- EM 算法

    EM算法的适用场景: EM算法用于估计含有隐变量的概率模型参数的极大似然估计,或者极大后验概率估计. 当概率模型既含有观测值,又含有隐变量或潜在变量时,就可以使用EM算法来求解概率模型的参数. 当概率 ...

  4. js数组之有已有数组创建新的数组

    concat()和splice()方法允许通过已经有的数组创建新的数组 concat()这个方法可以合并多个数组创建一个数组 splice()这个方法是获得截取一个数组中的子集创建一个新的数组. 理论 ...

  5. LeetCode-EvaluteReversePolishNotation

    题目: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are + ...

  6. Python2 简明教程

    Python 由 Guido Van Rossum 在90年代初创建. 它现在是最流行的语言之一 我喜爱python是因为它有极为清晰的语法,甚至可以说,它就是可以执行的伪代码. 注意: 这篇文章针对 ...

  7. 阿里云服务器ECS web环境配置(LNAP)ubantu

    Ubuntu 系统中,可以使用 apt-get 命令来搭建 LNMP环境.这种方式较编译方式安装更加简便 安装Nginx 1.使用 sudo apt-get install nginx 就能自动安装 ...

  8. office2007-安装程序找不到office.zh-cn\*.文件

    在同时安装vs2008 和 office2007 有可能会遇到个问题(如果现在安装office2007,在安装vs2008 不会出现问题, 反正会出现下面问题) 在安装office2007时总是提示“ ...

  9. vue v-for 和 v-if 、v-else一起使用造成的bug

    现象:导致v-else 执行v-for的length次数, 从现象看应该v-for先解析,然后将v-if和v-else包在其中 解决方案:很简单,tempalte 将v-if v-else 隔离到最外 ...

  10. Java lambda 表达式

    其实是试验一下markdown... 原: 只有一个抽象方法的接口称为函数式接口(functional interface). 当需要实现了这种接口的类的对象的时候,就可以提供一个lambda表达式. ...