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. elasticsearch-head 安装

    一.安装phantomjs(由于入坑多写一步,此步骤可省掉) 1.下载phantomjs 安装npm的时候会依赖phantomjs 所以我们先安装phantomjs phantomjs 下载地址:ht ...

  2. 基于JWT实现token验证

    JWT的介绍 Json Web Token(JWT)是目前比较流行的跨域认证解决方案,是一种基于JSON的开发标准,由于数据是可以经过签名加密的,比较安全可靠,一般用于前端和服务器之间传递信息,也可以 ...

  3. python使用while循环实现九九乘法表

    a = 1while a <= 9: b = 1 while b <= a: print("%d*%d=%d\t" % (b, a, a * b), end=" ...

  4. utuntu sever1804显示中文putty可以输入中文

    默认情况下,putty连接ubuntu server以后,哪怕设置的Utf-8的连接,也是无法显示中文的. 应该是ubuntu服务器端,没有字库的问题. 如果在putty显示和输入中文呢,因为配置信息 ...

  5. 软件WEB自动化测试工具之智能元素定位

    江湖一直有着这么一句名言“天下武功,唯快不破".那么在软件测试领域,自然而然我们会想到软件自动化测试.软件自动化测试的实现自然离不开软件自动化测试工具.软件自动化测试工具是软件自动化的载体, ...

  6. vue-cli2.0项目 添加骨架屏

    1.创建项目 npm init webpack project 3.下载   vue-skeleton-webpack-plugin 插件 npm install vue-skeleton-webpa ...

  7. 如何找回微信小程序源码?2020年微信小程序反编译最新教程 小宇子李

    前言:在网上看了找回微信小程序源码很多教程,都没法正常使用.微信版本升级后,会遇到各种报错, 以及无法获取到wxss的问题.查阅各种资料,最终解决,于是贴上完整的微信小程序反编译方案与教程. 本文章仅 ...

  8. C++ 顺序表练习

    #include <stdio.h> #include <stdlib.h> #include <string.h> struct Array //定义一个数据存储 ...

  9. js函数的三种成创建方式以及它们各自的不同

    js有三种创建函数的方式: 1.function语句(也叫函数声明) function sum(a, b) { return a + b; } sum(1, 2); // 3 2. 函数直接量,又叫函 ...

  10. SQLserver用Image格式储存图片

    前言 最近项目更新一个新需求,要求把图片储存在SQLserver中,而不是储存在本地磁盘.很好,又有新东西可以学了. 正文 一.建表 这里大概建几个字段演示一下 CREATE TABLE [dbo]. ...