Farmer John and Bessie the cow love to exchange math puzzles in their free time. The last puzzle FJ gave Bessie was quite difficult and she failed to solve it. Now she wants to get even with FJ by giving him a challenging puzzle. Bessie gives FJ the expression, containing the seven variablesB,E,S,I,G,O,MB,E,S,I,G,O,M. For each variable, she gives FJ a list of up to 500 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 a multiple of 7. Note that the answer to this problem can be too large to fit into a 32-bit integer, so you probably want to use 64-bit integers .
Farmer John and Bessie the cow love to exchange math puzzles in their free time. The last puzzle FJ gave Bessie was quite difficult and she failed to solve it. Now she wants to get even with FJ by giving him a challenging puzzle. Bessie gives FJ the expression (B+E+S+S+I+E)(G+O+E+S)(M+O+O)(B+E+S+S+I+E)(G+O+E+S)(M+O+O), containing the seven variables B,E,S,I,G,O,MB,E,S,I,G,O,M (the "OO" is a variable, not a zero). For each variable, she gives FJ a list of up to 500 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 a multiple of 7. Note that the answer to this problem can be too large to fit into a 32-bit integer, so you probably want to use 64-bit integers (e.g., "long long"s in C or C++).