hdu3579(线性同余方程组)
Hello Kiki
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2734 Accepted Submission(s): 1010
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.
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
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.
2
14 57
5 56
5
19 54 40 24 80
11 2 36 20 76
Case 2: 5996
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
using namespace std; /*对于x=r0(mod m0)
x=r1(mod m1)
...
x=rn(mod mn)
输入数组m和数组r,返回[0,[m0,m1,...,mn]-1] 范围内满足以上等式的x0。
x的所有解为:x0+z*[m0,m1,...mn](z为整数)
*/
long long cal_axb(long long a,long long b,long long mod)
{
//防乘法溢出
long long sum=;
while(b)
{
if(b&) sum=(sum+a)%mod;
b>>=;
a=(a+a)%mod;
}
return sum;
} //ax + by = gcd(a,b)
//传入固定值a,b.放回 d=gcd(a,b), x , y
void extendgcd(long long a,long long b,long long &d,long long &x,long long &y)
{
if(b==){d=a;x=;y=;return;}
extendgcd(b,a%b,d,y,x);
y -= x*(a/b);
} long long Multi_ModX(long long m[],long long r[],int n,long long &M)
{
long long m0,r0;
m0=m[]; r0=r[];
for(int i=;i<n;i++)
{
long long m1=m[i],r1=r[i];
long long k0,k1;
long long tmpd;
extendgcd(m0,m1,tmpd,k0,k1);
if( (r1 - r0)%tmpd!= ) return -;
k0 *= (r1-r0)/tmpd;
m1 *= m0/tmpd;
r0 = ( cal_axb(k0,m0,m1)+r0)%m1;
m0=m1;
}
M=m0;
return (r0%m0+m0)%m0;
} int main()
{
int T;
cin>>T;
int tt=;
while(T--)
{
int n;
cin>>n;
long long a[],b[];
for(int i=;i<n;i++)
cin>>a[i];
for(int i=;i<n;i++)
cin>>b[i];
long long M;
long long ans=Multi_ModX(a,b,n,M);
printf("Case %d: ",tt++);
if(ans==) ans+=M;
cout<<ans<<endl;
}
return ;
}
hdu3579(线性同余方程组)的更多相关文章
- HDU3579:Hello Kiki(解一元线性同余方程组)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3579 题目解析:求一元线性同余方程组的最小解X,需要注意的是如果X等于0,需要加上方程组通解的整数区间lc ...
- HDU1573:X问题(解一元线性同余方程组)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1573 题目解析;HDU就是坑,就是因为n,m定义成了__int64就WAY,改成int就A了,无语. 这题 ...
- HDU1573 X问题【一元线性同余方程组】
题目链接: http://acm.hdu.edu.cn/showproblem.php? pid=1573 题目大意: 求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], 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 ...
- poj3708(公式化简+大数进制装换+线性同余方程组)
刚看到这个题目,有点被吓到,毕竟自己这么弱. 分析了很久,然后发现m,k都可以唯一的用d进制表示.也就是用一个ai,和很多个bi唯一构成. 这点就是解题的关键了. 之后可以发现每次调用函数f(x),相 ...
- hdu1573(线性同余方程组)
套模板,因为要是正整数,所以处理一下x=0的情况. X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- poj2891(线性同余方程组)
一个exgcd解决一个线性同余问题,多个exgcd解决线性同余方程组. Strange Way to Express Integers Time Limit: 1000MS Memory Limi ...
- POJ2891Strange Way to Express Integers (线性同余方程组)
Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative ...
- HDU-1573-X问题(线性同余方程组)
链接: https://vjudge.net/problem/HDU-1573 题意: 求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X mod a[1] = b[1] ...
随机推荐
- selenium 截图
http://blog.csdn.net/u010953692/article/details/78320025 # coding:utf-8 # coding:cp936 from selenium ...
- python字典里的update函数
>>> print d {'age': 34, 'name': 'jeapedu'} >>> d1={"age":38} >>> ...
- 第一章 初识shiro
shiro学习教程来自开涛大神的博客:http://jinnianshilongnian.iteye.com/blog/2018936 第一章 初识shiro 简单了解shiro主要记住三张图即可. ...
- shell中declare命令
declare命令有如下选项: -a 声明一个数组 -i 声明一个整型 -f 打印所有函数定义 -F 仅打印函数名字 -r 声明一个readonly变量,该变量的值无法改变,并且不能为unset -x ...
- 编译安装Apache httpd和php搭建KodExplorer网盘
编译安装Apache httpd和php搭建KodExplorer网盘 环境说明: 系统版本 CentOS 6.9 x86_64 软件版本 httpd-2.2.31 php- ...
- 2017.6.26 接口测试工具postman使用总结
参考来自: http://www.cnblogs.com/sunshine-sky66/p/6369963.html http://www.cnplugins.com/tool/specify-pos ...
- 自定义ViewPager的兼容性问题及解决办法
通过它我们可以给图片增加组合动画效果,也可以写成一个图片查看器. 比如我们首次安装应用的时候,很多就会用到ViewPager给我们做一个应用简介.今天要写的也是这个--怎么用ViewPager实现动画 ...
- RxJava操作符总结之过滤
RxJava操作符总结之过滤 jsut() just(T t1, T t2, T t3 ....) ,just能够传入多个同样类型的參数,并将当前參数一个接着一个的发送. Observable.jus ...
- Yii 获得当前控制器和方法
[怎样获得当前控制器和方法] 控制器:$this -> id ; 方法:$this->action->id ; 这主要是用在视图中,进行高亮显示. <div id=" ...
- ASP.NET综合管理ERP系统100%源代码+所有开发文档
该系统开发环境为:VS2010,数据库採用SQL Server,框架为ASP.NET. 源代码包含所有文档说明,代码简单易懂,凝视完整. 提示:假设没有安装水晶报表系统执行会报错,报表安装程序已经打包 ...