博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【题解】Luogu P3123 [USACO15OPEN]贝茜说哞Bessie Goes Moo
阅读量:5011 次
发布时间:2019-06-12

本文共 2952 字,大约阅读时间需要 9 分钟。

Luogu P3123 [USACO15OPEN]贝茜说哞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)(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,MB,E,S,I,G,O,M (the “OOO “ 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整除的方案有多少种.

输入输出格式

输入格式:

The first line of the input contains an integer NNN . The next NNN 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}$.

输出格式:

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.

输入输出样例

输入样例#1: 复制

10

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

输出样例#1: 复制

2

说明

The two possible assignments are

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

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

思路

  • 用 $f[i][j]$表示: 第i个字母变量所代表的数mod7==j的次数
  • 由于每个字母mod 7只可能有0~6共7种结果,所以直接7^7暴力枚举
  • $b[k]$表示第k个变量mod 7的值 i
  • $c[k]$表示输入中的数mod 7== i 的出现次数
  • 当s1,s2,s3中有一个为0时,就可以累加答案

代码

#include
#include
#include
#include
#include
#include
#define re register intusing namespace std;int n,x;char a[2],s[10]; long long ans,f[10][20],c[10],b[10];inline int read(){ int x=0,w=1; char ch=getchar(); while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar(); if(ch=='-') w=-1,ch=getchar(); while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-48,ch=getchar(); return x*w;}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(re i=0;i<=6;i++) { b[k]=i; c[k]=f[k][i]; if(c[k]==0) continue; dfs(k+1,sum*c[k]); }}int main() { //freopen("p3123.in","r",stdin); //freopen("p3123.out","w",stdout); s[1]='B';s[2]='E';s[3]='S';s[4]='I';s[5]='G';s[6]='O';s[7]='M'; n=read(); for(re i=1;i<=n;i++) { scanf("%s%d",a,&x); for(re j=1;j<=7;j++) if(a[0]==s[j]) f[j][(x%7+7)%7]++; } dfs(1,1); printf("%lld\n",ans); return 0; }

转载于:https://www.cnblogs.com/bbqub/p/8430286.html

你可能感兴趣的文章
python中的字符串格式化
查看>>
JVM平台上的响应式流(Reactive Streams)规范
查看>>
南阳理工57---6174问题
查看>>
Spinner实现列表下拉功能
查看>>
HTML5全栈工程师学什么?
查看>>
【Dart】生成固定长度随机数
查看>>
.NET 使用 RabbitMQ 图文简介
查看>>
php中的namespace 命名空间
查看>>
python数据写入Excel表格
查看>>
linux shell 发送email 附件
查看>>
人群密度估计 CrowdCount
查看>>
京东为什么不会死
查看>>
JSON.parse()和JSON.stringify()
查看>>
.net 常用正则表达式
查看>>
JAVA动态代理机制解析
查看>>
浏览器样式(css)兼容
查看>>
Java泛型中的标记符含义:
查看>>
初遇GitHub
查看>>
[C# 网络编程系列]专题八:P2P编程
查看>>
Jsの练习-数组常用方法 -forEach()
查看>>