题目描述

Farmer John's NN 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 1 \ldots 61…6, 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 NN (1 \leq N \leq 50,0001≤N≤50,000). The next NN

lines each contain the NN integer IDs of the cows (all are in the range

0 \ldots 1,000,0000…1,000,000).

输出格式:

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.

思路:前缀和+二分答案。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,l,r,mid;
int num[],sum[];
bool judge(){
for(int i=;i<=n-mid;i++)
if((sum[i+mid]-sum[i])%==) return true;
return false;
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&num[i]),sum[i]=sum[i-]+num[i];
l=;r=n;
while(l<=r){
mid=(l+r)/;
if(judge()) l=mid+;
else r=mid-;
}
cout<<l-;
}

80

思路:求出前缀和mod7,然后遍历,如果拥有相同的余数,说明这个区间是可以被7整除的记录。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 50010
using namespace std;
int n;
int pri[],v[];
int a[MAXN],sum[MAXN];
int main(){
scanf("%d",&n);
sum[]=;
for(int i=;i<=n;i++)
scanf("%lld",&a[i]),sum[i]=(sum[i-]+a[i])%;
for(int i=;i<=n;i++){
if(!v[sum[i]])
v[sum[i]]=i,pri[sum[i]]=i;
else pri[sum[i]]=i;
}
int ans=-;
for(int i=;i<;i++){
if(!v[i]) continue;
ans=max(ans,pri[i]-v[i]);
}
printf("%d\n",ans);
}
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 50010
using namespace std;
int n;
int pri[],v[];
int a[MAXN],sum[MAXN];
int main(){
scanf("%d",&n);
sum[]=;
for(int i=;i<=n;i++)
scanf("%lld",&a[i]),sum[i]=(sum[i-]+a[i])%;
for(int i=;i<=n;i++){
if(!v[sum[i]])
v[sum[i]]=i,pri[sum[i]]=i;
else pri[sum[i]]=i;
}
int ans=-;
for(int i=;i<;i++){
if(!v[i]) continue;
ans=max(ans,pri[i]-v[i]);
}
printf("%d\n",ans);
}

洛谷 P3131 [USACO16JAN]子共七Subsequences Summing to Sevens的更多相关文章

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

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

  2. 【洛谷P3131】 【USACO16JAN】子共七

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

  3. Subsequences Summing to Sevens

    Subsequences Summing to Sevens 题目描述 Farmer John's N cows are standing in a row, as they have a tende ...

  4. 洛谷 P3131 子共七

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

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

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

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

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

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

  8. [Luogu] 子共七

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

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

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

随机推荐

  1. 记一次 Apache HUE 优化之因使用 Python 魔术方法而遇到的坑

    最近的工作是基于 Apache HUE 做二次开发.刚接手 HUE 的代码的时候,内心是崩溃的:开源的代码,风格很多种, 代码比较杂乱; 虽是基于 Django 开发的,但是项目的结构改变很大; 很多 ...

  2. CentOS-6.4-minimal版中安装JDK_Maven_Subversion以及改动rpm包安装路径

    完整版见https://jadyer.github.io/2013/09/07/centos-config-develop/ /** * @see -------------------------- ...

  3. 一个Java基础练习

    今天在群里又有一个朋友问到了这样一个练习,我索性将代码贴到这里,下次须要的朋友能够来这里看. 用到知识点:数组.集合.IO流 问题描写叙述:在例如以下图所看到的的一个txt文件里读取数据到内存,然后统 ...

  4. android中9-patch图片的使用

    看了非常多文章的介绍,9.png图片有两种区域:可扩展区和内容显示区. 弄了半天才明确什么叫做可扩展区,什么叫做内容显示区. 分享一下自己的理解. 下图是某博客的截图: 图片来自:http://blo ...

  5. Handle-postDelayed 延迟操作

    今天在工作的时候,遇到了一个方法,是关于Handle来实现延时操作的,自己写了一个小demo,学习总结如下 xml <?xml version="1.0" encoding= ...

  6. Android 学习笔记之Bitmap位图虽触摸点移动

    package xiaosi.bitmap; import android.app.Activity; import android.os.Bundle; public class mianActiv ...

  7. MyBatis自动生成代码之generatorConfig配置文件及其详细解读

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE generatorConfiguratio ...

  8. Day5费用流

    算法 zkw费用流:多路增广,增光D[y]=D[i]+c的边 无源汇上下界最小费用可行流 每次强行增加下界的流量 类似网络流,拆边 原边的费用为c,拆出来的边费用为0 负边和负圈 直接应用 SDOI2 ...

  9. Lusac定理

    转载大佬的模版:http://www.cnblogs.com/vongang/archive/2012/12/02/2798138.html

  10. 从硬件到语言,详解C++的内存对齐(memory alignment)(一)

    作者:赵宗晟 出处:https://www.cnblogs.com/zhao-zongsheng/p/9099603.html 很多写C/C++的人都知道“内存对齐”的概念以及规则,但不一定对他有很深 ...