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 的数 ...
随机推荐
- 修改UISearchBar背景色
//iOS7.1 [[[[_searchBar.subviews objectAtIndex:] subviews] objectAtIndex:] removeFromSuperview]; [_s ...
- A10 平板开发一硬件平台搭建
A10板子从原理图设计.接插件布局.PCB设计到物料采购以及贴片,最后调试,花了不少时间,刚刚把屏点亮了,系统总算跑起来了.整个过程遇到不少问题,包括与外面工程师沟通.硬件测试.软件调试,还有很多问题 ...
- Spring项目的建立-移植流程(非入门教程)
Creat by Zhou yong in 2016/4/15/19:00 jar包 java 1.7 tomcat的两个jar包 2个文件上传的jar包 json支持的jar包 hibernate- ...
- PHP学习笔记二十三【This】
<?php Class Person { function test1() { $this->test2();//类里面的方法互相调用要加$this } protected functio ...
- OD调试2---TraceMe
OD调试2---TraceMe 拆解一个Windows程序要比拆解一个DOS程序容易得多,因为在Windows中,只要API函数被使用,想对寻找蛛丝马迹的人隐藏一些东西是比较困难的.因此分析一个程序, ...
- (a*b)%c 问题
给你两个数__int64 类型的整数 a ,b,c.问你,(a*b)%c的值是多少??我们知道: (a*b)%c = ((a%c)*(b%c))%c .不过即使这样做,在c很大的情况下,(a%c)*( ...
- (原)Ubuntu16中安装cuda toolkit
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5655957.html 参考网址: https://devtalk.nvidia.com/default ...
- oracle中anyData数据类型的使用实例
---创建waterfallcreate or replace type waterfall is object(name varchar2(30),height number); --创建river ...
- 解决MYSQL弃用模块错误Deprecated: mysql_query(): The mysql extension is deprecated and will be removed in the future
今天使用了mysql 5.5版本,就出现了错误.错误提示如下: Deprecated: mysql_connect(): The mysql extension is deprecated and w ...
- 身份证校验程序(上)- 零基础入门学习Delphi48
身份证校验程序 让编程改变世界 Change the world by program [caption id="attachment_2699" align="alig ...