Negative and Positive (NP) (Hash maintenance)

Source: Internet
Author: User

Negative and Positive (NP)
Time limit:3000/1500 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 2846 Accepted Submission (s): 782


Problem Description
When the given an array (a0,a1,a2,?an?1) and an integer K, the expected to judge whether there is a pair (i,j) (0≤i≤j<n) Which makes that np?sum (i,j) equals to K true. Here Np?sum (i,j) =ai?ai+1+ai+2+?+ (? 1) J?iaj


Input
Multi test Cases. The first line of the input file there is an integer T indicates the number of test cases.
In the next 2? T lines, it would list the data for each test case.
Each case occupies a lines, the first line contain both integers n and K which are mentioned above.
The second line contain (a0,a1,a2,?an?1) is separated by exact one space.
[Technical specification]
All input items is integers.
0<t≤25,1≤n≤1000000,?1000000000≤ai≤1000000000,?1000000000≤k≤1000000000


Output
For each case,the the output should occupies exactly one line. The output format is case #id: ans, here ID is the data number starting from 1; Ans is "Yes." or "No." (without quote) according to whether you can find (i,j) which makes Pn?sum (i,j) equals to K.
See the sample for more details.


Sample Input

2
1 1
1
2 1
-1 0



Sample Output

Case #1: Yes.
Case #2: No.

Hint
If input is huge, the fast IO method is recommended.




Source

Bestcoder Round #32

Positive and negative
Maintenance prefixes and sum[i]=a[0]-a[1]+a[2]-a[3]+...+ ( -1) ^i*a[i]
Enumerates the end I, and then queries the hash table for the existence of the sum[i]-k value.
If the current i is an odd number, insert sum[i] into the hash table.
The above consideration is the case that begins with an even number from I.
Then consider starting with an odd number of cases, follow the above method to do it again.
The difference is this time to maintain the prefix and is sum[i]=-(a[0]-a[1]+a[2]-a[3]+...+ ( -1) ^i*a[i])
When I is even, insert sum[i] into the hash table.
Total complexity O (n)

#include <bits/stdc++.h>using namespace Std;template<class t>inline T Read (t&x) {char C;    while ((C=getchar ()) <=32) if (c==eof) return 0;    BOOL Ok=false;    if (c== '-') Ok=true,c=getchar ();    for (x=0; c>32; C=getchar ()) x=x*10+c-' 0 ';    if (OK) x=-x; return 1;} Template<class t> inline T read_ (t&x,t&y) {return read (x) &&read (y);} Template<class t> inline T read__ (t&x,t&y,t&z) {return read (x) &&read (y) &&read (z);}    Template<class t> inline void Write (T x) {if (x<0) Putchar ('-'), x=-x;    if (x<10) putchar (x+ ' 0 '); else write (X/10), Putchar (x%10+ ' 0 ');}    Template<class t>inline void Writeln (T x) {write (x); Putchar (' \ n ');} -------ZCC IO template------const int MAXN=1E6+1000;CONST double inf=999999999; #define Lson (rt<<1), L,m#define Rson (rt<<1|1), M+1,r#define M ((l+r) >>1) #define for (i,t,n) for (int i= (t);i< (n); i++) typedef long Long LL; typedef double DB;TYPEDEF PAir<int,int> P; #define BUG printf ("---\ n"); #define MOD 100007LL SUM[MAXN];    ll a[maxn];vector<ll> hash_[mod];void Insert (ll key) {int pos= (key%mod+mod)%mod; Hash_[pos].push_back (key);}    BOOL Find (LL key) {int pos= (key%mod+mod)%mod;    int n=hash_[pos].size ();    for (int i=0;i<n;i++) if (Hash_[pos][i]==key) return true; return false;}    BOOL Solve (LL N,ll k) {for (int i=0;i<mod;i++) hash_[i].clear ();    Insert (0);        for (int i=0;i<n;i++) {if (Find (Sum[i]-k)) return true;    if (i&1) insert (Sum[i]);    } for (int i=0;i<mod;i++) hash_[i].clear ();        for (int i=0;i<n;i++) {if (Find (-sum[i]-k)) return true; if (! (    i&1)) Insert (-sum[i]); } return false;}    int main () {int t,cas=1;    Read (T);        while (t--) {LL k,n;        Read_ (N,K);        for (int i=0;i<n;i++) {read (a[i]);        } Sum[0]=a[0]; for (int i=1;i<n;i++) if (i&1) sum[i]=sum[I-1]-a[i];        else Sum[i]=sum[i-1]+a[i]; printf ("Case #%d:%s\n", Cas++,solve (n,k)? "    Yes. ":" No. "); } return 0;}

Negative and Positive (NP) (Hash maintenance)

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.