2019ICPC Xuzhou network game A.Who is better -? Qi Bo Yi && Fibonacci extension Chinese remainder theorem

The meaning of problems

A pile of stones, the top two smart people play the game, first take can take away as many, but not all get done, each person takes after a few stones can not exceed twice the individual. The number of stones is given by the equation molding.

Topic Link

analysis

Fibonacci Qi Bo Yi conclusions: if and only if the number of stones as Fibonacci, losing the upper hand.

And because $ n \ leq 10 ^ {15} $, Fibonacci within this range only that the number of conveyance 72, it may be pretreated.

Note will burst long long !!

#include<iostream>
#include<cstdio>
#define LL __int128
using namespace std;

const LL MAXN = 15;
LL fibo[80] = {1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025,121393,196418,317811,514229,832040,1346269,2178309,3524578,5702887,9227465,14930352,24157817,39088169,63245986,102334155,165580141,267914296,433494437,701408733,1134903170,1836311903,2971215073,4807526976,7778742049,12586269025,20365011074,32951280099,53316291173,86267571272,139583862445,225851433717,365435296162,591286729879,956722026041,1548008755920,2504730781961,4052739537881,6557470319842,10610209857723,17167680177565,27777890035288,44945570212853,72723460248141,117669030460994,190392490709135,308061521170129,498454011879264,806515533049393};

int K;
LL C[MAXN], M[MAXN], x, y;
LL gcd(LL a, LL b) {
    return b == 0 ? a : gcd(b, a % b);
}
LL exgcd(LL a, LL b, LL &x, LL &y) {
    if (b == 0) {x = 1, y = 0; return a;}
    LL r = exgcd(b, a % b, x, y), tmp;
    tmp = x; x = y; y = tmp - (a / b) * y;
    return r;
}
LL inv(LL a, LL b) {
    LL r = exgcd(a, b, x, y);
    while (x < 0) x += b;
    return x;
}
int main() {
    scanf("%d", &K);
    for (int i = 1; i <= K; i++)  //x = C[i](mod M[i])
    {
        long long _m, _c;
        scanf("%lld%lld", &_m, &_c);
        M[i] = _m, C[i] = _c;
    }
    bool flag = 1;
    for (LL i = 2; i <= K; i++) {
        LL M1 = M[i - 1], M2 = M[i], C2 = C[i], C1 = C[i - 1], T = gcd(M1, M2);
        if ((C2 - C1) % T != 0) {flag = 0; break;}
        M[i] = (M1 * M2) / T;  //可能爆long long
        C[i] = ( inv( M1 / T , M2 / T ) * (C2 - C1) / T ) % (M2 / T) * M1 + C1;
        C[i] = (C[i] % M[i] + M[i]) % M[i];
    }
    //printf("%lld\n", flag ? C[K] : -1);
    if(flag == 0)  printf("Tankernb!\n");
    else
    {
        bool flg = false;
        for(int i = 0;i < 80;i++)
            if(fibo[i] == C[K])
            {
                flg = true;
                break;
            }
        if(flg)  printf("Lbnb!\n");
        else printf("Zgxnb!\n");
    }

    return 0;
}

 

Guess you like

Origin www.cnblogs.com/lfri/p/11482466.html