首页 技术 正文
技术 2022年11月9日
0 收藏 580 点赞 2,803 浏览 1264 个字

http://acm.hdu.edu.cn/showproblem.php?pid=1573

求小于等于N的正整数中有多少个X满足:

X mod a0 = b0

X mod a1 = b1

……

X mod ai = bi

(0<ai<=10)

输入:第一行为一个正整数T,表示有T组测试数据。每组测试数据的第一行为两个正整数N,M (0 < N <= 1000,000,000 , 0 < M <= 10),表示X小于等于N,数组a和b中各有M个元素。接下来两行,每行各有M个正整数,分别为a和b中的元素。

输出:对应每一组输入,在独立一行中输出一个正整数,表示满足条件的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

题解:同余方程组的裸题。尤其要注意的是所求的是正整数,也就是0的时候要特殊处理。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;typedef long long LL;
const LL M=;
LL a[M],b[M];LL exgcd(LL u,LL v,LL &x,LL &y)
{
if(v==){ x=;y=;return u;}
else
{
LL tx,ty;
LL d=exgcd(v,u%v,tx,ty);
x=ty;
y=tx-(u/v)*ty;
return d;
}
}int main()
{
// freopen("a.in","r",stdin);
// freopen("a.out","w",stdout);
LL T;
scanf("%I64d",&T);
while(T--)
{
LL n,m;
bool bk=;
scanf("%I64d%I64d",&n,&m);
for(LL i=;i<=m;i++) scanf("%I64d",&a[i]);
for(LL i=;i<=m;i++) scanf("%I64d",&b[i]);
LL A,B,C,g,x,tx,ty,a1,b1;
a1=a[],b1=b[];
for(LL i=;i<=m;i++)
{
A=a1;B=a[i];C=b[i]-b1;
g=exgcd(A,B,tx,ty);
if(C%g) {bk=;break;}
x=((tx*C/g)%(B/g)+(B/g))%(B/g);
b1=a1*x+b1;
a1=a1/g*a[i];
}
if(bk)
{
/*
得出不定方程a1x+b1=P后,x=0时b1=P。
因为x可以等于0:ans=(n-b1)/a1+1;
若P=0,则b1=x=0,不满足正整数,所以减去。
*/
LL ans=;
if(n>=b1) ans=(n-b1)/a1+;
if(ans && b1==) ans--;
printf("%I64d\n",ans);
}
else printf("0\n");
}
return ;
}
上一篇: js 异步请求封装
下一篇: mfc和win32区别
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,154
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,623
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,465
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,239
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,874
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,042