【题解】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的正方形草皮),他已经把草皮放在了一个奶牛贝茜已经知 ...
随机推荐
- 有关80386cpu在保护模式下的虚拟地址,线性地址和实际物理地址的关系
80386cpu是8086cpu的升级版,其具有32位的寄存器.(32根地址线和32根数据线) 8086cpu其是16位的寄存器但是其地址线有20根,其寻址范围为2的20次方,但是有一个16位的寄存器 ...
- 1. Java概述
1.1 Java语言背景介绍(了解) 语言:人与人交流沟通的表达方式. 计算机语言:人与计算机之间进行信息交流沟通的一种特殊语言. Java语言是美国Sun公司(Stanford University ...
- [C] gcc
概述 GNU C Compiler 流程 预处理,生成.i文件(中间文件,看不到) 编译,生成.s文件(中间文件,看不到) 汇编,生成.o文件 链接,生成可执行文件 参数 -E:预处理 -S:预处理, ...
- gdb 调试,当发现程序退出,需要定位程序退出位置时。
在进入gdb后设置,执行下面语句 handle SIGSEGV nopass handle SIGSEGV nostop 执行程序,触发问题,gdb侧执行c 故障出现时,执行bt,显示堆栈调用.
- Linux_网络进阶管理
一.链路聚合 1.什么是链路聚合? 网卡的链路聚合就是将多块网卡连接起来,当-块网卡损坏,网络依旧可以正常运行,可以有效的防止因为网卡损坏带来的损失,同时也可以提高网络访问速度. 2.链路聚合方式: ...
- nginx的四层转发功能
架构图 配置过程 配置web服务器 # 1.配置web01,更改配置文件 [root@web01 /etc/nginx/conf.d]# vi test1.conf server { listen 8 ...
- python基础之python牛逼的设计模式
七大设计原则: 1.单一职责原则[SINGLE RESPONSIBILITY PRINCIPLE]:一个类负责一项职责. 2.里氏替换原则[LISKOV SUBSTITUTION PRINCIPLE] ...
- Scala 关键字
Java关键字 Java 一共有 50 个关键字(keywords),其中有 2 个是保留字,目前还不曾用到:goto 和 const.true.false 和 null 看起来很像关键字,但实际上只 ...
- 物联网技术nbiot与LoRa的区别有哪些
http://zixun.258.com/1870021.html 物联网技术nbiot与LoRa的区别有哪些 万物物联是大趋势,在中国nbiot与LoRa是热门的低功耗广域网技术,这两者作为最典型的 ...
- 端口映射(socket应用)
# coding=gb2312 import sys import time import socket import thread import subprocess ############### ...