• 2019ICPC徐州网络赛 A.Who is better?——斐波那契博弈&&扩展中国剩余定理


    题意

    有一堆石子,两个顶尖聪明的人玩游戏,先取者可以取走任意多个,但不能全取完,以后每人取的石子数不能超过上个人的两倍。石子的个数是通过模方程组给出的。

    题目链接

    分析

    斐波那契博弈有结论:当且仅当石子数为斐波那契数时,先手必败。

    又因为 $n leq 10^{15}$,在这个范围内的斐波那契数只有72个,可以预处理出来。

    注意会爆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
    ", flag ? C[K] : -1);
        if(flag == 0)  printf("Tankernb!
    ");
        else
        {
            bool flg = false;
            for(int i = 0;i < 80;i++)
                if(fibo[i] == C[K])
                {
                    flg = true;
                    break;
                }
            if(flg)  printf("Lbnb!
    ");
            else printf("Zgxnb!
    ");
        }
    
        return 0;
    }
  • 相关阅读:
    c++11 standardized memory model 内存模型
    C++和C API调用
    c+11 std::condition_variable and mutex
    Linux thread process and kernel mode and user mode page table
    c++ Initialization
    c++11 perfect forwarding
    C++11 template parameter deduction
    Cache缓存设计
    c++11 enable_shared_from_this
    大数相乘
  • 原文地址:https://www.cnblogs.com/lfri/p/11482466.html
Copyright © 2020-2023  润新知