列表

详情


NC24222. [USACO 2015 Ope S]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), containing the seven variables B,E,S,I,G,O,MB,E,S,I,G,O,M (the "OO" 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 NN. The next NN 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−105 to 105105.

输出描述

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

输出:

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

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

pypy3(pypy3.6.1) 解法, 执行用时: 310ms, 内存消耗: 33032K, 提交时间: 2020-08-31 02:44:25

#!/usr/bin/env python3
#
# Bessie Goes Moo
#
import sys, os

def read_int(): return int(input())
def read_strs(): return input().split()

n = read_int()

s = "BESIGOM"
d = {x: [0] * 7 for x in s}

for _ in range(n):
	c, v = read_strs()
	d[c][int(v) % 7] += 1

ans = 0
for i in range(7**7):
	x, c, a = i, [0] * 7, [0] * 7
	for j in range(7):
		a[j] = x % 7
		c[j] = d[s[j]][x % 7]
		x //= 7
	e1 = a[0] + a[1] * 2 + a[2] * 2 + a[3]
	e2 = a[1] + a[2] + a[4] + a[5]
	e3 = a[5] * 2 + a[6]
	if e1 % 7 == 0 or e2 % 7 == 0 or e3 % 7 == 0:
		ans += c[0] * c[1] * c[2] * c[3] * c[4] * c[5] * c[6]

print(ans)

C++14(g++5.4) 解法, 执行用时: 5ms, 内存消耗: 380K, 提交时间: 2020-08-31 22:07:53

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll s[95][7];
ll ans=0;
int main()
{
	int n;
	cin>>n;
	char c;
	int id1;
	for(int i=1;i<=n;i++)
	{
		cin>>c>>id1;
		s[c][(id1%7+7)%7]++;
	}
	for(int B=0;B<7;B++)
    	for(int E=0;E<7;E++)
    		for(int S=0;S<7;S++)
    			for(int I=0;I<7;I++)
    				for(int G=0;G<7;G++)
    					for(int O=0;O<7;O++)
							for(int M=0;M<7;M++)
								if(((B+E+S+S+I+E)*(G+O+E+S)*(M+O+O))%7==0)
          						ans+=s['B'][B]*s['E'][E]*s['S'][S]*s['I'][I]*s['G'][G]*s['O'][O]*s['M'][M];
	cout<<ans;
	return 0;
} 

C++11(clang++ 3.9) 解法, 执行用时: 7ms, 内存消耗: 376K, 提交时间: 2020-08-10 16:50:49

#include <bits/stdc++.h>
using namespace std;
long long s[95][7];
long long ans=0;
int main()
{
	int n;
	cin>>n;
	char c;
	int id1;
	for(int i=1;i<=n;i++)
	{
		cin>>c>>id1;
		s[c][(id1%7+7)%7]++;
	}
	for(int B=0;B<7;B++)
    	for(int E=0;E<7;E++)
    		for(int S=0;S<7;S++)
    			for(int I=0;I<7;I++)
    				for(int G=0;G<7;G++)
    					for(int O=0;O<7;O++)
							for(int M=0;M<7;M++)
								if(((B+E+S+S+I+E)*(G+O+E+S)*(M+O+O))%7==0)
          						ans+=s['B'][B]*s['E'][E]*s['S'][S]*s['I'][I]*s['G'][G]*s['O'][O]*s['M'][M];
	cout<<ans;
	return 0;
} 

上一题