Codeforces Round #440 C - Maximum splitting

23 篇文章 0 订阅

原题连接:http://codeforces.com/contest/872/problem/C

题目大意:q个询问,每个询问给一个群,问你这个数最多分成多少个合数和

思路:写几组数据观察发现,4,6,9是能够构成所有足够大的数的最小合数,所以我们的拆分,就是由它们构成,毕竟能4就不6,能6就不9。然后就是对4取余判断余数,余1就拿出两个4和1凑成9,余2就拿出一个4和2凑成6.

 还有另一种做法,先直接 ans = n/4, 只有n的成分含有9(为奇数)的时候,9/4=2,可以看到多算了一个

所以奇数的时候就减一好了,6的时候不用管,6/4=1,如果两个6就是3个4了,所以结果就是 ans = n/4-n%2

AC代码(做法一):

#include<cstdio>
#include<cmath>
using namespace std;
const int MAXN = 1e5 + 5;
const int MOD = 1e9 + 7;

int main() {
	int q;
	scanf("%d", &q);
	while (q--) {
		int n;
		scanf("%d", &n);
		if (n % 4 == 0) {
			printf("%d\n", n / 4);
		}
		else if (n % 4 == 1) {
			if (n >= 9) {
				printf("%d\n", (n - 9) / 4 + 1);
			}
			else {
				printf("%d\n", -1);
			}
		}
		else if (n % 4 == 2) {
			if (n >= 6) {
				printf("%d\n", (n - 6) / 4 + 1);
			}
			else {
				printf("-1\n");
			}
		}
		else {
			if (n >= 15) {
				printf("%d\n", (n - 15) / 4 + 2);
			}
			else {
				printf("-1\n");
			}
		}
	}

	return 0;
}

AC代码(做法二): 短了很多。。

#include<cstdio>
#include<cmath>
using namespace std;
const int MAXN = 1e5 + 5;
const int MOD = 1e9 + 7;

int main() {
	int q;
	scanf("%d", &q);
	while (q--) {
		int n;
		scanf("%d", &n);
		if (n == 1 || n == 2 || n == 3 || n == 5 || n == 7 || n == 11) {
			printf("%d\n", -1);
		}
		else {
			printf("%d\n", n / 4 - n % 2);
		}
	}
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值