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:
How many ways can you balance this strange scale with the given numbers?
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.
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.
87 33 98 83 67 97 44 72 91 78 46 49 64 59 85 88 0
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<