TOJ 5021: Exchange Puzzle

5021: Exchange Puzzle 分享至QQ空间

Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByte
Total Submit: 24            Accepted:4

Description

 

Given 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, given a list of up to 500 integer values the variable can possibly take, you need to count the number of different ways that 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.

 

Input

 

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

 

Print a single integer, giving the number of ways that you 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

Hint

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

Source

USACO 2015 US Open

我感觉我的代码稳稳稳,因为时间限制,不可能枚举那么多数,只存在+的关系,所以先取模分下类

#include<stdio.h>
int S[128],v[10][10];
int main(){
int n;S['B']=0,S['E']=1,S['S']=2,S['I']=3,S['G']=4,S['O']=5,S['M']=6;
scanf("%d",&n);
for(int i=0;i<n;i++){
    getchar();
    char c;int x;
    scanf("%c%d",&c,&x);
    v[S[c]][((x%7)+7)%7]++;
}
__int64 cnt=0;
for(int i=0;i<7;i++){__int64 B=v[0][i];
for(int j=0;j<7;j++){int E=v[1][j];
for(int k=0;k<7;k++){int S=v[2][k];
for(int l=0;l<7;l++){int I=v[3][l];
for(int m=0;m<7;m++){int G=v[4][m];
for(int n=0;n<7;n++){int O=v[5][n];
for(int o=0;o<7;o++){int M=v[6][o];
if((i+2*j+2*k+l)*(m+n+j+k)*(o+2*n)%7==0)
cnt+=B*E*S*I*G*O*M;}}}}}}}
printf("%I64d
",cnt);
return 0;}
原文地址:https://www.cnblogs.com/BobHuang/p/7283475.html