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.

Example

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和本身整除外,还能被其他的数整除的数。"0"“1”既不是质数也不是合数。

合数素数

折叠概念

除了2之外,所有的偶数都是合数。反之,除了2之外,所有的素数都是奇数。但是奇数包括了合数和素数。合数根和素数根的概念就是用来区分任何一个大于9的奇数属于合数还是素数。任何一个奇数都可以表示为2n+1(n是非0的自然数)。我们将n命名为数根。当2n+1属于合数时,我们称之为合数根;反之,当2n+1是素数时,我们称之为素数根。

折叠规律

任何一个奇数,如果它是合数,都可以分解成两个奇数的乘积。设2n+1是一个合数,将它分解成两个奇数2a+1和2b+1的积(其中a、b都属于非0的自然数),则有

2n+1=(2a+1)(2b+1)=4ab+2(a+b)+1=2(2ab+a+b)+1

可见,任何一个合数根都可以表示为"2ab+a+b",反之,不能表示为"2ab+a+b"的数根,就称为素数根。由此可以得到合数根表。判断一个大奇数属于合数还是素数,只需在合数根表中查找是否存在它的数根就知道了。

折叠合数根表

表中第一行表示a的取值,第一列表示b的取值,其余表示2ab+a+b

2ab+a+b a=1 a=2 a=3 a=4 a=5 a=6 a=7 a=8 a=9 a=10 a=n
b=1 4 7 10 13 16 19 22 25 28 31 1+3n
b=2 7 12 17 22 27 32 37 42 47 52 2+5n
b=3 10 17 24 31 38 45 52 59 66 73 3+7n
b=4 13 22 31 40 49 58 67 76 85 94 4+9n
b=5 16 27 38 49 60 71 82 93 104 115 5+11n
b=6 19 32 45 58 71 84 97 110 123 136 6+13n
b=7 22 37 52 67 82 97 112 127 142 157 7+15n
b=8 25 42 59 76 93 110 127 144 161 178 8+17n
b=9 28 47 66 85 104 123 142 161 180 199 9+19n
b=10 31 52 73 94 115 136 157 178 199 220 10+21n
…… ……
b=n 1+3n 2+5n 3+7n 4+9n 5+11n 6+13n 7+15n 8+17n 9+19n 10+21n n^2+2n

折叠意义

通过研究合数根表,对研究素数的规律会有深远的意义。


这道题主要是理解题意,还有几个特判的数1,2,3,5,7,11,

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; int main()
{
int q;
int p;
int i,j;
int ans;
int num;
scanf("%d",&q);
while(q--)
{
scanf("%d",&p);
num = p / 4;
ans = p % 4;
if(ans == 0)
printf("%d\n",num);
else if(ans == 1)
{
if(num > 1)
printf("%d\n",num-1);
else
printf("-1\n");
}
else if(ans == 2)
{
if(num > 0)
printf("%d\n",num);
else
printf("-1\n");
}
else
{
if(num > 2)
printf("%d\n",num-1);
else
printf("-1\n");
}
}
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

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

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

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

  5. 【NOIP训练】【规律+数论】欧拉函数的应用

    Problem 1 [题目大意] 给出 多组数据 ,给出  求出 . 题解 证明:  除了 以为均为偶数, 所以互质的个数成对. 由 得 . 所以对于每对的和为 , 共有 对 . 则 Problem ...

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

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

  7. 【HDOJ6298】Maximum Multiple(数论)

    题意:给定n,求x,y,z三个整数,使得x|n,y|n,z|n,且xyz最小 n<=1e6 思路: 不定方程1/x+1/y+1/z=1 只有(2,3,6)(2,4,4) (3,3,3)三组正整数 ...

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

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

  9. Codeforces Round #651 (Div. 2) A. Maximum GCD(数论)

    题目链接:https://codeforces.com/contest/1370/problem/A 题意 有 $n$ 个数大小分别为 $1$ 到 $n$,找出两个数间最大的 $gcd$ . 题解 若 ...

随机推荐

  1. 自动补齐flexselect+级联下拉框案例

    在开发web应用时,经常遇到类似省市区级联下拉框操作,即选中省份自动级联加载该省份所有的市,选中市自动级联加载该市所有的区:假设省市区的数据量很大,此时用户想选中某市,因而要从上往下查找,可能半天都找 ...

  2. 前端开发之JavaScript HTML DOM实战篇

    实战案例一: “灯泡发光” <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...

  3. Python下载与安装

    Python目前已支持所有主流操作系统,在Linux,Unix,Mac系统上自带Python环境,一般默认装的是Python2版本,Windows系统上没有Pyhton环境,需要我们手动安装一下,过程 ...

  4. Android Studio 无法正确引入包内存在的类

    Android Studio 无法识别同一个 package 里的类,显示为红色,但是 compile 没有问题. 重启,rebuild,clean都没有用. 多半是因为 Android Studio ...

  5. [cogs2638]数列操作ψ(双标记线段树)

    题目大意:给定一个数列a,你需要支持的操作:区间and,区间or,询问区间最大值 解题关键: 1.双标记线段树,注意优先级(超时) 当涉及多重标记时,定义出标记的优先级,修改操作时用优先级高(先下放) ...

  6. Linux cheat命令

    一.简介 cheat是在GNU通用公共许可证下,为Linux命令行用户发行的交互式备忘单应用程序.它提供显示Linux命令使用案例,包括该命令所有的选项和简短但尚可理解的功能. 二.安装配置 安装步骤 ...

  7. Spring.net方法的替换

    .为什么有时候你再执行某个方法的时候比如某个操作 a.权限验证 b.任务执行 当我执行到这个方法的时候,我可以先验证权限,如果验证不通过则替换到另一个方法去执行 public class MyValu ...

  8. javascript和jquery比较

    <h1>我的第一段 JavaScript</h1> <p>请输入数字.如果输入值不是数字,浏览器会弹出提示框.</p> <input id=&qu ...

  9. 【转】C++中#if #ifdef 的作用

    一般情况下,源程序中所有的行都参加编译.但是有时希望对其中一部分内容只在满足一定条件才进行编译,也就是对一部分内容指定编译的条件,这就是“条件 编译”.有时,希望当满足某条件时对一组语句进行编译,而当 ...

  10. javascript总结41:表格全选反选,经典案例详解

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...