A. Greatest Convex

You are given an integer \(k\). Find the largest integer \(x\), where \(1≤x<k\), such that \(x!+(x−1)!\)† is a multiple of ‡ \(k\), or determine that no such \(x\) exists.

† \(y!\) denotes the factorial of \(y\), which is defined recursively as \(y!=y⋅(y−1)!\) for \(y≥1\) with the base case of \(0!=1\). For example, \(5!=5⋅4⋅3⋅2⋅1⋅0!=120\).

‡ If \(a\) and \(b\) are integers, then \(a\) is \(a\) multiple of \(b\) if there exists an integer \(c\) such that \(a=b⋅c\). For example, \(10\) is a multiple of \(5\) but \(9\) is not a multiple of \(6\).

Input

The first line contains a single integer \(t\) \((1≤t≤10^4)\) — the number of test cases. The description of test cases follows.

The only line of each test case contains a single integer \(k\) \((2≤k≤10^9)\).

Output

For each test case output a single integer — the largest possible integer \(x\) that satisfies the conditions above.

If no such \(x\) exists, output \(−1\).

Example

input

4

3

6

8

10

output

2

5

7

9

Note

In the first test case, \(2!+1!=2+1=3\), which is a multiple of \(3\).

In the third test case, \(7!+6!=5040+720=5760\), which is a multiple of \(8\).

原题链接

简述题意

给出\(t\)个\(k\),找出是否存在一个\(x\)满足\(1≤x<k\)且\(x!+(x−1)!\) % \(k==0\),输出\(x\)的最大值,否则输出\(-1\)

思路

  1. 由于\(k\)的范围是\((2≤k≤10^9)\),因此不能直接枚举求阶乘
  2. 观察exampleinputoutput数据的特性我们可以猜测总是存在最大的\(x\)使得\(x = k - 1\)满足条件
  3. 当\(x = k - 1\)时\(x! + (x − 1)! = (x + 1) * (x - 1)! = k * (k - 2)!\)总是满足是k的倍数

代码

点击查看代码
#include<iostream>
using namespace std;
int k,t; int main(){
cin >> t;
while(t -- ){
cin >> k;
cout << k - 1 << endl;
}
}

解题历程

  1. 错误方向浪费大量时间:多种方式求阶乘,分解质因数,二分搜索
  2. Runtime error on pretest 2(218 ms,262100 KB) 【00:19】//阶乘
  3. Wrong answer on pretest 1(0 ms,3900 KB) 【00:45】 //分解质因数
  4. Compilation error(0 ms,0 KB) 【00:55】 //看出规律,蒙出答案k-1
  5. AC(46 ms,0 KB) 【00:57】

经验总结

  1. 仔细思考数据范围与关系式的关系
  2. 签到题就会有签到题的样子:代码量小,如果代码量大了一个认真分析是否方向错误
  3. 可以看排名中的ac时间确定题目的难易
  4. 注意对已知关系式的进一步解析
  5. Codeforces:†, ‡, §, ¶分别代表1,2,3,4,一般是对题目信息的补充

原因

  1. 不熟悉codeforces
  2. 手疏

A. Greatest Convex【Codeforces Round #842 (Div. 2)】的更多相关文章

  1. B. Quick Sort【Codeforces Round #842 (Div. 2)】

    B. Quick Sort You are given a permutation[排列]† \(p\) of length \(n\) and a positive integer \(k≤n\). ...

  2. 【Codeforces Round #406 (Div. 2)】题解

    The Monster 签到题,算一下b+=a和d+=c,然后卡一下次数就可以了. Not Afraid 只要一组出现一对相反数就是安全的. Berzerk 题意:[1,n],两个人轮流走,谁能走到1 ...

  3. 【Codeforces Round#279 Div.2】B. Queue

    这题看别人的.就是那么诚实.http://www.cnblogs.com/zhyfzy/p/4117481.html B. Queue During the lunch break all n Ber ...

  4. 【Codeforces Round #405 ( Div 2)】题解

    Bear and Big Brother 签到题,直接模拟就可以了. Bear and Friendship Condition 满足只能是每个朋友圈中每个人和其他人都是朋友,这样的边数的确定的. 然 ...

  5. 【Codeforces Round #404 (Div. 2)】题解

    A. Anton and Polyhedrons 直接统计+答案就可以了. #include<cstdio> #include<cstring> #include<alg ...

  6. 【Codeforces Round #518 (Div. 2)】

    A:https://www.cnblogs.com/myx12345/p/9847588.html B:https://www.cnblogs.com/myx12345/p/9847590.html ...

  7. 【Codeforces Round #506 (Div. 3) 】

    A:https://www.cnblogs.com/myx12345/p/9844334.html B:https://www.cnblogs.com/myx12345/p/9844368.html ...

  8. 【Codeforces Round #503 (Div. 2)】

    A:https://www.cnblogs.com/myx12345/p/9843198.html B:https://www.cnblogs.com/myx12345/p/9843245.html ...

  9. 【Codeforces Round #411 (Div. 1)】Codeforces 804C Ice cream coloring (DFS)

    传送门 分析 这道题做了好长时间,题意就很难理解. 我们注意到这句话Vertices which have the i-th (1 ≤ i ≤ m) type of ice cream form a ...

随机推荐

  1. react.js+easyui 做一个简单的商品表

    效果图:     import React from 'react'; import { Form, FormField, Layout,DataList,LayoutPanel,Panel, Lab ...

  2. 作用域通信对象:session用户在登录时通过`void setAttribute(String name,Object value)`方法设置用户名和密码。点击登录按钮后,跳转到另外一个页面显示用户

    作用域通信对象:session session对象基于会话,不同用户拥有不同的会话.同一个用户共享session对象的所有属性.作用域开始客户连接到应用程序的某个页面,结束与服务器断开连接.sessi ...

  3. web3.0、比特币、区块链、元宇宙,以及那些待收割的韭菜们!

    前几天看到周星驰在社交账号上招聘web3.0的人才,感觉有必要说说web3.0,当然不是基于技术层面,而是从另一个维度说说web3.0以及其它相关的概念,从而做到如何反欺诈,如何避免被资本割韭菜.想到 ...

  4. python不确定性计算之模糊动态聚类实验

    模糊动态聚类实验 本实验所采用的模糊聚类分析方法是基于模糊关系上的模糊聚类法,也称为系统聚类分析法,可分为三步: 第一步:数据标准化,建立模糊矩阵 第二步:建立模糊相似矩阵 第三步:聚类 本程序读取E ...

  5. 网络安全(一):信息收集之玩转nmap(理论篇)

    更新时间 2022年09月06日16:20:10 完成nmap介绍,目标选择,主机发现部分 2022年10月28日21:19:20 完成最基本的内容,端口扫描,版本和系统探测,安全其他等 打算的更新计 ...

  6. Spring Boot:自定义 Whitelabel 错误页面

    一.概述在本文中,我们将研究如何禁用和自定义 Spring Boot 应用程序的默认错误页面,因为正确的错误处理描述了专业性和质量工作. 2.禁用白标错误页面 首先,让我们看看如何通过将server. ...

  7. 看了同事这10个IDEA神级插件,我也悄悄安装了

    昨天,有读者私信发我一篇文章,说里面提到的 Intellij IDEA 插件真心不错,基本上可以一站式开发了,希望能分享给更多的小伙伴,我在本地装了体验了一下,觉得确实值得推荐,希望小伙伴们有时间也可 ...

  8. Django系列---理论一

    教程:http://c.biancheng.net/django/ 特点 集成 ORM 组件:Django 的 Model 层自带数据库 ORM 组件,为操作不同类型的数据库提供了统一的方式. URL ...

  9. 修改linux系统时间

    在Linux系统中,可以用date命令来显示或设定系统的日期与时间 1. 查看系统时间 [root@iZ2ze0gm3scdypc0i15r8yZ ~]# date Tue Aug 16 00:10: ...

  10. 版本控制工具Git介绍-01

    使用版本控制工具是为了方便团队开发,比如多人共同维护一个项目的时候,用版本控制工具可以很方便的维护项目代码,如果哪天你改了一个版本,出问题了,我们也可以很快的找到你改了什么,这里介绍使用比较多的版本控 ...