【题解】Luogu P3123 [USACO15OPEN]贝茜说哞Bessie Goes Moo
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<cmath>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#define re register int
using 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;
}
【题解】Luogu P3123 [USACO15OPEN]贝茜说哞Bessie Goes Moo的更多相关文章
- 【题解】Luogu P2214 [USACO14MAR]哞哞哞Mooo Moo
P2214 [USACO14MAR]哞哞哞Mooo Moo 题目描述 Farmer John has completely forgotten how many cows he owns! He is ...
- BZOJ1613: [Usaco2007 Jan]Running贝茜的晨练计划
1613: [Usaco2007 Jan]Running贝茜的晨练计划 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1138 Solved: 554[ ...
- BZOJ 1613: [Usaco2007 Jan]Running贝茜的晨练计划
题目 1613: [Usaco2007 Jan]Running贝茜的晨练计划 Time Limit: 5 Sec Memory Limit: 64 MB Description 奶牛们打算通过锻炼来 ...
- 3407: [Usaco2009 Oct]Bessie's Weight Problem 贝茜的体重问题
3407: [Usaco2009 Oct]Bessie's Weight Problem 贝茜的体重问题 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: ...
- 1613: [Usaco2007 Jan]Running贝茜的晨练计划
1613: [Usaco2007 Jan]Running贝茜的晨练计划 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 1252 Solved: 609 ...
- 1643: [Usaco2007 Oct]Bessie's Secret Pasture 贝茜的秘密草坪
1643: [Usaco2007 Oct]Bessie's Secret Pasture 贝茜的秘密草坪 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 2 ...
- 【u025】贝茜的晨练计划
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 奶牛们打算通过锻炼来培养自己的运动细胞,作为其中的一员,贝茜选择的运动方式是每天进行N(1 <= ...
- BZOJ_1613_ [Usaco2007_Jan]_Running_贝茜的晨练计划_(动态规划)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1613 n分钟,疲劳值上限是m,开始时疲劳值为0.第i分钟可以跑d[i]米.在某一时刻,如果疲劳 ...
- bzoj1643 [Usaco2007 Oct]Bessie's Secret Pasture 贝茜的秘密草坪
Description 农夫约翰已经从他的牧场中取得了数不清块数的正方形草皮,草皮的边长总是整数(有时农夫约翰割草皮的刀法不合适,甚至切出了边长为0的正方形草皮),他已经把草皮放在了一个奶牛贝茜已经知 ...
随机推荐
- 03.21 ICPC训练联盟周赛:UCF Local Programming Contest 2018正式赛
B Breaking Branches 题意:两个人比赛折枝,谁剩下最后1,无法折出整数即为输 思路:树枝长n,若是奇数,则Bob胜出,若是偶数,则Alice胜出,且需要输出1: 1 #include ...
- 读HikariCP源码学Java(一)-- 通过ConcurrentBag类学习并发编程思想
前言 ConcurrentBag是HikariCP中实现的一个池化资源的并发管理类.它是一个高性能的生产者-消费者队列. ConcurrentBag的并发性能优于LinkedBlockingQueue ...
- [c++] 模板、迭代器、泛型
模板 函数模板:重载的进一步抽象,只需定义一个函数体即可用于所有类型 在C++中,数据的类型也可以通过参数来传递,在函数定义时可以不指明具体的数据类型,当发生函数调用时,编译器可以根据传入的实参自动推 ...
- Ansible_实施处理程序
一.Ansible配置处理程序 1.处理程序 1️⃣:处理程序是响应由其他任务触发的通知的任务 2️⃣:仅当任务在受管主机上更改了某些内容时,任务才通知其处理程序 3️⃣:每个处理程序具有全局唯一的名 ...
- Linux_ACL文件访问控制列表
一.ACL文件访问控制列表 前言 1️⃣:ACL-文件访问控制列表: 2️⃣:ACL可以针对单个用户,单个文件或目录来进行r.w.x的权限设定,特别适用于需要特殊权限的使用情况. 3️⃣:ACL就是可 ...
- centos7下iperf的安装
场景: 1.系统centos7.0-123,该版本下的网络测试工具iperf3 下载gz包#wget http://downloads.es.net/pub/iperf/iperf-3.0.6.tar ...
- Linux进阶之seq,pidof,wget,curl,tr,grep命令
本节内容 seq pidof wget curl tr grep 1.seq(sequence) 生成数列 例子1:指定结束位置 [root@renyz ~]# seq 5 1 2 3 4 ...
- numpy tile()函数
tile(A,B)即在B的方向上,重复A 直接举栗子: A=[1,2] tile(A,2) 此时B=(2) ,B的方向仅包含列方向,将A在列方向上重复一次,得出结果如图1所示 图1-将A在列方向重 ...
- 3 当某个应用的CPU使用达到100%,该怎么办?
你最常用什么指标来描述系统的 CPU 性能呢?我想你的答案,可能不是平均负载,也不是 CPU 上下文切换,而是另一个更直观的指标-- CPU 使用率.CPU 使用率是单位时间内 CPU 使用情况的统计 ...
- 七牛云-上传、删除文件,工具类(Day49)
要求: 1. java1.8以上 2. Maven: 这里的version指定了一个版本范围,每次更新pom.xml的时候会尝试去下载7.5.x版本中的最新版本,你可以手动指定一个固定的版本. < ...