Subsequences Summing to Sevens

Source: Internet
Author: User
Tags gcd

Subsequences Summing to Sevens The title describes farmer John's N cows is standing in a row, as they has a tendency to does from time to time. Each cow are labeled with a distinct an 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 Numbe Rs 1 ... 6, he is 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. Enter 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 is in the range 0 ... 1,000,000). Output 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.

Want to note the sum of the IDs of a large group of cows might is too large to fit into a standard 32-bit int Eger. If you is summing up large groups of IDs, the therefore want to use a larger the integer data type, like a 64-bit "long l Ong "in C + +. Sample input
7351621410
Sample output
5
Tips

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

Analysis: (I+J)%k=i%k, then J is a multiple of k;

Code:

#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>#defineRep (I,m,n) for (i=m;i<=n;i++)#defineRSP (It,s) for (Set<int>::iterator It=s.begin (); It!=s.end (); it++)#defineVI vector<int>#definePII pair<int,int>#defineMoD 1000000007#defineINF 0x3f3f3f3f#definePB Push_back#defineMP Make_pair#defineFi first#defineSe Second#definell Long Long#definePi ACOs (-1.0)Const intmaxn=1e6+Ten;Const intdis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};using namespacestd;ll gcd (ll p,ll q) {returnq==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;}returnF;}intn,m;ll a[maxn],pre[7],last[7],now;intMain () {inti,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; Elsepre[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;}

Subsequences Summing to Sevens

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.