Subsequences Summing to Sevens

题目描述

Farmer John's N 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…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.

输入

The first line of input contains N (1≤N≤50,000). The next N lines each contain the N integer IDs of the cows (all are in the range 0…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.

You may want to note that the sum of the IDs of a large group of cows might be too large to fit into a standard 32-bit integer. If you are summing up large groups of IDs, you may therefore want to use a larger integer data type, like a 64-bit "long long" in C/C++.

样例输入

7
3
5
1
6
2
14
10

样例输出

5

提示

In this example, 5+1+6+2+14 = 28.

分析:(i+j)%k=i%k,则j是k的倍数;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <bitset>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define vi vector<int>
#define pii pair<int,int>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e6+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m;
ll a[maxn],pre[],last[],now;
int main()
{
int i,j,k,t;
scanf("%d",&n);
rep(i,,n){
scanf("%lld",&a[i]);
now=(now+a[i])%;
if(pre[now])last[now]=i;
else pre[now]=i;
}
ll ma=;
rep(i,,)
{
if(pre[i]&&last[i])ma=max(ma,last[i]-pre[i]);
}
printf("%lld\n",ma);
//system ("pause");
return ;
}

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]子共七Subsequences Summing to Sevens

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

  3. bzoj4511:[Usaco2016 Jan]Subsequences Summing to Sevens

    题目大意:给个序列,求最长的连续子序列使其为7的倍数 又是一道令人欢喜的不用怎么用脑的水题.. 边读入,边计算前缀和 分别保存前缀和%7结果为1,2,3,4,5,6的第一次的位置 然后减一减就知道长度 ...

  4. bzoj4511: [Usaco2016 Jan]Subsequences Summing to Sevens

    前缀和. 设f[i]为前缀和%7=i的第一个点.那么答案就是max(i-f[s[i]%7])了. #include<cstdio> #include<algorithm> #i ...

  5. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  6. BZOJ 刷题总结(持续更新)

    本篇博客按照题号排序(带*为推荐题目) 1008 [HNOI2008]越狱 很经典的题了..龟速乘,龟速幂裸题,, 1010 [HNOI2008]玩具装箱toy* 斜率优化 基本算是裸题. 1012 ...

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

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

  8. codeforces 597C C. Subsequences(dp+树状数组)

    题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...

  9. [LeetCode] Distinct Subsequences 不同的子序列

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

随机推荐

  1. ueditor浏览器 无法上传文件.问题

    dll也都引用了 路径绝对tmd没问题 最后 我一点一点的调试发现了问题 草tmd百度程序员 */UE.ajax = function() { //创建一个ajaxRequest对象 var fnSt ...

  2. JavaScript跨域总结与解决办法 什么是跨域

    什么是跨域 1.document.domain+iframe的设置 2.动态创建script 3.利用iframe和location.hash 4.window.name实现的跨域数据传输 5.使用H ...

  3. Spring.NET 中的 ADO.NET 数据访问的示例

    Spring.NET 1.3.1 中提供了一个使用 AdoTemplate 的完整示例,包括使用泛型和使用非泛型技术进行数据访问,这个示例位于下载的压缩包中\Spring.NET-1.3.1\Spri ...

  4. C/C++时间函数的使用

    来源:http://blog.csdn.net/apull/article/details/5379819 一.获取日历时间time_t是定义在time.h中的一个类型,表示一个日历时间,也就是从19 ...

  5. opencv----人脸美白算法,祛斑,祛痘,磨皮等

    现在各种手机camera软件都自带图像美颜处理,但是成熟的算法在网上很难搜到,博主也是自己摸索了自己做出来了,跟美图秀秀的处理效果相比,还不错,感觉很好,所以PO上来,与各位博友分享之. 首先是根据网 ...

  6. iOS xcode工程了解

    一个xcode工程默认是在一个主线程的,有需要可以创建分线程 判断是否是主线程: NSThread *thread1=[NSThread currentThread]; if ([red isMain ...

  7. Java BigDecimal Class

    Using BigDecimal to perform precise calculations with floats. BigDecimal is a class type. So declare ...

  8. Template - Strategy

    模板模式是一种基于继承的松耦合模式,其设计思路为,abstract类提供一组接口但不实现,不同concrete类继承同一接口并完成不同功能.如下图所示: 模板模式实现较为简单,TemplateMeth ...

  9. Cloudsim 3.0在myclipse下的安装过程

    (1)下载cloudsim 3.0: http://code.google.com/p/cloudsim/downloads/list (2)下载flanaga.jar包  下载地址:http://w ...

  10. Windows编译安装mod_wsgi,配合使用Django+Apahce

    编译环境: 均是32位版本 Microsoft Visual Studio 10.0 Microsoft SDKs v7.1 Apache2.4 Python3.4 mod_wsgi-4.5.0 要求 ...