답안 #58939

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
58939 2018-07-19T21:12:33 Z TadijaSebez Snake Escaping (JOI18_snake_escaping) C++11
0 / 100
7 ms 4528 KB
#include <stdio.h>
const int N=1<<20;
int dp0[N],dp1[N],pc[N];
char s[N],t[20];
int min(int a, int b){ return a>b?b:a;}
int abs(int x){ return x>0?x:-x;}
int main()
{
	int n,q,i,mask;
	for(i=1;i<N;i++) pc[i]=pc[i-(i&-i)]+1;
	scanf("%i %i",&n,&q);
	scanf("%s",s);
	for(i=0;i<(1<<n);i++)
	{
		dp1[i]=dp0[i]=s[i]-'0';
	}
	for(i=0;i<n;i++)
	{
		for(mask=0;mask<(1<<n);mask++)
		{
			if((mask>>i)&1) dp1[mask]+=dp1[mask^(1<<i)];	
			else dp0[mask]+=dp0[mask^(1<<i)];
		}
	}
	while(q--)
	{
		scanf("%s",t);
		int a=0,b=0,c=0;
		for(i=0;i<n;i++)
		{
			if(t[n-i-1]=='0') a+=1<<i;
			else if(t[n-i-1]=='1') b+=1<<i;
			else c+=1<<i;
		}
		int ans=0;
		if(pc[a]<=min(pc[b],pc[c]))
		{
			for(mask=a;;mask=(mask-1)&a)
			{
				//printf("a:%i %i\n",a,mask);
				int go=(b|a)^mask;
				if(pc[mask]==pc[a]) ans+=dp0[go];
				else ans-=dp0[go];
				if(!mask) break;
			}
		}
		else if(pc[b]<=min(pc[a],pc[c]))
		{
			for(mask=b;;mask=(mask-1)&b)
			{
				//printf("b:%i %i\n",b,mask);
				if(pc[mask]==pc[b]) ans+=dp1[mask|c];
				else ans-=dp1[mask|c];
				if(!mask) break;
			}
		}
		else
		{
			for(mask=c;;mask=(mask-1)&c)
			{
				//printf("c:%i %i\n",c,mask);
				ans+=s[mask|b]-'0';
				if(!mask) break;
			}
		}
		printf("%i\n",abs(ans));
	}
	return 0;
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:11:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%i %i",&n,&q);
  ~~~~~^~~~~~~~~~~~~~~
snake_escaping.cpp:12:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s",s);
  ~~~~~^~~~~~~~
snake_escaping.cpp:27:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s",t);
   ~~~~~^~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 4472 KB Output is correct
2 Correct 6 ms 4472 KB Output is correct
3 Incorrect 6 ms 4528 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 4472 KB Output is correct
2 Correct 6 ms 4472 KB Output is correct
3 Incorrect 6 ms 4528 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 4472 KB Output is correct
2 Correct 6 ms 4472 KB Output is correct
3 Incorrect 6 ms 4528 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 4472 KB Output is correct
2 Correct 6 ms 4472 KB Output is correct
3 Incorrect 6 ms 4528 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 4472 KB Output is correct
2 Correct 6 ms 4472 KB Output is correct
3 Incorrect 6 ms 4528 KB Output isn't correct
4 Halted 0 ms 0 KB -