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.

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+ba=1a=2a=3a=4a=5a=6a=7a=8a=9a=10a=n
b=14710131619222528311+3n
b=271217222732374247522+5n
b=3101724313845525966733+7n
b=4132231404958677685944+9n
b=516273849607182931041155+11n
b=6193245587184971101231366+13n
b=72237526782971121271421577+15n
b=825425976931101271441611788+17n
b=9284766851041231421611801999+19n
b=103152739411513615717819922010+21n
…………
b=n1+3n2+5n3+7n4+9n5+11n6+13n7+15n8+17n9+19n10+21nn^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;
}

  

 

转载于:https://www.cnblogs.com/ruruozhenhao/p/7748272.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值