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 的数 ...
随机推荐
- Handsontable通用方法
1.clear():清空数据 2.createCol(index,amount,createAutomatically):添加列 index:列索引,amount:添加的列总数,crea ...
- Duanxx的STM32学习:STM32命名规则
- 【RequireJS--API学习笔记】
原文:http://blog.csdn.net/pigpigpig4587/article/details/23427573 目录 RequireJS 加载javascript文件 定义模块 简单的值 ...
- Html5+css3实现3D转动效果
由于最近一直在忙着筹划去上海工作的事情,所以博客更新的速度也就慢了下来.前几天面试了几家公司,也拿到几份offer,总结了一些面试中遇到的问题,最近整理一下会和大家一起分享.今天呢,就和大家分享一下前 ...
- ScriptManager局部刷新
ScriptManager和UpdatePanel控件联合使用可以实现页面异步局部更新的效果.其中的UpdatePanel就是设置页面中异步局部更新区域,它必须依赖于ScriptManager存在,因 ...
- C#鼠标键盘钩子
using System;using System.Collections.Generic; using System.Reflection; using System.Runtime.Interop ...
- Java访问kafka的时候java.nio.channels.ClosedChannelException解决办法
import java.util.Properties; import kafka.javaapi.producer.Producer; import kafka.producer.KeyedMess ...
- 1218.2——property关键字
由于我们定义的成员变量是私有的,所有外部是无法访问的,为了能够访问我们写set和get方法 但是由于代码量过于复杂冗长,引入了@property (声明)和 @synthesize(实现) 后来又简化 ...
- java静态成员的初始化过程
public class Price{ final static Price INSTANCE = new Price(2.8); static double initPrice = 20; doub ...
- HttpServletResponse HttpServletRequest RequestDispatcher
HttpServletResponse HttpServletRequest RequestDispatcher 07. 五 / J2EE / 没有评论 一.以字节为单位向客户端发送中文数据1.服 ...