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的更多相关文章

  1. 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 ...

  2. codeforces Round #440 C Maximum splitting【数学/素数与合数/思维/贪心】

    C. Maximum splitting time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  3. Maximum splitting(规律,数论)

    You are given several queries. In the i-th query you are given a single positive integer ni. You are ...

  4. Codeforces 870C Maximum splitting (贪心+找规律)

    <题目链接> 题目大意: 给定数字n,让你将其分成合数相加的形式,问你最多能够将其分成几个合数相加. 解题分析: 因为要将其分成合数相加的个数最多,所以自然是尽可能地将其分成尽可能小的合数 ...

  5. Codeforces 872C Maximum splitting:数学【分解成合数之和】

    题目链接:http://codeforces.com/contest/872/problem/C 题意: 给你一个数n,问你最多能将n分解成多少个合数之和.(若不能分解,输出-1) 题解: 若要让合数 ...

  6. 【Codeforces Round #440 (Div. 2) C】 Maximum splitting

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 肯定用尽量多的4最好. 然后对4取模的结果 为0,1,2,3分类讨论即可 [代码] #include <bits/stdc++ ...

  7. 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 ...

  8. Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2)

    A. Search for Pretty Integers 题目链接:http://codeforces.com/contest/872/problem/A 题目意思:题目很简单,找到一个数,组成这个 ...

  9. Codeforces Round #440 (Div. 2)【A、B、C、E】

    Codeforces Round #440 (Div. 2) codeforces 870 A. Search for Pretty Integers(水题) 题意:给两个数组,求一个最小的数包含两个 ...

随机推荐

  1. 峰哥说技术: 05-Spring Boot条件注解注解

    Spring Boot深度课程系列 峰哥说技术—2020庚子年重磅推出.战胜病毒.我们在行动 05 峰哥说技术  Spring Boot条件注解 @EnableAutoConfiguration开启自 ...

  2. java线程并发工具类

    本次内容主要讲Fork-Join.CountDownLatch.CyclicBarrier以及Callable.Future和FutureTask,最后再手写一个自己的FutureTask,绝对干货满 ...

  3. 添加谷歌拓展程序 vue.js devtools过程中的问题

    在用vue做项目过程中,需要用到vue.js devtools,在从github上面clone下来代码,然后再npm install ,过程报错,然后更新npm包也是会有问题,以下是install的问 ...

  4. python初学者必看学习路线图!!!

    python应该是近几年比较火的语言之一,很多人刚学python不知道该如何学习,尤其是没有编程基础想要从事程序员工作的小白,想必应该都会有此疑惑,包括我刚学python的时候也是通过从网上查找相关资 ...

  5. plsql乱码问题

    1,问题:在plsql 中执行sql语句,查询结果带有中文,出现乱码,即" ??? ":如下: 2,解决: 1)输入sql语句 select * from V$NLS_PARAME ...

  6. http2 技术整理 nginx 搭建 http2 wireshark 抓包分析 server push 服务端推送

    使用 nginx 搭建一个 http2 的站点,准备所需: 1,域名 .com .net 均可(国内域名需要 icp 备案) 2,云主机一个,可以自由的安装配置软件的服务器 3,https 证书 ht ...

  7. 音频相关 ALSA ffmpeg ffplay 命令用法 g7xx

    采样率: samples 441100 每秒 DAC/ADC 采样的频率,声卡一般还支持 48k 8k 等模式. 通道:channels 2声道 左右声道 也有单声道的声音,5.1 声道 位数: 16 ...

  8. c++第一周测验

    本次得分为:14.00/14.00, 本次测试的提交时间为:2020-03-08, 如果你认为本次测试成绩不理想,你可以选择再做一次. 1 单选(1分) 下面程序片段哪个没错? 得分/总分 A. in ...

  9. windows下安装spark-python

    首先需要安装Java 下载安装并配置Spark 从官方网站Download Apache Spark™下载相应版本的spark,因为spark是基于hadoop的,需要下载对应版本的hadoop才行, ...

  10. WPF学习概述

    引言 在桌面开发领域,虽然在某些领域,基于electron的跨平台方案能够为我们带来某些便利,但是由于WPF技术能够更好的运用Direct3D带来的性能提升.以及海量Windows操作系统和硬件资源的 ...