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.

输入输出样例

输入样例#1:

7
3
5
1
6
2
14
10
输出样例#1:

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】子共七的更多相关文章

  1. 洛谷 P3131 [USACO16JAN]子共七Subsequences Summing to Sevens

    P3131 [USACO16JAN]子共七Subsequences Summing to Sevens 题目描述 Farmer John's NN cows are standing in a row ...

  2. [USACO16JAN]子共七Subsequences Summing to Sevens

    [USACO16JAN]子共七Subsequences Summing to Sevensa[i]表示前缀和如果a[i]%7==t&&a[j]%7==t那么a[j]-a[i-1]一定是 ...

  3. 洛谷 P3131 子共七

    看到这一题第一印象就是暴力好打,$O(n^2)$,预计得分$70$分 这明显满足不了啊,我们要用到前缀和. $sum[i]$记录到i的前缀和,区间$[a,b]$的和就是$sum[b]-sum[a-1] ...

  4. 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和最后一次出现的次数 ...

  5. [洛谷 P3788] 幽幽子吃西瓜

    妖梦费了好大的劲为幽幽子准备了一个大西瓜,甚至和兔子铃仙打了一架.现在妖梦闲来无事,就蹲在一旁看幽幽子吃西瓜.西瓜可以看作一个标准的球体,瓜皮是绿色的,瓜瓤是红色的,瓜皮的厚度可视为0.妖梦恰好以正视 ...

  6. 洛谷 P3133 [USACO16JAN]无线电联系Radio Contact

    P3133 [USACO16JAN]无线电联系Radio Contact 题目描述 Farmer John has lost his favorite cow bell, and Bessie the ...

  7. [Luogu] 子共七

    https://www.luogu.org/problemnew/show/P3131 A表示前缀和数组 A[r] - A[l - 1] = 0 (mod 7) 得 A[r] = A[l - 1] ( ...

  8. 2018.08.17 洛谷P3135 [USACO16JAN]堡哞(前缀和处理)

    传送门 有趣的前缀和. 数据范围中的n≤200" role="presentation" style="position: relative;"> ...

  9. 洛谷P2346四子连棋

    题目描述 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步. 黑白双方交替走棋,任意一方可 ...

随机推荐

  1. centos 6.5 安装dotnet core 2.2

    .net core 官网地址 https://dotnet.microsoft.com/download 本次安装版本为.net core SDK v2.2.101 1.查看系统版本, 升级系统基本l ...

  2. php中Cookies

    PHP Cookies cookie 是一种在远程浏览器端储存数据并以此来跟踪和识别用户的机制,PHP 透明地支持 HTTP cookie. cookie 常用于识别用户. Cookie 是什么? c ...

  3. 19-11-12-Aftern-℘

    我饿死了,于是写写博客安慰一下即将退役的自己. ZJ: T1. 三种颜色,想到一道神奇的‘天空龙’. 于是觉得此题可做. 那好了. 于是切掉,还拿了一个暴力对拍.疯狂A. 啊dfs慢的要死了 T2一眼 ...

  4. Docker系列(十六):搭建Openshift环境

    目的: 搭建Linux下的Openshift环境. 参考资料: 开源容器云OpenShift 构建基于Kubernetes的企业应用云平台 ,陈耿 ,P253 ,2017.06 .pdf 下载地址:h ...

  5. HTML 颜色表示

    三种表示方法 1 颜色单词 : blue green red pink 2 10进制表示: RGB(255, 10, 0) 3 16进制表示: #FF0000(红)  #00FF00(绿)

  6. 《DSP using MATLAB》Problem 8.13

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  7. <每日一题>题目23:桶排序

    ''' 桶排序:最快最简单的排序 缺点:最占内存 类型:分布式排序 ''' import cProfile import random def bucketSort(nums): #选出最大的数 ma ...

  8. C#端一个不错的订单号生成规则

    /// <summary> /// 订单助手 /// </summary> public class OrderHelper { /// <summary> /// ...

  9. Android开发 Camera2开发_2_预览分辨率或拍照分辨率的计算

    前言 不管在Camera1或者Camera2在适配不同手机/不同使用场景的情况下都需要计算摄像头里提供的分辨率列表中最合适的那一个分辨率.所以在需要大量机型适配的app,是不建议不经过计算直接自定义分 ...

  10. webpack中所使用到的npm常用命令

    :D进入D盘 mkdir webapp 创建webapp文件夹 cd webapp 进入webapp文件夹 mkdir webapp && cd webapp 以上两步创建和进入文件夹 ...