一本通1640:C Looooops

#include <cctype>
#include <cstdio>
namespace io {
using namespace std;
#define fastcall __attribute__((optimize("-O3")))
#define IL __inline__ __attribute__((always_inline))
struct istream {
#define M (1 << 25)
    int f;
    char buf[M], *ch = buf - 1;
    fastcall IL istream() {
        fread(buf, 1, M, stdin);
    }
    fastcall IL istream &operator>>(int &x) {
        f = 1;

        while (!isdigit(*++ch))
            if (*ch == '-')
                f = -1;

        for (x = *ch & 15; isdigit(*++ch);)
            x = x * 10 + (*ch & 15);

        x *= f;
        return *this;
    }
    fastcall IL istream &operator>>(long long &x) {
        f = 1;

        while (!isdigit(*++ch))
            if (*ch == '-')
                f = -1;

        for (x = *ch & 15; isdigit(*++ch);)
            x = x * 10 + (*ch & 15);

        x *= f;
        return *this;
    }
    fastcall IL istream &operator>>(char &c) {
        c = *++ch;
        return *this;
    }
#undef M
} cin;
struct ostream {
#define M (1 << 23)
    char buf[M], *ch = buf - 1;
#define endl (char)10
    fastcall IL ostream &operator<<(int x) {
        if (!x) {
            *++ch = '0';
            return *this;
        }

        if (x < 0) {
            x = -x;
            *++ch = '-';
        }

        static int S[20], *top;
        top = S;

        while (x) {
            *++top = x % 10 ^ 48;
            x /= 10;
        }

        for (; top != S; --top)
            *++ch = *top;

        return *this;
    }
    fastcall IL ostream &operator<<(long long x) {
        if (!x) {
            *++ch = '0';
            return *this;
        }

        if (x < 0) {
            x = -x;
            *++ch = '-';
        }

        static int S[20], *top;
        top = S;

        while (x) {
            *++top = x % 10 ^ 48;
            x /= 10;
        }

        for (; top != S; --top)
            *++ch = *top;

        return *this;
    }
    fastcall IL ostream &operator<<(const char &x) {
        *++ch = x;
        return *this;
    }
    fastcall IL ~ostream() {
        fwrite(buf, 1, ch - buf + 1, stdout);
    }
#undef M
} cout;
}  // namespace io
using namespace io;

template <typename T>
fastcall void exgcd(const T &, const T &, T &, T &, T &);
template <typename T>
fastcall IL bool Equation(const T &, const T &, const T &, T &, T &);

int main() {
    long long a, b, c, k, x, t;
    cin >> a >> b >> c >> k;

    while (a || b || c || k) {
        k = 1LL << k;
        a = (b - a + k) % k;
        x = t = 0;

        if (Equation(c, k, a, x, t))
            cout << x << endl;
        else
            cout << 'F' << 'O' << 'R' << 'E' << 'V' << 'E' << 'R' << endl;

        cin >> a >> b >> c >> k;
    }

    return 0;
}

template <typename T>
fastcall void exgcd(const T &a, const T &b, T &g, T &x, T &y) {
    if (b) {
        exgcd(b, a % b, g, y, x);
        y -= x * (a / b);
        return;
    }

    g = a, x = 1, y = 0;
    return;
}

// ax + by = c
template <typename T>
fastcall IL bool Equation(const T &a, const T &b, const T &c, T &x, T &y) {
    T g = 0;
    exgcd(a, b, g, x, y);

    if (c % g)
        return false;

    T t = b / g;
    x = ((x + t) * (c / g)) % t;

    if (x < 0)
        x += t;

    y = (c - a * x) / b;
    return true;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值