UVa 756 Biorhythms

方法:中国剩余定理

列出方程,发现是求解线性模方程,而且三个mod 两两互质,所以用中国剩余定理即可。 注意,最后求具体日期时,我采用了试一试的方法,因为方程组的解 D 和 d 之间的大小关系不确定。

code:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
#include <fstream>
#include <cassert>
#include <unordered_map>
#include <cmath>
#include <sstream>
#include <time.h>
#include <complex>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define FOR(a,b,c) for (int (a)=(b);(a)<(c);++(a))
#define FORN(a,b,c) for (int (a)=(b);(a)<=(c);++(a))
#define DFOR(a,b,c) for (int (a)=(b);(a)>=(c);--(a))
#define FORSQ(a,b,c) for (int (a)=(b);(a)*(a)<=(c);++(a))
#define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
#define FOREACH(a,b) for (auto &(a) : (b))
#define rep(i,n) FOR(i,0,n)
#define repn(i,n) FORN(i,1,n)
#define drep(i,n) DFOR(i,n-1,0)
#define drepn(i,n) DFOR(i,n,1)
#define MAX(a,b) a = Max(a,b)
#define MIN(a,b) a = Min(a,b)
#define SQR(x) ((LL)(x) * (x))
#define Reset(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(v) v.begin(),v.end()
#define ALLA(arr,sz) arr,arr+sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(all(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr,sz) sort(ALLA(arr,sz))
#define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
#define PERMUTE next_permutation
#define TC(t) while(t--)
#define forever for(;;)
#define PINF 1000000000000
#define newline ‘\n‘

#define test if(1)if(0)cerr
using namespace std;
  using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
typedef pair<double,double> dd;
typedef pair<char,char> cc;
typedef vector<ii> vii;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> l4;
const double pi = acos(-1.0);

void gcd(ll a, ll b, ll &d, ll &x, ll &y)
{
    if (!b)
    {
        d = a; x = 1; y = 0;
    }
    else
    {
        gcd(b, a%b, d, y, x);
        y -= x*(a/b);
    }
}
ll crt(int n, ll *a, ll *m)
{
    ll M = 1, d, y, x = 0;
    for (int i = 0; i < n; ++i) M *= m[i];
    for (int i = 0; i < n; ++i)
    {
        ll w = M / m[i];
        gcd(m[i], w, d, d, y);
        x = (x + y*w*a[i]) % M;
    }
    return (x+M)%M;
}
ll m[3] = {23, 28, 33};
ll a[3];
ll M = 23 * 28 * 33;
ll d;

int main()
{
    int kase = 0;
    while (cin >> a[0] >> a[1] >> a[2] >> d)
    {
        if (a[0] == -1) break;
        ll D = crt(3, a, m);
        ll k =(d-D)/M;
        ll ans = k*M+D-d;
        if (ans <= 0) ans += M;
        //cerr << D << " " << d << " " << M << newline;
        cout << "Case " << ++kase << ": the next triple peak occurs in " << ans << " days." << newline;
    }
}

/*

 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
*/

  

时间: 2024-08-02 15:08:53

UVa 756 Biorhythms的相关文章

uva 756 - Biorhythms(中国剩余定理)

题目链接:uva 756 - Biorhythms 题目大意:三个周期,23,28,33,输入为分别为在新一年中(三个周期均从0开始),出现周期中峰值的一天,以及当前的日子,问说需要经过多少天,才能使得三个峰值的在同一天. 解题思路:裸的中国剩余定理. #include <cstdio> #include <cstring> typedef long long ll; const int maxn = 5; const ll m[maxn] = {23,28,33}; ll M,

UVA 756 - Biorhythms(数论)

756 - Biorhythms 题目链接 基本就是裸的中国剩余定理. 代码: #include <stdio.h> #include <string.h> const int M = 23 * 28 * 33; const int m[3] = {23, 28, 33}; int p[3], d; int gcd(int a, int b, int &x, int &y) { if (!b) {x = 1; y = 0; return a;} int d = gc

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te

UVA 11014 - Make a Crystal(容斥原理)

UVA 11014 - Make a Crystal 题目链接 题意:给定一个NxNxN的正方体,求出最多能选几个整数点.使得随意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O).那么全部点就是f(1),之后要去除掉共线的,就是扣掉f(2), f(3), f(5)..f(n).n为素数.由于这些素数中包括了合数的情况,而且这些点必定与f(1)除去这些点以外的点共线,所以扣掉.可是扣掉后会扣掉一些反复的.比方f(6)在f

[UVa] Palindromes(401)

UVA - 401 Palindromes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDED

uva 401.Palindromes

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=342 题目意思:给出一段字符串(大写字母+数字组成).判断是否为回文串 or 镜像串 or 回文镜像串 or 什么都不是.每个字母的镜像表格如下 Character Reverse Character Reverse Character Reverse A A M M Y Y B

[2016-02-19][UVA][129][Krypton Factor]

UVA - 129 Krypton Factor Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description You have been employed by the organisers of a Super Krypton Factor Contest in which contestants have very high mental and physica

[2016-02-03][UVA][514][Rails]

时间:2016-02-03 22:24:52 星期三 题目编号:UVA 514 题目大意:给定若干的火车(编号1-n),按1-n的顺序进入车站, 给出火车出站的顺序,问是否有可能存在 分析:    FIFO,用栈模拟一遍即可, 方法:    根据输入的顺序,从1-n开始,当前操作的为i 如果i是当前对应的编号,那么直接跳过(进入B) 如果不是,根据当前需求的编号,小于i,就从栈顶弹出一个元素, 看这个元素是否是需求的,是则继续.否则NO 1 2 3 4 5 6 7 8 9 10 11 12 13