8182: Bessie Goes Moo 贝茜哞哞

内存限制:128 MB 时间限制:1 S 标准输入输出
题目类型:传统 评测方式:文本比较 上传者:
提交:3 通过:2

题目描述

农夫约翰和奶牛贝茜喜欢在业余时间互相出数学题。

约翰给贝茜出了一道相当难的问题,导致她没能解决。

现在,她希望通过给约翰出一道有挑战性的难题来报复他。

贝茜给了约翰一个表达式 (B+E+S+S+I+E)(G+O+E+S)(M+O+O),其中包含七个变量 B,E,S,I,G,O,M(O 是变量,不是零)。

对于每个变量,她给约翰一个列表,表中包含该变量可采用的最多 500 个整数值。

她要求约翰计算,共有多少种给变量赋值的方法可以使得表达式的计算结果是 7 的倍数。

注意,答案可能超过标准的 32 位整数型能够表示的范围,所以你可能需要使用更大的数据类型,例如C/C++中的“long long”。

输入格式

第一行包含一个整数 N。

接下来 N 行,每行包含一个变量和该变量的一个可能值。

每个变量至少出现 1 次,最多出现 500 次。

同一变量不会多次列出同一可能值。

输出格式

输出可以使得表达式的计算结果是 7 的倍数的给变量赋值的方法总数。

数据范围

7≤N≤3500,
所有变量的可能取值范围 [−105,105]

输入样例:

10
B 2
E 5
S 7
I 10
O 16
M 19
B 3
G 1
I 9
M 2

输出样例:

2

样例解释

两种可能的赋值方式为: 

(B,E,S,I,G,O,M) = (2, 5, 7, 9,  1, 16, 19) -> 51,765
                = (2, 5, 7, 9,  1, 16, 2 ) -> 34,510


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), 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 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++).

INPUT FORMAT (file bgm.in):

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 500 times. No possible value will be listed more than once for the same variable. All possible values will be in the range −105 to 105.

OUTPUT FORMAT (file bgm.out):

Print a single integer, giving the number of ways FJ can assign values to variables so the expression above evaluates to a multiple of 7.

SAMPLE INPUT:

10
B 2
E 5
S 7
I 10
O 16
M 19
B 3
G 1
I 9
M 2

SAMPLE OUTPUT:

2

The two possible assignments are

(B,E,S,I,G,O,M) = (2, 5, 7, 9,  1, 16, 19) -> 51,765
                = (2, 5, 7, 9,  1, 16, 2 ) -> 34,510

[Problem credits: Brian Dean, 2015]

输入样例 复制


输出样例 复制