hdu X问题 (中国剩余定理不互质)
http://acm.hdu.edu.cn/showproblem.php?pid=1573
X问题
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4439 Accepted Submission(s): 1435
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<stdlib.h> using namespace std; const int M = ;
typedef long long ll;
int r;
ll n[M], b[M], N; void gcd(ll a, ll b, ll &x, ll &y)
{
if(b == )
{
x = ;
y = ;
r = a;
return ;
}
gcd(b, a % b, x, y);
ll t = x;
x = y;
y = t - a / b * y;
}
ll CRT2(ll b[], ll n[], int m)
{
int f = ;
ll n1 = n[], n2, b1 = b[], b2, c, t, k, x, y;
for(int i = ; i < m ; i++)
{
n2 = n[i];
b2 = b[i];
c = b2 - b1;
gcd(n1, n2, x, y);
if(c % r != )
{
f = ;
break;
}
k = c / r * x;
t = n2 / r;
k = (k % t + t) % t;
b1 = b1 + n1 * k;
n1 = n1 * t;
}
if(f)//无解
return ;
if(b1 == )
b1 = n1;
if(b1 > N)
return ;
return (N - b1) / n1 + ;
} int main()
{
int t, m;
scanf("%d", &t);
while(t--)
{
scanf("%lld%d", &N, &m);
for(int i = ; i < m ; i++)
scanf("%lld", &n[i]);
for(int i = ; i < m ; i++)
scanf("%lld", &b[i]);
printf("%lld\n", CRT2(b, n, m));
}
return ;
}
hdu X问题 (中国剩余定理不互质)的更多相关文章
- POJ 1006 Biorhythms --中国剩余定理(互质的)
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 103539 Accepted: 32012 Des ...
- HDU 5768 Lucky7 容斥原理+中国剩余定理(互质)
分析: 因为满足任意一组pi和ai,即可使一个“幸运数”被“污染”,我们可以想到通过容斥来处理这个问题.当我们选定了一系列pi和ai后,题意转化为求[x,y]中被7整除余0,且被这一系列pi除余ai的 ...
- X问题(中国剩余定理+不互质版应用)hdu1573
X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- Hello Kiki(中国剩余定理——不互质的情况)
Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- poj 2981 Strange Way to Express Integers (中国剩余定理不互质)
http://poj.org/problem?id=2891 Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 13 ...
- Strange Way to Express Integers(中国剩余定理+不互质)
Strange Way to Express Integers Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & ...
- 中国剩余定理模数互质的情况模板(poj1006
http://poj.org/problem?id=1006 #include <iostream> #include <cstdio> #include <queue& ...
- hdu 5768 Lucky7 中国剩余定理+容斥+快速乘
Lucky7 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem D ...
- HDU 5768 Lucky7 (中国剩余定理+容斥)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5768 给你n个同余方程组,然后给你l,r,问你l,r中有多少数%7=0且%ai != bi. 比较明显 ...
随机推荐
- MySQL转Oracle,MyBatis Mapper XML 文件修改项总结
1.对于批量插入 需要更改成 <insert id="saveAll"> insert into(a,b,c) <foreach collection=" ...
- javax.persistence.RollbackException: Error while committing the transaction
the valid jpa update entity code gives the exception below in the case of wrong dependency( org.hib ...
- (转 留存)Windows环境下的NodeJS+NPM+GIT+Bower安装配置步骤
Windows环境下的NodeJS+NPM+GIT+Bower安装配置步骤 标签: NodeJSnpmbower 2015-07-17 16:38 3016人阅读 评论(0) 收藏 举报 分类: G ...
- (4)activiti工作流引擎之uel表达式
有了前面几章,我们肯定有一定的困惑,activiti如何与实际业务整合,比如一条采购单,如何跟一个流程实例互相关联起来? 这里就需要使用到activiti启动流程实例时设置一个流程实例的busines ...
- tomcat安装出现问题及解决方法
1. tomcat安装: 安装目录-->D:\Program Files\apache-tomcat-7.0.59 2. tomcat环境变量配置: 3. D:\Program Files\ap ...
- Redis AOF文件
[Redis AOF文件] 1.关于AOF AOF 持久化记录服务器执行的所有写操作命令,并在服务器启动时,通过重新执行这些命令来还原数据集. AOF 文件中的命令全部以 Redis 协议的格式来保存 ...
- 克拉 & 24K
[克拉] 克拉(Ct)是宝石的质量(重量)单位,现定1克拉等于0.2克或200毫克.一克拉又分为100分,如50分即0.5克拉,以用作计算较为细小的宝石.因为宝石的密度基本上相同,因此越重的宝石体积越 ...
- python之time&datetime
[time] secs:统一值,无local.UTC之分. struct_time:有local.UTC之分. time.time():返回secs,secs为统一值,无local&utc之分 ...
- Oracle的服务端_默认_启动的服务
- 使用ES(elasticsearch) 搜索引擎
介绍 https://blog.csdn.net/andyzhaojianhui/article/details/75195296 创建语句 { "company":{ &quo ...