hdu1573X问题(不互素的中国剩余定理)
X问题
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3295 Accepted Submission(s): 1068
接下来两行。每行各有M个正整数。分别为a和b中的元素。
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
1
0
3
求解x在n的范围内的数量。由于全部的ai不是互质的,所以不能直接用中国剩余定理,可是能够採用http://blog.csdn.net/winddreams/article/details/38425477
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL __int64
LL t , n , m , d , x , y , i , bb , aa , flag ;
void gcd(LL a,LL b)
{
if(b == 0)
{
d = a ; x = 1 ; y = 0 ;
}
else
{
gcd(b,a%b);
swap(x,y);
x = -x ; y = -y ;
y += (a/b)*x ;
}
return ;
}
LL a[30] , b[30] ; int main()
{
scanf("%I64d", &t);
while(t--)
{
scanf("%I64d %I64d", &n, &m);
for(i = 0 ; i < m ; i++)
scanf("%I64d", &a[i]);
for(i = 0 ; i < m ; i++)
scanf("%I64d", &b[i]);
aa = a[0] ;
bb = b[0] ;
flag = 1 ;
for(i = 1 ; i < m ; i++)
{
gcd(aa,a[i]);
if( (b[i]-bb)%d != 0 )
flag = 0 ;
if( flag )
{
x = (b[i]-bb)/d*x ;
y = a[i] / d ;
x = ( x%y + y )%y ;
bb = bb + x * aa ;
aa = aa*a[i]/d ;
}
}
gcd(1,aa);
if( bb%d != 0 )
flag = 0 ;
if( flag )
{
x = ( bb/d )*x ;
y = aa / d ;
x = (x % y + y) % y ;
}
if( flag == 0 || x > n )
printf("0\n");
else
{
if( !x )
printf("%I64d\n", (n-x)/y );
else
printf("%I64d\n", (n-x)/y+1 );
}
}
return 0;
}
hdu1573X问题(不互素的中国剩余定理)的更多相关文章
- hdu_1573 X问题(不互素的中国剩余定理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1573 X问题 Time Limit: 1000/1000 MS (Java/Others) Me ...
- hdu_1370Biorhythms(互素的中国剩余定理)
Biorhythms Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 数论F - Strange Way to Express Integers(不互素的的中国剩余定理)
F - Strange Way to Express Integers Time Limit:1000MS Memory Limit:131072KB 64bit IO Format: ...
- POJ 2891 中国剩余定理(不互素)
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 17877 ...
- hdu 3579 Hello Kiki【中国剩余定理】(模数不要求互素)(模板题)
<题目链接> 题目大意: 给你一些模数和余数,让你求出满足这些要求的最小的数的值. 解题分析: 中国剩余定理(模数不一定互质)模板题 #include<stdio.h> usi ...
- hdu1573-X问题-(扩展欧几里得定理+中国剩余定理)
X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- ACM/ICPC 之 中国剩余定理+容斥原理(HDU5768)
二进制枚举+容斥原理+中国剩余定理 #include<iostream> #include<cstring> #include<cstdio> #include&l ...
- 中国剩余定理(Chinese Remainder Theorem)
我理解的中国剩余定理的含义是:给定一个数除以一系列互素的数${p_1}, \cdots ,{p_n}$的余数,那么这个数除以这组素数之积($N = {p_1} \times \cdots \tim ...
- 【中国剩余定理】【容斥原理】【快速乘法】【数论】HDU 5768 Lucky7
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 题目大意: T组数据,求L~R中满足:1.是7的倍数,2.对n个素数有 %pi!=ai 的数 ...
随机推荐
- Go语言程序的状态监控
Go是很实在的编程语言,从一开始就提供了很详细的运行状态信息.产品上线后的调优和排查疑难杂症都得靠这些状态信息.这边总结一些我们项目里用到的状态监控手段. pprof Go自带了一个pprof工具,这 ...
- 设计一个算法,求非空二叉树中指定的第k层(k>1)的叶子节点的个数
思想:採用基于层序遍历的方法. 用level扫描各层节点,若某一层的节点出队后.rear指向该层中最右节点.则将rear赋值给last(对于第一层.last=1).在出队时,若front=last,表 ...
- htm跨域锚点定位
a.html <div class="title" id="aaaa">保险服务</div> b.html <div class= ...
- 手机版和PC版识别
1.C#通过User-Agent 处理 //判断 是否来自手机终端 public bool checkMoble() { string userAgent = Request.Headers[&qu ...
- Visual Studio 2013 Web开发、新增功能:“Browser Link”
微软正式发布Visual Studio 2013 RTM版,微软还发布了Visual Studio 2013的最终版本..NET 4.5.1以及Team Foundation Server 2013. ...
- C# 执行存储过程
SqlParameter[] paras = BuildParas(id, time, name); bool bRet = ExcuteProcedure(“数据库连接字符串”, "存储过 ...
- QQ中未读气泡拖拽消失的实现(参照一位年轻牛B的博主的思路自己实现了一下)
原文链接:http://kittenyang.com/drawablebubble/,博主年轻却很有思想.相仿的年纪,很佩服他! 首先分析拖拽时的图,大圆.不规则的图(实际上时有规律的不然也画不出来, ...
- automaticallyAdjustsScrollViewInsets的作用
简单点说就是automaticallyAdjustsScrollViewInsets根据按所在界面的status bar,navigationbar,与tabbar的高度,自动调整scrollview ...
- UIView的一些常用属性和方法
UIView的一些常用属性和方法 1. UIView的属性 UIView继承自UIResponder,拥有touches方法. - (instancetype)initWithFrame:(CGRec ...
- (原)ubuntu16中安装moses
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5653186.html 在ubuntu14中,可以使用下面的语句安装moses: luarocks in ...