bzoj 4100 [Usaco2015 Open]Bessie Goes Moo
来源:互联网 发布:系统备份软件 编辑:程序博客网 时间:2024/06/05 19:17
Description
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++).
七个变量B,E,S,I,G,O,M;使得(B+E+S+S+I+E)(G+O+E+S)(M+O+O)被7整除的方案有多少种.
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 −10^5 to 10^5.
第一行一个整数N,接下来N行
每行一个字母C,一个数字k;表示变量C可以为k.
保证每个变量有不超过500种可能的值
Output
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.
一个整数方案数
注意使用64位变量来存储
Sample Input
10
B 2
E 5
S 7
I 10
O 16
M 19
B 3
G 1
I 9
M 2
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
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
f[i][j]表示第i个字母模7等于j的方案数
然后dfs一下,三个式子有一个等于0就累加起来
发现关于倍数的问题取模特别好用
#include<cstdio>#include<iostream>using namespace std;int n,x;char a[2],s[10];long long ans,f[10][20],c[10],b[10];void dfs(int k,long long sum){ if(k>7) { int s1=(b[1]+b[2]*2+b[3]*2+b[4])%7; int s2=(b[5]+b[6]+b[2]+b[3])%7; int s3=(b[7]+2*b[6])%7; if(s1==0||s2==0||s3==0) ans=ans+sum; return; } for(int i=0;i<=6;i++) { b[k]=i-7; c[k]=f[k][i]; if(c[k]==0) continue; dfs(k+1,sum*c[k]); }}int main(){ s[1]='B';s[2]='E';s[3]='S';s[4]='I';s[5]='G';s[6]='O';s[7]='M'; cin>>n; for(int i=1;i<=n;i++) { scanf("%s%d",a,&x); for(int j=1;j<=7;j++) if(a[0]==s[j]) f[j][(x%7+7)%7]++; //负数取模 } dfs(1,1); cout<<ans; return 0;}
0 0
- bzoj 4100 [Usaco2015 Open]Bessie Goes Moo
- bzoj4100【Usaco2015 Open】Bessie Goes Moo
- bzoj4102【Usaco2015 Open】Bessie
- 暴搜——Luogu3123 [USACO15OPEN]Bessie Goes Moo
- bzoj4394【Usaco2015 Dec】Bessie
- bzoj 4098 [Usaco2015 Open]Palindromic Paths
- bzoj 4098: [Usaco2015 Open]Palindromic Paths 动态规划
- BZOJ 4102 [Usaco2015 Open] 图上DP 解题报告
- BZOJ 3888: [Usaco2015 Jan]Stampede
- BZOJ 3943 [Usaco2015 Feb]SuperBull
- BZOJ 3940 [Usaco2015 Feb]Censoring
- BZOJ 3942 [Usaco2015 Feb] Censoring
- BZOJ 3940 [Usaco2015 Feb] Censoring
- bzoj 3943: [Usaco2015 Feb]SuperBull
- bzoj4099【Usaco2015 Open】Trapped in the Haybales
- bzoj4099: [Usaco2015 Open]Trapped in the Haybales
- bzoj4101[Usaco2015 Open]Trapped in the Haybales
- bzoj4098 [Usaco2015 Open]Palindromic Paths dp
- VC经验
- 118. Pascal's Triangle
- 华为机试题--4.字符串分隔
- jquery的get,post和ajax方法的使用
- #pragma 预处理指令详解
- bzoj 4100 [Usaco2015 Open]Bessie Goes Moo
- 查漏补缺——函数(C++ Primer)
- CDC::CreateCompatibleDC 的整理(转)
- 手机号码两两颠倒函数
- Run-Time Check Failure #2 - Stack around the variable '***' was corrupted.
- Android自定义View初步(一)
- 单例模式
- 【数字图像处理学习笔记之四】图像腐蚀、膨胀
- 1.命名空间2.字符串3.容器(STL)4.迭代器