UVA 10317 Equating Equations (状态压缩)

状态压缩求解等式
本文介绍了一种使用状态压缩算法解决等式重新排序问题的方法,确保等式两边相等。通过对输入等式的预处理,将问题转化为寻找特定状态的过程,并通过枚举实现解决方案。
题意:

给一个等式,但是这个等式不一定是正确的,要你对等式中的数字重新排序,使得等式成立。等式只有+和-,数字个数小于16。

分析:

由于数字只有16,总共有2^16种可能,所以可以用状态压缩求解。
以a + b - c = d - e为例子。
1. 我们把等式右边的各项都换到左边,a + b - c - d + e = 0
2. 把+项和-项放一起,就变成(a + b + e) - (c + d) = 0,所以所有数字的和sum应该为偶数。如果不是剪枝。
3. 然后(a + b + e) = (c + d)
4. 令+为1,-为0,枚举所有的状态。
5. 然后我们只要找到多少个-项,状态压缩枚举同样多的数,且和正好是sum / 2,那等式就能成立了。

AC代码

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <sstream>
#include <cstdlib>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
vector<int> rec; //保存数字
vector<char> op; //保存操作符
int pos , neg; //记录正负数的个数
void init() {
    rec.clear();
    op.clear();
}
void preProc(string str) {
    string buf;
    stringstream ss(str);
    int x;
    bool equal = false;
    pos = 1, neg = 0;
    while(ss >> buf) {
        if(isdigit(buf[0])) {
            x = atoi(buf.c_str());
            rec.push_back(x);
        }else {
            op.push_back(buf[0]);
            if(buf[0] == '=')
                equal = true, neg++;
            else if(equal ^ (buf[0] == '+'))
                pos++;
            else
                neg++;
        }
    }
}
int getSum() {
    int sum = 0;
    for(int i = 0; i < rec.size(); i++) {
        sum += rec[i];
    }
    return sum;
}
void print_state(int st, int cnt) {
    vector<int> postive, negative; //新建两个向量用于保存正数和负数

    for(int i = 0; i < rec.size(); i++) {
        if(st & (1 << i)) //保存正数
            postive.push_back(rec[i]);
        else //保存负数
            negative.push_back(rec[i]);
    }

    int np = 0, nn = 0; //保存正数的个数,保存负数的个数
    bool equal = false;
    if(cnt == pos) {
        printf("%d", postive[np++]);
        for(int i = 0; i < op.size(); i++) {
            if(op[i] == '=') {
                equal = true; printf(" = %d", negative[nn++]);
            }else if(op[i] == '+') {
                equal ? printf(" + %d",negative[nn++]) : printf(" + %d",postive[np++]);
            }else {
                equal ? printf(" - %d",postive[np++]) : printf(" - %d",negative[nn++]);
            }
        }
    }else {
        printf("%d", negative[nn++]);
        for(int i = 0; i < op.size(); i++) {
            if(op[i] == '=') {
                equal = true; printf(" = %d", postive[np++]);
            }else if(op[i] == '+') {
                equal ? printf(" + %d",postive[np++]) : printf(" + %d",negative[nn++]);
            }else {
                equal ? printf(" - %d",negative[nn++]) : printf(" - %d",postive[np++]);
            }
        }
    }
    puts("");
}
void solve() {
    int posSum, negSum, cnt;
    int sum = getSum();
    if(sum & 1) { //如果sum为奇数不符合条件
        puts("no solution");
        return;
    }

    sum = sum / 2;

    int end = 1 << rec.size();
    for(int i = 0; i < end; i++) { //遍历最多 2^16种情况
        posSum = negSum = cnt = 0;
        for(int j = 0; j < rec.size(); j++) {
            if(posSum > sum || neg > sum) break;
            if(i & (1 << j))
                posSum += rec[j] , cnt++;
            else
                negSum += rec[j];
        }
        if(posSum == negSum && (cnt == pos || cnt == neg)) {
            print_state(i, cnt);
            return ;
        }
    }
    puts("no solution");
}
int main() {
    string str;
    while(getline(cin, str)) {
        init();
        preProc(str);
        solve();
    }
    return 0;
}
### STM32L031 IO Pin Voltage Characteristics For the STM32L0 series microcontrollers, including the STM32L031 device, a single 3.3V power supply is used to operate these devices efficiently while maintaining low-power consumption features[^1]. The input/output (IO) pins of the STM32L031 are designed with specific voltage characteristics that ensure reliable operation within defined limits. The absolute maximum ratings specify that the IO pin voltages should not exceed VDD + 0.3V or go below VSS - 0.3V where VDD typically stands at 3.3V for this family of controllers. Operating outside these bounds can lead to damage or unpredictable behavior of the IC. In terms of logic levels: - For an input signal to be recognized as high (logic '1'), it must reach at least 70% of VDD which translates into approximately 2.31V. - Conversely, for recognizing a low state (logic '0') on inputs, signals need to stay under 30% of VDD equating roughly to 0.99V. Output drive capabilities also adhere closely around the supplied rail; outputs will sink/source current up until they approach their respective rails but won't surpass them unless stressed beyond specification parameters outlined by manufacturer guidelines. ```cpp // Example C code snippet showing GPIO configuration for STM32L031 GPIO_InitTypeDef GPIO_InitStruct = {0}; __HAL_RCC_GPIOA_CLK_ENABLE(); // Enable clock for GPIO port A GPIO_InitStruct.Pin = GPIO_PIN_5; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值