HDU-3579-Hello Kiki (利用拓展欧几里得求同余方程组)
设 ans 为满足前 n - 1个同余方程的解,lcm是前n - 1个同余方程模的最小公倍数,求前n个同余方程组的解的过程如下:
①设lcm * x + ans为前n个同余方程组的解,lcm * x + ans一定能满足前n - 1个同余方程;
②第 n 个同余方程可以转化为a[n] * y + b;
合并①②得:lcm * x + ans = a[n] * y + b; => lcm * x - a[n] * y = b - ans(可以用拓展欧几里得求解x和y)
但是拓展欧几里得要求取余的数是正数,我们可以转化上面的方程为lcm * x + a[n] * -y = b - ans(后面我们用x得到解,所以不关心y的正负)
解得一组x和y;
x += k * (a[n] / gcd);(k为任意整数)
我们可以求得最小非负数x,在带入①得到前n个同余方程的最小非负数解;
代码如下:
Accepted | 3579 | 15MS | 1368K | 1112 B | G++ |
#include "bits/stdc++.h"
using namespace std;
int a[], b[];
// 拓展欧几里得C++模板
int ex_gcd(int a, int b, int &x, int &y) {
if (b == ) {
x = ;
y = ;
return a;
}
int ans = ex_gcd(b, a % b, y, x);
y -= a / b * x;
return ans;
}
int solve(int n) {
// 任何数对1取余都是0,所以初始化ans = 0, lcm = 1;
int x, y, ans = , lcm = ;
for (int i = ; i < n; i++) {
int gcd = ex_gcd(lcm, a[i], x, y);
if ((b[i] - ans) % gcd) {
return -;
}
// 拓展欧几里得求得的x和y是对于gcd而言的。乘完之后才是对于 (b[i] - ans) 的 x
x *= (b[i] - ans) / gcd;
a[i] /= gcd;
// 使 x 成为最小非负数解
x = (x % a[i] + a[i]) % a[i];
// 更新ans
ans += x * lcm;
// 更新lcm
lcm *= a[i];
}
// 本题要求最小正整数解,如果ans是0,要加一个最小公倍数也就是lcm
return ans ? ans : lcm;
}
int main() {
int t, n, ca = ;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = ; i < n; i++) {
scanf("%d", &a[i]);
}
for (int i = ; i < n; i++) {
scanf("%d", &b[i]);
}
printf("Case %d: %d\n", ca++, solve(n));
}
return ;
}
HDU-3579-Hello Kiki (利用拓展欧几里得求同余方程组)的更多相关文章
- 【hdu3579-Hello Kiki】拓展欧几里得-同余方程组
http://acm.hdu.edu.cn/showproblem.php?pid=3579 题解:同余方程组的裸题.注意输出是最小的正整数,不包括0. #include<cstdio> ...
- 【poj2891-Strange Way to Express Integers】拓展欧几里得-同余方程组
http://poj.org/problem?id=2891 题意:与中国剩余定理不同,p%ai=bi,此处的ai(i=1 2 3 ……)是不一定互质的,所以要用到的是同余方程组,在网上看到有人称为拓 ...
- 【hdu1573-X问题】拓展欧几里得-同余方程组
http://acm.hdu.edu.cn/showproblem.php?pid=1573 求小于等于N的正整数中有多少个X满足: X mod a0 = b0 X mod a1 = b1 …… X ...
- hdu 1576 A/B(拓展欧几里得)
A/B Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- POJ 2891 Strange Way to Express Integers(拓展欧几里得)
Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...
- ZOJ Problem Set - 3593 拓展欧几里得 数学
ZOJ Problem Set - 3593 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3593 One Person ...
- BZOJ-1407 Savage 枚举+拓展欧几里得(+中国剩余定理??)
zky学长实力ACM赛制测试,和 大新闻(YveH) 和 华莱士(hjxcpg) 组队...2h 10T,开始 分工我搞A,大新闻B,华莱士C,于是开搞: 然而第一题巨鬼畜,想了40min发现似乎不可 ...
- 【lydsy1407】拓展欧几里得求解不定方程+同余方程
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1407 题意: 有n个野人,野人各自住在第c[i]个山洞中(山洞成环状),每年向前走p[i] ...
- NOIP2012拓展欧几里得
拉板题,,,不说话 我之前是不是说过数据结构很烦,,,我想收回,,,今天开始的数论还要恶心,一早上听得头都晕了 先来一发欧几里得拓展裸 #include <cstdio> void gcd ...
随机推荐
- sudo apt-get update数字签名错误解决方法
lzb@lzb:~/projects/curl-master$ sudo apt-get update 命中: http://mirrors.aliyun.com/ubuntu xenial InRe ...
- java实现图片文件与Base64的互转
通过form表单上传图片时,有时候web容器对文件大小的限制会影响我们上传.这时,前端页面可以考虑将图片转换成base64串来实现上传. 图片与Base64的互转,其实就是利用了文件流与Base64的 ...
- Python合成GIF图片 -- imageio库
pip install imageio import imageio # 需要合在一起的图片 image_list = [r'C:\Users\Hlzy\Desktop\\' + str(x) + & ...
- UVA 10269 Super Mario,最短路+动态规划
这个题目我昨晚看到的,没什么思路,因为马里奥有boot加速器,只要中间没有城堡,即可不耗时间和脚力,瞬间移动不超过L距离,遇见城堡就要停下来,当然不能该使用超过K次...我纠结了很久,最终觉得还是只能 ...
- ant design vue 时间选择器只能到最大日期
<a-date-picker :disabledDate="disabledEndDate" style="width: 100%" placehold ...
- SQL查询出一个表数据插入到另一个表里
下面两中方式都是将 srcTbl 的数据插入到 destTbl,但两句又有区别的: 方式一 (select into from)要求目标表(destTbl)不存在,因为在插入时会自动创建. selec ...
- cJSON api的使用教程
背景说明:由于和后台通信,为了统一数据格式,选择使用json格式,客户端开发语言使用的是c,故需要借助第三方库来实现json格式的转化,cJSON是一个很好的第三方库, 下载链接1:https://g ...
- dubbo使用Spring配置暴露服务和使用Spring配置引用远程服务
提供者: <!-- 1.指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名) --> <dubbo:application name="user-servic ...
- 吴裕雄--天生自然TensorFlow高层封装:使用TensorFlow-Slim处理MNIST数据集实现LeNet-5模型
# 1. 通过TensorFlow-Slim定义卷机神经网络 import numpy as np import tensorflow as tf import tensorflow.contrib. ...
- mybatis学习笔记四
记录下动态sql的常用标签: 1.where 一般用作数据操作添加的条件 例子: <select id="selectByRoleId" resultMap="re ...