Disgruntled Judge UVA - 12169

本文介绍了一种通过枚举和扩展欧几里得算法解决特定递推数列问题的方法。给定一个模运算下的递推公式及部分序列,文章详细阐述了如何找到合适的参数以完整构造出整个数列。

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

题目传送门

题意:给你一个递推式x[i] = (a * x[i - 1] + b) mod 10001,然后输入T,x[1], x[3], ….., x[2 * T - 1],求出剩下的序列。

思路:我们可以枚举a的值,然后用扩展欧几里得来求出来b然后进行验证当前的a,b是不是可以构成整个序列。

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define MAXN 11000
#define MAXE 5
#define INF 100000000
#define MOD 10001
#define LL long long
#define pi 3.14159

using namespace std;

LL arr[MAXN];

void exgcd(LL a, LL b, LL &d, LL &x, LL &y) {
    if (b == 0) {
        d = a;
        x = 1;
        y = 0;
    } else  {
        exgcd(b, a % b, d, y, x);
        y -= x * (a / b);
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    int T;
    while (cin >> T) {
        for (int i = 1; i <= 2 * T; i += 2) {
            cin >> arr[i];
        }
        LL a, b;
        LL x = 0, y = 0, d = 0;
        for (a = 1; a <= 10000; ++a) {
            exgcd(MOD, a + 1, d, x, y);
            if ((a * a * arr[1] - arr[3]) % d == 0) {
                y = y * (a * a * arr[1] - arr[3]) / d;
                if (y < 0) {
                    b = -y;
                } else {
                    b = y;
                }
                b %= MOD;
                bool flag = true;
                for (int j = 2; j <= 2 * T; ++j) {
                    if (j % 2) {
                        if (arr[j] != (arr[j - 1] * a + b) % MOD) {
                            flag = false;
                            break;
                        }
                    } else {
                        arr[j] = (arr[j - 1] * a + b) % MOD;
                    }
                }
                if (flag) {
                    break;
                }
            }
        }
        for (int i = 2; i <= 2 * T; i += 2) {
            cout << arr[i] << endl;
        }
    }
    return 0;
}

/*
3
17
822
3014
 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值