一本通1640:C Looooops

文章详细介绍了如何使用C++编写模板函数,包括整数输入处理、整数除法和扩展欧几里得算法,以解决线性方程ax+by=c的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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;
}

### C语言实现开关灯问题算法 以下是一个基于引用描述的开关灯问题的C语言实现方案。该问题是过模拟多个操作者依次改变灯光状态来解决的。 #### 代码示例 ```c #include <stdio.h> #include <stdlib.h> #define MAX_LIGHTS 100 // 定义最大灯的数量 void toggleLights(int lights[], int n, int person) { for (int i = person - 1; i < n; i += person) { // 遍历当前人的影响范围 lights[i] ^= 1; // 切换灯的状态,0表示关,1表示开 } } void printLightsState(int lights[], int n) { printf("最终灯的状态:\n"); for (int i = 0; i < n; ++i) { printf("灯 %d: %s\n", i + 1, lights[i] ? "开" : "关"); } } int main() { int n = MAX_LIGHTS; int* lights = (int*)malloc(n * sizeof(int)); // 动态分配数组空间 if (!lights) { fprintf(stderr, "内存分配失败!\n"); return EXIT_FAILURE; } // 初始化所有灯为关闭状态 for (int i = 0; i < n; ++i) { lights[i] = 1; // 所有灯初始都打开 [^2] } // 模拟每个人的操作 for (int person = 2; person <= n; ++person) { // 第一个人不需要额外处理 toggleLights(lights, n, person); } // 输出最终结果 printLightsState(lights, n); free(lights); // 释放动态分配的空间 return EXIT_SUCCESS; } ``` #### 解决思路说明 上述代码实现了开关灯问题的核心逻辑: - 使用`toggleLights`函数遍历并切换指定编号倍数的灯。 - 数组中的每个元素代表一盏灯的状态(0 表示关,1 表示开)[^2]。 - 主循环中调用了多次`toggleLights`函数,分别对应不同的人按压开关的行为。 #### 结果分析 运行此程序后会得到每盏灯的最终状态。根据数学规律可知,只有那些编号为完全平方数的灯会在最后保持开启状态,因为它们会被按压奇数次。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值