博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Subsequences Summing to Sevens
阅读量:4341 次
发布时间:2019-06-07

本文共 2266 字,大约阅读时间需要 7 分钟。

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++.

样例输入

7351621410

样例输出

5

提示

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

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

代码:

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define rep(i,m,n) for(i=m;i<=n;i++)#define rsp(it,s) for(set
::iterator it=s.begin();it!=s.end();it++)#define vi vector
#define pii pair
#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+10;const int dis[4][2]={ { 0,1},{-1,0},{ 0,-1},{ 1,0}};using namespace std;ll gcd(ll p,ll q){ return q==0?p:gcd(q,p%q);}ll qpow(ll p,ll q){ll f=1;while(q){ if(q&1)f=f*p;p=p*p;q>>=1;}return f;}int n,m;ll a[maxn],pre[7],last[7],now;int main(){ int i,j,k,t; scanf("%d",&n); rep(i,1,n){ scanf("%lld",&a[i]); now=(now+a[i])%7; if(pre[now])last[now]=i; else pre[now]=i; } ll ma=0; rep(i,0,6) { if(pre[i]&&last[i])ma=max(ma,last[i]-pre[i]); } printf("%lld\n",ma); //system ("pause"); return 0;}

 

转载于:https://www.cnblogs.com/dyzll/p/5743093.html

你可能感兴趣的文章
16-镜像命名的最佳实践
查看>>
【概率论】5-6:正态分布(The Normal Distributions Part III)
查看>>
课后作业-阅读任务-阅读提问-4
查看>>
The City of Song
查看>>
前端关于居中问题
查看>>
关于JVM的垃圾回收(GC) 这可能是你想了解的
查看>>
转:AFNetworking 与 UIKit+AFNetworking 详解
查看>>
回顾冒泡排序
查看>>
Ajax
查看>>
LeetCode 235 Lowest Common Ancestor of a Binary Search Tree
查看>>
SpringBoot webSocket
查看>>
C# DateTime变量不能赋null值
查看>>
[zoj]3576 Count the Length
查看>>
yii 表单小部件使用
查看>>
关于java泛型1
查看>>
solr教程,值得刚接触搜索开发人员一看
查看>>
面试笔试-脚本-1:使用shell脚本输出登录次数最多的用户
查看>>
Cordys BOP 4平台开发入门实战演练——Webservices开发(0基础)
查看>>
《Pro Android Graphics》读第三季度票据
查看>>
pinyin4j新手教程
查看>>