Disgruntled Judge UVA

来源:互联网 发布:车床倒角编程 编辑:程序博客网 时间:2024/06/05 11:36

题目传送门

题意:给你一个递推式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.14159using 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;}/*3178223014 */