A. Greatest Convex【Codeforces Round #842 (Div. 2)】
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\)
思路
- 由于\(k\)的范围是\((2≤k≤10^9)\),因此不能直接枚举求阶乘
- 观察example的input和output数据的特性我们可以猜测总是存在最大的\(x\)使得\(x = k - 1\)满足条件
- 当\(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;
}
}
解题历程
- 错误方向浪费大量时间:多种方式求阶乘,分解质因数,二分搜索
- Runtime error on pretest 2(218 ms,262100 KB) 【00:19】//阶乘
- Wrong answer on pretest 1(0 ms,3900 KB) 【00:45】 //分解质因数
- Compilation error(0 ms,0 KB) 【00:55】 //看出规律,蒙出答案k-1
- AC(46 ms,0 KB) 【00:57】
经验总结
- 仔细思考数据范围与关系式的关系
- 签到题就会有签到题的样子:代码量小,如果代码量大了一个认真分析是否方向错误
- 可以看排名中的ac时间确定题目的难易
- 注意对已知关系式的进一步解析
- Codeforces:†, ‡, §, ¶分别代表1,2,3,4,一般是对题目信息的补充
原因
- 不熟悉codeforces
- 手疏
A. Greatest Convex【Codeforces Round #842 (Div. 2)】的更多相关文章
- 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\). ...
- 【Codeforces Round #406 (Div. 2)】题解
The Monster 签到题,算一下b+=a和d+=c,然后卡一下次数就可以了. Not Afraid 只要一组出现一对相反数就是安全的. Berzerk 题意:[1,n],两个人轮流走,谁能走到1 ...
- 【Codeforces Round#279 Div.2】B. Queue
这题看别人的.就是那么诚实.http://www.cnblogs.com/zhyfzy/p/4117481.html B. Queue During the lunch break all n Ber ...
- 【Codeforces Round #405 ( Div 2)】题解
Bear and Big Brother 签到题,直接模拟就可以了. Bear and Friendship Condition 满足只能是每个朋友圈中每个人和其他人都是朋友,这样的边数的确定的. 然 ...
- 【Codeforces Round #404 (Div. 2)】题解
A. Anton and Polyhedrons 直接统计+答案就可以了. #include<cstdio> #include<cstring> #include<alg ...
- 【Codeforces Round #518 (Div. 2)】
A:https://www.cnblogs.com/myx12345/p/9847588.html B:https://www.cnblogs.com/myx12345/p/9847590.html ...
- 【Codeforces Round #506 (Div. 3) 】
A:https://www.cnblogs.com/myx12345/p/9844334.html B:https://www.cnblogs.com/myx12345/p/9844368.html ...
- 【Codeforces Round #503 (Div. 2)】
A:https://www.cnblogs.com/myx12345/p/9843198.html B:https://www.cnblogs.com/myx12345/p/9843245.html ...
- 【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 ...
随机推荐
- 一天五道Java面试题----第八天(怎么处理慢查询--------->简述Myisam和innodb的区别)
这里是参考B站上的大佬做的面试题笔记.大家也可以去看视频讲解!!! 文章目录 1.怎么处理慢查询 2.ACID靠什么保证的 3.什么是MVCC 4.mysql主从同步原理 5.简述Myisam和inn ...
- docker常用配置以及命令
1. Docker基本概念 1.1 什么是 docker hub DockHub是一个仓库 https://hub.docker.com/ 仓库是集中存放镜像文件的场所 仓库分为公开仓库(Public ...
- 不一样的纯H5C3动画爱心
最近抖音很火的让你会计算机的朋友给你做个爱心突然火了,我也不出意外的收到了朋友的邀请,自己做肯定太麻烦了于是乎百度第一步,惊呆了!网上全都是一个爱心,变着法的火焰爱心,换汤不换药,那我们肯定是要整点不 ...
- networkQuality
基本使用 networkQuality 是一个命令行工具,需要使用「终端」App(或者你首选的其他终端模拟器)运行.方法是: 首先,点按「程序坞」(Dock)中的「启动台」(LaunchPad)图标, ...
- XTDrone和PX4学习期间问题记录(一)
XTDrone和PX4学习期间问题记录(一) Written By PiscesAlpaca 前言: 出现问题可以去官方网站http://ceres-solver.org/index.html查看文档 ...
- Seata 1.5.2 源码学习(事务执行)
关于全局事务的执行,虽然之前的文章中也有所涉及,但不够细致,今天再深入的看一下事务的整个执行过程是怎样的. 1. TransactionManager io.seata.core.model.Tran ...
- JDK动态代理深入剖析
1 基于接口的代理模式 什么是代理? 简单来说,代理是指一个对象代替另一个对象去做某些事情. 例如,对于每个程序员来说,他都有编程的能力: interface Programmable { void ...
- 基于.NetCore开发博客项目 StarBlog - (20) 图片显示优化
前言 我的服务器带宽比较高,博客部署在上面访问的时候几乎没感觉有加载延迟,就没做图片这块的优化,不过最近有小伙伴说博客的图片加载比较慢,那就来把图片优化完善一下吧~ 目前有两个地方需要完善 图片瀑布流 ...
- Linux下用rm误删除文件的三种恢复方法
Linux下用rm误删除文件的三种恢复方法 对于rm,很多人都有惨痛的教训.我也遇到一次,一下午写的程序就被rm掉了,幸好只是一个文件,第二天很快又重新写了一遍.但是很多人可能就不像我这么幸运了.本文 ...
- 爬虫之xpath插件下载与安装
目录 简介: 下载xpath文件 打开chrome浏览器 点击右上角小圆点 更多工具.阔展程序 拖拽xpath插件放到阔展程序 如果失效,再次拖拽 关闭浏览器重新打开 按ctrl+shift+x 出现 ...