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. mysql 实时统计脚本 QPS,TPS和线程连接数等

    #!/bin/bash mysqladmin -uroot -p'root' extended-status -i1|awk 'BEGIN{local_switch=0;print "QPS ...

  2. [LeetCode] 298. Binary Tree Longest Consecutive Sequence_Medium tag: DFS recursive

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  3. [LeetCode] 345. Reverse Vowels of a String_Easy tag:Two Pointers

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1: In ...

  4. 一个新人对HTML内JavaScript的理解

    首先是对于JavaScript(以后简称JS)的定义: ① JS他是一个脚本语言,有点类似于外部插件,需要插入引用才会有效 ② 他需要一个宿主文件,就是他插入到谁里面进行运算,谁就是这个JS的宿主文件 ...

  5. APB总线

    APB(Advance Peripheral Bus)是AMBA总线的一部分,从1998年第一版至今共有3个版本. AMBA 2 APB Specfication:定义最基本的信号interface, ...

  6. 开发人员必备的几款bug管理工具

    Bug是软件开发过程中的“副产品”,也是开发人员最不想见到的状况.如果没有跟踪和梳理各种bug和问题并及时解决,项目就会花费非常多的时间,导致整个项目的重心偏移.如果在产品开发过程中,使用一个合适的B ...

  7. K好数

    有点坑 在他这里 0不算一位数 #include <iostream> #include <cstdio> #include <string.h> using na ...

  8. Instagram 在 PyCon 2017 的演讲摘要

    Instagram 在 PyCon 2017 的演讲摘要 PyCon 简介 PyCon 是全世界最大的以 Python 编程语言 为主题的技术大会.大会由 Python 社区组织,每年举办一次.在大会 ...

  9. Centos7下添加Tomcat为系统服务

    文章参考:点击打开链接 因为个人感觉在centos中启动tomcat比较麻烦.要一直cd到目录下面startup.sh才可以,所以网上找到将tomcat作为系统服务,使用systemctl直接启动方法 ...

  10. 如何在 Linux 中将文件编码转换为 UTF-8

    一个文件:vim 文件名.txt输入 :e ++enc=gbk 强制用gbk打开输入 :w ++enc=utf8 转换到utf8保存. 多个文件:for i in *.txt; do iconv -f ...