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 ...
随机推荐
- vulnhub靶场之EMPIRE
准备: 攻击机:虚拟机kali.本机win10. 靶机:EMPIRE: BREAKOUT,地址我这里设置的桥接,下载地址:https://download.vulnhub.com/empire/02- ...
- 前端框架Vue------>第一天学习、Vue学习的路径、Vue官网(1)
文章目录 1.学习目标 2.前端知识体系 2.1 前端三要素 2.2.MVVM 3.第一个Vue程序 4.Vue实例的生命周期 vue的官方文档:https://cn.vuejs.org/ 1.学习目 ...
- springboot+vue 实现校园二手商城(毕业设计一)
1.功能划分 2.实现的效果 2.1 登录页面 2.2 注册页面 2.3 商城首页 2.4 商品详情 2.5 购物车 2.6 订单 2.7 在线交流 2.8 公告信息 2.9 个人信息 3.后台管理界 ...
- 【UML】统一建模语言
如果是准备学习设计模式的同学,可以只了解类图相关的知识 而如果是在准备软件设计师考试的同学,或许会对你有点帮助 正在施工...... 参考博客:https://blog.csdn.net/unique ...
- 2流高手速成记(之六):从SpringBoot到SpringCloudAlibaba
咱们接上回 2流高手速成记(之五):Springboot整合Shiro实现安全管理 - 14号程序员 - 博客园 (cnblogs.com) 身边常有朋友说:小项目用PHP.大项目用Java(这里绝无 ...
- Transformer 结构分析
self-attetion 1. 输入 \[X = EmbeddingLookup(X) + PositionalEncoding \\ X.shape == (batch\_size, seq\_l ...
- java代码整洁之道
package Day01;import org.junit.Test;import java.text.NumberFormat;import java.util.Scanner;public cl ...
- MFC 学习笔记
MFC 学习笔记 一.MFC编程基础: 概述: 常用头文件: MFC控制台程序: MFC库程序: 规则库可以被各种程序所调用,扩展库只能被MFC程序调用. MFC窗口程序: 示例: MFC库中类的简介 ...
- 部署grafana+telegraf+influxdb 及 配置 jmeter后端监听
搞性能测试,可以搭建Grafana+Telegraf+InfluxDB 监控平台,监控服务器资源使用率.jmeter性能测试结果等. telegraf: 是一个用 Go 编写的代理程序,可收集系统和服 ...
- Azure DevOps Server 设置项目管理用户,用户组
一,引言 Azure DevOps Server 搭建完成后,关于如何进行项目管理,项目成员管理等,我们接着上一篇文章,继续讲解 Azure DevOps Server 的用户,用户组.首先,我们需要 ...