【洛谷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颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步. 黑白双方交替走棋,任意一方可 ...
随机推荐
- CSS动画之transition属性
transition 属性 简介 transition(过渡)) 是指从一个状态到另一个状态的变化.比如当鼠标在某个元素上悬停时,我们会修改它的样式,采用 transition 可以创建一个平滑的动画 ...
- ssh 与服务器保持连接
直接改客户端,服务器端不应该更改. sudo vi /etc/ssh/ssh_config # 或 ~/.ssh/config TCPKeepAlive=yes # Client每隔 60 秒发送一次 ...
- Leetcode958. Check Completeness of a Binary Tree二叉树的完全验证性
给定一个二叉树,确定它是否是一个完全二叉树. 百度百科中对完全二叉树的定义如下: 若设二叉树的深度为 h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集 ...
- P1249 最大乘积
打暴力找规律,都是连续自然数去掉一个 n=int(input()) a=[] cnt=0 i=2 tot=0 ans=1 while tot<=n: tot+=i cnt+=1 a.append ...
- 从github下载项目出现yes/no的选项,无法下载项目
解决办法: # 本地执行: ssh-keygen # 将id_rsa_pub文件中公钥拷贝到github上的ssh认证 oodful@:~/Volumes/Term2 :::$cat ~/.ssh/i ...
- ROS 日志消息(C++)
1.日志级别 日志消息分为五个不同的严重级别宏,与Android的Log定义的严重级别类似,如下基础宏: ROS_DEBUG_STREAM.ROS_INFO_STREAM.ROS_WARN_STREA ...
- Android SDK上手指南:知识测试
Android SDK上手指南:知识测试 2014-01-22 10:00 核子可乐 译 51CTO 字号:T | T 在从零开始学习Android开发系列教程当中,我们已经了解了为Android平台 ...
- Python导出DBF文件到Excel的方法
Python导出DBF文件到Excel的方法 这篇文章主要介绍了Python导出DBF文件到Excel的方法,实例分析了Python基于win32com模块实现文件导出与转换的相关技巧,分享给大家供大 ...
- VitualBox虚拟机安装CentOS, shell模式与图形化界面的相互切换
方法一:永久切换 # vi /etc/inittab 编辑 init 5 为 init 3,重启就自动进入控制台方式:反之桌面模式 方法二:当前有效 桌面模式切换shell模式:Ctrl + Alt ...
- PAT甲级——A1020 Tree Traversals
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...