UVA756:https://www.luogu.org/problemnew/show/UVA756

思路

几乎是裸的中国剩余定理模板题

但是需要注意的是此题并不是求最小正整数解

而是求大于d的解

因此对于ans我们需要分类讨论

  • 当ans<=d时 ans=M-(d-ans)
  • 当ans>d时 输出ans即可

代码

#include<iostream>
#include<cstdio>
using namespace std;
int d,M=**,ans,t;
int m[]={,,,};//存mod数
int a[];
void exgcd(int a,int b,int &d,int &x,int &y)
{
if(!b)
{
x=;
y=;
d=a;
}
else
{
exgcd(b,a%b,d,x,y);
int t=x;
x=y;
y=t-a/b*y;
}
}
int intchina(int n)//中国剩余定理
{
ans=;//注意初始化
int Mi,x,y,g;
for(int i=;i<=n;i++)
{
Mi=M/m[i];
exgcd(Mi,m[i],g,x,y);
ans=(ans+Mi*x*a[i])%M;
}
if(ans<=d) return -(d-ans);
else return ans-d;//ans的分类讨论
}
int main()
{
while()
{
t++;
scanf("%d%d%d%d",&a[],&a[],&a[],&d);
if(a[]==-&&a[]==-&&a[]==-&&d==-) break;
a[]%=,a[]%=,a[]%=;
ans=intchina();
printf("Case %d: the next triple peak occurs in %d days.\n", t, ans);
}
}

【题解】UVA756 Biorhythms (中国剩余定理)的更多相关文章

  1. Biorhythms(中国剩余定理)

    http://shuxueshi.jie.blog.163.com/blog/static/13611628820104179856631/ 这篇博客写的很棒! #include<stdio.h ...

  2. POJ 1006 - Biorhythms (中国剩余定理)

    B - Biorhythms Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Subm ...

  3. POJ 1006 Biorhythms --中国剩余定理(互质的)

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 103539   Accepted: 32012 Des ...

  4. POJ 1006 Biorhythms(中国剩余定理)

    题目地址:POJ 1006 学习了下中国剩余定理.參考的该博客.博客戳这里. 中国剩余定理的求解方法: 假如说x%c1=m1,x%c2=m2,x%c3=m3.那么能够设三个数R1,R2,R3.R1为c ...

  5. PKU POJ 1006 Biorhythms (中国剩余定理)

    中国剩余定理 x = ai (mod mi)  ai和mi是一组数,mi两两互质,求x 令Mi = m1*m2*~mk     其中,mi不包含在内. 因为mi两两互质,所以存在x和y, st   M ...

  6. poj1006 / hdu1370 Biorhythms (中国剩余定理)

    Biorhythms 题意:读入p,e,i,d 4个整数,已知(n+d)%23=p;   (n+d)%28=e;   (n+d)%33=i ,求n .        (题在文末) 知识点:中国剩余定理 ...

  7. POJ1006——Biorhythms(中国剩余定理)

    Biorhythms Description人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这天,人会在相应的方面表现出色. ...

  8. [poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)

    题目链接:http://poj.org/problem?id=2891 题目大意: 求解同余方程组,不保证模数互质 题解: 扩展中国剩余定理板子题 #include<algorithm> ...

  9. Biorhythms(中国剩余定理)

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 127339   Accepted: 40342 Des ...

随机推荐

  1. C# Uploadify 文件上传组件的使用

    一.页面的构建 1.要引用的JS和CSS <link href="../css/jquery-ui.css" rel="stylesheet" type= ...

  2. Unity3D第二课之通过键盘、鼠标移动物体

    public class xuanzhuan : MonoBehaviour { //平移速度变量 public float MoveSpeed;// Use this for initializat ...

  3. Java集合篇三:Vector

    package com.test.collection; import java.util.Vector; public class MyVector { /** * @param args */ p ...

  4. ASTreeView Demo:Add, Edit & Delete nodes

    http://www.jinweijie.com/ http://www.astreeview.com/astreeviewdemo/astreeviewdemo1.aspx 選擇節點: <sc ...

  5. 洛谷P1970 花匠(dp)

    题意 题目链接 Sol 直接用\(f[i][0/1]\)表示到第\(i\)个位置,该位置是以上升结尾还是以下降结尾 转移的时候只需枚举前一个即可 #include<cstdio> #inc ...

  6. 纯css 简单网页

    <div id="wrapper"> <header> <section> <h1>Web Design<h1> < ...

  7. javascript实现数据结构:稀疏矩阵的十字链表存储表示

    当矩阵的非零个数和位置在操作过程中变化大时,就不宜采用顺序存储结构来表示三元组的线性表.例如,在作“将矩阵B加到矩阵A上”的操作时,由于非零元的插入或删除将会引起A.data中元素的移动.为此,对这种 ...

  8. 【PIC单片机】Pic单片机基础知识

    本次学习采用PIC16F877A芯片及HJ-5G 开发板 一.IO口操作 1.1 设置I/O口方向:input or output TRISx 方向寄存器 (Transport and Receive ...

  9. 内存分配malloc函数注意事项。

    malloc的全称是memory allocation,中文叫动态内存分配,用于向系统申请分配指定字节的内存空间 原型:extern void *malloc(unsigned int num_byt ...

  10. linux c开发: 在程序退出时进行处理

    有时候,希望程序退出时能进行一些处理,比如保存状态,释放一些资源.c语言开发的linux程序,有可能正常退出(exit),有可能异常crash,而异常crash可能是响应了某信号的默认处理.这里总结一 ...