hdu 3579 Hello Kiki (中国剩余定理)
Hello Kiki
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1943 Accepted Submission(s): 693
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
模板题,注意ans=0的情况,题目要求的是最小的正整数。
//0MS 244K 1571 B C++
#include<stdio.h>
__int64 gcd(__int64 a,__int64 b)
{
return b?gcd(b,a%b):a;
}
__int64 extend_euclid(__int64 a,__int64 b,__int64 &x,__int64 &y)
{
if(b==){
x=;y=;
return a;
}
__int64 d=extend_euclid(b,a%b,x,y);
__int64 t=x;
x=y;
y=t-a/b*y;
return d;
}
__int64 inv(__int64 a,__int64 n)
{
__int64 x,y;
__int64 t=extend_euclid(a,n,x,y);
if(t!=) return -;
return (x%n+n)%n;
}
bool merge(__int64 a1,__int64 n1,__int64 a2,__int64 n2,__int64 &a3,__int64 &n3)
{
__int64 d=gcd(n1,n2);
__int64 c=a2-a1;
if(c%d) return false;
c=(c%n2+n2)%n2;
c/=d;
n1/=d;
n2/=d;
c*=inv(n1,n2);
c%=n2;
c*=n1*d;
c+=a1;
n3=n1*n2*d;
a3=(c%n3+n3)%n3;
return true;
}
__int64 china_reminder2(int len,__int64 *a,__int64 *n,__int64 &lcm)
{
__int64 a1=a[],n1=n[];
__int64 a2,n2;
for(int i=;i<len;i++){
__int64 aa,nn;
a2=a[i],n2=n[i];
if(!merge(a1,n1,a2,n2,aa,nn)) return -;
a1=aa;
n1=nn;
}
lcm=n1;
return (a1%n1+n1)%n1;
}
int main(void)
{
int t,n,k=;
__int64 a[],b[],lcm;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%I64d",&a[i]);
for(int i=;i<n;i++)
scanf("%I64d",&b[i]);
printf("Case %d: ",k++);
__int64 ans=china_reminder2(n,b,a,lcm);
if(ans==) ans+=lcm;
printf("%I64d\n",ans);
}
return ;
}
hdu 3579 Hello Kiki (中国剩余定理)的更多相关文章
- HDU 3579 Hello Kiki 中国剩余定理(合并方程
题意: 给定方程 res % 14 = 5 res % 57 = 56 求res 中国剩余定理裸题 #include<stdio.h> #include<string.h> # ...
- hdu 3579 Hello Kiki【中国剩余定理】(模数不要求互素)(模板题)
<题目链接> 题目大意: 给你一些模数和余数,让你求出满足这些要求的最小的数的值. 解题分析: 中国剩余定理(模数不一定互质)模板题 #include<stdio.h> usi ...
- HDU 5768 Lucky7 (中国剩余定理 + 容斥 + 快速乘法)
Lucky7 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 Description When ?? was born, seven crow ...
- hdu X问题 (中国剩余定理不互质)
http://acm.hdu.edu.cn/showproblem.php?pid=1573 X问题 Time Limit: 1000/1000 MS (Java/Others) Memory ...
- HDU 5768 Lucky7 容斥原理+中国剩余定理(互质)
分析: 因为满足任意一组pi和ai,即可使一个“幸运数”被“污染”,我们可以想到通过容斥来处理这个问题.当我们选定了一系列pi和ai后,题意转化为求[x,y]中被7整除余0,且被这一系列pi除余ai的 ...
- hdu 5446 Unknown Treasure 中国剩余定理+lucas
题目链接 求C(n, m)%p的值, n, m<=1e18, p = p1*p2*...pk. pi是质数. 先求出C(n, m)%pi的值, 然后这就是一个同余的式子. 用中国剩余定理求解. ...
- hdu 3579 Hello Kiki
不互质的中国剩余定理…… 链接http://acm.hdu.edu.cn/showproblem.php?pid=3579 #include<iostream>#include<st ...
- hdu 3579 Hello Kiki 不互质的中国剩余定理
Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Probl ...
- HDU 3579——Hello Kiki
好久没写什么数论,同余之类的东西了. 昨天第一次用了剩余定理解题,今天上百度搜了一下hdu中国剩余定理.于是就发现了这个题目. 题目的意思很简单.就是告诉你n个m[i],和n个a[i].表示一个数对m ...
随机推荐
- jQuery String Functions
In today's post, I have put together all jQuery String Functions. Well, I should say that these are ...
- C#读取Excel遇到无法读取的解决方法
C#读取Excel遇到无法读取的解决方法:1.在导入数据连接字符串中,将IMEX=1加入,“Provider=Microsoft.Jet.OLEDB.4.0;Data Source="C:\ ...
- 怎么创建一个Database快照
How to create the database Snapshot: use mastergoif not exists (select * from sys.databases where na ...
- 关于计算机的ID和用户ID之间的关系
关于计算机的ID和用户ID之间的关系 计算机安装完系统后就会生成计算机ID,然后系统会以计算机ID为前缀附加数字创建Administrator(500)和Guest(501)用户ID,其他用户的ID将 ...
- js判断是否绑定了事件。
if ($._data($("#moreDescription")[0], "events")) { return; }
- BroadCast小结
1.BroadCast注册方式 静态注册:即在AndroidManifest.xml 文件中定义 <receiver android:name=".BroadCastB"&g ...
- The C Programming Language Exercise
1-9 : Write a program to copy its input to its output, replacing each string of one or more blanks b ...
- 6、SQL Server 数据查询
一.使用SELECT检索数据 数据查询是SQL语言的中心内容,SELECT 语句的作用是让数据库服务器根据客户要求检索出所需要的信息资料,并按照规定的格式进行整理,返回给客户端. SELECT 语句的 ...
- asp.net mvc4 Html.BeginForm表单提交
默认是get提交,如果是post提交需要在控制器ActionResult上加:[AcceptVerbs(HttpVerbs.Post)] 举例: 在HelpController中,会定义如下的Acti ...
- WIn7系统下 打开.exe程序出现已停止工作关闭程序之解决办法
新装WIN7系统出现 .NET组建没有安装 可到官网下载安装 NETFx4.0 运行MVB 上位机SIM.EXE出现应用程序已停止工作问题 解决办法: 需关闭WIN7 DEP 如下 开始-运行( ...