前言

这道题比较简单,但我还是想了好一会

题意简述

Abu Tahun很喜欢回文。

一个数组若是回文的,那么它从前往后读和从后往前读都是一样的,比如数组\(\left\{1\right\},\left\{1,1,1\right\},\left\{1,2,1\right\},\left\{1,3,2,3,1\right\}\)都是回文数组,但是数组\(\left\{11,3,5,11\right\},\left\{1,12\right\}\)不是回文的。

Abu Tahun有个包含\(n\)个整数的数组\(A\),他想让它变成回文的。他可以任意选择一个整数\(m\),然后让所有元素\(A_i\) 变成\(A_i\ mod\ m\)。

求最大的\(m\)的值。

输入格式

第一行一个整数\(n\)\(\left(1 \leq n \leq 10^5\right)\)

第二行\(n\)个整数\(A_1,A_2,\dots,A_n\)\(\left(1 \leq A_i \leq 10^9\right)\)

输出格式

输出最大的Abu Tahun能取的\(m\)的值,如果\(m\)可以是任意大小输出\(-1\)

样例

\ Input\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ output

\ 4\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ -1

\ 1 1 1 1

\ 4\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 1

\ 1 2 3 4

\ 3\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 8

\ 8 12 16

数据范围与约定

\(50\%\)的数据\(1 \leq n, A_i \leq 1000\)

\(100\%\)的数据\(1 \leq n \leq 10^5, 1 \leq A_i \leq 10^9\)

解法

我们发现对于每一组\([A_i,A_{n - i + 1}]\),有两种方法使其变为0,第一种是mod两个数的差的任意一个约数,第二种是mod两个数的gcd使之都变为0,根据辗转相减,我们发现第一种完全包含第二种,因此只需考虑第一种情况。

那么对于\(A\)数组,只需要枚举所有的\([A_i,A_{n - i + 1}]\),然后用它们的差求一个gcd即可

代码

#include <cstdio>
#include <cmath>
#include <algorithm>
#define ll long long using namespace std; inline ll read(){
ll x = 0; int zf = 1; char ch = ' ';
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;
} ll gcd(ll a, ll b){
if (b > a) swap(a, b);
while (b > 0){
ll tmp = a % b;
a = b, b = tmp;
}
return a;
} ll a[100005]; int main(){
freopen("palindrome.in", "r", stdin);
freopen("palindrome.out", "w", stdout);
int n = read(); int flg = (n <= 1000) ? 1 : 0, is_same = 1;
for (int i = 1; i <= n; ++i)
a[i] = read(), flg = (a[i] <= 1000) ? (flg & 1) : 0;
for (int i = 1; i <= n; ++i)
is_same = (a[i] == a[n - i + 1]) ? (is_same & 1) : 0;
if (is_same){
printf("-1");
return 0;
}
/*//brute force
if (flg){
int m = (n >> 1);
for (int i = 1000; i >= 1; --i){
int j;
for (j = 1; j <= m; ++j)
if ((abs(a[j] - a[i])) % i != 0 && gcd(a[i], a[j]) % i != 0)
break;
if (j == m + 1){
printf("%d", i);
return 0;
}
}
}*/
ll _gcd = 0; int m = n >> 1;
for (int i = 1; i <= m; ++i){
ll y = abs(a[i] - a[n - i + 1]);
_gcd = gcd(_gcd, y);
}
if (gcd == 0)
printf("-1");
else
printf("%lld", _gcd);
fclose(stdin);
fclose(stdout);
return 0;
}

[CF-GYM]Abu Tahun Mod problem题解的更多相关文章

  1. CF Gym 102028G Shortest Paths on Random Forests

    CF Gym 102028G Shortest Paths on Random Forests 抄题解×1 蒯板子真jir舒服. 构造生成函数,\(F(n)\)表示\(n\)个点的森林数量(本题都用E ...

  2. Codeforces GYM 100876 J - Buying roads 题解

    Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...

  3. Codeforces Gym 100015A Another Rock-Paper-Scissors Problem 找规律

    Another Rock-Paper-Scissors Problem 题目连接: http://codeforces.com/gym/100015/attachments Description S ...

  4. CF gym 101933 K King's Colors —— 二项式反演

    题目:http://codeforces.com/gym/101933/problem/K 其实每个点的颜色只要和父亲不一样即可: 所以至多 i 种颜色就是 \( i * (i-1)^{n-1} \) ...

  5. POJ2826:An Easy Problem?!——题解(配特殊情况图)

    http://poj.org/problem?id=2826 题目大意:给两条线,让它接竖直下的雨,问其能装多少横截面积的雨. ———————————————————————————— 水题,看题目即 ...

  6. 51NOD 1038:X^A Mod P——题解

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1038 X^A mod P = B,其中P为质数.给出P和A B,求< ...

  7. CF 990C. Bracket Sequences Concatenation Problem【栈/括号匹配】

    [链接]:CF [题意]: 给出n个字符串,保证只包含'('和')',求从中取2个字符串链接后形成正确的括号序列的方案数(每个串都可以重复使用)(像'()()'和'(())'这样的都是合法的,像')( ...

  8. cf Gym 101086M ACPC Headquarters : AASTMT (Stairway to Heaven)

    题目: Description standard input/output As most of you know, the Arab Academy for Science and Technolo ...

  9. HDU 1016 Prime Ring Problem 题解

    Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...

随机推荐

  1. VUE 全局监听sessionStorage变化

    在做项目的时候,可能需要在其他模块获取推送的信息或者变量,但是数据量或者说数目少,而且项目中也没有引用VUEX,那么可以下手的方法之一也就是sessionStorage类的浏览器存储了. 首先在全局的 ...

  2. Git - 版本回溯

    在git push的时候,有时候我们会想办法撤销git commit的内容 1.找到之前提交的git commit的id git log 找到想要撤销的id 2.git reset –hard id  ...

  3. GIt 工作区与暂存区

    转载:https://www.liaoxuefeng.com/wiki/896043488029600/897271968352576 工作区与暂存区 工作区(Working Directory) 就 ...

  4. HDFS数据流——写数据流程

    剖析HDFS文件写入 假设文件ss.avi共200m,其写入HDFS指定路径/user/atguigu/ss.avi流程如下: 1)客户端向namenode请求上传文件到指定路径,namenode通过 ...

  5. C89标准和C99标准C11标准的区别

    转载 C89标准和C99标准C11标准的区别 C99对C89的改变 1.增加restrict指针 C99中增加了公适用于指针的restrict类型修饰符,它是初始访问指针所指对象的惟一途径,因此只有借 ...

  6. [Codeforces 1191D] Tokitsukaze, CSL and Stone Game(博弈论)

    [Codeforces 1191D] Tokitsukaze, CSL and Stone Game(博弈论) 题面 有n堆石子,两个人轮流取石子,一次只能从某堆里取一颗.如果某个人取的时候已经没有石 ...

  7. wxpython模板程序,包括各个实例

    #coding=utf-8 import wx import time import os class MyApp(wx.App): def __init__(self): wx.App.__init ...

  8. js超简单冒泡算法

    点击按钮--从大到小排序,可以通过代码中大于号小于号的选择来判定从小到大或者从大到小. <!DOCTYPE html> <html> <head> <titl ...

  9. Java开发桌面程序学习(10)——css样式表使用以及Button使用

    css 样式表使用 javafx中的css样式,与html的有些不一样,javafx中的css,是以-fx-background-color这种样子的,具体可以参考文档JavaFx css官方文档 简 ...

  10. qemu-kvm使用

    创建镜像qemu-img create -f qcow2 test-vm.qcow2 10g 修改镜像大小qemu-img  resize  test-vm.qcow2 +10G   安装系统 qem ...