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(水题) 题意:给两个数组,求一个最小的数包含两个 ...
随机推荐
- 有关js的date的相关知识
最近做项目,用了new Date().getTime()获取本地时间,但是如果用户篡改了手机时间,程序漏洞明显暴露.所以如果为保证程序的稳健安全性,应该是要使用网络时间的,也就是服务器时间.原理就是使 ...
- C# BASS音频库 + 频谱基本用法
效果图: 使用了 BASS.dll. BASS.NET.dll 和 PeakMeterCtrl.dll 前面两个负责播放 最后一个负责绘制频谱,本文重点讲的是频谱部分,播放音频部分注意一点 ...
- css 超过标签定义的宽度后显示----
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- touch事件中的touches、targetTouches和changedTouches
touches: 当前屏幕上所有触摸点的列表; targetTouches: 当前对象上所有触摸点的列表; changedTouches: 涉及当前(引发)事件的触摸点的列表; 通过一个例子来区分一下 ...
- 当AI遇上K8S:使用Rancher安装机器学习必备工具JupyterHub
Jupyter Notebook是用于科学数据分析的利器,JupyterHub可以在服务器环境下为多个用户托管Jupyter运行环境.本文将详细介绍如何使用Rancher安装JupyterHub来为数 ...
- 关于Resouces.resx 在WPF中{x:Static}不显示内容只显示字段的问题解决办法
问题现象:<object property="{x:Static prefix:typeName.staticMemberName}" .../> 界面中只显示资源引用 ...
- 多线程的CAS
CAS Compare And Swap (Compare And Exchange) / 自旋 / 自旋锁 / 无锁 独占锁:独占锁是一种悲观锁,synchronized就是一种独占锁,会导致其它所 ...
- 大数据软件安装之Hadoop(Apache)(数据存储及计算)
大数据软件安装之Hadoop(Apache)(数据存储及计算) 一.生产环境准备 1.修改主机名 vim /etc/sysconfig/network 2.修改静态ip vim /etc/udev/r ...
- Journal of Proteomics Research | 自动的、可重复的免疫多肽数据分析流程MHCquant
题目:MHCquant: Automated and reproducible data analysis for immunopeptidomics 期刊:Journal of Proteome R ...
- if-else代码优化的八种方案
前言 代码中如果if-else比较多,阅读起来比较困难,维护起来也比较困难,很容易出bug,接下来,本文将介绍优化if-else代码的八种方案. 优化方案一:提前return,去除不必要的else 如 ...