codeforces 870C
2 seconds
256 megabytes
standard input
standard output
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.
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.
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.
1
12
3
2
6
8
1
2
3
1
2
3
-1
-1
-1
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.
说出来你们可能不信。。这题到最后我也没读懂啥意思(composite是合数的意思。。
满脑子想的是筛法,没想到就是考个思维。。。
题意:给一个数,问这个数最多能由多少个合数组成,不能组成就输出-1.
解题思路:因为最小的合数是4,所以如果n正好是4的倍数就是最多的情况。
那么就可以想到考虑n%4的情况。
1.n%4==0 这个时候直接输出n/4即可。
2.n%4==2 这个时候还是直接输出n/4,因为n%4==2相当于(n-6)%4==0,而6也是合数。
3.n%4==1 || n%4==3 那么n自然是个奇数,这个时候我们找个最小的奇合数处理下就行(n-=9;
此时n就是个偶数了,如果n!=0 && n<4 ,那么这个数就没法全由合数构成。
反之,则能构成,ans=1+n/4;
ac代码:
1 #include <cstdio>
2 #include <iostream>
3 using namespace std;
4 int main() {
5 ios::sync_with_stdio(false);
6 cin.tie(0);cout.tie(0);
7 int q,n;
8 cin>>q;
9 while(q--) {
10 cin>>n;
11 int ans=0;
12 if(n%4==0||n%4==2) {
13 ans=n/4;
14 }
15 else if(n%4==1 ||n%4==3) {
16 n-=9;
17 ans=n/4+1;
18 }
19 if(n<4 && n!=0) ans=-1;
20 cout<<ans<<endl;
21 }
22 return 0;
23 }
codeforces 870C的更多相关文章
- Codeforces 870C Maximum splitting (贪心+找规律)
<题目链接> 题目大意: 给定数字n,让你将其分成合数相加的形式,问你最多能够将其分成几个合数相加. 解题分析: 因为要将其分成合数相加的个数最多,所以自然是尽可能地将其分成尽可能小的合数 ...
- 【CodeForces - 870C】
题意: 计算一个整数最多可以拆分为多少个合数(要求拆分的全是合数).例如:12的拆分法案是12=4+4+4. 合数是指除了1之外的非素数(正整数). 思路: 偶数的话用4.6就可以满足,奇数的话减去一 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
随机推荐
- windows下的:开始→运行→命令
开始→运行→命令 集锦 winver---------检查Windows版本wmimgmt.msc----打开windows管理体系结构(WMI)wu ...
- vue中computed/method/watch的区别
摘要:本文通过官方文档结合源码来分析computed/method/watch的区别. Tips:本文分析的源码版本是v2.6.11,文章中牵涉到vue响应式系统原理部分,如果不是很了解,建议先阅读上 ...
- 论super().__init__()的用法
当我们调用 super() 的时候,实际上是实例化了一个 super 类. super 是个类,既不是关键字也不是函数等其他数据结构,该对象就是专门用来访问父类中的属性的(严格按照继承的关系). -- ...
- 从定义到AST及其遍历方式,一文带你搞懂Antlr4
摘要:本文将首先介绍Antlr4 grammer的定义方式,如何通过Antlr4 grammer生成对应的AST,以及Antlr4 的两种AST遍历方式:Visitor方式和Listener方式. 1 ...
- 转 Fiddler1 简单使用
Fiddler1 简单使用 文章转自:https://www.cnblogs.com/zhengna/p/9008014.html 1.Fiddler下载地址:https://www.tele ...
- 基于源码分析Vue的nextTick
摘要:本文通过结合官方文档.源码和其他文章整理后,对Vue的nextTick做深入解析.理解本文最好有浏览器事件循环的基础,建议先阅读上文<事件循环Event loop到底是什么>. 一. ...
- 对于Spring MVC 拦截器的一些了解
Spring MVC 拦截器的执行顺序 应用场景 假设请求 localhost:8080/ 则要求直接重定向到 localhost:8080/login ; 定义拦截器顺序 permission lo ...
- java-数据类型复习
java中共有8种基本的数据类型,分别为 字节型byte(8字节,32位),短整型short(16字节),整型int(32字节),长整型long(64字节), 字符型char(16字节),浮点型flo ...
- Linux 从4.12内核版本开始移除了 tcp_tw_recycle 配置。 tcp_max_tw_buckets TIME-WAIT 稳定值
被抛弃的tcp_recycle_小米云技术-CSDN博客_sysctl: cannot stat /proc/sys/net/ipv4/tcp_tw_recy https://blog.csdn.ne ...
- (转载)微软数据挖掘算法:Microsoft 决策树分析算法(1)
微软数据挖掘算法:Microsoft 目录篇 介绍: Microsoft 决策树算法是分类和回归算法,用于对离散和连续属性进行预测性建模. 对于离散属性,该算法根据数据集中输入列之间的关系进行预测. ...