【洛谷P3131】 【USACO16JAN】子共七
P3131 [USACO16JAN]子共七Subsequences Summing to Sevens
题目描述
Farmer John's cows are standing in a row, as they have a tendency to do from time to time. Each cow is labeled with a distinct integer ID number so FJ can tell them apart. FJ would like to take a
photo of a contiguous group of cows but, due to a traumatic childhood incident involving the numbers, he only wants to take a picture of a group of cows if their IDs add up to a multiple
of 7.
Please help FJ determine the size of the largest group he can photograph.
给你n个数,求一个最长的区间,使得区间和能被7整除
输入输出格式
输入格式:
The first line of input contains (
). The next
lines each contain the integer IDs of the cows (all are in the range
).
输出格式:
Please output the number of cows in the largest consecutive group whose IDs sum
to a multiple of 7. If no such group exists, output 0.
输入输出样例
7
3
5
1
6
2
14
10
5
说明
In this example, 5+1+6+2+14 = 28.
这个题我看了一些题解的代码,发现
自己的代码真是太棒了!
不要问我为什么都用了longlong,数组开的那么大,因为我下面会解释的
我第一次交了80分,然后十分自信地认为开小数组或者没用longlong,然后改了,然后就过了
写一写题解吧,首先是进制的转化,这里用的是很常规的取摸(不懂得童鞋可以自行百度进制转换方法,这种方法很类似于短除法)。
先记录一个前缀和。两个前缀和mod7同余,则这两个前缀和的差值一定被7整除。
这里采取记余数,b[i]表示余数为i的前缀的最小下标
好了看程序吧,正确性应该是显然的
不懂得私信评论或Q:568251782均可
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring> const int MAXN = 50000 + 10; long long sum[MAXN],n;
long long b[7];
long long ans; int main()
{
scanf("%d", &n);
for(long long i = 1;i <= n;i++)
{
int num;
scanf("%d", &num);
sum[i] = sum[i-1] + num;
}
for(int i = 1;i <= n;i++)
{
long long a;
a = sum[i] % 7;
if(a == 0)
{
ans = i;
}
else if(b[a])
{
if(ans < i - b[a])
{
ans = i - b[a];
}
}
else
{
b[a] = i;
}
}
printf("%d", ans);
return 0;
}
【洛谷P3131】 【USACO16JAN】子共七的更多相关文章
- 洛谷 P3131 [USACO16JAN]子共七Subsequences Summing to Sevens
P3131 [USACO16JAN]子共七Subsequences Summing to Sevens 题目描述 Farmer John's NN cows are standing in a row ...
- [USACO16JAN]子共七Subsequences Summing to Sevens
[USACO16JAN]子共七Subsequences Summing to Sevensa[i]表示前缀和如果a[i]%7==t&&a[j]%7==t那么a[j]-a[i-1]一定是 ...
- 洛谷 P3131 子共七
看到这一题第一印象就是暴力好打,$O(n^2)$,预计得分$70$分 这明显满足不了啊,我们要用到前缀和. $sum[i]$记录到i的前缀和,区间$[a,b]$的和就是$sum[b]-sum[a-1] ...
- BZOJ 4511 洛谷3131 USACO 16.Jan 七子共
用sum[i]表示前缀和模7的值,若存在i≤j,满足sum[i]==sum[j],则区间(i,j]的和为7的倍数. O(N)扫出sum[0]~sum[6]第一次出现的位置first和最后一次出现的次数 ...
- [洛谷 P3788] 幽幽子吃西瓜
妖梦费了好大的劲为幽幽子准备了一个大西瓜,甚至和兔子铃仙打了一架.现在妖梦闲来无事,就蹲在一旁看幽幽子吃西瓜.西瓜可以看作一个标准的球体,瓜皮是绿色的,瓜瓤是红色的,瓜皮的厚度可视为0.妖梦恰好以正视 ...
- 洛谷 P3133 [USACO16JAN]无线电联系Radio Contact
P3133 [USACO16JAN]无线电联系Radio Contact 题目描述 Farmer John has lost his favorite cow bell, and Bessie the ...
- [Luogu] 子共七
https://www.luogu.org/problemnew/show/P3131 A表示前缀和数组 A[r] - A[l - 1] = 0 (mod 7) 得 A[r] = A[l - 1] ( ...
- 2018.08.17 洛谷P3135 [USACO16JAN]堡哞(前缀和处理)
传送门 有趣的前缀和. 数据范围中的n≤200" role="presentation" style="position: relative;"> ...
- 洛谷P2346四子连棋
题目描述 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步. 黑白双方交替走棋,任意一方可 ...
随机推荐
- 解决python中import时无法识别自己写的包和模块的方法
我们用pycharm打开自己写的代码,当多个文件之间有相互依赖的关系的时候,import无法识别自己写的文件,但是我们写的文件又确实在同一个文件夹中, 这种问题可以用下面的方法解决: 1)打开File ...
- 在Xsheel Linux上安装nodejs和npm
最近window系统转向linux系统开发,linux系统的确适合程序员的开发. 作为前端安装了nodejs和npm,遇到了一些坑,赶紧记录下来 第一种安装方法:安装nodejs : sudo a ...
- 服务器重启,自动重启httpd
1. 手动重启 cd http ll cd /etc/httpd/ ll service httpd restart 2. 查看服务器内存使用情况 df -h 3. 自动重启 cat /etc/i ...
- 2016年深圳市服务业占GDP比重首次突破六成
2016年深圳市服务业占GDP比重首次突破六成 中商产业研究院 中商情报网 2017-01-12 11:08 分享: 中商情报网讯 1月10日,深圳市财政委员会召开新闻发布会,就深圳市2016 ...
- WebLogic使用总结(二)——WebLogic卸载[转]
一.WebLogic 12c的卸载 WebLogic的卸载是非常容易的,找到WebLogic的卸载程序,如下图所示:
- 【转载】Python eval
转载 作者博文地址:https://www.cnblogs.com/liu-shuai/ eval 功能:将字符串str当成有效的表达式来求值并返回计算结果. 语法: eval(source[, gl ...
- 05-python 学习第五天-简单验证码
通过python 随机数可以制作简单的验证码. 1.0版本来了,这验证码,只有一个码,功能虽然达不到,逻辑还是准确的,目前还不能算是验证码,但是我们会继续完善的. import random # 导入 ...
- C#计算两个时间的时间差,精确到年月日时分秒
喏,计算两个时间的时间差,精确到年月日时分秒 看起来比较笨的方法了,不知道有没有改进 DateTime d1 = new DateTime(2016, 4, 1, 0, 0, 0); DateTime ...
- wps中,怎么快速查看xls中隐藏的图片
步骤: 页面布局->选择窗格 即可在右方看到"文档中的对象“
- Linux文件句柄数配置
1.单程序句柄数限制 查看配置的句柄数:ulimit -n cat /etc/security/limits.conf 参考配置: * soft nofile 655360* hard nofile ...