hdu 5138

时间:2021-12-29 14:54:55

参考……!!!!

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <stack>
#include <cctype>
#include <string>
#include <malloc.h>
#include <queue>
#include <map>
#include <set> using namespace std; const int INF = 0xffffff;
const double esp = 10e-;
const double Pi = * atan(1.0);
const int maxn = + ;
const long long mod = ;
const int dr[] = {,,-,,-,,-,};
const int dc[] = {,,,-,,-,-,};
typedef long long LL; LL gac(LL a,LL b){
return b?gac(b,a%b):a;
}
const int hashtable = ;
struct Hash{
int head[hashtable];
int _next[maxn];
long long state[maxn];
int cnt;
void init(){
cnt = ;
memset(head,,sizeof(head));
}
bool _find(LL s){
int h = (s % hashtable+hashtable)%hashtable;
int u = head[h];
while(u){
if(s == state[u])
return ;
u = _next[u];
}
return ;
}
bool try_to_insert(LL s){
int h = (s % hashtable+hashtable)%hashtable;
_next[cnt] = head[h];
state[cnt] = s;
head[h] = cnt;
cnt++;
return ;
} };
Hash H1,H2;
inline LL read()
{
char ch=getchar();LL x=,f=;
while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
while(ch<=''&&ch>=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
LL a[maxn];
int main()
{
#ifndef ONLINE_JUDGE
freopen("inpt.txt","r",stdin);
// freopen("output.txt","w",stdout);
#endif
int t;
scanf("%d",&t);
for(int cas = ;cas <= t;cas++){
int n,k;
scanf("%d%d",&n,&k);
H1.init();
H2.init();
LL sum = ;
bool ok = ;
H1.try_to_insert();
H2.try_to_insert();
for(int i = ;i < n;i++){
a[i] = read();
}
for(int i = n-;i > - && !ok;i--){
if(ok)
continue;
if(i % == ){
sum -= a[i];
if(H2._find(-sum-k)){
ok = ;
}
}
else{
sum += a[i];
if(H1._find(sum-k)){
ok = ;
}
}
H1.try_to_insert(sum);
H2.try_to_insert(-sum);
}
printf("Case #%d: %s.\n",cas,ok?"Yes":"No");
}
return ;
}