Power and Modulo

[Link](Problem - E - Codeforces)

题意

给你一个序列,第iii个的值是2i−1modM2^{i-1}modM2i−1modM,问你是否存在一个唯一的M使得该序列成立。

题解

找到第一个ai!=ai−1∗2a_{i}!= a_{i-1}*2ai​!=ai−1​∗2的位置,则M=2∗ai−1−aiM=2*a_{i-1}-a_iM=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;
}

Power and Modulo(思维)相关推荐

  1. 2021 CCPC 哈尔滨 E. Power and Modulo (思维题)

    Problem - E - Codeforces 思路: 这个题搞清楚那几种情况,特判一下第一个数,需要注意的是不能简单直接的按照题意使用 pow(2,n - 1),这样会wa,因为n是1e5的数量级 ...

  2. CodeForces - 1646E Power Board (思维,数学)

    题目链接:点击这里 题目大意: 给定一个 n×mn\times mn×m 的矩阵,其中 a[i][j]=ija[i][j]=i^ja[i][j]=ij ,求这个 n×mn\times mn×m 的矩阵 ...

  3. python数字类型-Python数字类型及其操作

    数字类型 Python 语言提供了3种数字类型:整数.浮点数和复数. 布尔型 In addition, Booleans are a subtype of integers. 整数类型(int)与数学 ...

  4. Leetcode周赛复盘——第 278 场力扣周赛

    5993. 将找到的值乘以 2 我的做法是将数组从小到大排序之后,再将找到的值乘以2: class Solution:def findFinalValue(self, nums: List[int], ...

  5. LeetCode 2156. 查找给定哈希值的子串(字符串哈希)

    文章目录 1. 题目 2. 解题 1. 题目 给定整数 p 和 m ,一个长度为 k 且下标从 0 开始的字符串 s 的哈希值按照如下函数计算: hash(s,p,m)=(val(s[0])∗p0+v ...

  6. python pow_Python pow()

    python pow Python pow() function usually takes two number arguments and returns their power. Python ...

  7. 2018 11.1 PION 模拟赛

    期望:250  100+100+50 实际:210   80+100+30 期望:100   实际:80 最后:两个点T了.可能是求逆元的方法太慢了,也可能是闲的又加了一个快速乘的原因. #inclu ...

  8. python的内置函数功能[翻译]

    Python 解释器内置了许多始终可用的函数和类型.它们按字母顺序列在这里.   内置功能     abs() dict() help() min() setattr() all() dir() he ...

  9. 完美解决Pytorch在Pycharm没有代码提示的问题

    在Pytorch的旧版本中,没有__init__.pyi这个文件(Pycharm就从__init__.pyi读取函数的声明以及参数类型),因此在Pycharm中torch.sum.torch.abs等 ...

最新文章

  1. redis持久化 mysql_Redis 如何保持和MySQL数据一致
  2. 移动H5前端性能优化指南[转]
  3. 可重入锁ReentrantLock--转载
  4. mfc从mysql中读取数据类型_在MFC中使用SQlite数据库读取数据
  5. 浅谈 JDBC 中 CreateStatement 和 PrepareStatement 的区别与优劣。
  6. EasyEarth三维可视化解决方案——智慧园区
  7. 【问题记录】python 函数 传入一个对象返回一个对象值得注意
  8. JAVA基础知识汇总(思维导图)
  9. tcl电视显示服务器异常1500,TCL电视机使用常见故障及维修方法
  10. 联想微型计算机m4350q升级,拆解:高度集成化的联想M4350q
  11. 怎么设置计算机升级更新失败怎么办,windows update更新失败怎么办,教您windows update更新失败怎么办...
  12. Golang多线程文件传输
  13. pyton 爬虫-图片
  14. 更新至OSX 10.10后MBA外接网卡无法使用的解决
  15. SPI器件的菊链配置
  16. (已完善)基于Python的TCP 协议实现人机聊天(程序具有服务端和客户端)
  17. 【手动安装Python包】
  18. Python 面试实训 100 题,哪道难住了你?
  19. 火山PC抓取快递物流查询接口教程第二课
  20. 14.11 基类与派生类关系的详细再探讨

热门文章

  1. android高仿微信表情输入与键盘输入详解-解决跳闪与表情切换问题
  2. 如何锻炼自己的逻辑思维
  3. SAP 采购合同案例教程金额合同前台
  4. 从首届微商博览会看2015年微商的趋势
  5. reboot复位ipcam,为何不可靠?
  6. 大法将致:将Windows Server 2016打造成个人办公系统,WIN2016打造成超级WIN10,WIN2016优化设置
  7. XQ6657Z35-EVM 的DSP + ZYNQ核心板,SRIO通讯
  8. 出现ora-01400错误解决办法
  9. class07:Express框架、中间件
  10. 语音信号短时时域分析