hdu 1370 Biorthythms 中国剩余定理
Biorhythms
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2481 Accepted Submission(s): 1091
Since the three cycles have different periods, the peaks of the three cycles generally occur at different times. We would like to determine when a triple peak occurs (the peaks of all three cycles occur in the same day) for any person. For each cycle, you will be given the number of days from the beginning of the current year at which one of its peaks (not necessarily the first) occurs. You will also be given a date expressed as the number of days from the beginning of the current year. You task is to determine the number of days from the given date to the next triple peak. The given date is not counted. For example, if the given date is 10 and the next triple peak occurs on day 12, the answer is 2, not 3. If a triple peak occurs on the given date, you should give the number of days to the next occurrence of a triple peak.
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
Case 1: the next triple peak occurs in 1234 days.
Use the plural form ``days'' even if the answer is 1.
0 0 0 0
0 0 0 100
5 20 34 325
4 5 6 7
283 102 23 320
203 301 203 40
-1 -1 -1 -1
Case 2: the next triple peak occurs in 21152 days.
Case 3: the next triple peak occurs in 19575 days.
Case 4: the next triple peak occurs in 16994 days.
Case 5: the next triple peak occurs in 8910 days.
Case 6: the next triple peak occurs in 10789 days.
#include<iostream>
#include<stdio.h>
using namespace std;
int ext_gcd(int a,int b,int *x,int *y)
{
if(b==)
{
*x=,*y=;
return a;
}
int r = ext_gcd(b,a%b,x,y);
int t =*x;
*x=*y;
*y=t-a/b**y;
return r;
}
int chinese_remainder(int a[],int w[],int len)//a存放余数,w存放两两互质的数
{
int i,d,x,y,m,n,ret;
ret=;
n=;
for(i=; i<len; i++)
{
n*=w[i];
}
for(i=; i<len; i++)
{
m=n/w[i];
d=ext_gcd(w[i],m,&x,&y);
ret=(ret+y*m*a[i])%n;
}
return(ret%n+n)%n;
}
int main()
{
int a[];
int w[]= {,,};
int t;
scanf("%d",&t);
while(t--)
{
int cas=;
int d;
while(scanf("%d%d%d%d",&a[],&a[],&a[],&d))
{
if(a[]==-) break;
for(int i=; i<; i++)
a[i]%=w[i];
int ans=chinese_remainder(a,w,);
ans=ans-d;
if(ans<=) ans+=;
printf("Case %d: the next triple peak occurs in %d days.\n",cas++,ans); }
}
return ;
}
点这里看我整理中国剩余定理的分析
点这里看注释代码
hdu 1370 Biorthythms 中国剩余定理的更多相关文章
- hdu 5668 Circle 中国剩余定理
Circle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem D ...
- hdu 3579 Hello Kiki 不互质的中国剩余定理
Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Probl ...
- 《孙子算经》之"物不知数"题:中国剩余定理
1.<孙子算经>之"物不知数"题 今有物不知其数,三三数之剩二,五五数之剩七,七七数之剩二,问物几何? 2.中国剩余定理 定义: 设 a,b,m 都是整数. 如果 m ...
- POJ 1006 中国剩余定理
#include <cstdio> int main() { // freopen("in.txt","r",stdin); ; while(sca ...
- [TCO 2012 Round 3A Level3] CowsMooing (数论,中国剩余定理,同余方程)
题目:http://community.topcoder.com/stat?c=problem_statement&pm=12083 这道题还是挺耐想的(至少对我来说是这样).开始时我只会60 ...
- poj1006中国剩余定理
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 103506 Accepted: 31995 Des ...
- (伪)再扩展中国剩余定理(洛谷P4774 [NOI2018]屠龙勇士)(中国剩余定理,扩展欧几里德,multiset)
前言 我们熟知的中国剩余定理,在使用条件上其实是很苛刻的,要求模线性方程组\(x\equiv c(\mod m)\)的模数两两互质. 于是就有了扩展中国剩余定理,其实现方法大概是通过扩展欧几里德把两个 ...
- 洛谷P2480 [SDOI2010]古代猪文(费马小定理,卢卡斯定理,中国剩余定理,线性筛)
洛谷题目传送门 蒟蒻惊叹于一道小小的数论题竟能涉及这么多知识点!不过,掌握了这些知识点,拿下这道题也并非难事. 题意一行就能写下来: 给定\(N,G\),求\(G^{\sum \limits _{d| ...
- 洛谷P3868 [TJOI2009]猜数字(中国剩余定理,扩展欧几里德)
洛谷题目传送门 90分WA第二个点的看过来! 简要介绍一下中国剩余定理 中国剩余定理,就是用来求解这样的问题: 假定以下出现数都是自然数,对于一个线性同余方程组(其中\(\forall i,j\in[ ...
随机推荐
- Spring4 与 Hibernate4 整合过程中的问题记录
Spring4使用注解配置,很方便也很有趣,就是有一些坑需要自己去发现和解决,笔者列出自己在使用过程中遇到的问题,希望对您有所帮助. 1.如果使用hibernate.cfg.xml配置文件配置Hibe ...
- Effective C++ -----条款40:明智而审慎地使用多重继承
多重继承比单一继承复杂.它可能导致新的歧义性,以及对virtual继承的需要. virtual继承会增加大小.速度.初始化(及赋值)复杂度等等成本.如果virtual base classes不带任何 ...
- sqlserverJDBC驱动链接
final String DRIVER="com.microsoft.jdbc.sqlserver.SQLServerDriver"; final String URL=" ...
- 【linux】linux下运行java程序
参考了http://www.cnblogs.com/howard-queen/archive/2012/01/30/2331795.html 第一步:用vim先写一个java程序 first.jav ...
- 【C语言】结构体
不能定义 struct Node { struct Node a; int b; } 这样的结构,因为为了建立Node 需要 建立一个新的Node a, 可为了建立Node a, 还需要再建立Node ...
- IntelliJ IDEA 15.0.4常用快捷键整理
一.背景 最近刚转了IDEA,感觉真是爽的一逼,太智能了,回不去Eclipse了,还有些淡淡的忧伤呢~在使用中很多的快捷键帮了开发的大忙,让我可以达到事半功倍的效果,下面就罗列出来,与大家共同分享. ...
- CSS3–1.css3 新增选择器
1.后代级别选择器 2.同辈级别选择器 3.伪类选择器 4.属性选择器 5.UI伪类选择器 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 T ...
- 使用Autofac在ASP.NET Web API上实现依赖注入
在ASP.NET Web API里使用Autofac 1.通过NuGet安装Autofac.WebApi(当时安装的是Autofac 3.1.0) PM > Install-Package Au ...
- 记录一次冷备恢复遇到的 ORA-00304问题
希望通过冷备一个数据库,然后在另外一台数据库进行恢复 1.打tar包 tar -czvf Prod.tar.gz *control01.ctlcontrol02.ctlredo01.logredo02 ...
- .NET Framework 4 与 .NET Framework 4 Client Profile
今天碰到的一个问题和Client Profile相关的.问题是这样的:一个WPF工程,需要引用另外几个.NET的assembly, 在WPF工程中添加了对这几个assembly的引用,并在程序中可以添 ...