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. day52-正则表达式03

    正则表达式03 5.6正则表达式三个常用类 java.util.regex 包主要包括以下三个类:Pattern类.Matcher类和PatternSyntaxException类 Pattern类 ...

  2. 10.pygame-碰撞检测

    添加并监听英雄发射子弹事件 class Hero(GameSprite): def __init__(self): # 调用父类方法,设置image super().__init__('./image ...

  3. CSS 渐变锯齿消失术

    在 CSS 中,渐变(Gradient)可谓是最为强大的一个属性之一. 但是,经常有同学在使用渐变的过程中会遇到渐变图形产生的锯齿问题. 何为渐变锯齿? 那么,什么是渐变图形产生的锯齿呢? 简单的一个 ...

  4. python的一些运算符

    # 1.算术运算符 print('1.算术运算符') # 1.1 + 求和 a = 10 b = 20 c = a + b print(c) print('a+b={}'.format(c)) pri ...

  5. ARM TrustZone白皮书部分阅读

    嵌入式系统安全的一些解决方法及缺陷 外部硬件安全模块:在主SoC之外包含一个专用的硬件安全模块或可信元件,e.g. 手机的SIM卡.隔离仅限于可以从非易失性存储器运行的相对静态程序 内部硬件安全模块: ...

  6. CF815D

    模拟赛遇到的题目. 看各位大佬的做法都不是很懂,于是自己一通乱搞搞出来了. 题意 翻译清楚的不能再清楚了 做法 为了方便叙述,我们将每个给出的三元组表示成 \((a_i,b_i,c_i)\),所选的三 ...

  7. [leetcode] 706. Design HashMap

    题目 Design a HashMap without using any built-in hash table libraries. Implement the MyHashMap class: ...

  8. 嵌入式-C语言基础:指针偏移打印数组

    在C语言中,数组名代表数组中首元素的地址,所以,下面两句获取数组的首地址是等价的: #include<stdio.h> int main() { int a[5]={1,2,3,4,5}; ...

  9. 2022春每日一题:Day 7

    题目:Fire 先预处理出每个F蔓延的时间,再bfs走迷宫. 代码: #include <cstdio> #include <cstdlib> #include <cst ...

  10. 利用KubeEdge在A500部署边缘推理任务

      利用KubeEdge在A500部署边缘推理任务 目  录 1 环境介绍... 1 2 云端环境部署... 2 2.1 在master节点安装Docker和k8S (ubuntu) 2 2.1.1 ...