HDU3579 线性同余方程(模板 余数不一定互质)
Hello Kiki |
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
Total Submission(s): 258 Accepted Submission(s): 111 |
Problem Description
One day I was shopping in the supermarket. There was a cashier counting coins seriously when a little kid running and singing \\\\\\\"门前大桥下游过一群鸭,快来快来 数一数,二四六七八\\\\\\\". And then the cashier put the counted coins back morosely and count again...
Hello Kiki is such a lovely girl that she loves doing counting in a different way. For example, when she is counting X coins, she count them N times. Each time she divide the coins into several same sized groups and write down the group size Mi and the number of the remaining coins Ai on her note. One day Kiki\\\\\\\'s father found her note and he wanted to know how much coins Kiki was counting. |
Input
The first line is T indicating the number of test cases.
Each case contains N on the first line, Mi(1 <= i <= N) on the second line, and corresponding Ai(1 <= i <= N) on the third line. All numbers in the input and output are integers. 1 <= T <= 100, 1 <= N <= 6, 1 <= Mi <= 50, 0 <= Ai < Mi |
Output
For each case output the least positive integer X which Kiki was counting in the sample output format. If there is no solution then output -1.
|
Sample Input
2 |
Sample Output
Case 1: 341 |
Author
digiter (Special Thanks echo)
|
Source
2010 ACM-ICPC Multi-University Training Contest(14)——Host by BJTU
|
题意:
求同于方程。上模板。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=;
const int inf=0x7fffffff;
typedef long long ll;
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y)//扩展欧几里得
{
if(!b) {d=a;x=;y=;}
else{
ex_gcd(b,a%b,d,y,x);
y-=x*(a/b);
}
}
ll ex_crt(ll *m,ll *r,int n)
{
ll M=m[],R=r[],x,y,d;
for(int i=;i<=n;i++){
ex_gcd(M,m[i],d,x,y);
if((r[i]-R)%d) return -;
x=(r[i]-R)/d*x%(m[i]/d);
R+=x*M;
M=M/d*m[i];
R%=M;
}
return R>?R:R+M;
}
int main()
{
int t,n;
scanf("%d",&t);
for(int cas=;cas<=t;cas++){
scanf("%d",&n);
ll m[maxn],r[maxn];//m除数,r余数
for(int i=;i<=n;i++) scanf("%lld",&m[i]);
for(int i=;i<=n;i++) scanf("%lld",&r[i]);
printf("Case %d: %I64d\n",cas,ex_crt(m,r,n));
}
return ;
}
HDU3579 线性同余方程(模板 余数不一定互质)的更多相关文章
- hdu3579(线性同余方程组)
Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 线性同余方程模板( A+C*x=B(mod D) )
void extendgcd(long long a,long long b,long long &d,long long &x,long long &y) { ){d=a;x ...
- AcWing 204. 表达整数的奇怪方式 (线性同余方程组)打卡
给定2n个整数a1,a2,…,ana1,a2,…,an和m1,m2,…,mnm1,m2,…,mn,求一个最小的整数x,满足∀i∈[1,n],x≡mi(mod ai)∀i∈[1,n],x≡mi(mod ...
- HDU3579:Hello Kiki(解一元线性同余方程组)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3579 题目解析:求一元线性同余方程组的最小解X,需要注意的是如果X等于0,需要加上方程组通解的整数区间lc ...
- 数论之同余性质 线性同余方程&拔山盖世BSGS&中国剩余定理
先记录一下一些概念和定理 同余:给定整数a,b,c,若用c不停的去除a和b最终所得余数一样,则称a和b对模c同余,记做a≡b (mod c),同余满足自反性,对称性,传递性 定理1: 若a≡b (mo ...
- 高次同余方程模板BabyStep-GiantStep
/************************************* ---高次同余方程模板BabyStep-GiantStep--- 输入:对于方程A^x=B(mod C),调用BabySt ...
- poj2115-C Looooops -线性同余方程
线性同余方程的模板题.和青蛙的约会一样. #include <cstdio> #include <cstring> #define LL long long using nam ...
- 扩展欧几里得,解线性同余方程 逆元 poj1845
定理:对于任意整数a,b存在一堆整数x,y,满足ax+by=gcd(a,b) int exgcd(int a,int b,int &x,int &y){ ){x=,y=;return ...
- poj3708(公式化简+大数进制装换+线性同余方程组)
刚看到这个题目,有点被吓到,毕竟自己这么弱. 分析了很久,然后发现m,k都可以唯一的用d进制表示.也就是用一个ai,和很多个bi唯一构成. 这点就是解题的关键了. 之后可以发现每次调用函数f(x),相 ...
随机推荐
- 数据库Mysql的学习(二)-数据类型和创建
数据类型:数据列,存储过程参数,表达式和局部变量的数据特征. 整形: tinyint:一个字节,-128到127:2的7次方 smallint:两个字节,-32768到32767:2的15次方 med ...
- OpenMPI源码剖析1:MPI_Init初探
OpenMPI的底层实现: 我们知道,OpenMPI应用起来还是比较简单的,但是如果让我自己来实现一个MPI的并行计算,你会怎么设计呢?————这就涉及到比较底层的东西了. 回想起我们最简单的代码,通 ...
- [leetcode-718-Maximum Length of Repeated Subarray]
Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...
- js经典试题之原型与继承
js经典试题之原型与继承 1:以下代码中hasOwnProperty的作用是? var obj={} …….. obj.hasOwnProperty("val") 答案:判断obj ...
- YaoLingJump开发者日志(九)V1.1.1版本完成
跳跃吧瑶玲下载连接 百度网盘下载 介绍 Android版本升级到8.0,发现原来的图标不显示了: 百度了一下解决方案,用了该博客提供的第二种方法,修改roundIcon的内容. 可爱的图标 ...
- node中的path.resolve
path.resolve([arg1,arg2,...])根据参数的不同,返回值存在两种情况. 以下为参数的两种情况: 1.每个参数都不带'/',比如path.resolve(),或者path.res ...
- C# Find()和First()与FirstOrDefault(
1. Find方法只能在List<T>上使用,而后者能更广泛应用在IEnemerable<T>上. Find最终是建立在Array的查找之上,而在IEnemerable上的Fi ...
- tcp传送报文
707 void tcp_init_xmit_timers(struct sock *sk)708 {709 inet_csk_init_xmit_timers(sk, &tcp_wr ...
- spring cloud 之 客户端负载均衡 Ribbon
一.负载均衡 负载均衡(Load Balance): 建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽.增加吞吐量.加强网络数据处理能力.提高网络的灵活性和可用性.其意 ...
- JAVA IDE IntelliJ IDEA使用简介(二)—之基本操作
一.在编辑器中打开文件 1.可以使用下面的几种方式打开project内的文件进行编辑 (·)在project窗口中双击需要编辑的文件. (·)在project窗口选择需要编辑的文件,按F4 ( ...