Power and Modulo
[Link](Problem - E - Codeforces)
题意
给你一个序列,第 i i i个的值是 2 i − 1 m o d M 2^{i-1}modM 2i−1modM,问你是否存在一个唯一的M使得该序列成立。
题解
找到第一个 a i ! = a i − 1 ∗ 2 a_{i}!= a_{i-1}*2 ai!=ai−1∗2的位置,则 M = 2 ∗ a i − 1 − a i M=2*a_{i-1}-a_i M=2∗ai−1−ai,如果没有这样的位置就无解。然后从前往后暴力判断一下是否符合即可,记得特判一下首项是否为0。
Code
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <bitset>
#include <unordered_map>
#include <cmath>
#include <stack>
#include <iomanip>
#include <deque>
#include <sstream>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 2e5 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
#define tpyeinput int
inline char nc() {static char buf[1000000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;}
inline void read(tpyeinput &sum) {char ch=nc();sum=0;while(!(ch>='0'&&ch<='9')) ch=nc();while(ch>='0'&&ch<='9') sum=(sum<<3)+(sum<<1)+(ch-48),ch=nc();}
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
int n, m, k;
int a[N];
int main() {
ios::sync_with_stdio(false), cin.tie(0);
int T;
cin >> T;
while (T -- ) {
cin >> n;
for (int i = 1; i <= n; i ++ ) cin >> a[i];
if (!a[1]) {
bool ok = true;
for (int i = 1; i <= n; i ++ )
if (a[i]) {
ok = false;
break;
}
cout << (ok ? 1 : -1) << endl;
continue ;
}
int M = 1;
bool ok = false;
for (int i = 2; i <= n; i ++ )
if (a[i] != a[i - 1] * 2) {
M = a[i - 1] * 2 - a[i];
ok = true;
break;
}
if (!ok) {
cout << -1 << endl;
continue ;
}
for (int i = 2; i <= n; i ++ )
if (a[i] != (a[i - 1] * 2 % M)) {
ok = false;
break;
}
cout << (ok ? M : -1) << endl;
}
return 0;
}
这篇博客主要解析了Codeforces上的Problem E题目,该题涉及序列和模运算。题目要求找到唯一的一个M值,使得序列满足特定条件。博主提供了题解思路:找到序列中第一个不满足连续倍数关系的位置,计算出M值,然后回溯检查序列是否都符合这个M值。如果找不到这样的M值则无解。博主给出了C++代码实现,并特别处理了首项为0的情况。

被折叠的 条评论
为什么被折叠?



