Maximum splitting
Maximum splitting
You are given several queries. In the i-th query you are given a
single positive integer ni. You are to represent ni as a sum of
maximum possible number of composite summands and print this maximum
number, or print -1, if there are no such splittings.An integer greater than 1 is composite, if it is not prime, i.e. if it
has positive divisors not equal to 1 and the integer itself.Input The first line contains single integer q (1 ≤ q ≤ 105) — the
number of queries.q lines follow. The (i + 1)-th line contains single integer ni
(1 ≤ ni ≤ 109) — the i-th query.Output For each query print the maximum possible number of summands in
a valid splitting to composite summands, or -1, if there are no such
splittings.
Examples
Input
1
12
Output
3
Input
2
6
8
Output
1
2
Input
3
1
2
3
Output
-1
-1
-1
Note 12 = 4 + 4 + 4 = 4 + 8 = 6 + 6 = 12, but the first splitting has
the maximum possible number of summands.8 = 4 + 4, 6 can’t be split into several composite summands.
1, 2, 3 are less than any composite number, so they do not have valid
splittings.
思路如下
- 题意:这一题让判断一个数 最多能够被分成几个
合数
相加,能被分成几个答案就是 输出 几 ,但是如果某个数没法被分成几个合数相加 那么直接输出 - 1 - 既然是让求最多可以被分成几个合数之和,那么我们考虑,每一份分的越小越好,而最小的合数是
4
,那么我们先分尽量多的4,最后对 余数 进行一一讨论即可
题解如下
#include <bits/stdc++.h>
using namespace std;
int solve(int x)
{
int t = x/4; //最多分成t个4
int rest = x%4; //求出剩余部分
if (rest==0) //刚好整除4 t就是最大值返回
return t;
if (rest==1) //余1 咋办 取出一个4 发现4+1=5 不是合数 那就取出两个 4*2+1=9 是合数 那么就9了
{
if (t>=2)
{
t-=2;
}else
return -1;
t++;
return t;
}
if (rest==2) //同理 余2 取出一个4 发现4+2=6是合数 那就取出一个就行
{
if (t>=1)
{
t--;
}else return -1;
t++;
return t;
}
if (rest==3) //同理 取出3个4 3*4+3=15是合数,这里切记 15 还可以 分成两个合数 6与9.因此 在下面的代码中 t += 2;
{
if (t>=3)
{
t-=3;
}else return -1;
t+=2;
return t; //返回t
}
}
int main()
{
int q;
scanf("%d",&q);
for (int i = 1;i <= q;i++)
{
int x;
scanf("%d",&x);
printf("%d\n",solve(x));
}
return 0;
}
Maximum splitting的更多相关文章
- Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2) C. Maximum splitting
地址: 题目: C. Maximum splitting time limit per test 2 seconds memory limit per test 256 megabytes input ...
- codeforces Round #440 C Maximum splitting【数学/素数与合数/思维/贪心】
C. Maximum splitting time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Maximum splitting(规律,数论)
You are given several queries. In the i-th query you are given a single positive integer ni. You are ...
- Codeforces 870C Maximum splitting (贪心+找规律)
<题目链接> 题目大意: 给定数字n,让你将其分成合数相加的形式,问你最多能够将其分成几个合数相加. 解题分析: 因为要将其分成合数相加的个数最多,所以自然是尽可能地将其分成尽可能小的合数 ...
- Codeforces 872C Maximum splitting:数学【分解成合数之和】
题目链接:http://codeforces.com/contest/872/problem/C 题意: 给你一个数n,问你最多能将n分解成多少个合数之和.(若不能分解,输出-1) 题解: 若要让合数 ...
- 【Codeforces Round #440 (Div. 2) C】 Maximum splitting
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 肯定用尽量多的4最好. 然后对4取模的结果 为0,1,2,3分类讨论即可 [代码] #include <bits/stdc++ ...
- Codeforces Round #440 (Div. 2) A,B,C
A. Search for Pretty Integers time limit per test 1 second memory limit per test 256 megabytes input ...
- Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2)
A. Search for Pretty Integers 题目链接:http://codeforces.com/contest/872/problem/A 题目意思:题目很简单,找到一个数,组成这个 ...
- Codeforces Round #440 (Div. 2)【A、B、C、E】
Codeforces Round #440 (Div. 2) codeforces 870 A. Search for Pretty Integers(水题) 题意:给两个数组,求一个最小的数包含两个 ...
随机推荐
- python笔记26
一.今日内容 python中的方法 python中的方法+正则表达式的新内容 #分组 #分组命名 #引用分组 #爬虫的小例子 1.转义符 如:\n--->\\n--->print('\\n ...
- 负载均衡框架 ribbon 二
Ribbon 负载均衡机制 官方文档地址:https://github.com/Netflix/ribbon/wiki/Working-with-load-balancers 1. Ribbon 内置 ...
- RStudio终端操作
转于:https://support.rstudio.com/hc/en-us/articles/115010737148-Using-the-RStudio-Terminal#send 原文是英文版 ...
- Yaml文件,超详细讲解
YAML文件简单介绍 YAML 是一种可读性非常高,与程序语言数据结构非常接近.同时具备丰富的表达能力和可扩展性,并且易于使用的数据标记语言. YAML全称其实是"YAML Ain't a ...
- activiti工作流解决历史批注中文乱码
/** * 根据流程实例查询流程的批注信息 * * @param processInstanceId * @return */ private List<Comment> findComm ...
- $api 回调函数then应用
getReceiveListAPI (param) { return new Promise(resolve => { let params = { // idCard: this.idCard ...
- 2016 Multi-University Training Contest 4 T9
http://acm.hdu.edu.cn/showproblem.php?pid=5772 最大权闭合子图. 得到价值w[i][j]的条件是选了i,j这两个位置的字符.选择位置的i字符花费为 第一次 ...
- EF Core-1
带着问题去思考,大家好! 前几天了解到EF Core的开发模式:DB First(数据库优先),Model First(模式优先),Code First(代码优先). 我所接触的大多是DB First ...
- Linux服务器(Centos)上安装jexus
哈子是Jexus Jexus是一款Linux平台上的高性能WEB服务器和负载均衡网关,Jexus Web Service,简称JWS,以支持ASP.NET.ASP.NET CORE.PHP为特色, 同 ...
- iOS开发:十六进制颜色转UIColor
Objective-C UIColor * __nullable UIColorFromHexValue(NSUInteger hexValue) { CGFloat red = (hexValue ...