首页 > 代码库 > UVa 756 Biorhythms

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

  

UVa 756 Biorhythms