Bessie Goes Moo
题目描述
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++).
输入
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.
输出
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.
样例输入
10
B 2
E 5
S 7
I 10
O 16
M 19
B 3
G 1
I 9
M 2
样例输出
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
分析:对每个数取模,然后暴力即可,复杂度O(7^7);
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e3+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t;
ll ans;
char a[];
int gao(char p)
{
int i;
string q="BESIGOM";
for(i=;q[i]!=p;i++);
return i;
}
set<int>p[];
int q[][];
int main()
{
int i,j;
scanf("%d",&n);
while(n--)
{
scanf("%s%d",a,&m);
m%=;
while(m<)m+=;
p[gao(a[])].insert(m);
q[gao(a[])][m]++;
}
for(int x:p[])
for(int y:p[])
for(int z:p[])
for(int s:p[])
for(int t:p[])
for(int e:p[])
for(int r:p[])
{
if((x+y+y+z+z+s)*(t+e+y+z)*(r+e+e)%==)
ans+=1LL*q[][x]*q[][r]*q[][y]*q[][z]*q[][s]*q[][t]*q[][e];
}
printf("%lld\n",ans);
//system("pause");
return ;
}