NC24227. [USACO 2015 Ope B]Bessie Gets Even
描述
Bessie gives FJ the expression (B+E+S+S+I+E)(G+O+E+S)(M+O+O), containing the seven variables B,E,S,I,G,O,M (the "O" is a variable, not a zero). For each variable, she gives FJ a list of up to 20 integer values the variable can possibly take. She asks FJ to count the number of different ways he can assign values to the variables so the entire expression evaluates to an even number.
输入描述
The first line of the input contains an integer N. The next N lines each contain a variable and a possible value for that variable. Each variable will appear in this list at least once and at most 20 times. No possible value will be listed more than once for the same variable. All possible values will be in the range −300 to 300.
输出描述
Print a single integer, giving the number of ways FJ can assign values to variables so the expression above evaluates to an even result.
示例1
输入:
10 B 2 E 5 S 7 I 10 O 16 M 19 B 3 G 1 I 9 M 2
输出:
6
说明:
There are six possible variable assignments:C++11(clang++ 3.9) 解法, 执行用时: 3ms, 内存消耗: 504K, 提交时间: 2020-09-03 15:54:26
#include<cstdio> #define LL long long using namespace std; int N,x; char ch; LL ans,hsh[95][2]; inline int read(){ int ret=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if (ch=='-')f=-f;ch=getchar();} while (ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar(); return ret*f; } int main(){ N=read(); for (int i=0;i<N;i++){ ch=getchar(),x=read(); hsh[ch][(x%2+2)%2]++; } for(int B=0;B<2;B++) for(int E=0;E<2;E++) for(int S=0;S<2;S++) for(int I=0;I<2;I++) for(int G=0;G<2;G++) for(int O=0;O<2;O++) for(int M=0;M<2;M++) if (((B+E+S+S+I+E)*(G+O+E+S)*(M+O+O))%2==0){ ans+=hsh['B'][B]*hsh['E'][E]*hsh['S'][S]*hsh['I'][I]*hsh['G'][G]*hsh['O'][O]*hsh['M'][M]; } printf("%lld\n",ans); return 0; }
C++14(g++5.4) 解法, 执行用时: 2ms, 内存消耗: 372K, 提交时间: 2020-08-22 18:08:16
#include<cstdio> #define LL long long using namespace std; int N,x; char ch; LL ans,hsh[95][2]; inline int read(){ int ret=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if (ch=='-')f=-f;ch=getchar();} while (ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar(); return ret*f; } int main(){ N=read(); for (int i=0;i<N;i++){ ch=getchar(),x=read(); hsh[ch][(x%2+2)%2]++; } for(int B=0;B<2;B++) for(int E=0;E<2;E++) for(int S=0;S<2;S++) for(int I=0;I<2;I++) for(int G=0;G<2;G++) for(int O=0;O<2;O++) for(int M=0;M<2;M++) if (((B+E+S+S+I+E)*(G+O+E+S)*(M+O+O))%2==0){ ans+=hsh['B'][B]*hsh['E'][E]*hsh['S'][S]*hsh['I'][I]*hsh['G'][G]*hsh['O'][O]*hsh['M'][M]; } printf("%lld\n",ans); return 0; }