程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 1381 - Balancing the Scale (技巧枚舉+位運算)

1381 - Balancing the Scale (技巧枚舉+位運算)

編輯:C++入門知識

You are given a strange scale (see the figure below), and you are wondering how to balance this scale. After several attempts, you have discovered the way to balance it -- you need to put different numbers on different squares while satisfying the following two equations:


x1 * 4 + x2 * 3 + x3 * 2 + x4 = x5 + x6 * 2 + x7 * 3 + x8 * 4

y1 * 4 + y2 * 3 + y3 * 2 + y4 = y5 + y6 * 2 + y7 * 3 + y8 * 4


How many ways can you balance this strange scale with the given numbers?

Input

\epsfbox{p3693.eps}

There are multiple test cases in the input file. Each test case consists of 16 distinct numbers in the range [1, 1024] on one separate line. You are allowed to use each number only once.

A line with one single integer 0 indicates the end of input and should not be processed by your program.

Output

For each test case, if it is possible to balance the scale in question, output one number, the number of different ways to balance this scale, in the format as indicated in the sample output. Rotations and reversals of the same arrangement should be counted only once.

Sample Input

87 33 98 83 67 97 44 72 91 78 46 49 64 59 85 88 
0

Sample Output

Case 1: 15227223

題意:給定16個數字,求題目中給定公式的平衡方法有幾種

思路:技巧枚舉+位運算,通過位運算每次枚舉4個數字,通過全排列求出和,算出2邊相同時候狀態的種數,最後在把x的情況和y的情況數相乘並所有都相加就是答案,計算全排列前要排序一開始忘了答案一直少,後面看了題解才發現。一開始還理解錯題目了以為x和y的和都是要相同的。

代碼:

#include 
#include 
#include 
#include 
using namespace std;

int num[16], st[16], Sum[(1<<16)];
vector sum[22222];

bool judge(int state) {
	int sn = 0;
	for (int i = 0; i < 16; i++) {
		if (state&(1<

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved