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. Windows开启关闭测试模式的方法(含开启测试模式失败的解决办法)

    前言:           内含:Windows开启关闭测试模式的方法.开启测试模式失败的解决办法.win10进入bios的方式.BitLocker恢复方式.           对于互联网从业者来说 ...

  2. 4.pytest结合allure-pytest插件生成allure测试报告

    之前我们使用的测试报告插件是pytest-html 这次使用的插件是allure-pytest,更加美观强大 安装插件 pip3 install allure-pytest 安装allure(Mac) ...

  3. 闻道Go语言,6月龄必知必会

    大家好,我是马甲哥, 学习新知识, 我的策略是模仿-->归纳--->举一反三, 在同程倒腾Go语言一年有余,本次记录<闻道Go语言,6月龄必知必会>,形式是同我的主力语言C#做 ...

  4. 题解 P3395 路障

    前言 没想到这是\(\sf {tgD1T1}\)难度-- 题目大意 有一个\(n\times n\) 的棋盘,有\(2n-2\) 个路障,在第\(i\) 秒会在\((x_i,y_i)\) 放置路障.求 ...

  5. [AGC057D] Sum Avoidance

    Link 本篇题解大部分内容来自这篇文章 首先题意翻译: 给定一个正整数 \(S\) ,称一个正整数集合 \(A\) 是好的,当且仅当它满足以下条件: \(A\) 中元素在 \((0,S)\) 之间 ...

  6. VUE学习2

    目录分析 public目录 index.html是起始的html文件 # 这是关键 <div id="app"></div> src目录 main.js是V ...

  7. 「浙江理工大学ACM入队200题系列」问题 J: 零基础学C/C++83——宁宁的奥数路

    本题是浙江理工大学ACM入队200题第八套中的J题 我们先来看一下这题的题面. 题面 题目描述 宁宁参加奥数班,他遇到的第一个问题是这样的:口口口+口口口=口口口,宁宁需要将1~9 九个数分别填进对应 ...

  8. Redis管理及监控工具推荐

    推荐一款Redis管理软件.[官网 http://www.redisant.cn/] 功能描述: 1. 键和字段CRUD和glob. 2. 适用于字符串.列表.散列.集合.有序集合. 通过漂亮的用户界 ...

  9. 第一章:TypeScript快速入门

    一.TypeScript 开发环境搭建 1.TypeScript 是什么? TypeScript 是一种由微软开发的自由和开源的编程语言.它是 JavaScript 的一个超集,而且本质上向这个语言添 ...

  10. Go语言核心36讲31

    我们在前两篇文章中讨论了互斥锁.读写锁以及基于它们的条件变量,先来总结一下. 互斥锁是一个很有用的同步工具,它可以保证每一时刻进入临界区的goroutine只有一个.读写锁对共享资源的写操作和读操作则 ...