【洛谷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颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步. 黑白双方交替走棋,任意一方可 ...
随机推荐
- Android基础控件ProgressBar进度条的使用
1.简介 ProgressBar继承与View类,直接子类有AbsSeekBar和ContentLoadingProgressBar, 其中AbsSeekBar的子类有SeekBar和RatingBa ...
- OS -- (python)文件和目录操作方法大全
一.python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法.1.得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd()2.返回指定目录下的所有文件和目 ...
- quatz调度-手动终止线程(2) Cleaner线程做清理工作
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import ja ...
- flock文件锁的学习和应用
flock文件锁 学习与应用 2016-9-20 作用: 可以使用flock文件锁,避免指定命令的同时执行.(实现任务锁定,解决冲突) 用法: # flock -xn /opt/lock_file ...
- java代码优化写法(转摘)
本文源地址:https://blog.csdn.net/syc001/article/details/72841650 可供程序利用的资源(内存.CPU时间.网络带宽等)是有限的,优化的目的就是让程序 ...
- Spring Cloud Config-Client 无法获取 Config-Server 在 github 上的配置文件的属性值,竟然是因为
Spring Cloud Config-Client 无法获取 Config-Server 在 github 上的配置文件的属性值,竟然是因为!!! 2018年07月23日 16:33:25 一颗很菜 ...
- docker上构建redis容器
1.查看docker上的镜像 [root@holly ~]# docker images 2.搜索docker上的redis镜像,选择下载的版本 [root@holly ~]# docker sear ...
- js中控制流管理的四种方法
引自http://es6.ruanyifeng.com/#docs/generator#yield--表达式 1.常用的回调方法 step1(function (value1) { step2(val ...
- [Ceoi2010]Pin
#2012. [Ceoi2010]Pin Online Judge:Bzoj-2012 Label:容斥,STL 题目描述 给出N(2<=N<=50000)个长度为4的字符串,问有且仅有D ...
- spring boot项目搭建中遇到的问题
自己动手搭建一下spring boot的项目,中途遇到了几个问题,在这里记录一下! 一.关于数据库中的表设计的问题 1.设计表的时候一定要添加的两个字段created updated 创建时间与更新时 ...