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),相 ...
随机推荐
- 查看python中包的文档
核心命令:python -m pydoc 查询某包:python -m pydoc 包名 示例: C:\Users\xxx>python -m pydoc pydoc - the Python ...
- Python 再谈字符串
字符串除了要用引号来创建之外,其他和元组一样,不能修改,如果要修改只能用切片或者拼接的方式. 其他的什么乱七八糟的运算符都一样 一些不同 capitalize()-将字符串的第一个字母大写 str1. ...
- 《剑指offer》---字符串的全排列
本文算法使用python3实现 1.问题一 1.1 题目描述: 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc ...
- <Effective C++>读书摘要--Resource Management<一>
1.除了内存资源以外,Other common resources include file descriptors, mutex locks, fonts and brushes in graphi ...
- C# 创建Excel或需不安装Office
第一种.Aspose.Cells.dll //如果需要饶过office Excel那么就看我最后的实现方法吧~! //我最后的实现是使用的第三方Aspose.Cells.dll //具了解这个dll一 ...
- SVM之问题形式化
>>>SVM之问题形式化 SVM之对偶问题 SVM之核函数 SVM之解决线性不可分 写在SVM之前——凸优化与对偶问题 SVM内容繁多,打算用五篇文章来记述.SVM之问题形式化描述给 ...
- 个人作业4 alpha阶段 个人总结
一.个人总结 二.回答问题 三.再提问题 Q1:关于第三章过早优化 过早优化:既然软件是"软"的,那么它就有很大的可塑性,可以不断改进.放眼望去,一个复杂的软件似乎很多的模块都可以 ...
- python爬虫 --- 简书评论
某些网站的一些数据是通过js加载的 ,所以爬取下来的数据拿不到, 找到评论的地址 .进行请求获取评论数据 #coding=utf-8 import json import requests def r ...
- table与div互相嵌套注意
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- Ubuntu编译内核树
什么是内核树?刚开始我也没弄明白,通过这几天的学习,有所感悟,就说说我的理解吧!从形式上看,内核树与内核源码的目录结构形式是相同的,都是由各个层次的文件目录结构组成,但是其中的具体内容肯定是不同的.从 ...