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. Codeforces Round #295 (Div. 2) B. Two Buttons 520B

    B. Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  2. Nuxt简单使用Google/Baidu Analyze

    博客地址: https://www.seyana.life/post/17 具体账号注册方法和绑定方法可以去到官网下,都有相应的指南, 一般设置也比较简单,只需要把对应js代码添加到head中即可, ...

  3. Nacos 数据持久化 mysql8.0

    一.问题描述 直接下载的稳定版本nacos编译后的文件,不支持mysql8及其以上版本,按照官网文档:https://nacos.io/zh-cn/docs/deployment.html 执行完成之 ...

  4. Z字头:逐浪字库入选微软全球主流字体厂商列表

    北京时间2019年6月20日消息: 来自中国的字库厂商--逐浪,成功获得全球软件巨擎.电子出版与数字印刷权威平台-微软的认证,成为获此国际认证的首家字体厂商. 微软公司为了更好的规范国际字库与出版,制 ...

  5. javaee作业

    一.单选题(共5题,50.0分) 1 在SqlSession对象的openSession()方法中,不能作为参数executorType的可选值 的是(     ).   A. ExecutorTyp ...

  6. java批量处理

    最近用到Java批量处理,一次性处理多个文件夹下的多个文件,在此记录一下. 我的思路:首先要保证文件夹和文件夹下的文件的命名是有规律的,利用for循环,每次自增变量,再拼接字符串,从而得到各个文件的路 ...

  7. Posix线程编程指南(4)

    Posix线程编程指南(4) 杨沙洲 原文地址:http://www.ibm.com/developerworks/cn/linux/thread/posix_threadapi/part4/ 线程终 ...

  8. Antd 表格内通过rowClassName实现隔行变色的显示方法(转载)

    ant design中 table组件很方便,在项目中遇到了需要实现奇偶行颜色不同以方便阅读的功能,主要用到了rowClassName这一api,通过判断index的奇偶来实现不同的样式分配. row ...

  9. Data-independent acquisition mass spectrometry in metaproteomics of gut microbiota - implementation and computational analysis DIA技术在肠道宏蛋白质组研究中的方法实现和数据分析 (解读人:闫克强)

    文献名:Data-independent acquisition mass spectrometry in metaproteomics of gut microbiota - implementat ...

  10. 支付宝小程序获取 user_id(openid) ThinkPHP版

    支付宝小程序获取 user_id(openid) ThinkPHP版 近期支付宝小程序个人公测了,就想着玩一下,没想到就获取用户唯一标识都这么麻烦,微信的openid的话Get请求一下就完事了,支付宝 ...